List of usage examples for org.apache.commons.httpclient HttpClient getState
public HttpState getState()
From source file:com.cloud.utils.net.HTTPUtils.java
/** * @param proxy/*www . j a v a 2s .c o m*/ * @param httpClient */ public static void setProxy(Proxy proxy, HttpClient httpClient) { if (proxy != null && httpClient != null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Setting proxy with host " + proxy.getHost() + " and port " + proxy.getPort() + " for host " + httpClient.getHostConfiguration().getHost() + ":" + httpClient.getHostConfiguration().getPort()); } httpClient.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); if (proxy.getUserName() != null && proxy.getPassword() != null) { httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword())); } } }
From source file:de.mpg.mpdl.inge.util.AdminHelper.java
/** * Logs in the given user with the given password. * // www .j a va2 s.c o m * @param userid The id of the user to log in. * @param password The password of the user to log in. * @return The handle for the logged in user. * @throws HttpException * @throws IOException * @throws ServiceException * @throws URISyntaxException */ public static String loginUser(String userid, String password) throws HttpException, IOException, ServiceException, URISyntaxException { String frameworkUrl = PropertyReader.getLoginUrl(); int delim1 = frameworkUrl.indexOf("//"); int delim2 = frameworkUrl.indexOf(":", delim1); String host; int port; if (delim2 > 0) { host = frameworkUrl.substring(delim1 + 2, delim2); port = Integer.parseInt(frameworkUrl.substring(delim2 + 1)); } else { host = frameworkUrl.substring(delim1 + 2); port = 80; } HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check"); login.addParameter("j_username", userid); login.addParameter("j_password", password); ProxyHelper.executeMethod(client, login); login.releaseConnection(); CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies()); Cookie sessionCookie = logoncookies[0]; PostMethod postMethod = new PostMethod(frameworkUrl + "/aa/login"); postMethod.addParameter("target", frameworkUrl); client.getState().addCookie(sessionCookie); ProxyHelper.executeMethod(client, postMethod); if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) { throw new HttpException("Wrong status code: " + login.getStatusCode()); } String userHandle = null; Header headers[] = postMethod.getResponseHeaders(); for (int i = 0; i < headers.length; ++i) { if ("Location".equals(headers[i].getName())) { String location = headers[i].getValue(); int index = location.indexOf('='); userHandle = new String( Base64.getDecoder().decode(location.substring(index + 1, location.length()))); // System.out.println("location: "+location); // System.out.println("handle: "+userHandle); } } if (userHandle == null) { throw new ServiceException("User not logged in."); } return userHandle; }
From source file:it.geosolutions.figis.requester.HTTPUtils.java
private static void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException { URL u = new URL(url); if ((username != null) && (pw != null)) { Credentials defaultcreds = new UsernamePasswordCredentials(username, pw); client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds); client.getParams().setAuthenticationPreemptive(true); // GS2 by default always requires authentication } else {//from w w w. j a v a 2 s . co m if (LOGGER.isDebugEnabled()) { LOGGER.debug("Not setting credentials to access to " + url); } } }
From source file:it.geosolutions.geonetwork.util.HTTPUtils.java
private static void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException { URL u = new URL(url); if (username != null && pw != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, pw); client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds); client.getParams().setAuthenticationPreemptive(true); // GS2 by default always requires authentication } else {/*from w w w.j a v a 2s . co m*/ if (LOGGER.isTraceEnabled()) { LOGGER.trace("Not setting credentials to access to " + url); } } }
From source file:at.gv.egovernment.moa.id.commons.utils.HttpClientWithProxySupport.java
public static HttpClient getHttpClient() { HttpClient client = new HttpClient(); String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$ String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$ if (MiscUtil.isNotEmpty(host) && MiscUtil.isNotEmpty(port)) { int p = Integer.parseInt(port); client.getHostConfiguration().setProxy(host, p); Logger.info("Initial HTTPClient with proxy usage. " + "ProxyHost=" + host + " ProxyPort=" + port); String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ if (MiscUtil.isNotEmpty(user) && pass != null) { client.getState().setProxyCredentials(new AuthScope(host, p), new UsernamePasswordCredentials(user, pass)); }//from www . j av a2s. c o m } return client; }
From source file:de.mpg.escidoc.services.framework.AdminHelper.java
/** * Logs in the given user with the given password. * // w w w .ja v a 2 s. c o m * @param userid The id of the user to log in. * @param password The password of the user to log in. * @return The handle for the logged in user. * @throws HttpException * @throws IOException * @throws ServiceException * @throws URISyntaxException */ public static String loginUser(String userid, String password) throws HttpException, IOException, ServiceException, URISyntaxException { String frameworkUrl = ServiceLocator.getLoginUrl(); int delim1 = frameworkUrl.indexOf("//"); int delim2 = frameworkUrl.indexOf(":", delim1); String host; int port; if (delim2 > 0) { host = frameworkUrl.substring(delim1 + 2, delim2); port = Integer.parseInt(frameworkUrl.substring(delim2 + 1)); } else { host = frameworkUrl.substring(delim1 + 2); port = 80; } HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check"); login.addParameter("j_username", userid); login.addParameter("j_password", password); ProxyHelper.executeMethod(client, login); login.releaseConnection(); CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies()); Cookie sessionCookie = logoncookies[0]; PostMethod postMethod = new PostMethod(frameworkUrl + "/aa/login"); postMethod.addParameter("target", frameworkUrl); client.getState().addCookie(sessionCookie); ProxyHelper.executeMethod(client, postMethod); if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) { throw new HttpException("Wrong status code: " + login.getStatusCode()); } String userHandle = null; Header headers[] = postMethod.getResponseHeaders(); for (int i = 0; i < headers.length; ++i) { if ("Location".equals(headers[i].getName())) { String location = headers[i].getValue(); int index = location.indexOf('='); userHandle = new String(Base64.decode(location.substring(index + 1, location.length()))); //System.out.println("location: "+location); //System.out.println("handle: "+userHandle); } } if (userHandle == null) { throw new ServiceException("User not logged in."); } return userHandle; }
From source file:it.geosolutions.geoserver.rest.HTTPUtils.java
private static void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException { URL u = new URL(url); if (username != null && pw != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, pw); client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds); client.getParams().setAuthenticationPreemptive(true); // GS2 by // default // always // requires // authentication } else {// w w w . ja v a2s . co m if (LOGGER.isDebugEnabled()) { LOGGER.debug("Not setting credentials to access to " + url); } } }
From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java
/** * authentication.//from ww w .j a va2 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.cloudbees.api.HttpClientHelper.java
public static HttpClient createClient(BeesClientConfiguration beesClientConfiguration) { HttpClient client = new HttpClient(); String proxyHost = beesClientConfiguration.getProxyHost(); if (proxyHost != null) { int proxyPort = beesClientConfiguration.getProxyPort(); client.getHostConfiguration().setProxy(proxyHost, proxyPort); //if there are proxy credentials available, set those too Credentials proxyCredentials = null; String proxyUser = beesClientConfiguration.getProxyUser(); String proxyPassword = beesClientConfiguration.getProxyPassword(); if (proxyUser != null || proxyPassword != null) proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword); if (proxyCredentials != null) client.getState().setProxyCredentials(AuthScope.ANY, proxyCredentials); }//from w w w . ja v a 2 s . c o m return client; }
From source file:de.mpg.escidoc.services.tools.scripts.person_grants.Util.java
/** * Logs in the given user with the given password. * //from w w w. j ava 2 s .com * @param userid The id of the user to log in. * @param password The password of the user to log in. * @return The handle for the logged in user. * @throws HttpException * @throws IOException * @throws ServiceException * @throws URISyntaxException */ public static String loginUser(String userid, String password, String frameworkUrl) throws HttpException, IOException, ServiceException, URISyntaxException { int delim1 = frameworkUrl.indexOf("//"); int delim2 = frameworkUrl.indexOf(":", delim1); String host; int port; if (delim2 > 0) { host = frameworkUrl.substring(delim1 + 2, delim2); port = Integer.parseInt(frameworkUrl.substring(delim2 + 1)); } else { host = frameworkUrl.substring(delim1 + 2); port = 80; } HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check"); login.addParameter("j_username", userid); login.addParameter("j_password", password); client.executeMethod(login); login.releaseConnection(); CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); // Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies()); Cookie sessionCookie = client.getState().getCookies()[0]; PostMethod postMethod = new PostMethod(frameworkUrl + "/aa/login"); postMethod.addParameter("target", frameworkUrl); client.getState().addCookie(sessionCookie); client.executeMethod(postMethod); if (HttpStatus.SC_SEE_OTHER != postMethod.getStatusCode()) { throw new HttpException("Wrong status code: " + login.getStatusCode()); } String userHandle = null; Header headers[] = postMethod.getResponseHeaders(); for (int i = 0; i < headers.length; ++i) { if ("Location".equals(headers[i].getName())) { String location = headers[i].getValue(); int index = location.indexOf('='); userHandle = new String(Base64.decode(location.substring(index + 1, location.length()))); // System.out.println("location: "+location); // System.out.println("handle: "+userHandle); } } if (userHandle == null) { throw new ServiceException("User not logged in."); } return userHandle; }