List of usage examples for org.apache.commons.httpclient HostConfiguration HostConfiguration
public HostConfiguration()
From source file:org.talend.designer.components.exchange.util.ExchangeUtils.java
public static String sendGetRequest(String urlAddress) throws Exception { HttpClient httpclient = new HttpClient(); GetMethod getMethod = new GetMethod(urlAddress); TransportClientProperties tcp = TransportClientPropertiesFactory.create("http"); if (tcp.getProxyHost().length() != 0) { UsernamePasswordCredentials creds = new UsernamePasswordCredentials( tcp.getProxyUser() != null ? tcp.getProxyUser() : "", tcp.getProxyPassword() != null ? tcp.getProxyUser() : ""); httpclient.getState().setProxyCredentials(AuthScope.ANY, creds); HostConfiguration hcf = new HostConfiguration(); hcf.setProxy(tcp.getProxyHost(), Integer.parseInt(tcp.getProxyPort())); httpclient.executeMethod(hcf, getMethod); } else {/*from w w w. j ava2s.c om*/ httpclient.executeMethod(getMethod); } String response = getMethod.getResponseBodyAsString(); getMethod.releaseConnection(); return response; }
From source file:org.talend.mdm.bulkload.client.BulkloadClientUtil.java
public static void bulkload(String url, String cluster, String concept, String datamodel, boolean validate, boolean smartpk, boolean insertonly, InputStream itemdata, String username, String password, String transactionId, String sessionId, String universe, String tokenKey, String tokenValue) throws Exception { HostConfiguration config = new HostConfiguration(); URI uri = new URI(url, false, "UTF-8"); //$NON-NLS-1$ config.setHost(uri);/*from ww w. ja v a 2 s. com*/ NameValuePair[] parameters = { new NameValuePair("cluster", cluster), //$NON-NLS-1$ new NameValuePair("concept", concept), //$NON-NLS-1$ new NameValuePair("datamodel", datamodel), //$NON-NLS-1$ new NameValuePair("validate", String.valueOf(validate)), //$NON-NLS-1$ new NameValuePair("action", "load"), //$NON-NLS-1$ //$NON-NLS-2$ new NameValuePair("smartpk", String.valueOf(smartpk)), //$NON-NLS-1$ new NameValuePair("insertonly", String.valueOf(insertonly)) }; //$NON-NLS-1$ HttpClient client = new HttpClient(); String user = universe == null || universe.trim().length() == 0 ? username : universe + "/" + username; //$NON-NLS-1$ client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); HttpClientParams clientParams = client.getParams(); clientParams.setAuthenticationPreemptive(true); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); PutMethod putMethod = new PutMethod(); // This setPath call is *really* important (if not set, request will be sent to the JBoss root '/') putMethod.setPath(url); String responseBody; try { // Configuration putMethod.setRequestHeader("Content-Type", "text/xml; charset=utf8"); //$NON-NLS-1$ //$NON-NLS-2$ if (transactionId != null) { putMethod.setRequestHeader("transaction-id", transactionId); //$NON-NLS-1$ } if (sessionId != null) { putMethod.setRequestHeader("Cookie", STICKY_SESSION + "=" + sessionId); //$NON-NLS-1$ //$NON-NLS-2$ } if (tokenKey != null && tokenValue != null) { putMethod.setRequestHeader(tokenKey, tokenValue); } putMethod.setQueryString(parameters); putMethod.setContentChunked(true); // Set the content of the PUT request putMethod.setRequestEntity(new InputStreamRequestEntity(itemdata)); client.executeMethod(config, putMethod); responseBody = putMethod.getResponseBodyAsString(); if (itemdata instanceof InputStreamMerger) { ((InputStreamMerger) itemdata).setAlreadyProcessed(true); } } catch (Exception e) { throw new RuntimeException(e); } finally { putMethod.releaseConnection(); } int statusCode = putMethod.getStatusCode(); if (statusCode >= 500) { throw new BulkloadException(responseBody); } else if (statusCode >= 400) { throw new BulkloadException("Could not send data to MDM (HTTP status code: " + statusCode + ")."); //$NON-NLS-1$ //$NON-NLS-2$ } }
From source file:org.tuckey.web.filters.urlrewrite.RequestProxy.java
/** * This method performs the proxying of the request to the target address. * * @param target The target address. Has to be a fully qualified address. The request is send as-is to this address. * @param hsRequest The request data which should be send to the * @param hsResponse The response data which will contain the data returned by the proxied request to target. * @throws java.io.IOException Passed on from the connection logic. *///from w w w . j a v a 2 s.c o m public static void execute(final String target, final HttpServletRequest hsRequest, final HttpServletResponse hsResponse) throws IOException { if (log.isInfoEnabled()) { log.info("execute, target is " + target); log.info("response commit state: " + hsResponse.isCommitted()); } if (StringUtils.isBlank(target)) { log.error("The target address is not given. Please provide a target address."); return; } log.info("checking url"); final URL url; try { url = new URL(target); } catch (MalformedURLException e) { log.error("The provided target url is not valid.", e); return; } log.info("seting up the host configuration"); final HostConfiguration config = new HostConfiguration(); ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy")); if (proxyHost != null) config.setProxyHost(proxyHost); final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort(); config.setHost(url.getHost(), port, url.getProtocol()); if (log.isInfoEnabled()) log.info("config is " + config.toString()); final HttpMethod targetRequest = setupProxyRequest(hsRequest, url); if (targetRequest == null) { log.error("Unsupported request method found: " + hsRequest.getMethod()); return; } //perform the reqeust to the target server final HttpClient client = new HttpClient(new SimpleHttpConnectionManager()); if (log.isInfoEnabled()) { log.info("client state" + client.getState()); log.info("client params" + client.getParams().toString()); log.info("executeMethod / fetching data ..."); } final int result; if (targetRequest instanceof EntityEnclosingMethod) { final RequestProxyCustomRequestEntity requestEntity = new RequestProxyCustomRequestEntity( hsRequest.getInputStream(), hsRequest.getContentLength(), hsRequest.getContentType()); final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) targetRequest; entityEnclosingMethod.setRequestEntity(requestEntity); result = client.executeMethod(config, entityEnclosingMethod); } else { result = client.executeMethod(config, targetRequest); } //copy the target response headers to our response setupResponseHeaders(targetRequest, hsResponse); InputStream originalResponseStream = targetRequest.getResponseBodyAsStream(); //the body might be null, i.e. for responses with cache-headers which leave out the body if (originalResponseStream != null) { OutputStream responseStream = hsResponse.getOutputStream(); copyStream(originalResponseStream, responseStream); } log.info("set up response, result code was " + result); }
From source file:org.wso2.carbon.device.mgt.extensions.push.notification.provider.http.HTTPNotificationStrategy.java
public HTTPNotificationStrategy(PushNotificationConfig config) { this.config = config; if (this.config == null) { throw new InvalidConfigurationException("Properties Cannot be found"); }/*from www.ja v a2 s . co m*/ 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); } }
From source file:pt.webdetails.cns.notifications.sparkl.kettle.baserver.web.utils.HttpConnectionHelper.java
public static Response callHttp(String url, String user, String password) throws IOException, KettleStepException { // used for calculating the responseTime long startTime = System.currentTimeMillis(); HttpClient httpclient = SlaveConnectionManager.getInstance().createHttpClient(); HttpMethod method = new GetMethod(url); httpclient.getParams().setAuthenticationPreemptive(true); Credentials credentials = new UsernamePasswordCredentials(user, password); httpclient.getState().setCredentials(AuthScope.ANY, credentials); HostConfiguration hostConfiguration = new HostConfiguration(); int status;/*from w w w .j av a 2 s. c o m*/ try { status = httpclient.executeMethod(hostConfiguration, method); } catch (IllegalArgumentException ex) { status = -1; } Response response = new Response(); if (status != -1) { String body = null; String encoding = ""; Header contentTypeHeader = method.getResponseHeader("Content-Type"); if (contentTypeHeader != null) { String contentType = contentTypeHeader.getValue(); if (contentType != null && contentType.contains("charset")) { encoding = contentType.replaceFirst("^.*;\\s*charset\\s*=\\s*", "").replace("\"", "").trim(); } } // get the response InputStreamReader inputStreamReader = null; if (!Const.isEmpty(encoding)) { inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream(), encoding); } else { inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream()); } StringBuilder bodyBuffer = new StringBuilder(); int c; while ((c = inputStreamReader.read()) != -1) { bodyBuffer.append((char) c); } inputStreamReader.close(); body = bodyBuffer.toString(); // Get response time long responseTime = System.currentTimeMillis() - startTime; response.setStatusCode(status); response.setResult(body); response.setResponseTime(responseTime); } return response; }
From source file:voldemort.client.HttpStoreClientFactory.java
public HttpStoreClientFactory(ClientConfig config) { super(config); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); this.httpClient = new HttpClient(connectionManager); this.httpClient.setHostConfiguration(hostConfig); HttpClientParams clientParams = this.httpClient.getParams(); clientParams.setConnectionManagerTimeout(config.getConnectionTimeout(TimeUnit.MILLISECONDS)); clientParams.setSoTimeout(config.getSocketTimeout(TimeUnit.MILLISECONDS)); clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); clientParams.setParameter("http.useragent", VOLDEMORT_USER_AGENT); HttpConnectionManagerParams managerParams = this.httpClient.getHttpConnectionManager().getParams(); managerParams.setConnectionTimeout(config.getConnectionTimeout(TimeUnit.MILLISECONDS)); managerParams.setMaxTotalConnections(config.getMaxTotalConnections()); managerParams.setStaleCheckingEnabled(false); managerParams.setMaxConnectionsPerHost(httpClient.getHostConfiguration(), config.getMaxConnectionsPerNode()); this.reroute = config.getRoutingTier().equals(RoutingTier.SERVER); this.requestFormatFactory = new RequestFormatFactory(); }
From source file:voldemort.performance.HttpClientBench.java
private static HttpClient createClient() { HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpClient httpClient = new HttpClient(connectionManager); HttpClientParams clientParams = httpClient.getParams(); clientParams.setConnectionManagerTimeout(DEFAULT_CONNECTION_MANAGER_TIMEOUT); clientParams.setSoTimeout(500);// ww w . ja v a 2 s .c o m clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); clientParams.setBooleanParameter("http.tcp.nodelay", false); clientParams.setIntParameter("http.socket.receivebuffer", 60000); clientParams.setParameter("http.useragent", VOLDEMORT_USER_AGENT); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost("localhost"); hostConfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); httpClient.setHostConfiguration(hostConfig); HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams(); managerParams.setConnectionTimeout(DEFAULT_CONNECTION_MANAGER_TIMEOUT); managerParams.setMaxTotalConnections(DEFAULT_MAX_CONNECTIONS); managerParams.setMaxConnectionsPerHost(httpClient.getHostConfiguration(), DEFAULT_MAX_HOST_CONNECTIONS); managerParams.setStaleCheckingEnabled(false); return httpClient; }
From source file:voldemort.performance.RemoteStoreComparisonTest.java
public static void main(String[] args) throws Exception { if (args.length != 2) Utils.croak("USAGE: java " + RemoteStoreComparisonTest.class.getName() + " numRequests numThreads [useNio]"); int numRequests = Integer.parseInt(args[0]); int numThreads = Integer.parseInt(args[1]); boolean useNio = args.length > 2 ? args[2].equals("true") : false; /** * In memory test ** */ final Store<byte[], byte[], byte[]> memStore = new InMemoryStorageEngine<byte[], byte[], byte[]>("test"); PerformanceTest memWriteTest = new PerformanceTest() { @Override// w w w .java 2s . co m public void doOperation(int i) { byte[] key = String.valueOf(i).getBytes(); memStore.put(key, new Versioned<byte[]>(key), null); } }; System.out.println("###########################################"); System.out.println("Performing memory write test."); memWriteTest.run(numRequests, numThreads); memWriteTest.printStats(); System.out.println(); PerformanceTest memReadTest = new PerformanceTest() { @Override public void doOperation(int i) { try { memStore.get(String.valueOf(i).getBytes(), null); } catch (Exception e) { System.out.println("Failure on i = " + i); e.printStackTrace(); } } }; System.out.println("Performing memory read test."); memReadTest.run(numRequests, numThreads); memReadTest.printStats(); System.out.println(); System.out.println(); /** * Do Socket tests ** */ String storeName = "test"; StoreRepository repository = new StoreRepository(); repository.addLocalStore(new InMemoryStorageEngine<ByteArray, byte[], byte[]>(storeName)); SocketStoreFactory storeFactory = new ClientRequestExecutorPool(10, 1000, 1000, 32 * 1024); final Store<ByteArray, byte[], byte[]> socketStore = storeFactory.create(storeName, "localhost", 6666, RequestFormatType.VOLDEMORT_V1, RequestRoutingType.NORMAL); RequestHandlerFactory factory = ServerTestUtils.getSocketRequestHandlerFactory(repository); AbstractSocketService socketService = ServerTestUtils.getSocketService(useNio, factory, 6666, 50, 50, 1000); socketService.start(); PerformanceTest socketWriteTest = new PerformanceTest() { @Override public void doOperation(int i) { byte[] bytes = String.valueOf(i).getBytes(); ByteArray key = new ByteArray(bytes); socketStore.put(key, new Versioned<byte[]>(bytes), null); } }; System.out.println("###########################################"); System.out.println("Performing socket write test."); socketWriteTest.run(numRequests, numThreads); socketWriteTest.printStats(); System.out.println(); PerformanceTest socketReadTest = new PerformanceTest() { @Override public void doOperation(int i) { try { socketStore.get(TestUtils.toByteArray(String.valueOf(i)), null); } catch (Exception e) { System.out.println("Failure on i = " + i); e.printStackTrace(); } } }; System.out.println("Performing socket read test."); socketReadTest.run(numRequests, 1); socketReadTest.printStats(); System.out.println(); System.out.println(); socketStore.close(); storeFactory.close(); socketService.stop(); /** * Do HTTP tests ** */ repository.addLocalStore(new InMemoryStorageEngine<ByteArray, byte[], byte[]>(storeName)); HttpService httpService = new HttpService(null, null, repository, RequestFormatType.VOLDEMORT_V0, numThreads, 8080); httpService.start(); HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); HttpClientParams clientParams = httpClient.getParams(); clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); clientParams.setParameter("http.useragent", "test-agent"); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); httpClient.setHostConfiguration(hostConfig); HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams(); managerParams.setConnectionTimeout(10000); managerParams.setMaxTotalConnections(numThreads); managerParams.setStaleCheckingEnabled(false); managerParams.setMaxConnectionsPerHost(httpClient.getHostConfiguration(), numThreads); final HttpStore httpStore = new HttpStore("test", "localhost", 8080, httpClient, new RequestFormatFactory().getRequestFormat(RequestFormatType.VOLDEMORT_V0), false); Thread.sleep(400); PerformanceTest httpWriteTest = new PerformanceTest() { @Override public void doOperation(int i) { byte[] key = String.valueOf(i).getBytes(); httpStore.put(new ByteArray(key), new Versioned<byte[]>(key), null); } }; System.out.println("###########################################"); System.out.println("Performing HTTP write test."); httpWriteTest.run(numRequests, numThreads); httpWriteTest.printStats(); System.out.println(); PerformanceTest httpReadTest = new PerformanceTest() { @Override public void doOperation(int i) { httpStore.get(new ByteArray(String.valueOf(i).getBytes()), null); } }; System.out.println("Performing HTTP read test."); httpReadTest.run(numRequests, numThreads); httpReadTest.printStats(); httpService.stop(); }
From source file:webdav.ManageWebDAV.java
public void connectWebDAVServer(String strUri, int intMaxConnections, String strUserId, String strPassword) { HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(strUri);//from w w w .j ava 2s . c o m HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setMaxConnectionsPerHost(hostConfig, intMaxConnections); connectionManager.setParams(params); this.client = new HttpClient(connectionManager); client.setHostConfiguration(hostConfig); Credentials creds = new UsernamePasswordCredentials(strUserId, strPassword); client.getState().setCredentials(AuthScope.ANY, creds); }
From source file:webdav.ManageWebDAVContacts.java
/** * * Public Section/*from w w w . j a v a 2 s . c o m*/ * */ 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"); }