@Override public HttpParams getDefaultParams() { if (httpParams == null) { synchronized (this) { if (httpParams == null) { httpParams = createParams(); } } } return httpParams; }
@Override public Object getParameter(final String name) { // See if the parameter has been explicitly defined Object param = null; paramLock.readLock().lock(); try { param = this.parameters.get(name); } finally { paramLock.readLock().unlock(); } if (param == null) { // If not, see if defaults are available HttpParams defaults = getDefaults(); if (defaults != null) { // Return default parameter value param = defaults.getParameter(name); } } return param; }
public static void main(String[] args) { // register the auth scheme AuthPolicy.registerAuthScheme(SecretAuthScheme.NAME, SecretAuthScheme.class); // include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY preference, // this can be done on a per-client or per-method basis but we'll do it // globally for this example HttpParams params = DefaultHttpParams.getDefaultParams(); ArrayList schemes = new ArrayList(); schemes.add(SecretAuthScheme.NAME); schemes.addAll((Collection) params.getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY)); params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes); // now that our scheme has been registered we can execute methods against // servers that require "Secret" authentication... }
public void testChallengeSelection() throws Exception { List authPrefs = new ArrayList(3); authPrefs.add(AuthPolicy.NTLM); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); HttpParams httpparams = new DefaultHttpParams(); httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams); Map map = new HashMap(); map.put("unknown", "unknown realm=\"whatever\""); map.put("basic", "basic realm=\"whatever\""); AuthScheme authscheme = processor.selectAuthScheme(map); assertTrue(authscheme instanceof BasicScheme); }
public void testInvalidChallenge() throws Exception { List authPrefs = new ArrayList(3); authPrefs.add("unsupported1"); authPrefs.add("unsupported2"); HttpParams httpparams = new DefaultHttpParams(); httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams); Map map = new HashMap(); map.put("unsupported1", "unsupported1 realm=\"whatever\""); map.put("unsupported2", "unsupported2 realm=\"whatever\""); try { AuthScheme authscheme = processor.selectAuthScheme(map); fail("AuthChallengeException should have been thrown"); } catch (AuthChallengeException e) { //ignore } }
public void testUnsupportedChallenge() throws Exception { List authPrefs = new ArrayList(3); authPrefs.add(AuthPolicy.NTLM); authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.DIGEST); HttpParams httpparams = new DefaultHttpParams(); httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams); Map map = new HashMap(); map.put("unsupported1", "unsupported1 realm=\"whatever\""); map.put("unsupported2", "unsupported2 realm=\"whatever\""); try { AuthScheme authscheme = processor.selectAuthScheme(map); fail("AuthChallengeException should have been thrown"); } catch (AuthChallengeException e) { //expected } }
public void testInvalidChallengeProcessing() throws Exception { HttpParams httpparams = new DefaultHttpParams(); AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams); Map map = new HashMap(); map.put("basic", "basic realm=\"whatever\", param=\"value\""); AuthState authstate = new AuthState(); AuthScheme authscheme = processor.processChallenge(authstate, map); assertTrue(authscheme instanceof BasicScheme); assertEquals("whatever", authscheme.getRealm()); assertEquals(authscheme, authstate.getAuthScheme()); assertEquals("value", authscheme.getParameter("param")); Map map2 = new HashMap(); map2.put("ntlm", "NTLM"); try { // Basic authentication scheme expected authscheme = processor.processChallenge(authstate, map2); fail("AuthenticationException should have been thrown"); } catch (AuthenticationException e) { //expected } }
public static void setParameter(HttpParams params, String paramName, String paramValue) { Class<?> paramType = getParameterType(paramName); if (paramType.equals(Boolean.class)) { params.setBooleanParameter(paramName, Boolean.parseBoolean(paramValue)); } else if (paramType.equals(Integer.class)) { params.setIntParameter(paramName, Integer.parseInt(paramValue)); } else if (paramType.equals(Long.class)) { params.setLongParameter(paramName, Long.parseLong(paramValue)); } else if (paramType.equals(Double.class)) { params.setDoubleParameter(paramName, Double.parseDouble(paramValue)); } else if (paramType.equals(String.class)) { params.setParameter(paramName, paramValue); } else if (paramType.equals(Class.class)) { try { Class<?> configuredClass = Class.forName(paramValue, true, ClassLoaderUtils.getDefaultClassLoader()); params.setParameter(paramName, configuredClass); } catch (ClassNotFoundException e) { throw new RuntimeException("Could not locate the class needed to configure the HttpClient.", e); } } else { throw new RuntimeException("Attempted to configure an HttpClient parameter '" + paramName + "' " + "of a type not supported through Workflow configuration: " + paramType.getName()); } }
protected void configureDefaultHttpClientParams(HttpParams params) { params.setParameter(HttpClientParams.CONNECTION_MANAGER_CLASS, MultiThreadedHttpConnectionManager.class); params.setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109); params.setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 10000); Map<HostConfiguration, Integer> maxHostConnectionsMap = new HashMap<HostConfiguration, Integer>(); maxHostConnectionsMap.put(HostConfiguration.ANY_HOST_CONFIGURATION, new Integer(20)); params.setParameter(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, maxHostConnectionsMap); params.setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 20); params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000); params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2*60*1000); boolean retrySocketException = new Boolean(ConfigContext.getCurrentContextConfig().getProperty(RETRY_SOCKET_EXCEPTION_PROPERTY)); if (retrySocketException) { LOG.info("Installing custom HTTP retry handler to retry requests in face of SocketExceptions"); params.setParameter(HttpMethodParams.RETRY_HANDLER, new CustomHttpMethodRetryHandler()); } }
/** * Creates an authentication challenge processor with the given {@link HttpParams HTTP * parameters} * * @param params the {@link HttpParams HTTP parameters} used by this processor */ public AuthChallengeProcessor(final HttpParams params) { super(); if (params == null) { throw new IllegalArgumentException("Parameter collection may not be null"); } this.params = params; }
private Credentials promptForCredentials( final AuthScheme authScheme, final HttpParams params, final AuthScope authscope) { LOG.debug("Credentials required"); Credentials creds = null; CredentialsProvider credProvider = (CredentialsProvider)params.getParameter(CredentialsProvider.PROVIDER); if (credProvider != null) { try { creds = credProvider.getCredentials( authScheme, authscope.getHost(), authscope.getPort(), false); } catch (CredentialsNotAvailableException e) { LOG.warn(e.getMessage()); } if (creds != null) { this.state.setCredentials(authscope, creds); if (LOG.isDebugEnabled()) { LOG.debug(authscope + " new credentials given"); } } } else { LOG.debug("Credentials provider not available"); } return creds; }
private Credentials promptForProxyCredentials( final AuthScheme authScheme, final HttpParams params, final AuthScope authscope) { LOG.debug("Proxy credentials required"); Credentials creds = null; CredentialsProvider credProvider = (CredentialsProvider)params.getParameter(CredentialsProvider.PROVIDER); if (credProvider != null) { try { creds = credProvider.getCredentials( authScheme, authscope.getHost(), authscope.getPort(), true); } catch (CredentialsNotAvailableException e) { LOG.warn(e.getMessage()); } if (creds != null) { this.state.setProxyCredentials(authscope, creds); if (LOG.isDebugEnabled()) { LOG.debug(authscope + " new credentials given"); } } } else { LOG.debug("Proxy credentials provider not available"); } return creds; }
private void checkDate(String date) throws Exception { Header header = new Header("Set-Cookie", "custno=12345;Expires='"+date+"';"); HttpParams params = new DefaultHttpParamsFactory().getDefaultParams(); CookieSpec cookiespec = new CookieSpecBase(); cookiespec.setValidDateFormats( (Collection)params.getParameter(HttpMethodParams.DATE_PATTERNS)); cookieParse(cookiespec, "localhost", 80, "/", false, header); }
public void testChallengeProcessing() throws Exception { HttpParams httpparams = new DefaultHttpParams(); AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams); Map map = new HashMap(); map.put("basic", "basic realm=\"whatever\", param=\"value\""); AuthState authstate = new AuthState(); AuthScheme authscheme = processor.processChallenge(authstate, map); assertTrue(authscheme instanceof BasicScheme); assertEquals("whatever", authscheme.getRealm()); assertEquals(authscheme, authstate.getAuthScheme()); assertEquals("value", authscheme.getParameter("param")); }
public static void main(String[] args) { // register the auth scheme AuthPolicy.registerAuthScheme("Negotiate", NegotiateScheme.class); // include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY preference ArrayList schemes = new ArrayList(); schemes.add("Negotiate"); HttpParams params = DefaultHttpParams.getDefaultParams(); params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes); // now that our scheme has been registered we can execute methods against // servers that require "Negotiate" authentication... HttpClient client = new HttpClient(); // The Negotiate scheme uses JAAS as credential provider but the // httpclient api require us to supply cred anyway. // a work around is to provide an empty set of creds. Credentials use_jaas_creds = new Credentials() {}; client.getState().setCredentials( new AuthScope(null, -1, null), use_jaas_creds); GetMethod httpget = new GetMethod(args[0]); try { client.executeMethod(httpget); //System.out.println(httpget.getStatusLine()); //System.out.println(httpget.getResponseBodyAsString()); } catch (Exception e) { e.printStackTrace(); } finally { // release any connection resources used by the method httpget.releaseConnection(); } }
/** * Requests the received method. * * Executes the method through the inherited HttpClient.executedMethod(method). * * @param method HTTP method request. */ @Override public int executeMethod(HttpMethod method) throws IOException { try { // Update User Agent HttpParams params = method.getParams(); String userAgent = OwnCloudClientManagerFactory.getUserAgent(); params.setParameter(HttpMethodParams.USER_AGENT, userAgent); Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath()); // logCookiesAtRequest(method.getRequestHeaders(), "before"); // logCookiesAtState("before"); method.setFollowRedirects(false); int status = super.executeMethod(method); if (mFollowRedirects) { status = followRedirection(method).getLastStatus(); } // logCookiesAtRequest(method.getRequestHeaders(), "after"); // logCookiesAtState("after"); // logSetCookiesAtResponse(method.getResponseHeaders()); return status; } catch (IOException e) { //Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e); throw e; } }
public NonBlockingHttpParams(HttpParams defaults) { super(defaults); }
public void setConnectionParams(HttpParams httpParams) { this.connectionParams = httpParams; }
/** * @return */ public HttpParams getParams() { return this.params; }