List of usage examples for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER
String RETRY_HANDLER
To view the source code for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER.
Click Source Link
From source file:com.yahoo.flowetl.services.http.BaseHttpGenerator.java
/** * Applies any common properties to the http method based on properties of * the http params given.//from w ww. j av a 2s. c o m */ protected void applyCommonProperties(HttpParams in, HttpMethod toCall) { // common ops if (in.headers != null) { for (Entry<String, String> e : in.headers.entrySet()) { toCall.addRequestHeader(e.getKey(), e.getValue()); } } // we handle our own retries toCall.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); if (StringUtils.isBlank(in.userAgent) == false && toCall.getRequestHeader(USER_AGENT_HEADER) == null) { toCall.setRequestHeader(USER_AGENT_HEADER, in.userAgent); } else if (toCall.getRequestHeader(USER_AGENT_HEADER) == null) { toCall.setRequestHeader(USER_AGENT_HEADER, userAgent); } }
From source file:com.taobao.ad.easyschedule.action.schedule.ScheduleAction.java
private void controlRemoteInstance(String action) { try {/*from www. j a v a2 s . c om*/ if (!checkInstance(instance)) { return; } Long currentTime = System.currentTimeMillis() / 1000; String token = TokenUtils.generateToken(currentTime.toString()); HttpClient client = new HttpClient(); int connTimeout = 1000; client.getHttpConnectionManager().getParams().setConnectionTimeout(connTimeout); GetMethod getMethod = null; String controlInterface = configBO.getStringValue(ConfigDO.CONFIG_KEY_INSTANCE_CONTROL_INTERFACE); controlInterface = controlInterface.replaceAll("#instance#", getInstanceName(instance)); StringBuilder url = new StringBuilder(controlInterface); url = url.append(action); url = url.append("?").append(Const.TOKEN).append("=").append(token); url = url.append("&").append(Const.SIGNTIME).append("=").append(currentTime.toString()); url = url.append("&").append("userName").append("=").append(getLogname()); getMethod = new GetMethod(url.toString()); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); client.executeMethod(getMethod); } catch (Exception e) { } }
From source file:fr.openwide.talendalfresco.rest.client.ClientCommandBase.java
/** * Can be overriden to tune the default behaviour, * check or build parameters.../*from w ww . j a va2 s. c o m*/ * The client should set the params (query string) itself. */ public HttpMethodBase createMethod() throws RestClientException { // instantiating a new method and configuring it HttpMethodBase method; switch (this.getMethodType()) { case POST: //case PUT: method = new PostMethod(); break; case GET: default: method = new GetMethod(); method.setFollowRedirects(true); } method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); //method.setQueryString(params.toArray(new NameValuePair[0])); // done in client //method.getParams().setContentCharset(this.restEncoding); // done in client // set request entity (allow to fill method body for posts) : if (requestEntity != null && method instanceof EntityEnclosingMethod) { ((EntityEnclosingMethod) method).setRequestEntity(requestEntity); } return method; }
From source file:fr.cls.atoll.motu.library.misc.cas.TestCASRest.java
public static Cookie[] validateFromCAS2(String username, String password) throws Exception { String url = casServerUrlPrefix + "/v1/tickets?"; String s = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); s += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); HttpState initialState = new HttpState(); // Initial set of cookies can be retrieved from persistent storage and // re-created, using a persistence mechanism of choice, // Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false); // Create an instance of HttpClient. // HttpClient client = new HttpClient(); HttpClient client = new HttpClient(); Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 8443); URI uri = new URI(url + s, true); // use relative url only PostMethod httpget = new PostMethod(url); httpget.addParameter("username", username); httpget.addParameter("password", password); HostConfiguration hc = new HostConfiguration(); hc.setHost("atoll-dev.cls.fr", 8443, easyhttps); // client.executeMethod(hc, httpget); client.setState(initialState);/*from www. j a va2 s. c o m*/ // Create a method instance. System.out.println(url + s); GetMethod method = new GetMethod(url + s); // GetMethod method = new GetMethod(url ); HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setProxy("proxy.cls.fr", 8080); client.setHostConfiguration(hostConfiguration); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // String username = "xxx"; // String password = "xxx"; // Credentials credentials = new UsernamePasswordCredentials(username, password); // AuthScope authScope = new AuthScope("proxy.cls.fr", 8080); // // client.getState().setProxyCredentials(authScope, credentials); Cookie[] cookies = null; try { // Execute the method. // int statusCode = client.executeMethod(method); int statusCode = client.executeMethod(hc, httpget); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } for (Header header : method.getRequestHeaders()) { System.out.println(header.getName()); System.out.println(header.getValue()); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data System.out.println(new String(responseBody)); System.out.println("Response status code: " + statusCode); // Get all the cookies cookies = client.getState().getCookies(); // Display the cookies System.out.println("Present cookies: "); for (int i = 0; i < cookies.length; i++) { System.out.println(" - " + cookies[i].toExternalForm()); } Assertion assertion = AssertionHolder.getAssertion(); if (assertion == null) { System.out.println("<p>Assertion is null</p>"); } } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } return cookies; }
From source file:edu.uci.ics.external.connector.asterixdb.ConnectorUtils.java
public static void cleanDataset(StorageParameter storageParameter, DatasetInfo datasetInfo) throws Exception { // DDL service URL. String url = storageParameter.getServiceURL() + "/ddl"; // Builds the DDL string to delete and (re-)create the sink datset. StringBuilder ddlBuilder = new StringBuilder(); // use dataverse statement. ddlBuilder.append("use dataverse "); ddlBuilder.append(storageParameter.getDataverseName()); ddlBuilder.append(";"); // drop dataset statement. ddlBuilder.append("drop dataset "); ddlBuilder.append(storageParameter.getDatasetName()); ddlBuilder.append(";"); // create datset statement. ddlBuilder.append("create temporary dataset "); ddlBuilder.append(storageParameter.getDatasetName()); ddlBuilder.append("("); ddlBuilder.append(datasetInfo.getRecordType().getTypeName()); ddlBuilder.append(")"); ddlBuilder.append(" primary key "); for (String primaryKey : datasetInfo.getPrimaryKeyFields()) { ddlBuilder.append(primaryKey);//from ww w . ja v a2 s . c o m ddlBuilder.append(","); } ddlBuilder.delete(ddlBuilder.length() - 1, ddlBuilder.length()); ddlBuilder.append(";"); // Create a method instance. PostMethod method = new PostMethod(url); method.setRequestEntity(new StringRequestEntity(ddlBuilder.toString())); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Execute the method. executeHttpMethod(method); }
From source file:grafix.basedados.Download.java
public int baixaArquivo() { // Create an instance of HttpClient. HttpClient client = new HttpClient(); int retorno = 0; // Create a method instance. if (this.usaProxy) { client.getHostConfiguration().setProxy(this.servidorProxy, this.portaProxy); client.getState().setProxyCredentials(new AuthScope(this.servidorProxy, this.portaProxy), new UsernamePasswordCredentials(this.usuarioProxy, this.senhaProxy)); client.getParams().setAuthenticationPreemptive(true); }//from w w w . jav a 2 s. c om URI url2; String ff = null; try { url2 = new URI(this.getUrl(), false); ff = url2.toString(); } catch (URIException ex) { ex.printStackTrace(); } catch (NullPointerException ex) { ex.printStackTrace(); } GetMethod method = new GetMethod(ff); byte[] arquivo = null; long totalBytesRead = 0; long loopBytesRead = 0; // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); byte[] buffer = new byte[4096]; int progresso = 0; try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusCode() + " " + method.getStatusLine()); retorno = method.getStatusCode(); } else { long contentLength = method.getResponseContentLength(); File file = new File(this.getArquivo()); FileOutputStream os = new FileOutputStream(file); InputStream stream = method.getResponseBodyAsStream(); while ((loopBytesRead = stream.read(buffer)) != -1) { for (int i = 0; i < loopBytesRead; i++) { os.write(buffer[i]); } totalBytesRead += loopBytesRead; progresso = (int) ((float) totalBytesRead / contentLength * 100); if (progresso >= 0 || progresso <= 100) { // System.out.println("download " + progresso + " %"); if (this.mostraProgresso) formAtualizacao.definirPercentualProgresso(progresso); } } os.flush(); os.close(); stream.close(); } } catch (HttpException e) { retorno = 2; System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { retorno = 3; System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } if (this.mostraProgresso) formAtualizacao.definirPercentualProgresso(0); return retorno; }
From source file:net.bpelunit.framework.control.deploy.ode.ODEDeployer.java
public void deploy(String pathToTest, ProcessUnderTest put) throws DeploymentException { LOGGER.info("ODE deployer got request to deploy " + put); check(fArchive, "Archive Location"); check(fDeploymentAdminServiceURL, "deployment admin server URL"); String archivePath = getArchiveLocation(pathToTest); if (!new File(archivePath).exists()) { throw new DeploymentException(String.format("The archive location '%s' does not exist", archivePath)); }// w ww . j av a 2 s . c o m boolean archiveIsTemporary = false; if (!FilenameUtils.getName(archivePath).endsWith(".zip")) { // if the deployment is a directory and not a zip file if (new File(archivePath).isDirectory()) { archivePath = zipDirectory(new File(archivePath)); // Separate zip file was created and should be later cleaned up archiveIsTemporary = true; } else { throw new DeploymentException("Unknown archive format for the archive " + fArchive); } } java.io.File uploadingFile = new java.io.File(archivePath); // process the bundle for replacing wsdl eprs here. requires base url // string from specification loader. // should be done via the ODEDeploymentArchiveHandler. hard coded ode // process deployment string will be used // to construct the epr of the process wsdl. this requires the location // of put wsdl in order to obtain the // service name of the process in process wsdl. alternatively can use // inputs from deploymentsoptions. // test coverage logic. Moved to ProcessUnderTest deploy() method. HttpClient client = new HttpClient(new NoPersistenceConnectionManager()); PostMethod method = new PostMethod(fDeploymentAdminServiceURL); RequestEntity re = fFactory.getDeployRequestEntity(uploadingFile); method.setRequestEntity(re); LOGGER.info("ODE deployer about to send SOAP request to deploy " + put); // Provide custom retry handler if necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); method.addRequestHeader("SOAPAction", ""); String responseBody; int statusCode = 0; try { statusCode = client.executeMethod(method); responseBody = method.getResponseBodyAsString(); } catch (Exception e) { throw new DeploymentException("Problem contacting the ODE Server: " + e.getMessage(), e); } finally { method.releaseConnection(); if (uploadingFile.exists() && archiveIsTemporary) { uploadingFile.delete(); } } if (isHttpErrorCode(statusCode)) { throw new DeploymentException("ODE Server reported a Deployment Error: " + responseBody); } try { fProcessId = extractProcessId(responseBody); } catch (IOException e) { throw new DeploymentException("Problem extracting deployment information: " + e.getMessage(), e); } }
From source file:com.exalead.io.failover.FailoverHttpClient.java
public int executeMethod(HttpMethod method, int timeout, int retries) throws HttpException, IOException { if (manager.hosts.size() == 0) { logger.error("Could not execute method without any host."); throw new HttpException("Trying to execute methods without host"); }//from www . ja v a 2s . c o m // Fake config, the underlying manager manages all HostConfiguration config = new HostConfiguration(); /* Set method parameters */ method.getParams().setSoTimeout(timeout); /* DO NOT retry magically the method */ method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); if (manager.getCredentials() != null) { client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(new AuthScope(AuthScope.ANY), manager.getCredentials()); } IOException fail = null; for (int i = 1; i <= retries; ++i) { try { return client.executeMethod(config, method); } catch (IOException e) { logger.warn("Failed to execute method - try " + i + "/" + retries); fail = e; continue; } } logger.warn("exception in executeMethod: " + fail.getMessage()); throw fail; }
From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java
private HttpMethod executeMethod(HttpMethod httpMethod) { for (Header header : this.headers.getHeaders()) { httpMethod.setRequestHeader(header); }// www .j a v a 2 s .co m httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new KsfxHttpRetryHandler(retryCount, retryDelay)); try { int tryCount = 0; int statusCode; do { if (tryCount > 1) { httpMethod = createNewHttpMethod(httpMethod); try { if (retryDelay == 0) { retryDelay = DEFAULT_RETRY_DELAY; } Thread.sleep(retryDelay); } catch (InterruptedException e) { logger.severe("InterruptedException"); } } //PROXY Configuration /* if (torify) { String proxyHost = ""; Integer proxyPort = 0; try { proxyHost = SpiderConfiguration.getConfiguration().getString("torifyHost"); proxyPort = SpiderConfiguration.getConfiguration().getInt("torifyPort"); } catch (Exception e) { logger.severe("Cannot get Proxy information"); } this.httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); } */ statusCode = this.httpClient.executeMethod(httpMethod); tryCount++; } while (!(statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_FORBIDDEN || statusCode == HttpStatus.SC_NOT_FOUND) && tryCount < retryCount); if (statusCode != HttpStatus.SC_OK) { System.out.println("HTTP method failed: " + httpMethod.getStatusLine() + " - " + httpMethod.getURI().toString()); } } catch (HttpException e) { e.printStackTrace(); httpMethod.abort(); try { logger.log(Level.SEVERE, "Redirrex " + e.getClass(), e); if (e.getClass().equals(RedirectException.class)) { logger.log(Level.SEVERE, "Is real redirect exception", e); throw new RuntimeException("HttpRedirectException"); } logger.log(Level.SEVERE, "HTTP protocol error for URL: " + httpMethod.getURI().toString(), e); } catch (URIException e1) { e.printStackTrace(); logger.log(Level.SEVERE, "URI exception", e); } throw new RuntimeException("HttpException"); } catch (IOException e) { try { e.printStackTrace(); logger.log(Level.SEVERE, "HTTP transport error for URL: " + httpMethod.getURI().toString(), e); } catch (URIException e1) { e.printStackTrace(); logger.log(Level.SEVERE, "URI exception", e); } throw new RuntimeException("IOException"); } return httpMethod; }
From source file:com.cloud.storage.template.HttpTemplateDownloader.java
public HttpTemplateDownloader(StorageLayer storageLayer, String downloadUrl, String toDir, DownloadCompleteCallback callback, long maxTemplateSizeInBytes, String user, String password, Proxy proxy, ResourceType resourceType) { this._storage = storageLayer; this.downloadUrl = downloadUrl; this.setToDir(toDir); this.status = TemplateDownloader.Status.NOT_STARTED; this.resourceType = resourceType; this.MAX_TEMPLATE_SIZE_IN_BYTES = maxTemplateSizeInBytes; this.totalBytes = 0; this.client = new HttpClient(s_httpClientManager); myretryhandler = new HttpMethodRetryHandler() { public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) { if (executionCount >= 2) { // Do not retry if over max retry count return false; }// w ww . j a v a2 s . c o m if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }; try { this.request = new GetMethod(downloadUrl); this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); this.completionCallback = callback; //this.request.setFollowRedirects(false); File f = File.createTempFile("dnld", "tmp_", new File(toDir)); if (_storage != null) { _storage.setWorldReadableAndWriteable(f); } toFile = f.getAbsolutePath(); Pair<String, Integer> hostAndPort = validateUrl(downloadUrl); if (proxy != null) { client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); if (proxy.getUserName() != null) { Credentials proxyCreds = new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword()); client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds); } } if ((user != null) && (password != null)) { client.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(user, password); client.getState().setCredentials( new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); } else { s_logger.info( "No credentials configured for host=" + hostAndPort.first() + ":" + hostAndPort.second()); } } catch (IllegalArgumentException iae) { errorString = iae.getMessage(); status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; inited = false; } catch (Exception ex) { errorString = "Unable to start download -- check url? "; status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; s_logger.warn("Exception in constructor -- " + ex.toString()); } catch (Throwable th) { s_logger.warn("throwable caught ", th); } }