public void testSuccessfulVerifyTargetOverHttps() throws Exception { //Stub HttpClient so that executeMethod returns a 200 response when(mockedHttpClient.executeMethod(any(HostConfiguration.class), any(HttpMethod.class), any(HttpState.class))).thenReturn(200); target.setEndpointProtocol(HTTPS_PROTOCOL); target.setEndpointPort(HTTPS_PORT); //Call verifyTarget transmitter.verifyTarget(target); ArgumentCaptor<HostConfiguration> hostConfig = ArgumentCaptor.forClass(HostConfiguration.class); ArgumentCaptor<HttpMethod> httpMethod = ArgumentCaptor.forClass(HttpMethod.class); ArgumentCaptor<HttpState> httpState = ArgumentCaptor.forClass(HttpState.class); verify(mockedHttpClient).executeMethod(hostConfig.capture(), httpMethod.capture(), httpState.capture()); assertEquals("port", HTTPS_PORT, hostConfig.getValue().getPort()); assertTrue("socket factory", hostConfig.getValue().getProtocol().getSocketFactory() instanceof SecureProtocolSocketFactory); assertEquals("protocol", HTTPS_PROTOCOL.toLowerCase(), hostConfig.getValue().getProtocol().getScheme().toLowerCase()); }
public SecureHttpMethodResponse(HttpMethod method, HostConfiguration hostConfig, EncryptionUtils encryptionUtils) throws AuthenticationException, IOException { super(method); this.hostConfig = hostConfig; this.encryptionUtils = encryptionUtils; if(method.getStatusCode() == HttpStatus.SC_OK) { this.decryptedBody = encryptionUtils.decryptResponseBody(method); // authenticate the response if(!authenticate()) { throw new AuthenticationException(method); } } }
@Override public HttpConnector clone() throws CloneNotSupportedException { HttpConnector clonedObject = (HttpConnector) super.clone(); clonedObject.httpStateListeners = new EventListenerList(); clonedObject.sUrl = ""; clonedObject.handleCookie = true; clonedObject.httpParameters = new XMLVector<XMLVector<String>>(); clonedObject.postQuery = ""; clonedObject.certificateManager = new CertificateManager(); clonedObject.hostConfiguration = new HostConfiguration(); clonedObject.givenAuthPassword = null; clonedObject.givenAuthUser = null; return clonedObject; }
/** * Gets the maximum number of connections to be used for a particular host config. If * the value has not been specified for the given host the default value will be * returned. * * @param hostConfiguration The host config. * @return The maximum number of connections to be used for the given host config. * * @see #MAX_HOST_CONNECTIONS */ public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) { Map m = (Map) getParameter(MAX_HOST_CONNECTIONS); if (m == null) { // MAX_HOST_CONNECTIONS have not been configured, using the default value return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS; } else { Integer max = (Integer) m.get(hostConfiguration); if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) { // the value has not been configured specifically for this host config, // use the default value return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION); } else { return ( max == null ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS : max.intValue() ); } } }
public void testDefaults() throws IOException { this.server.setHttpService(new SimpleService()); this.client.getParams().setParameter(HttpMethodParams.USER_AGENT, "test"); HostConfiguration hostconfig = new HostConfiguration(); hostconfig.setHost( this.server.getLocalAddress(), this.server.getLocalPort(), Protocol.getProtocol("http")); GetMethod httpget = new GetMethod("/miss/"); try { this.client.executeMethod(hostconfig, httpget); } finally { httpget.releaseConnection(); } assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); assertEquals("test", httpget.getParams(). getParameter(HttpMethodParams.USER_AGENT)); assertEquals("test", hostconfig.getParams(). getParameter(HttpMethodParams.USER_AGENT)); assertEquals("test", client.getParams(). getParameter(HttpMethodParams.USER_AGENT)); }
/** * Get a Protocol for the given parameters. The default implementation * selects a protocol based only on the scheme. Subclasses can do fancier * things, such as select SSL parameters based on the host or port. This * method must not return null. */ protected Protocol getProtocol(HostConfiguration old, String scheme, String host, int port) { final Protocol oldProtocol = old.getProtocol(); if (oldProtocol != null) { final String oldScheme = oldProtocol.getScheme(); if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) { // The old protocol has the desired scheme. return oldProtocol; // Retain it. } } Protocol newProtocol = (scheme != null && scheme.toLowerCase().endsWith("s")) ? httpsProtocol : httpProtocol; if (newProtocol == null) { newProtocol = Protocol.getProtocol(scheme); } return newProtocol; }
@Override protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL url) { Proxy proxy = null; String httpUser = null; String httpPassword = null; String serverUrl = url.getProtocol() + "://" + url.getHost(); TrackerWebServicesClient webServicesClient = TrackerClientManager .getInstance().getClient(serverUrl); proxy = webServicesClient.getProxy(); httpUser = webServicesClient.getHttpUser(); httpPassword = webServicesClient.getHttpPassword(); setupHttpClient(client, proxy, url.toString(), httpUser, httpPassword); return client.getHostConfiguration(); }
private void doSSL( HttpClient client, HostConfiguration hostConf, String host, int port){ if (hostConf.getProtocol().isSecure()) { // System.setProperty("javax.net.ssl.trustStore", sslKeystore); // Protocol sslPprotocol = new Protocol("https", new // org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory(), // 443); // client.getHostConfiguration().setHost(host, 443, sslPprotocol); try { ProtocolSocketFactory factory = new InsecureSSLProtocolSocketFactory(); Protocol https = new Protocol("https", factory, port); Protocol.registerProtocol("https", https); client.getHostConfiguration().setHost(host, port, https); } catch (GeneralSecurityException e) { throw new CoreException(CoreException.SSL, e); } } }
private synchronized void init() { client = new HttpClient(new MultiThreadedHttpConnectionManager()); HttpClientParams params = client.getParams(); if (encode != null && !encode.trim().equals("")) { params.setParameter("http.protocol.content-charset", encode); params.setContentCharset(encode); } if (timeout > 0) { params.setSoTimeout(timeout); } if (null != proxy) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(proxy.getHost(), proxy.getPort()); client.setHostConfiguration(hc); client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword())); } initialized = true; }
public int executeMethod(HostConfiguration hostconfig, HttpMethod method, HttpState state, boolean addCasTicket) throws IOException, HttpException { if (LOG.isDebugEnabled()) { LOG.debug("executeMethod(HostConfiguration, HttpMethod, HttpState) - entering"); } try { if (this.isAddCasTicketParams()) { HttpClientCAS.addCASTicket(method); } } catch (MotuCasException e) { throw new HttpException(e.notifyException(), e); } int returnint = super.executeMethod(hostconfig, method, state); if (LOG.isDebugEnabled()) { LOG.debug("executeMethod(HostConfiguration, HttpMethod, HttpState) - exiting"); } return returnint; }
/** * */ private void closeThreadLocalConnections() { // Does not need to be synchronised, as all access is from same thread Map<HostConfiguration, HttpClient> map = httpClients.get(); if ( map != null ) { for (HttpClient cl : map.values()) { // Can cause NPE in HttpClient 3.1 //((SimpleHttpConnectionManager)cl.getHttpConnectionManager()).shutdown();// Closes the connection // Revert to original method: cl.getHttpConnectionManager().closeIdleConnections(-1000);// Closes the connection } map.clear(); } }
public HTTPNotificationStrategy(PushNotificationConfig config) { this.config = config; if (this.config == null) { throw new InvalidConfigurationException("Properties Cannot be found"); } endpoint = config.getProperties().get(URL_PROPERTY); if (endpoint == null || endpoint.isEmpty()) { throw new InvalidConfigurationException("Property - 'url' cannot be found"); } try { this.uri = endpoint; URL url = new URL(endpoint); hostConfiguration = new HostConfiguration(); hostConfiguration.setHost(url.getHost(), url.getPort(), url.getProtocol()); this.authorizationHeaderValue = config.getProperties().get(AUTHORIZATION_HEADER_PROPERTY); executorService = Executors.newFixedThreadPool(1); httpClient = new HttpClient(); } catch (MalformedURLException e) { throw new InvalidConfigurationException("Property - 'url' is malformed.", e); } }
private void applyProxySettings() { String proxyHost = System.getProperty("http.proxyHost"); String proxyPortSt = System.getProperty("http.proxyPort"); int proxyPort = 0; try { if (proxyPortSt != null && proxyPortSt.length() > 0) { proxyPort = Integer.parseInt(proxyPortSt); } } catch (Exception e) { // nothing to do here } if (proxyHost != null && proxyHost.length() > 0) { HostConfiguration hostCfg = getHostConfiguration(); hostCfg.setProxy(proxyHost, proxyPort); Log_OC.d(TAG, "Proxy settings: " + proxyHost + ":" + proxyPort); } }
public String sendReport(Map<String, String> values) throws IOException { HttpClient httpClient = new HttpClient(); HostConfiguration hostConfiguration = new HostConfiguration(); if (!StringUtils.isBlank(proxy)) { hostConfiguration.setProxy(proxy, proxyPort); if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password)) { httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); } } httpClient.setHostConfiguration(hostConfiguration); PostMethod method = new PostMethod(getSendUrl()); addHttpPostParams(values, method); int executeMethod = httpClient.executeMethod(method); LOGGER.info("HTTP result of report send POST: " + executeMethod); return IOUtils.toString(method.getResponseBodyAsStream()); }
private static HTTPResponse _invoke(HttpMethod httpMethod, URL url, String username, String password, long timeout, int maxRedirect, String charset, String useragent, ProxyData proxy, Header[] headers, Map<String,String> params, Object body) throws IOException { HttpClient client = new HttpClient(); HostConfiguration config = client.getHostConfiguration(); HttpState state = client.getState(); setHeader(httpMethod,headers); if(CollectionUtil.isEmpty(params))setContentType(httpMethod,charset); setUserAgent(httpMethod,useragent); setTimeout(client,timeout); setParams(httpMethod,params); setCredentials(client,httpMethod,username,password); setProxy(config,state,proxy); if(body!=null && httpMethod instanceof EntityEnclosingMethod)setBody((EntityEnclosingMethod)httpMethod,body); return new HTTPResponse3Impl(execute(client,httpMethod,maxRedirect),url); }
/** * rewrite request method * @param method * @return * @throws MalformedURLException */ private static HttpMethod rewrite(HttpMethod method) throws MalformedURLException { org.apache.commons.httpclient.Header location = method.getResponseHeader("location"); if(location==null) return method; HostConfiguration config = method.getHostConfiguration(); URL url; try { url = new URL(location.getValue()); } catch (MalformedURLException e) { url=new URL(config.getProtocol().getScheme(), config.getHost(), config.getPort(), mergePath(method.getPath(),location.getValue())); } method= clone(method,url); return method; }
/** * @param target TransferTarget * @return HostConfiguration */ private HostConfiguration getHostConfig(TransferTarget target) { String requiredProtocol = target.getEndpointProtocol(); if (requiredProtocol == null) { throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {target.getEndpointProtocol()}); } Protocol protocol = protocolMap.get(requiredProtocol.toLowerCase().trim()); if (protocol == null) { log.error("Unsupported protocol: " + target.getEndpointProtocol()); throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {target.getEndpointProtocol()}); } HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(target.getEndpointHost(), target.getEndpointPort(), protocol); return hostConfig; }
/** * Test create target. * * @throws Exception */ public void testSuccessfulVerifyTargetOverHttp() throws Exception { //Stub HttpClient so that executeMethod returns a 200 response when(mockedHttpClient.executeMethod(any(HostConfiguration.class), any(HttpMethod.class), any(HttpState.class))).thenReturn(200); //Call verifyTarget transmitter.verifyTarget(target); ArgumentCaptor<HostConfiguration> hostConfig = ArgumentCaptor.forClass(HostConfiguration.class); ArgumentCaptor<HttpMethod> httpMethod = ArgumentCaptor.forClass(HttpMethod.class); ArgumentCaptor<HttpState> httpState = ArgumentCaptor.forClass(HttpState.class); verify(mockedHttpClient).executeMethod(hostConfig.capture(), httpMethod.capture(), httpState.capture()); assertTrue("post method", httpMethod.getValue() instanceof PostMethod); assertEquals("host name", TARGET_HOST, hostConfig.getValue().getHost()); assertEquals("port", HTTP_PORT, hostConfig.getValue().getPort()); assertEquals("protocol", HTTP_PROTOCOL.toLowerCase(), hostConfig.getValue().getProtocol().getScheme().toLowerCase()); assertEquals("path", TRANSFER_SERVICE_PATH + "/test", httpMethod.getValue().getPath()); }
protected void setUp() throws Exception { rs.setUp(); JMSConnectionMockupMQ.useJMSConnectionMockupMQ(); ChannelsTester.resetChannels(); Settings.set(CommonSettings.REMOTE_FILE_CLASS, "dk.netarkivet.common.distribute.TestRemoteFile"); TestFileUtils.copyDirectoryNonCVS(TestInfo.ORIGINALS_DIR, TestInfo.WORKING_DIR); TestFileUtils.copyDirectoryNonCVS(TestInfo.ORIGINALS_DIR, TestInfo.ARCHIVE_DIR); Settings.set(CommonSettings.CACHE_DIR, new File(TestInfo.WORKING_DIR, "cachedir").getAbsolutePath()); //Set up an HTTP client that can send commands to our proxy; int httpPort = Integer.parseInt(Settings.get( CommonSettings.HTTP_PORT_NUMBER)); httpClient = new HttpClient(); HostConfiguration hc = new HostConfiguration(); String hostName = "localhost"; hc.setProxy(hostName, httpPort); httpClient.setHostConfiguration(hc); }
/** * Queries the igniterealtime.org server for new server and plugin updates. * * @param notificationsEnabled true if admins will be notified when new updates are found. * @throws Exception if some error happens during the query. */ public synchronized void checkForServerUpdate(boolean notificationsEnabled) throws Exception { // Get the XML request to include in the HTTP request String requestXML = getServerUpdateRequest(); // Send the request to the server HttpClient httpClient = new HttpClient(); // Check if a proxy should be used if (isUsingProxy()) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(getProxyHost(), getProxyPort()); httpClient.setHostConfiguration(hc); } PostMethod postMethod = new PostMethod(updateServiceURL); NameValuePair[] data = { new NameValuePair("type", "update"), new NameValuePair("query", requestXML) }; postMethod.setRequestBody(data); if (httpClient.executeMethod(postMethod) == 200) { // Process answer from the server String responseBody = postMethod.getResponseBodyAsString(); processServerUpdateResponse(responseBody, notificationsEnabled); } }
public synchronized void checkForPluginsUpdates(boolean notificationsEnabled) throws Exception { // Get the XML request to include in the HTTP request String requestXML = getAvailablePluginsUpdateRequest(); // Send the request to the server HttpClient httpClient = new HttpClient(); // Check if a proxy should be used if (isUsingProxy()) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(getProxyHost(), getProxyPort()); httpClient.setHostConfiguration(hc); } PostMethod postMethod = new PostMethod(updateServiceURL); NameValuePair[] data = { new NameValuePair("type", "available"), new NameValuePair("query", requestXML) }; postMethod.setRequestBody(data); if (httpClient.executeMethod(postMethod) == 200) { // Process answer from the server String responseBody = postMethod.getResponseBodyAsString(); processAvailablePluginsResponse(responseBody, notificationsEnabled); } }
/** * * Public Section * */ public void connectHTTP(String strUser, String strPass, String strHost, boolean insecure) { //Connect WebDAV with credentials hostConfig = new HostConfiguration(); hostConfig.setHost(strHost); connectionManager = new MultiThreadedHttpConnectionManager(); connectionManagerParams = new HttpConnectionManagerParams(); connectionManagerParams.setMaxConnectionsPerHost(hostConfig, this.intMaxConnections); connectionManager.setParams(connectionManagerParams); client = new HttpClient(connectionManager); creds = new UsernamePasswordCredentials(strUser, strPass); client.getState().setCredentials(AuthScope.ANY, creds); client.setHostConfiguration(hostConfig); if (insecure) { Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", easyhttps); } Status.print("WebDav Connection generated"); }
/** * Configures the proxy using System properties, then returns an HostConfiguration object containing the proxy definition * * @return an instance of HostConfiguration containing the proxy definition, as per defined in the configuration file */ public HostConfiguration configureProxy() { if (isProxyNeeded()) { log.info("[EASYCUKES] Proxy required: Configuring HTTP Client..."); HostConfiguration hostCfg = new HostConfiguration(); hostCfg.setProxy(configurationBean.proxy.host, configurationBean.proxy.port); log.info(String.format("[EASYCUKES] Now using proxy: %s:%d", configurationBean.proxy.host, configurationBean.proxy.port)); System.setProperty("https.proxyHost", configurationBean.proxy.host); System.setProperty("https.proxyPort", Integer.toString(configurationBean.proxy.port)); if(configurationBean.proxy.byPassHost != null){ System.setProperty("http.nonProxyHosts", configurationBean.proxy.byPassHost); } return hostCfg; } return null; }
private void setProxy(HttpClient httpClient) { String proxyHost = AppSettings.get().get("http.proxy.host").trim(); Integer proxyPort = Integer.parseInt(AppSettings.get().get("http.proxy.port").trim()); HostConfiguration hostConfig = httpClient.getHostConfiguration(); hostConfig.setProxy(proxyHost, proxyPort); if (! AppSettings.get().get("http.proxy.user").equals("")) { String user; String pwd; org.apache.commons.httpclient.UsernamePasswordCredentials credentials; org.apache.commons.httpclient.auth.AuthScope authscope; user = AppSettings.get().get("http.proxy.user").trim(); pwd = AppSettings.get().get("http.proxy.password").trim(); credentials = new org.apache.commons.httpclient.UsernamePasswordCredentials(user, pwd); authscope = new org.apache.commons.httpclient.auth.AuthScope(proxyHost, proxyPort); httpClient.getState().setProxyCredentials(authscope, credentials); } }
@Test public void hostConfig() throws Exception { HttpClient client = new HttpClient(); client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT); client.getParams().setSoTimeout(SO_TIMEOUT); HostConfiguration config = new HostConfiguration(); config.setHost("weather.naver.com", 80, "http"); GetMethod method = new GetMethod("/rgn/cityWetrMain.nhn"); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); method.setQueryString(new NameValuePair[] { new NameValuePair("key2", "value2") }); try { // Execute the method. client.executeMethod(config, method); } catch (Exception ignored) { } finally { method.releaseConnection(); } PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); }
@Nullable private static HttpMethod handleCertificateExceptionAndRetry(@NotNull IOException e, @NotNull String host, @NotNull HttpClient client, @NotNull URI uri, @NotNull ThrowableConvertor<String, HttpMethod, IOException> methodCreator) throws IOException { if (!isCertificateException(e)) { throw e; } if (isTrusted(host)) { // creating a special configuration that allows connections to non-trusted HTTPS hosts // see the javadoc to EasySSLProtocolSocketFactory for details Protocol easyHttps = new Protocol("https", (ProtocolSocketFactory)new EasySSLProtocolSocketFactory(), 443); HostConfiguration hc = new HostConfiguration(); hc.setHost(host, 443, easyHttps); String relativeUri = new URI(uri.getPathQuery(), false).getURI(); // it is important to use relative URI here, otherwise our custom protocol won't work. // we have to recreate the method, because HttpMethod#setUri won't overwrite the host, // and changing host by hands (HttpMethodBase#setHostConfiguration) is deprecated. HttpMethod method = methodCreator.convert(relativeUri); client.executeMethod(hc, method); return method; } throw e; }
/** * Sets the maximum number of connections to be used for the given host config. * * @param hostConfiguration The host config to set the maximum for. Use * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value * per host. * @param maxHostConnections The maximum number of connections, <code>> 0</code> * * @see #MAX_HOST_CONNECTIONS */ public void setMaxConnectionsPerHost( HostConfiguration hostConfiguration, int maxHostConnections) { if (maxHostConnections <= 0) { throw new IllegalArgumentException("maxHostConnections must be greater than 0"); } Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS); // param values are meant to be immutable so we'll make a copy // to modify Map newValues = null; if (currentValues == null) { newValues = new HashMap(); } else { newValues = new HashMap(currentValues); } newValues.put(hostConfiguration, new Integer(maxHostConnections)); setParameter(MAX_HOST_CONNECTIONS, newValues); }
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()); } }