List of usage examples for org.apache.commons.httpclient DefaultHttpMethodRetryHandler DefaultHttpMethodRetryHandler
public DefaultHttpMethodRetryHandler()
From source file:com.taobao.ad.easyschedule.action.schedule.ScheduleAction.java
private SchedulerDTO getSchedulerInfo(String instanceName) { SchedulerDTO result = null;//w w w. ja v a 2 s . c o m try { HttpClient client = new HttpClient(); int connTimeout = 1000; client.getHttpConnectionManager().getParams().setConnectionTimeout(connTimeout); GetMethod getMethod = null; String url = configBO.getStringValue(ConfigDO.CONFIG_KEY_INSTANCE_CONTROL_INTERFACE); url = url.replaceAll("#instance#", instanceName); url = url + "getInstanceAjax"; getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = client.executeMethod(getMethod); if (statusCode == HttpStatus.SC_OK) { result = JSONObject.parseObject(getMethod.getResponseBodyAsString(), SchedulerDTO.class); } } catch (Exception e) { } return result; }
From source file:com.ning.http.client.providers.apache.ApacheAsyncHttpProvider.java
public ApacheAsyncHttpProvider(AsyncHttpClientConfig config) { this.config = config; connectionManager = new MultiThreadedHttpConnectionManager(); params = new HttpClientParams(); params.setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, Boolean.TRUE); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); AsyncHttpProviderConfig<?, ?> providerConfig = config.getAsyncHttpProviderConfig(); if (providerConfig != null && ApacheAsyncHttpProvider.class.isAssignableFrom(providerConfig.getClass())) { configure(ApacheAsyncHttpProviderConfig.class.cast(providerConfig)); }// www .j a va 2 s. c o m }
From source file:com.netflix.dynomitemanager.sidecore.utils.WarmBootstrapTask.java
private boolean sendCommand(String cmd) { DynamicStringProperty adminUrl = DynamicPropertyFactory.getInstance() .getStringProperty("florida.metrics.url", "http://localhost:22222"); String url = adminUrl.get() + cmd; HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); GetMethod get = new GetMethod(url); try {// w w w . jav a 2 s. c o m int statusCode = client.executeMethod(get); if (!(statusCode == 200)) { logger.error("Got non 200 status code from " + url); return false; } String response = get.getResponseBodyAsString(); //logger.info("Received response from " + url + "\n" + response); if (!response.isEmpty()) { logger.info("Received response from " + url + "\n" + response); } else { logger.error("Cannot parse empty response from " + url); return false; } } catch (Exception e) { logger.error("Failed to sendCommand and invoke url: " + url, e); return false; } return true; }
From source file:com.taobao.ad.easyschedule.commons.utils.JobUtil.java
/** * //from w ww. j ava2 s . com * @param jobId * @param jobDetail * @param target * @return */ public static JobResult executeRemoteJob(Long jobId, JobDetail jobDetail, String[] target) { JobResult result = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR, " "); try { JobDataMap data = jobDetail.getJobDataMap(); HttpClient client = new HttpClient(); int retries = JobUtil.getJobRetries(data.getString(Constants.JOBDATA_RETRIES)); client.getHttpConnectionManager().getParams().setConnectionTimeout(Constants.JOB_MIN_CONN_TIMEOUT); client.getHttpConnectionManager().getParams().setSoTimeout(Constants.JOB_MAX_SO_TIMEOUT); GetMethod getMethod = null; for (int i = 1; i <= retries + 1; i++) { try { getMethod = new GetMethod(JobUtil.getJobTargetUrl(jobId, jobDetail, target)); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); } catch (UnsupportedEncodingException e) { logger.error("ShellJob.execute,jobCommand:URL", e); result = JobResult.errorResult(JobResult.RESULTCODE_PARAMETER_ILLEGAL, "URL" + e.getMessage()); return result; } try { int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { ignoreLogger.warn(jobDetail.getFullName() + "" + i + "statuscode:" + statusCode); /*logger.warn(jobDetail.getFullName() + "" + i + "statuscode:" + statusCode);*/ result = JobResult.errorResult(JobResult.RESULTCODE_JOB_REQUEST_FAILURE, "? " + statusCode); Thread.sleep(Constants.JOB_RETRY_WAITTIME); if (i > 1) { result.getData().put(JobResult.JOBRESULT_DATA_RETRYCOUNT, String.valueOf(i)); } continue; } if (Constants.JOBDATA_CHECKRESULT_VAL_FALSE .equals(data.getString(Constants.JOBDATA_CHECKRESULT))) { result = new JobResult(true, JobResult.RESULTCODE_JOBRESULT_IGNORE, "" + i + "??"); break; } try { result = JSONObject.parseObject(getMethod.getResponseBodyAsString(), JobResult.class); if (i > 1) { result.getData().put(JobResult.JOBRESULT_DATA_RETRYCOUNT, String.valueOf(i)); } } catch (Exception e) { ignoreLogger.error(jobDetail.getFullName() + "?:" + e.getMessage()); /*logger.error(jobDetail.getFullName() + "?:" + e.getMessage());*/ result = JobResult.errorResult(JobResult.RESULTCODE_JOBRESULT_ILLEGAL, "?" + StringUtil.html(e.getMessage())); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } break; } catch (Exception e) { ignoreLogger.error(jobDetail.getFullName() + "" + i + "" + e); /*logger.error(jobDetail.getFullName() + "" + i + "" + e);*/ result = JobResult.errorResult(JobResult.RESULTCODE_JOB_REQUEST_FAILURE, "" + i + "" + e.getMessage()); try { Thread.sleep(Constants.JOB_RETRY_WAITTIME); } catch (InterruptedException e1) { } continue; } } } catch (Exception e) { logger.error("Job", e); result.setResultMsg(result.getResultMsg() + e.getMessage()); } return result; }
From source file:com.ning.http.client.providers.apache.TestableApacheAsyncHttpProvider.java
public TestableApacheAsyncHttpProvider(AsyncHttpClientConfig config) { super(config); this.config = config; connectionManager = new MultiThreadedHttpConnectionManager(); params = new HttpClientParams(); params.setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, Boolean.TRUE); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); }
From source file:com.silverwrist.venice.std.TrackbackManager.java
/** * Loads the HTTP content at the specified URL, scans it for RDF description blocks, and adds those blocks * as {@link com.silverwrist.venice.std.TrackbackItem TrackbackItem}s to our internal cache. Uses modification * detection to keep from reloading a page unless necessary. * * @param url The URL of the resource to be loaded. * @param attrs The attributes of the specified page; if this is <code>null</code>, we'll check the page * cache for the right attributes. * @return <code>true</code> if the page data was loaded and scanned for trackback items; <code>false</code> * if no data was loaded (because it was not modified since the last time we loaded it, for instance). * @exception com.silverwrist.venice.except.TrackbackException If there was an error loading or interpreting * the page data./*from www . j a v a2 s .c o m*/ */ private synchronized boolean load(URL url, PageAttributes attrs) throws TrackbackException { if (attrs == null) attrs = (PageAttributes) (m_page_cache.get(url)); // Create the GET method and set its headers. String s = url.toString(); int x = s.lastIndexOf('#'); if (x >= 0) s = s.substring(0, x); GetMethod getter = new GetMethod(s); HttpMethodParams params = getter.getParams(); getter.setDoAuthentication(false); getter.setFollowRedirects(true); getter.setRequestHeader("User-Agent", USER_AGENT); getter.setRequestHeader("Accept", "text/*"); getter.setRequestHeader("Accept-Encoding", "identity"); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); getter.setParams(params); boolean get_resp = false; PageAttributes newattrs = null; ContentType ctype = null; byte[] rawdata = null; try { // set the Last-Modified date as an If-Modified-Since header on the request java.util.Date lmod = null; if (attrs != null) lmod = attrs.getLastModified(); if (lmod != null) getter.setRequestHeader("If-Modified-Since", s_httpdate_format.format(lmod)); // execute the Get method! int rc = m_http_client.executeMethod(getter); get_resp = true; if ((lmod != null) && (rc == HttpStatus.SC_NOT_MODIFIED)) return false; // we were not modified if (rc == HttpStatus.SC_NO_CONTENT) return false; // there's no content there if (rc != HttpStatus.SC_OK) // this is farked! throw new TrackbackException("GET of " + url + " returned " + rc); // Get the new page attributes and save them off. newattrs = new PageAttributes(getter); m_page_cache.put(url, newattrs); // Get the Content-Type header and see if it's valid. Header hdr = getter.getResponseHeader("Content-Type"); if (hdr != null) s = hdr.getValue(); else s = "text/plain"; // necessary assumption ctype = new ContentType(s); if (!(ctype.getPrimaryType().equals("text"))) throw new TrackbackException("URL " + url + " does not point to a text-based resource"); // Load the resource in as byte data; we will determine the right character set for it later. rawdata = getter.getResponseBody(); get_resp = false; } // end try catch (IOException e) { // IO error getting the page throw new TrackbackException("I/O error retrieving " + url + ": " + e.getMessage(), e); } // end catch catch (javax.mail.internet.ParseException e) { // translate into TrackbackException throw new TrackbackException("invalid Content-Type received for URL " + url, e); } // end catch finally { // release the connection if possible try { // need to get the message body if (get_resp) getter.getResponseBody(); } // end try catch (IOException e) { // ignore these } // end catch getter.releaseConnection(); } // end finally // make a first guess at the charset from the HTTP header Content-Type String cset = ctype.getParameter("charset"); if (cset == null) cset = "US-ASCII"; String content = null; try { // interpret the content content = new String(rawdata, cset); } // end try catch (UnsupportedEncodingException e) { // fall back and try just using US-ASCII cset = null; try { // interpret the content content = new String(rawdata, "US-ASCII"); } // end try catch (UnsupportedEncodingException e2) { // can't happen logger.debug("WTF? US-ASCII should damn well be a supported character set!", e2); } // end catch } // end catch // Look for <META HTTP-EQUIV=...> tags in the content. Map http_attrs = extractHttpEquivTags(content); // Try to get a Content-Type attribute from there. s = (String) (http_attrs.get("CONTENT-TYPE")); String cset2 = null; if (s != null) { // look for the content type try { // parse into Content-Type ContentType c = new ContentType(s); if (c.getPrimaryType().equals("text")) cset2 = c.getParameter("charset"); } // end try catch (javax.mail.internet.ParseException e) { // can't get a second Content-Type logger.debug("parse of Content-Type from META tags failed", e); cset2 = null; } // end catch } // end if if ((cset == null) && (cset2 == null)) throw new TrackbackException("unable to determine character set for " + url); if ((cset2 != null) && ((cset == null) || !(cset.equalsIgnoreCase(cset2)))) { // reinterpret content in new character set try { // reinterpret content in new character set s = new String(rawdata, cset2); content = s; // the contents of the HTTP-EQUIV tags may have changed as a result http_attrs = extractHttpEquivTags(content); } // end try catch (UnsupportedEncodingException e) { // just use original character set if (cset == null) throw new TrackbackException("unable to determine character set for " + url); } // end catch } // end if newattrs.updateFromPage(http_attrs); // update the page attributes from the META tag data // Search the page content for RDF blocks. RE m = new RE(s_rdf_start, RE.MATCH_NORMAL); int pos = 0; while (m.match(content, pos)) { // look for the end of this RDF block RE m2 = new RE(getEndRecognizer(m.getParen(1)), RE.MATCH_NORMAL); if (m2.match(content, m.getParenEnd(0))) { // we now have a block to feed to the XML parser try { // run the block through the XML parser InputSource isrc = new InputSource( new StringReader(content.substring(m.getParenStart(0), m2.getParenEnd(0)))); Document doc = m_rdf_parser.parse(isrc); // examine topmost element, which should be rdf:RDF Element root = doc.getDocumentElement(); if (NS_RDF.equals(root.getNamespaceURI()) && (root.getLocalName() != null) && root.getLocalName().equals("RDF")) { // this is most definitely an rdf:RDF node...look for rdf:Description nodes under it NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { // check each node in the list Node n = nl.item(i); if ((n.getNodeType() == Node.ELEMENT_NODE) && NS_RDF.equals(n.getNamespaceURI()) && (n.getLocalName() != null) && n.getLocalName().equals("Description")) { // we've got an rdf:Description node...extract the attributes from it Element elt = (Element) n; try { // look for the item and trackback URLs URL item = null, trackback = null; s = elt.getAttributeNS(NS_DC, "identifier"); if ((s != null) && (s.length() > 0)) item = new URL(s); s = elt.getAttributeNS(NS_TRACKBACK, "ping"); if ((s != null) && (s.length() > 0)) trackback = new URL(s); if ((item != null) && (trackback != null)) { // create the item s = elt.getAttributeNS(NS_DC, "title"); m_item_cache.put(item, new MyTrackbackItem(item, trackback, s, newattrs)); } // end if } // end try catch (MalformedURLException e) { // this means skip this item logger.warn("URL parse failure", e); } // end catch } // end if } // end for } // end if } // end try catch (IOException e) { // disregard this block logger.warn("RDF block parse failure", e); } // end catch catch (SAXException e) { // disregard this block logger.warn("RDF block parse failure", e); } // end catch } // end if // else ignore this possible block pos = m.getParenEnd(0); } // end while return true; }
From source file:com.eucalyptus.imaging.backend.ImagingTaskStateManager.java
private boolean doesManifestExist(final String manifestUrl) throws Exception { // validate urls per EUCA-9144 final UrlValidator urlValidator = new UrlValidator(); if (!urlValidator.isEucalyptusUrl(manifestUrl)) throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + manifestUrl); final HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 30000); GetMethod method = new GetMethod(manifestUrl); String manifest = null;/* ww w . j ava 2 s .c om*/ try { // avoid TCP's CLOSE_WAIT method.setRequestHeader("Connection", "close"); client.executeMethod(method); manifest = method.getResponseBodyAsString(ImageConfiguration.getInstance().getMaxManifestSizeBytes()); if (manifest == null) { return false; } else if (manifest.contains("<Code>NoSuchKey</Code>") || manifest.contains("The specified key does not exist")) { return false; } } catch (final Exception ex) { return false; } finally { method.releaseConnection(); } final List<String> partsUrls = getPartsHeadUrl(manifest); for (final String url : partsUrls) { if (!urlValidator.isEucalyptusUrl(url)) throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + url); HeadMethod partCheck = new HeadMethod(url); int res = client.executeMethod(partCheck); if (res != HttpStatus.SC_OK) { return false; } } return true; }
From source file:com.eucalyptus.imaging.ImagingTaskStateManager.java
private boolean doesManifestExist(final String manifestUrl) throws Exception { // validate urls per EUCA-9144 final UrlValidator urlValidator = new UrlValidator(); if (!urlValidator.isEucalyptusUrl(manifestUrl)) throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + manifestUrl); final HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 30000); GetMethod method = new GetMethod(manifestUrl); String manifest = null;/* w w w.ja v a 2 s .c o m*/ try { // avoid TCP's CLOSE_WAIT method.setRequestHeader("Connection", "close"); client.executeMethod(method); manifest = method.getResponseBodyAsString(); if (manifest == null) { return false; } else if (manifest.contains("<Code>NoSuchKey</Code>") || manifest.contains("The specified key does not exist")) { return false; } } catch (final Exception ex) { return false; } finally { method.releaseConnection(); } final List<String> partsUrls = getPartsHeadUrl(manifest); for (final String url : partsUrls) { if (!urlValidator.isEucalyptusUrl(url)) throw new RuntimeException("Manifest's URL is not in the Eucalyptus format: " + url); HeadMethod partCheck = new HeadMethod(url); int res = client.executeMethod(partCheck); if (res != HttpStatus.SC_OK) { return false; } } return true; }
From source file:org.alfresco.web.bean.ajax.PresenceProxyBean.java
/** * Perform request/*from w ww . j av a 2s . c o m*/ */ public String getUrlResponse(String requestUrl) { String response = ""; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(requestUrl); method.setRequestHeader("Accept", "*/*"); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } else { response = method.getStatusText(); } } catch (HttpException e) { response = e.getMessage(); } catch (IOException e) { response = e.getMessage(); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:org.apache.commons.httpclient.demo.GetSample.java
public static void main(String[] args) { //HttpClient// w w w.ja v a 2 s. co m HttpClient httpClient = new HttpClient(); // //httpClient.getHostConfiguration().setProxy("90.0.12.21",808); //GET GetMethod getMethod = new GetMethod("http://www.baidu.com"); //GetMethod getMethod = new GetMethod("http://10.164.80.52/dav/5000/moban.rar"); // getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { //getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // byte[] responseBody = getMethod.getResponseBody(); String serverfile = "Test_baidu.html"; //String serverfile = "d:\\moban.rar"; OutputStream serverout = new FileOutputStream(serverfile); serverout.write(responseBody); serverout.flush(); serverout.close(); // //System.out.println(new String(responseBody)); System.out.println("OK!"); } catch (HttpException e) { // System.out.println("Please check your provided http address!"); e.printStackTrace(); } catch (IOException e) { // e.printStackTrace(); } finally { // getMethod.releaseConnection(); } }