List of usage examples for org.apache.commons.httpclient.params HttpClientParams setAuthenticationPreemptive
public void setAuthenticationPreemptive(boolean paramBoolean)
From source file:com.discursive.jccook.httpclient.BasicAuthExample.java
public static void main(String[] args) throws HttpException, IOException { // Configure Logging System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); HttpClient client = new HttpClient(); HttpState state = client.getState(); HttpClientParams params = client.getParams(); // Set credentials on the client Credentials credentials = new UsernamePasswordCredentials("testuser", "crazypass"); state.setCredentials(null, null, credentials); params.setAuthenticationPreemptive(true); String url = "http://www.discursive.com/jccook/auth/"; HttpMethod method = new GetMethod(url); client.executeMethod(method);/*from w ww. ja v a 2 s. c om*/ String response = method.getResponseBodyAsString(); System.out.println(response); method.releaseConnection(); }
From source file:com.datos.vfs.provider.http.HttpClientFactory.java
/** * Creates a new connection to the server. * @param builder The HttpFileSystemConfigBuilder. * @param scheme The protocol.//from w w w. ja va2 s .c om * @param hostname The hostname. * @param port The port number. * @param username The username. * @param password The password * @param fileSystemOptions The file system options. * @return a new HttpClient connection. * @throws FileSystemException if an error occurs. * @since 2.0 */ public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme, final String hostname, final int port, final String username, final String password, final FileSystemOptions fileSystemOptions) throws FileSystemException { HttpClient client; try { final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager(); final HttpConnectionManagerParams connectionMgrParams = mgr.getParams(); client = new HttpClient(mgr); final HostConfiguration config = new HostConfiguration(); config.setHost(hostname, port, scheme); if (fileSystemOptions != null) { final String proxyHost = builder.getProxyHost(fileSystemOptions); final int proxyPort = builder.getProxyPort(fileSystemOptions); if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { config.setProxy(proxyHost, proxyPort); } final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions); if (proxyAuth != null) { final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD }); if (authData != null) { final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials( UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)), UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null))); final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT); client.getState().setProxyCredentials(scope, proxyCreds); } if (builder.isPreemptiveAuth(fileSystemOptions)) { final HttpClientParams httpClientParams = new HttpClientParams(); httpClientParams.setAuthenticationPreemptive(true); client.setParams(httpClientParams); } } final Cookie[] cookies = builder.getCookies(fileSystemOptions); if (cookies != null) { client.getState().addCookies(cookies); } } /** * ConnectionManager set methods must be called after the host & port and proxy host & port * are set in the HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams * tries to locate the host configuration. */ connectionMgrParams.setMaxConnectionsPerHost(config, builder.getMaxConnectionsPerHost(fileSystemOptions)); connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions)); connectionMgrParams.setConnectionTimeout(builder.getConnectionTimeout(fileSystemOptions)); connectionMgrParams.setSoTimeout(builder.getSoTimeout(fileSystemOptions)); client.setHostConfiguration(config); if (username != null) { final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT); client.getState().setCredentials(scope, creds); } } catch (final Exception exc) { throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname); } return client; }
From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java
/** * authentication.//from w ww. j a v a 2 s. c o m * * @param httpMethod * the http method * @param httpClientConfig * the http client config * @param httpClient * the http client */ private static void setAuthentication(HttpMethod httpMethod, HttpClientConfig httpClientConfig, HttpClient httpClient) { UsernamePasswordCredentials usernamePasswordCredentials = httpClientConfig.getUsernamePasswordCredentials(); // ? if (Validator.isNotNullOrEmpty(usernamePasswordCredentials)) { httpMethod.setDoAuthentication(true); AuthScope authScope = AuthScope.ANY; Credentials credentials = usernamePasswordCredentials; httpClient.getState().setCredentials(authScope, credentials); // 1.1?(Preemptive Authentication) // ??HttpClientbasic????????? // ??? HttpClientParams httpClientParams = new HttpClientParams(); httpClientParams.setAuthenticationPreemptive(true); httpClient.setParams(httpClientParams); } }
From source file:com.liferay.portal.search.solr.server.BasicAuthSolrServer.java
public BasicAuthSolrServer(AuthScope authScope, String username, String password, String url) throws MalformedURLException { _username = username;/* ww w . j a v a 2s . co m*/ _password = password; _multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager(); HttpClient httpClient = new HttpClient(_multiThreadedHttpConnectionManager); if ((_username != null) && (_password != null)) { if (authScope == null) { authScope = AuthScope.ANY; } HttpState httpState = httpClient.getState(); httpState.setCredentials(authScope, new UsernamePasswordCredentials(_username, _password)); HttpClientParams httpClientParams = httpClient.getParams(); httpClientParams.setAuthenticationPreemptive(true); } _server = new CommonsHttpSolrServer(url, httpClient); }
From source file:com.serena.rlc.jenkins.plugins.rlcnotifier.RLCSite.java
public String executeJSONGet(URI uri) throws Exception { String result = null;//from ww w .j a va2s .c o m HttpClient httpClient = new HttpClient(); if ("https".equalsIgnoreCase(uri.getScheme())) { ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory(); Protocol https = new Protocol("https", socketFactory, 443); Protocol.registerProtocol("https", https); } GetMethod method = new GetMethod(uri.toString()); try { HttpClientParams params = httpClient.getParams(); params.setAuthenticationPreemptive(true); UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password); httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials); int responseCode = httpClient.executeMethod(method); //if (responseCode < 200 || responseCode < 300) { if (responseCode == 401) { throw new Exception("Error connecting to Serena RLC: Invalid user and/or password"); } else if (responseCode != 200) { throw new Exception("Error connecting to Serena RLC: " + responseCode); } else { result = method.getResponseBodyAsString(); } } finally { method.releaseConnection(); } return result; }
From source file:com.serena.rlc.jenkins.plugins.rlcnotifier.RLCSite.java
public String executeJSONPost(URI uri, String postContents) throws Exception { String result = null;//from w w w .j av a 2s .c o m HttpClient httpClient = new HttpClient(); if ("https".equalsIgnoreCase(uri.getScheme())) { ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory(); Protocol https = new Protocol("https", socketFactory, 443); Protocol.registerProtocol("https", https); } PostMethod method = new PostMethod(uri.toString()); if (postContents != null) method.setRequestBody(postContents); method.setRequestHeader("Content-Type", "application/json"); method.setRequestHeader("charset", "utf-8"); try { HttpClientParams params = httpClient.getParams(); params.setAuthenticationPreemptive(true); UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password); httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials); int responseCode = httpClient.executeMethod(method); if (responseCode != 200) { throw new Exception("Serena RLC returned error code: " + responseCode); } else { result = method.getResponseBodyAsString(); } } catch (Exception e) { throw new Exception("Error connecting to Serena RLC: " + e.getMessage()); } finally { method.releaseConnection(); } return result; }
From source file:com.serena.rlc.jenkins.plugins.rlcnotifier.RLCSite.java
public String executeJSONPut(URI uri, String putContents) throws Exception { String result = null;//www . ja va 2 s .c om HttpClient httpClient = new HttpClient(); if ("https".equalsIgnoreCase(uri.getScheme())) { ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory(); Protocol https = new Protocol("https", socketFactory, 443); Protocol.registerProtocol("https", https); } PutMethod method = new PutMethod(uri.toString()); if (putContents != null) method.setRequestBody(putContents); method.setRequestHeader("Content-Type", "application/json"); method.setRequestHeader("charset", "utf-8"); try { HttpClientParams params = httpClient.getParams(); params.setAuthenticationPreemptive(true); UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password); httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials); int responseCode = httpClient.executeMethod(method); //if (responseCode < 200 || responseCode < 300) { if (responseCode != 200 && responseCode != 204) { throw new Exception("Serena RLC returned error code: " + responseCode); } else { result = method.getResponseBodyAsString(); } } catch (Exception e) { throw new Exception("Error connecting to Serena RLC: " + e.getMessage()); } finally { method.releaseConnection(); } return result; }
From source file:com.urbancode.ds.jenkins.plugins.serenarapublisher.UrbanDeploySite.java
public String executeJSONGet(URI uri) throws Exception { String result = null;/*from w w w . ja v a2 s . co m*/ HttpClient httpClient = new HttpClient(); if ("https".equalsIgnoreCase(uri.getScheme())) { ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory(); Protocol https = new Protocol("https", socketFactory, 443); Protocol.registerProtocol("https", https); } GetMethod method = new GetMethod(uri.toString()); setDirectSsoInteractionHeader(method); try { HttpClientParams params = httpClient.getParams(); params.setAuthenticationPreemptive(true); UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password); httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials); int responseCode = httpClient.executeMethod(method); //if (responseCode < 200 || responseCode < 300) { if (responseCode == 401) { throw new Exception("Error connecting to Serena DA: Invalid user and/or password"); } else if (responseCode != 200) { throw new Exception("Error connecting to Serena DA: " + responseCode); } else { result = method.getResponseBodyAsString(); } } finally { method.releaseConnection(); } return result; }
From source file:com.urbancode.ds.jenkins.plugins.serenarapublisher.UrbanDeploySite.java
public String executeJSONPost(URI uri, String postContents) throws Exception { String result = null;//from w w w.j av a2 s . c om HttpClient httpClient = new HttpClient(); if ("https".equalsIgnoreCase(uri.getScheme())) { ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory(); Protocol https = new Protocol("https", socketFactory, 443); Protocol.registerProtocol("https", https); } PostMethod method = new PostMethod(uri.toString()); setDirectSsoInteractionHeader(method); method.setRequestBody(postContents); method.setRequestHeader("Content-Type", "application/json"); method.setRequestHeader("charset", "utf-8"); try { HttpClientParams params = httpClient.getParams(); params.setAuthenticationPreemptive(true); UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password); httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials); int responseCode = httpClient.executeMethod(method); if (responseCode != 200) { throw new Exception("SerenaRA returned error code: " + responseCode); } else { result = method.getResponseBodyAsString(); } } catch (Exception e) { throw new Exception("Error connecting to Serena DA: " + e.getMessage()); } finally { method.releaseConnection(); } return result; }
From source file:com.urbancode.ds.jenkins.plugins.serenarapublisher.UrbanDeploySite.java
public String executeJSONPut(URI uri, String putContents) throws Exception { String result = null;/*from w w w. j av a 2 s . c om*/ HttpClient httpClient = new HttpClient(); if ("https".equalsIgnoreCase(uri.getScheme())) { ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory(); Protocol https = new Protocol("https", socketFactory, 443); Protocol.registerProtocol("https", https); } PutMethod method = new PutMethod(uri.toString()); setDirectSsoInteractionHeader(method); method.setRequestBody(putContents); method.setRequestHeader("Content-Type", "application/json"); method.setRequestHeader("charset", "utf-8"); try { HttpClientParams params = httpClient.getParams(); params.setAuthenticationPreemptive(true); UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password); httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials); int responseCode = httpClient.executeMethod(method); //if (responseCode < 200 || responseCode < 300) { if (responseCode != 200 && responseCode != 204) { throw new Exception("Serena DA returned error code: " + responseCode); } else { result = method.getResponseBodyAsString(); } } catch (Exception e) { throw new Exception("Error connecting to SerenaRA: " + e.getMessage()); } finally { method.releaseConnection(); } return result; }