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:org.easyframework.core.crawl.GetSample.java
public static Map<String, String> get(String url) { Map<String, String> result = new HashMap<String, String>(); HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try {/*w ww. ja v a 2 s.c om*/ int statusCode = client.executeMethod(method); String charset = method.getResponseCharSet(); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } byte[] responseBody = method.getResponseBody(); result.put(GetSample.RESPONSE_TEXT, new String(responseBody, charset)); result.put(GetSample.CHARSET, charset); } 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 { method.releaseConnection(); return result; } }
From source file:org.eclipse.andmore.android.localization.translators.GoogleTranslator.java
/** * Creates an HTTP request with the URL, execute it as a get, and returns * the a string with the result.// ww w. ja va 2 s . co m * * @param url * URL to be executed. * @return String with the URL execution result. * @throws IOException * If an exception occurs on transport * @throws HttpException * If an exception occurs on the protocol * @throws Exception * on error. */ protected static String executeHttpGetRequest(final URL url) throws HttpException { // Checking query size due to google policies if (url.toString().length() > MAX_QUERY_SIZE) { throw new HttpException(TranslateNLS.GoogleTranslator_Error_QueryTooBig); } // Try to retrieve proxy configuration to use if necessary IProxyService proxyService = ProxyManager.getProxyManager(); IProxyData proxyData = null; if (proxyService.isProxiesEnabled() || proxyService.isSystemProxiesEnabled()) { Authenticator.setDefault(new ProxyAuthenticator()); String urlStr = url.toString(); if (urlStr.startsWith("https")) { proxyData = proxyService.getProxyData(IProxyData.HTTPS_PROXY_TYPE); AndmoreLogger.debug(GoogleTranslator.class, "Using https proxy"); //$NON-NLS-1$ } else if (urlStr.startsWith("http")) { proxyData = proxyService.getProxyData(IProxyData.HTTP_PROXY_TYPE); AndmoreLogger.debug(GoogleTranslator.class, "Using http proxy"); //$NON-NLS-1$ } else { AndmoreLogger.debug(GoogleTranslator.class, "Not using any proxy"); //$NON-NLS-1$ } } // Creates the http client and the method to be executed HttpClient client = null; client = new HttpClient(); // If there is proxy data, work with it if (proxyData != null) { if (proxyData.getHost() != null) { // Sets proxy host and port, if any client.getHostConfiguration().setProxy(proxyData.getHost(), proxyData.getPort()); } if (proxyData.getUserId() != null && proxyData.getUserId().trim().length() > 0) { // Sets proxy user and password, if any Credentials cred = new UsernamePasswordCredentials(proxyData.getUserId(), proxyData.getPassword() == null ? "" : proxyData.getPassword()); //$NON-NLS-1$ client.getState().setProxyCredentials(AuthScope.ANY, cred); } } // Creating the method to be executed, the URL at this point is enough // because it is complete GetMethod method = new GetMethod(url.toString()); // Set method to be retried three times in case of error method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(RETRIES, false)); method.setRequestHeader(REFERER_HEADER, REFERER_SITE); // Set the connection timeout client.getHttpConnectionManager().getParams().setConnectionTimeout(new Integer(TIMEOUT)); String result = ""; //$NON-NLS-1$ try { // Execute the method. int statusCode; try { statusCode = client.executeMethod(method); result = method.getResponseBodyAsString(MAX_SIZE); } catch (IOException e) { throw new HttpException(TranslateNLS.GoogleTranslator_Error_CannotConnectToServer + e.getMessage()); } checkStatusCode(statusCode, result); // Unescape any possible unicode char result = unescapeUnicode(result); // Unescape any possible HTML sequence result = unescapeHTML(result); } finally { // Release the connection. method.releaseConnection(); } return result; }
From source file:org.eclipse.mylyn.commons.tests.net.WebUtilTest.java
public void testLocationSslConnectProxyTimeout() throws Exception { String url = "https://foo/bar"; final Proxy proxy = new Proxy(Type.HTTP, proxyAddress); AbstractWebLocation location = new WebLocation(url, null, null, new IProxyProvider() { public Proxy getProxyForHost(String host, String proxyType) { return proxy; }// w ww. j av a2 s . com }); HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null); testProxy.addResponse(TestProxy.OK); GetMethod method = new GetMethod("/"); // avoid second attempt to connect to proxy to get exception right away method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) { return false; } }); try { int statusCode = client.executeMethod(hostConfiguration, method); fail("Expected SSLHandshakeException, got status: " + statusCode); } catch (SSLHandshakeException e) { } catch (SocketException e) { // connection reset, happens in some environments instead of SSLHandshakeExecption depending on how much data has been written before the socket is closed } Message request = testProxy.getRequest(); assertEquals("CONNECT foo:443 HTTP/1.1", request.request); }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static boolean isAlive(URL url) { boolean isAlive = false; boolean recheck = true; String key = toIsAliveKey(url); Pair<Integer, Long> lastChecked = isAliveMap.get(key); if (lastChecked != null) { long checkedOffset = System.currentTimeMillis() - lastChecked.getSecond().longValue(); if (checkedOffset < CHECK_WINDOW) { recheck = false;//from ww w . ja v a2 s.com isAlive = lastChecked.getFirst() != -1; } } if (recheck) { try { GetMethod method = new GetMethod(url.toString()); try { HttpMethodParams params = new HttpMethodParams(); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); params.setSoTimeout(1000); method.setParams(params); int responseCode = executeMethod(url, method); if (responseCode == HttpURLConnection.HTTP_NOT_FOUND || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_OK) { isAlive = true; } } finally { method.releaseConnection(); } } catch (Exception ex) { // Do Nothing } } return isAlive; }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static String put(URL url, InputStream inputStream, String contentType, String encoding) throws OseeCoreException { int statusCode = -1; String response = null;/*from ww w.j a v a 2 s. c o m*/ PutMethod method = new PutMethod(url.toString()); InputStream responseInputStream = null; AcquireResult result = new AcquireResult(); try { method.setRequestHeader(CONTENT_ENCODING, encoding); method.setRequestEntity(new InputStreamRequestEntity(inputStream, contentType)); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); statusCode = executeMethod(url, method); responseInputStream = method.getResponseBodyAsStream(); result.setContentType(getContentType(method)); result.setEncoding(method.getResponseCharSet()); if (statusCode != HttpURLConnection.HTTP_CREATED) { String exceptionString = Lib.inputStreamToString(responseInputStream); throw new OseeCoreException(exceptionString); } else { responseInputStream = method.getResponseBodyAsStream(); response = Lib.inputStreamToString(responseInputStream); } } catch (Exception ex) { OseeExceptions.wrapAndThrow(ex); } finally { Lib.close(responseInputStream); method.releaseConnection(); } return response; }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static AcquireResult delete(URL url, String xml, String contentType, String encoding, OutputStream outputStream) throws OseeCoreException { AcquireResult result = new AcquireResult(); int statusCode = -1; org.eclipse.osee.framework.core.util.DeleteMethod method = new DeleteMethod(url.toString()); InputStream httpInputStream = null; try {//from w w w . j av a2 s . c om method.setRequestHeader(CONTENT_ENCODING, encoding); method.setRequestEntity(new InputStreamRequestEntity(Lib.stringToInputStream(xml), contentType)); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); statusCode = executeMethod(url, method); httpInputStream = method.getResponseBodyAsStream(); result.setContentType(getContentType(method)); result.setEncoding(method.getResponseCharSet()); if (statusCode == HttpStatus.SC_ACCEPTED || statusCode == HttpStatus.SC_OK) { Lib.inputStreamToOutputStream(httpInputStream, outputStream); } else { String exceptionString = Lib.inputStreamToString(httpInputStream); throw new OseeCoreException(exceptionString); } } catch (Exception ex) { OseeExceptions.wrapAndThrow(ex); } finally { Lib.close(httpInputStream); result.setCode(statusCode); method.releaseConnection(); } return result; }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static AcquireResult post(URL url, InputStream inputStream, String contentType, String encoding, OutputStream outputStream) throws OseeCoreException { AcquireResult result = new AcquireResult(); int statusCode = -1; PostMethod method = new PostMethod(url.toString()); InputStream httpInputStream = null; try {/*from w w w . j av a2 s .c o m*/ method.setRequestHeader(CONTENT_ENCODING, encoding); method.setRequestEntity(new InputStreamRequestEntity(inputStream, contentType)); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); statusCode = executeMethod(url, method); httpInputStream = method.getResponseBodyAsStream(); result.setContentType(getContentType(method)); result.setEncoding(method.getResponseCharSet()); if (statusCode == HttpStatus.SC_ACCEPTED || statusCode == HttpStatus.SC_OK) { Lib.inputStreamToOutputStream(httpInputStream, outputStream); } else { String exceptionString = Lib.inputStreamToString(httpInputStream); throw new OseeCoreException(exceptionString); } } catch (Exception ex) { OseeExceptions.wrapAndThrow(ex); } finally { Lib.close(httpInputStream); result.setCode(statusCode); method.releaseConnection(); } return result; }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static String post(URL url) throws Exception { AcquireResult result = new AcquireResult(); String response = null;//from ww w . j av a 2 s . com int statusCode = -1; PostMethod method = new PostMethod(url.toString()); InputStream responseInputStream = null; try { method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); statusCode = executeMethod(url, method); responseInputStream = method.getResponseBodyAsStream(); result.setContentType(getContentType(method)); result.setEncoding(method.getResponseCharSet()); if (statusCode != HttpStatus.SC_ACCEPTED) { String exceptionString = Lib.inputStreamToString(responseInputStream); throw new Exception(exceptionString); } else { responseInputStream = method.getResponseBodyAsStream(); response = Lib.inputStreamToString(responseInputStream); } } finally { Lib.close(responseInputStream); method.releaseConnection(); } return response; }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static AcquireResult acquire(URL url, OutputStream outputStream, int soTimeout) throws Exception { AcquireResult result = new AcquireResult(); int statusCode = -1; GetMethod method = new GetMethod(url.toString()); InputStream inputStream = null; try {/*from w w w. j a va2s .c o m*/ method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); method.getParams().setSoTimeout(soTimeout); statusCode = executeMethod(url, method); result.setEncoding(method.getResponseCharSet()); result.setContentType(getContentType(method)); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_ACCEPTED) { inputStream = method.getResponseBodyAsStream(); Lib.inputStreamToOutputStream(inputStream, outputStream); } else { String response = method.getResponseBodyAsString(); result.setResult(response); } } catch (Exception ex) { throw new Exception( String.format("Error acquiring resource: [%s] - status code: [%s]", url, statusCode), ex); } finally { Lib.close(inputStream); result.setCode(statusCode); method.releaseConnection(); } return result; }
From source file:org.eclipse.osee.framework.core.util.HttpProcessor.java
public static String delete(URL url) throws Exception { String response = null;//from w w w .j a va 2 s. c om int statusCode = -1; DeleteMethod method = new DeleteMethod(url.toString()); InputStream responseInputStream = null; try { method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); statusCode = executeMethod(url, method); if (statusCode == HttpStatus.SC_ACCEPTED) { responseInputStream = method.getResponseBodyAsStream(); response = Lib.inputStreamToString(responseInputStream); } } catch (Exception ex) { throw new Exception(String.format("Error deleting resource: [%s] - status code: [%s]", url, statusCode), ex); } finally { Lib.close(responseInputStream); method.releaseConnection(); } return response; }