List of usage examples for org.apache.commons.httpclient.params HttpClientParams HttpClientParams
public HttpClientParams()
From source file:eionet.cr.harvest.PullHarvest.java
/** * * @return// w w w . j a v a 2 s .c o m */ private EndpointHttpClient prepareEndpointHttpClient() { HttpConnectionManagerParams managerParams = new HttpConnectionManagerParams(); managerParams.setDefaultMaxConnectionsPerHost(20); managerParams.setStaleCheckingEnabled(false); int httpTimeout = GeneralConfig.getIntProperty(GeneralConfig.HARVESTER_HTTP_TIMEOUT, getTimeout()); managerParams.setConnectionTimeout(httpTimeout); managerParams.setSoTimeout(httpTimeout); HttpConnectionManager manager = new MultiThreadedHttpConnectionManager(); manager.setParams(managerParams); HttpClientParams clientParams = new HttpClientParams(); clientParams.setParameter("http.useragent", URLUtil.userAgentHeader()); clientParams.setParameter("http.protocol.max-redirects", MAX_REDIRECTIONS); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Connection", "close"); clientParams.setParameter("additionalHTTPHeaders", headers); return new EndpointHttpClient(clientParams, manager); }
From source file:de.ingrid.portal.scheduler.jobs.UpgradeClientJob.java
private InputStream getFeed(String url) { try {//w w w.j a v a2 s . c om HttpClientParams httpClientParams = new HttpClientParams(); HttpConnectionManager httpConnectionManager = new SimpleHttpConnectionManager(); httpClientParams.setSoTimeout(30 * 1000); httpConnectionManager.getParams().setConnectionTimeout(30 * 1000); httpConnectionManager.getParams().setSoTimeout(30 * 1000); HttpClient client = new HttpClient(httpClientParams, httpConnectionManager); HttpMethod method = new GetMethod(url); setCredentialsIfAny(client); int status = client.executeMethod(method); if (status == 200) { log.debug("Successfully received: " + url); return method.getResponseBodyAsStream(); } else { log.error("Response code for '" + url + "' was: " + status); return null; } } catch (HttpException e) { log.error("An HTTP-Exception occured when calling: " + url + " -> " + e.getMessage()); } catch (IOException e) { log.error("An IO-Exception occured when calling: " + url + " -> " + e.getMessage()); } return null; }
From source file:com.piaoyou.util.ImageUtil.java
@SuppressWarnings("finally") public static boolean compressImg(String imageUrl1, String path, int width, int height) { HttpClient client = null;/* w w w . j a v a 2s . c o m*/ GetMethod getMethod = null; boolean b = true; try { URI Url = new URI(imageUrl1, false, "UTF-8"); String imageUrl = Url.toString(); client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager()); // client.getHttpConnectionManager().getParams().setConnectionTimeout(2000); // ? client.getHttpConnectionManager().getParams().setSoTimeout(2000); String temStr = ""; if (imageUrl.contains(".jpg")) { imageUrl = imageUrl.replace(" ", "BLANK"); temStr = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.lastIndexOf(".")); imageUrl = imageUrl.replace(temStr, URLEncoder.encode(temStr, "UTF-8")); if (imageUrl.contains("eachnet")) imageUrl = URLEncoder.encode(imageUrl, "UTF-8"); imageUrl = imageUrl.replaceAll("BLANK", "%20").replaceAll("%3A", ":").replaceAll("%2F", "/"); } getMethod = new GetMethod(imageUrl); client.executeMethod(getMethod); ImageUtil.createThumbnail(getMethod.getResponseBodyAsStream(), path, width, height); } catch (Exception e) { b = false; delFile(path); e.printStackTrace(); } finally { if (getMethod != null) { getMethod.releaseConnection(); } return b; } }
From source file:com.piaoyou.util.ImageUtil.java
public static void compressImgSmall(String imageUrl, String path, int width, int height) { HttpClient client = null;//from www . ja va 2s.c om GetMethod getMethod = null; try { client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager()); getMethod = new GetMethod(imageUrl); client.executeMethod(getMethod); ImageUtil.createThumbnail(getMethod.getResponseBodyAsStream(), path, width, height); } catch (Exception e) { } finally { if (getMethod != null) getMethod.releaseConnection(); } }
From source file:com.zimbra.common.util.ZimbraHttpConnectionManager.java
private static String dumpParams(String notes, HttpConnectionManagerParams connMgrParams, HttpClientParams clientParams) { // dump httpclient package defaults if params is null if (connMgrParams == null) connMgrParams = new HttpConnectionManagerParams(); if (clientParams == null) clientParams = new HttpClientParams(); StringBuilder sb = new StringBuilder(); sb.append("======== " + notes + "========\n"); sb.append("HttpConnectionManagerParams DefaultMaxConnectionsPerHost : " + connMgrParams.getDefaultMaxConnectionsPerHost() + "\n"); sb.append("HttpConnectionManagerParams MaxTotalConnections : " + connMgrParams.getMaxTotalConnections() + "\n"); sb.append("HttpConnectionParams ConnectionTimeout : " + connMgrParams.getConnectionTimeout() + "\n"); sb.append(/*w ww . j ava2 s .c o m*/ "HttpConnectionParams Linger : " + connMgrParams.getLinger() + "\n"); sb.append("HttpConnectionParams ReceiveBufferSize : " + connMgrParams.getReceiveBufferSize() + "\n"); sb.append("HttpConnectionParams SendBufferSize : " + connMgrParams.getSendBufferSize() + "\n"); sb.append("HttpConnectionParams SoTimeout : " + connMgrParams.getSoTimeout() + "\n"); sb.append("HttpConnectionParams TcpNoDelay : " + connMgrParams.getTcpNoDelay() + "\n"); sb.append("HttpConnectionParams isStaleCheckingEnabled : " + connMgrParams.isStaleCheckingEnabled() + "\n"); // sb.append("HttpClientParams ALLOW_CIRCULAR_REDIRECTS (no corresponding method?) sb.append("HttpClientParams ConnectionManagerClass : " + clientParams.getConnectionManagerClass().getName() + "\n"); sb.append("HttpClientParams ConnectionManagerTimeout : " + clientParams.getConnectionManagerTimeout() + "\n"); // sb.append("HttpClientParams MAX_REDIRECTS (no corresponding method?) sb.append("HttpClientParams isAuthenticationPreemptive() : " + clientParams.isAuthenticationPreemptive() + "\n"); // sb.append("HttpClientParams REJECT_RELATIVE_REDIRECT (no corresponding method?) return sb.toString(); }
From source file:com.intellij.lang.jsgraphql.ide.project.JSGraphQLLanguageUIProjectService.java
public void executeGraphQL(Editor editor, VirtualFile virtualFile) { final JSGraphQLEndpointsModel endpointsModel = editor.getUserData(JS_GRAPH_QL_ENDPOINTS_MODEL); if (endpointsModel != null) { final JSGraphQLEndpoint selectedEndpoint = endpointsModel.getSelectedItem(); if (selectedEndpoint != null && selectedEndpoint.url != null) { final JSGraphQLQueryContext context = JSGraphQLQueryContextHighlightVisitor .getQueryContextBufferAndHighlightUnused(editor); final Map<String, Object> requestData = Maps.newLinkedHashMap(); requestData.put("query", context.query); try { requestData.put("variables", getQueryVariables(editor)); } catch (JsonSyntaxException jse) { if (myToolWindowManagerInitialized) { myToolWindowManager.logCurrentErrors(ContainerUtil.immutableList( new JSGraphQLErrorResult("Failed to parse variables as JSON: " + jse.getMessage(), virtualFile.getPath(), "Error", 0, 0)), true);//from w w w . j av a 2 s . co m } return; } final String requestJson = new Gson().toJson(requestData); final HttpClient httpClient = new HttpClient(new HttpClientParams()); try { final PostMethod method = new PostMethod(selectedEndpoint.url); setHeadersFromOptions(selectedEndpoint, method); method.setRequestEntity(new StringRequestEntity(requestJson, "application/json", "UTF-8")); ApplicationManager.getApplication().executeOnPooledThread(() -> { try { try { editor.putUserData(JS_GRAPH_QL_EDITOR_QUERYING, true); StopWatch sw = new StopWatch(); sw.start(); httpClient.executeMethod(method); final String responseJson = Optional.fromNullable(method.getResponseBodyAsString()) .or(""); sw.stop(); final Integer errorCount = getErrorCount(responseJson); if (fileEditor instanceof TextEditor) { final TextEditor textEditor = (TextEditor) fileEditor; UIUtil.invokeLaterIfNeeded(() -> { ApplicationManager.getApplication().runWriteAction(() -> { final Document document = textEditor.getEditor().getDocument(); document.setText(responseJson); if (requestJson.startsWith("{")) { final PsiFile psiFile = PsiDocumentManager.getInstance(myProject) .getPsiFile(document); if (psiFile != null) { new ReformatCodeProcessor(psiFile, false).run(); } } }); final StringBuilder queryResultText = new StringBuilder( virtualFile.getName()).append(": ").append(sw.getTime()) .append(" ms execution time, ") .append(bytesToDisplayString(responseJson.length())) .append(" response"); if (errorCount != null && errorCount > 0) { queryResultText.append(", ").append(errorCount).append(" error") .append(errorCount > 1 ? "s" : ""); if (context.onError != null) { context.onError.run(); } } queryResultLabel.setText(queryResultText.toString()); queryResultLabel.putClientProperty(FILE_URL_PROPERTY, virtualFile.getUrl()); if (!queryResultLabel.isVisible()) { queryResultLabel.setVisible(true); } querySuccessLabel.setVisible(errorCount != null); if (querySuccessLabel.isVisible()) { if (errorCount == 0) { querySuccessLabel .setBorder(BorderFactory.createEmptyBorder(2, 8, 0, 0)); querySuccessLabel.setIcon(AllIcons.General.InspectionsOK); } else { querySuccessLabel .setBorder(BorderFactory.createEmptyBorder(2, 12, 0, 4)); querySuccessLabel.setIcon(AllIcons.Ide.ErrorPoint); } } showToolWindowContent(myProject, fileEditor.getComponent().getClass()); textEditor.getEditor().getScrollingModel().scrollVertically(0); }); } } finally { editor.putUserData(JS_GRAPH_QL_EDITOR_QUERYING, null); } } catch (IOException e) { Notifications.Bus.notify( new Notification("GraphQL", "GraphQL Query Error", selectedEndpoint.url + ": " + e.getMessage(), NotificationType.WARNING), myProject); } }); } catch (UnsupportedEncodingException | IllegalStateException | IllegalArgumentException e) { Notifications.Bus.notify( new Notification("GraphQL", "GraphQL Query Error", selectedEndpoint.url + ": " + e.getMessage(), NotificationType.ERROR), myProject); } } } }
From source file:com.cws.esolutions.core.utils.NetworkUtils.java
/** * Creates an HTTP connection to a provided website and returns the data back * to the requestor.// ww w . ja v a 2 s .c o m * * @param hostName - The fully qualified URL for the host desired. MUST be * prefixed with http/https:// as necessary * @param methodType - GET or POST, depending on what is necessary * @return A object containing the response data * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing */ public static final synchronized Object executeHttpConnection(final URL hostName, final String methodType) throws UtilityException { final String methodName = NetworkUtils.CNAME + "#executeHttpConnection(final URL hostName, final String methodType) throws UtilityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", hostName); DEBUGGER.debug("Value: {}", methodType); } RequestConfig requestConfig = null; CloseableHttpClient httpClient = null; CredentialsProvider credsProvider = null; CloseableHttpResponse httpResponse = null; final HttpClientParams httpParams = new HttpClientParams(); final HTTPConfig httpConfig = appBean.getConfigData().getHttpConfig(); final ProxyConfig proxyConfig = appBean.getConfigData().getProxyConfig(); if (DEBUG) { DEBUGGER.debug("HttpClient: {}", httpClient); DEBUGGER.debug("HttpClientParams: {}", httpParams); DEBUGGER.debug("HTTPConfig: {}", httpConfig); DEBUGGER.debug("ProxyConfig: {}", proxyConfig); } try { final URI requestURI = new URIBuilder().setScheme(hostName.getProtocol()).setHost(hostName.getHost()) .setPort(hostName.getPort()).build(); if (StringUtils.isNotEmpty(httpConfig.getTrustStoreFile())) { System.setProperty("javax.net.ssl.trustStoreType", (StringUtils.isNotEmpty(httpConfig.getTrustStoreType()) ? httpConfig.getTrustStoreType() : "jks")); System.setProperty("javax.net.ssl.trustStore", httpConfig.getTrustStoreFile()); System.setProperty("javax.net.ssl.trustStorePassword", PasswordUtils.decryptText(httpConfig.getTrustStorePass(), httpConfig.getTrustStoreSalt(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getIterations(), secBean.getConfigData().getSecurityConfig().getKeyBits(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getEncryptionInstance(), appBean.getConfigData().getSystemConfig().getEncoding())); } if (StringUtils.isNotEmpty(httpConfig.getKeyStoreFile())) { System.setProperty("javax.net.ssl.keyStoreType", (StringUtils.isNotEmpty(httpConfig.getKeyStoreType()) ? httpConfig.getKeyStoreType() : "jks")); System.setProperty("javax.net.ssl.keyStore", httpConfig.getKeyStoreFile()); System.setProperty("javax.net.ssl.keyStorePassword", PasswordUtils.decryptText(httpConfig.getKeyStorePass(), httpConfig.getKeyStoreSalt(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getIterations(), secBean.getConfigData().getSecurityConfig().getKeyBits(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getEncryptionInstance(), appBean.getConfigData().getSystemConfig().getEncoding())); } if (proxyConfig.isProxyServiceRequired()) { if (DEBUG) { DEBUGGER.debug("ProxyConfig: {}", proxyConfig); } if (StringUtils.isEmpty(proxyConfig.getProxyServerName())) { throw new UtilityException( "Configuration states proxy usage is required, but no proxy is configured."); } if (proxyConfig.isProxyAuthRequired()) { List<String> authList = new ArrayList<String>(); authList.add(AuthPolicy.BASIC); authList.add(AuthPolicy.DIGEST); authList.add(AuthPolicy.NTLM); if (DEBUG) { DEBUGGER.debug("authList: {}", authList); } requestConfig = RequestConfig.custom() .setConnectionRequestTimeout( (int) TimeUnit.SECONDS.toMillis(httpConfig.getConnTimeout())) .setConnectTimeout((int) TimeUnit.SECONDS.toMillis(httpConfig.getConnTimeout())) .setContentCompressionEnabled(Boolean.TRUE) .setProxy(new HttpHost(proxyConfig.getProxyServerName(), proxyConfig.getProxyServerPort())) .setProxyPreferredAuthSchemes(authList).build(); if (DEBUG) { DEBUGGER.debug("requestConfig: {}", requestConfig); } String proxyPwd = PasswordUtils.decryptText(proxyConfig.getProxyPassword(), proxyConfig.getProxyPwdSalt(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getIterations(), secBean.getConfigData().getSecurityConfig().getKeyBits(), secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(), secBean.getConfigData().getSecurityConfig().getEncryptionInstance(), appBean.getConfigData().getSystemConfig().getEncoding()); if (DEBUG) { DEBUGGER.debug("proxyPwd: {}", proxyPwd); } if (StringUtils.equals(NetworkUtils.PROXY_AUTH_TYPE_BASIC, proxyConfig.getProxyAuthType())) { credsProvider = new SystemDefaultCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyConfig.getProxyServerName(), proxyConfig.getProxyServerPort()), new UsernamePasswordCredentials(proxyConfig.getProxyUserId(), proxyPwd)); } else if (StringUtils.equals(NetworkUtils.PROXY_AUTH_TYPE_NTLM, proxyConfig.getProxyAuthType())) { credsProvider = new SystemDefaultCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyConfig.getProxyServerName(), proxyConfig.getProxyServerPort()), new NTCredentials(proxyConfig.getProxyUserId(), proxyPwd, InetAddress.getLocalHost().getHostName(), proxyConfig.getProxyAuthDomain())); } if (DEBUG) { DEBUGGER.debug("httpClient: {}", httpClient); } } } synchronized (new Object()) { httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); if (StringUtils.equalsIgnoreCase(methodType, "POST")) { HttpPost httpMethod = new HttpPost(requestURI); httpMethod.setConfig(requestConfig); httpResponse = httpClient.execute(httpMethod); } else { HttpGet httpMethod = new HttpGet(requestURI); httpMethod.setConfig(requestConfig); httpResponse = httpClient.execute(httpMethod); } int responseCode = httpResponse.getStatusLine().getStatusCode(); if (DEBUG) { DEBUGGER.debug("responseCode: {}", responseCode); } if (responseCode != 200) { ERROR_RECORDER.error("HTTP Response Code received NOT 200: " + responseCode); throw new UtilityException("HTTP Response Code received NOT 200: " + responseCode); } return httpResponse.getEntity().toString(); } } catch (ConnectException cx) { throw new UtilityException(cx.getMessage(), cx); } catch (UnknownHostException ux) { throw new UtilityException(ux.getMessage(), ux); } catch (SocketException sx) { throw new UtilityException(sx.getMessage(), sx); } catch (IOException iox) { throw new UtilityException(iox.getMessage(), iox); } catch (URISyntaxException usx) { throw new UtilityException(usx.getMessage(), usx); } finally { if (httpResponse != null) { try { httpResponse.close(); } catch (IOException iox) { } // dont do anything with it } } }
From source file:io.hops.hopsworks.api.jobs.JobService.java
/** * Get the job ui for the specified job. * This act as a proxy to get the job ui from yarn * <p>//from w ww . j a v a2 s.c om * @param appId * @param param * @param sc * @param req * @return */ @GET @Path("/{appId}/prox/{path: .+}") @Produces(MediaType.WILDCARD) @AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST }) public Response getProxy(@PathParam("appId") final String appId, @PathParam("path") final String param, @Context SecurityContext sc, @Context HttpServletRequest req) { Response response = checkAccessRight(appId); if (response != null) { return response; } try { String trackingUrl; if (param.matches("http([a-zA-Z,:,/,.,0-9,-])+:([0-9])+(.)+")) { trackingUrl = param; } else { trackingUrl = "http://" + param; } trackingUrl = trackingUrl.replace("@hwqm", "?"); if (!hasAppAccessRight(trackingUrl)) { LOGGER.log(Level.SEVERE, "A user is trying to access an app outside their project!"); return Response.status(Response.Status.FORBIDDEN).build(); } org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(trackingUrl, false); HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); final HttpMethod method = new GetMethod(uri.getEscapedURI()); Enumeration<String> names = req.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); String value = req.getHeader(name); if (PASS_THROUGH_HEADERS.contains(name)) { //yarn does not send back the js if encoding is not accepted //but we don't want to accept encoding for the html because we //need to be able to parse it if (!name.toLowerCase().equals("accept-encoding") || trackingUrl.contains(".js")) { method.setRequestHeader(name, value); } } } String user = req.getRemoteUser(); if (user != null && !user.isEmpty()) { method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII")); } client.executeMethod(method); Response.ResponseBuilder responseBuilder = noCacheResponse .getNoCacheResponseBuilder(Response.Status.OK); for (Header header : method.getResponseHeaders()) { responseBuilder.header(header.getName(), header.getValue()); } //method.getPath().contains("/allexecutors") is needed to replace the links under Executors tab //which are in a json response object if (method.getResponseHeader("Content-Type") == null || method.getResponseHeader("Content-Type").getValue().contains("html") || method.getPath().contains("/allexecutors")) { final String source = "http://" + method.getURI().getHost() + ":" + method.getURI().getPort(); if (method.getResponseHeader("Content-Length") == null) { responseBuilder.entity(new StreamingOutput() { @Override public void write(OutputStream out) throws IOException, WebApplicationException { Writer writer = new BufferedWriter(new OutputStreamWriter(out)); InputStream stream = method.getResponseBodyAsStream(); Reader in = new InputStreamReader(stream, "UTF-8"); char[] buffer = new char[4 * 1024]; String remaining = ""; int n; while ((n = in.read(buffer)) != -1) { StringBuilder strb = new StringBuilder(); strb.append(buffer, 0, n); String s = remaining + strb.toString(); remaining = s.substring(s.lastIndexOf(">") + 1, s.length()); s = hopify(s.substring(0, s.lastIndexOf(">") + 1), param, appId, source); writer.write(s); } writer.flush(); } }); } else { String s = hopify(method.getResponseBodyAsString(), param, appId, source); responseBuilder.entity(s); responseBuilder.header("Content-Length", s.length()); } } else { responseBuilder.entity(new StreamingOutput() { @Override public void write(OutputStream out) throws IOException, WebApplicationException { InputStream stream = method.getResponseBodyAsStream(); org.apache.hadoop.io.IOUtils.copyBytes(stream, out, 4096, true); out.flush(); } }); } return responseBuilder.build(); } catch (Exception e) { LOGGER.log(Level.SEVERE, "exception while geting job ui " + e.getLocalizedMessage(), e); return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).build(); } }
From source file:fr.cls.atoll.motu.library.misc.netcdf.NetCdfReader.java
public void initNetcdfHttpClient() throws MotuException { try {// w w w .j a v a2 s . c om synchronized (this) { Field field = NetcdfDataset.class.getDeclaredField("httpClient"); field.setAccessible(true); HttpClient httpClientNetcdfDataset = (HttpClient) field.get(null); HttpClientCAS httpClientCAS = null; field = DConnect2.class.getDeclaredField("_httpClient"); field.setAccessible(true); HttpClient httpClientDConnect2 = (HttpClient) field.get(null); field = HTTPRandomAccessFile.class.getDeclaredField("_client"); field.setAccessible(true); HttpClient httpClientHTTPRandomAccessFile = (HttpClient) field.get(null); if ((httpClientNetcdfDataset != null) && (httpClientDConnect2 != null) && (httpClientHTTPRandomAccessFile != null)) { return; } if ((httpClientNetcdfDataset == null) && (httpClientDConnect2 == null) && (httpClientHTTPRandomAccessFile == null)) { final int SO_TIMEOUT_VALUE = 180000; // in milliseconds MultiThreadedHttpConnectionManager connectionManager = HttpUtil.createConnectionManager(); // WARNING: because socket read can raise an infinite time out, set an arbitrary socket read time out connectionManager.getParams().setSoTimeout(SO_TIMEOUT_VALUE); // in milliseconds httpClientCAS = new HttpClientCAS(connectionManager); HttpClientParams httpClientParams = new HttpClientParams(); httpClientParams.setParameter("http.protocol.allow-circular-redirects", true); httpClientCAS.setParams(httpClientParams); NetcdfDataset.setHttpClient(httpClientCAS); connectionManager = HttpUtil.createConnectionManager(); // WARNING: because socket read can raise an infinite time out, set an arbitrary socket read time out connectionManager.getParams().setSoTimeout(SO_TIMEOUT_VALUE); // in milliseconds httpClientCAS = new HttpClientCAS(connectionManager); httpClientParams = new HttpClientParams(); httpClientParams.setParameter("http.protocol.allow-circular-redirects", true); httpClientCAS.setParams(httpClientParams); DConnect2.setHttpClient(httpClientCAS); connectionManager = HttpUtil.createConnectionManager(); // WARNING: because socket read can raise an infinite time out, set an arbitrary socket read time out connectionManager.getParams().setSoTimeout(SO_TIMEOUT_VALUE); // in milliseconds httpClientCAS = new HttpClientCAS(connectionManager); httpClientParams = new HttpClientParams(); httpClientParams.setParameter("http.protocol.allow-circular-redirects", true); httpClientCAS.setParams(httpClientParams); HTTPRandomAccessFile.setHttpClient(httpClientCAS); } if ((httpClientNetcdfDataset != null) && !(httpClientNetcdfDataset instanceof HttpClientCAS)) { throw new MotuException(String.format( "Error in NetCdfReader acquireDataset - httpClientNetcdfDataset has been set but is no an HttpClientCAS object:'%s'", httpClientNetcdfDataset.getClass().getName())); } if ((httpClientDConnect2 != null) && !(httpClientDConnect2 instanceof HttpClientCAS)) { throw new MotuException(String.format( "Error in NetCdfReader acquireDataset - httpClientDConnect2 has been set but is no an HttpClientCAS object:'%s'", httpClientDConnect2.getClass().getName())); } if ((httpClientHTTPRandomAccessFile != null) && !(httpClientHTTPRandomAccessFile instanceof HttpClientCAS)) { throw new MotuException(String.format( "Error in NetCdfReader acquireDataset - httpClientHTTPRandomAccessFile has been set but is no an HttpClientCAS object:'%s'", httpClientHTTPRandomAccessFile.getClass().getName())); } } } catch (MotuException e) { throw e; } catch (Exception e) { throw new MotuException( "Error in NetCdfReader initNetcdfHttpClient - Unable to initialize httpClient object", e); } }
From source file:com.amazonaws.a2s.AmazonA2SClient.java
/** * Configure HttpClient with set of defaults as well as configuration * from AmazonA2SConfig instance// www . j av a 2 s . c om * */ private HttpClient configureHttpClient() { /* Set http client parameters */ HttpClientParams httpClientParams = new HttpClientParams(); httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent()); httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) { if (executionCount > 3) { log.debug("Maximum Number of Retry attempts reached, will not retry"); return false; } log.debug("Retrying request. Attempt " + executionCount); if (exception instanceof NoHttpResponseException) { log.debug("Retrying on NoHttpResponseException"); return true; } if (!method.isRequestSent()) { log.debug("Retrying on failed sent request"); return true; } return false; } }); /* Set host configuration */ HostConfiguration hostConfiguration = new HostConfiguration(); /* Set connection manager parameters */ HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams(); connectionManagerParams.setConnectionTimeout(50000); connectionManagerParams.setSoTimeout(50000); connectionManagerParams.setStaleCheckingEnabled(true); connectionManagerParams.setTcpNoDelay(true); connectionManagerParams.setMaxTotalConnections(3); connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, 3); /* Set connection manager */ MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.setParams(connectionManagerParams); /* Set http client */ httpClient = new HttpClient(httpClientParams, connectionManager); /* Set proxy if configured */ if (config.isSetProxyHost() && config.isSetProxyPort()) { log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: " + config.getProxyPort()); hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort()); } httpClient.setHostConfiguration(hostConfiguration); return httpClient; }