private void init(final HostConfiguration hostConfiguration) { // wrap all of the assignments in a synchronized block to avoid // having to negotiate the monitor for each method call synchronized (hostConfiguration) { try { if (hostConfiguration.host != null) { this.host = (HttpHost) hostConfiguration.host.clone(); } else { this.host = null; } if (hostConfiguration.proxyHost != null) { this.proxyHost = (ProxyHost) hostConfiguration.proxyHost.clone(); } else { this.proxyHost = null; } this.localAddress = hostConfiguration.getLocalAddress(); this.params = (HostParams)hostConfiguration.getParams().clone(); } catch (CloneNotSupportedException e) { throw new IllegalArgumentException("Host configuration could not be cloned"); } } }
protected void configureHttpClient() throws IOException, GeneralSecurityException { httpClient.getParams().setAuthenticationPreemptive(isAuthenticationPreemptive()); initCredentials(); initSocketFactory(); initProtocolIfNeeded(); if (httpConnectionManager != null) { httpClient.setHttpConnectionManager(httpConnectionManager); } List<Header> headers = getDefaultHeaders(); httpClient.getHostConfiguration().getParams().setParameter(HostParams.DEFAULT_HEADERS, headers); httpClient.getParams().setParameter(HttpClientParams.USER_AGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/11.04 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19"); httpClient.getParams().setParameter(HttpClientParams.HTTP_CONTENT_CHARSET, "UTF-8"); httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); httpClient.getParams().setConnectionManagerTimeout(connectionManagerTimeout); httpClient.getParams().setSoTimeout(soTimeout); if (connectionTimeout >= 0) { httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); } }
/** * Copy constructor for HostConfiguration * * @param hostConfiguration the hostConfiguration to copy */ public HostConfiguration (final HostConfiguration hostConfiguration) { // wrap all of the assignments in a synchronized block to avoid // having to negotiate the monitor for each method call synchronized (hostConfiguration) { try { if (hostConfiguration.host != null) { this.host = (HttpHost) hostConfiguration.host.clone(); } else { this.host = null; } if (hostConfiguration.proxyHost != null) { this.proxyHost = (ProxyHost) hostConfiguration.proxyHost.clone(); } else { this.proxyHost = null; } this.localAddress = hostConfiguration.getLocalAddress(); this.params = (HostParams)hostConfiguration.getParams().clone(); } catch (CloneNotSupportedException e) { throw new IllegalArgumentException("Host configuration could not be cloned"); } } }
public void testDefaultHeaders() throws IOException { this.server.setHttpService(new SimpleService()); ArrayList defaults = new ArrayList(); defaults.add(new Header("this-header", "value1")); defaults.add(new Header("that-header", "value1")); defaults.add(new Header("that-header", "value2")); defaults.add(new Header("User-Agent", "test")); HostConfiguration hostconfig = new HostConfiguration(); hostconfig.setHost( this.server.getLocalAddress(), this.server.getLocalPort(), Protocol.getProtocol("http")); hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS, defaults); GetMethod httpget = new GetMethod("/miss/"); try { this.client.executeMethod(hostconfig, httpget); } finally { httpget.releaseConnection(); } assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); Header[] thisheader = httpget.getRequestHeaders("this-header"); assertEquals(1, thisheader.length); Header[] thatheader = httpget.getRequestHeaders("that-header"); assertEquals(2, thatheader.length); assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); }
private static void setCredentials(final @NotNull HttpClient httpClient, final @NotNull Credentials credentials, final @NotNull URI serverUri) { if (credentials.getType() == Credentials.Type.Alternate) { HostParams parameters = httpClient.getHostConfiguration().getParams(); Collection<Header> headers = (Collection<Header>)parameters.getParameter(HostParams.DEFAULT_HEADERS); if (headers == null) { headers = new ArrayList<Header>(); parameters.setParameter(HostParams.DEFAULT_HEADERS, headers); } Header authHeader = ContainerUtil.find(headers, new Condition<Header>() { @Override public boolean value(Header header) { return header.getName().equals(HTTPConstants.HEADER_AUTHORIZATION); } }); if (authHeader == null) { authHeader = new Header(HTTPConstants.HEADER_AUTHORIZATION, ""); headers.add(authHeader); } authHeader .setValue(BasicScheme.authenticate(new UsernamePasswordCredentials(credentials.getUserName(), credentials.getPassword()), "UTF-8")); } else { final NTCredentials ntCreds = new NTCredentials(credentials.getUserName(), credentials.getPassword(), serverUri.getHost(), credentials.getDomain()); httpClient.getState().setCredentials(AuthScope.ANY, ntCreds); httpClient.getParams().setBooleanParameter(USE_NATIVE_CREDENTIALS, credentials.getType() == Credentials.Type.NtlmNative); } }
/** * Assigns {@link HostParams HTTP protocol parameters} specific to this host. * * @since 3.0 * * @see HostParams */ public void setParams(final HostParams params) { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } this.params = params; }
/** * Returns {@link HostParams HTTP protocol parameters} associated with this host. * * @return HTTP parameters. * * @since 3.0 */ public HostParams getParams() { return this.params; }