List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection
public abstract void releaseConnection();
From source file:org.apache.excalibur.source.factories.HTTPClientSource.java
/** * Method to make response data available if possible without * actually making an actual request (ie. via HTTP HEAD). */// w w w. j a va 2 s. com private void updateData() { // no request made so far, attempt to get some response data. if (!this.m_dataValid) { if (GET.equals(this.findMethodType())) { final HttpMethod head = this.createHeadMethod(this.m_uri); try { this.executeMethod(head); return; } catch (final IOException e) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Unable to determine response data, using defaults", e); } } finally { head.releaseConnection(); } } // default values when response data is not available this.m_exists = false; this.m_mimeType = null; this.m_contentLength = -1; this.m_lastModified = 0; this.m_dataValid = true; } }
From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java
/** * Download link and have it be the response. * @param req the http request//from w w w. jav a 2 s. c o m * @param resp the http response * @param link the link to download * @param c the cookie to set if any * @throws IOException on any error. */ private static void proxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c, String proxyHost) throws IOException { org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(link.toString(), false); HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); // Make sure we send the request from the proxy address in the config // since that is what the AM filter checks against. IP aliasing or // similar could cause issues otherwise. HostConfiguration config = new HostConfiguration(); InetAddress localAddress = InetAddress.getByName(proxyHost); if (LOG.isDebugEnabled()) { LOG.debug("local InetAddress for proxy host: " + localAddress.toString()); } config.setLocalAddress(localAddress); HttpMethod method = new GetMethod(uri.getEscapedURI()); @SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (passThroughHeaders.contains(name)) { String value = req.getHeader(name); LOG.debug("REQ HEADER: " + name + " : " + value); method.setRequestHeader(name, value); } } String user = req.getRemoteUser(); if (user != null && !user.isEmpty()) { method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII")); } OutputStream out = resp.getOutputStream(); try { resp.setStatus(client.executeMethod(config, method)); for (Header header : method.getResponseHeaders()) { resp.setHeader(header.getName(), header.getValue()); } if (c != null) { resp.addCookie(c); } InputStream in = method.getResponseBodyAsStream(); if (in != null) { IOUtils.copyBytes(in, out, 4096, true); } } finally { method.releaseConnection(); } }
From source file:org.apache.hcatalog.templeton.TestWebHCatE2e.java
/** * Does a basic HTTP GET and returns Http Status code + response body * Will add the dummy user query string/*from www . j av a2 s .c o m*/ */ private static MethodCallRetVal doHttpCall(String uri, HTTP_METHOD_TYPE type, Map<String, Object> data, NameValuePair[] params) throws IOException { HttpClient client = new HttpClient(); HttpMethod method; switch (type) { case GET: method = new GetMethod(uri); break; case DELETE: method = new DeleteMethod(uri); break; case PUT: method = new PutMethod(uri); if (data == null) { break; } String msgBody = JsonBuilder.mapToJson(data); LOG.info("Msg Body: " + msgBody); StringRequestEntity sre = new StringRequestEntity(msgBody, "application/json", charSet); ((PutMethod) method).setRequestEntity(sre); break; default: throw new IllegalArgumentException("Unsupported method type: " + type); } if (params == null) { method.setQueryString(new NameValuePair[] { new NameValuePair("user.name", username) }); } else { NameValuePair[] newParams = new NameValuePair[params.length + 1]; System.arraycopy(params, 0, newParams, 1, params.length); newParams[0] = new NameValuePair("user.name", username); method.setQueryString(newParams); } String actualUri = "no URI"; try { actualUri = method.getURI().toString();//should this be escaped string? LOG.debug(type + ": " + method.getURI().getEscapedURI()); int httpStatus = client.executeMethod(method); LOG.debug("Http Status Code=" + httpStatus); String resp = method.getResponseBodyAsString(); LOG.debug("response: " + resp); return new MethodCallRetVal(httpStatus, resp, actualUri, method.getName()); } catch (IOException ex) { LOG.error("doHttpCall() failed", ex); } finally { method.releaseConnection(); } return new MethodCallRetVal(-1, "Http " + type + " failed; see log file for details", actualUri, method.getName()); }
From source file:org.apache.kylin.engine.mr.common.HadoopStatusGetter.java
private String getHttpResponse(String url) throws IOException { HttpClient client = new HttpClient(); String response = null;/* w w w . j a v a2 s. c o m*/ while (response == null) { // follow redirects via 'refresh' if (url.startsWith("https://")) { registerEasyHttps(); } if (url.contains("anonymous=true") == false) { url += url.contains("?") ? "&" : "?"; url += "anonymous=true"; } HttpMethod get = new GetMethod(url); get.addRequestHeader("accept", "application/json"); try { client.executeMethod(get); String redirect = null; Header h = get.getResponseHeader("Location"); if (h != null) { redirect = h.getValue(); if (isValidURL(redirect) == false) { logger.info("Get invalid redirect url, skip it: " + redirect); Thread.sleep(1000L); continue; } } else { h = get.getResponseHeader("Refresh"); if (h != null) { String s = h.getValue(); int cut = s.indexOf("url="); if (cut >= 0) { redirect = s.substring(cut + 4); if (isValidURL(redirect) == false) { logger.info("Get invalid redirect url, skip it: " + redirect); Thread.sleep(1000L); continue; } } } } if (redirect == null) { response = get.getResponseBodyAsString(); logger.debug("Job " + mrJobId + " get status check result.\n"); } else { url = redirect; logger.debug("Job " + mrJobId + " check redirect url " + url + ".\n"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error(e.getMessage()); } finally { get.releaseConnection(); } } return response; }
From source file:org.apache.kylin.job.tools.HadoopStatusGetter.java
private String getHttpResponse(String url) throws IOException { HttpClient client = new HttpClient(); String response = null;/*from w w w . j a v a 2 s .c om*/ while (response == null) { // follow redirects via 'refresh' if (url.startsWith("https://")) { registerEasyHttps(); } if (url.contains("anonymous=true") == false) { url += url.contains("?") ? "&" : "?"; url += "anonymous=true"; } HttpMethod get = new GetMethod(url); try { client.executeMethod(get); String redirect = null; Header h = get.getResponseHeader("Refresh"); if (h != null) { String s = h.getValue(); int cut = s.indexOf("url="); if (cut >= 0) { redirect = s.substring(cut + 4); } } if (redirect == null) { response = get.getResponseBodyAsString(); log.debug("Job " + mrJobId + " get status check result.\n"); } else { url = redirect; log.debug("Job " + mrJobId + " check redirect url " + url + ".\n"); } } finally { get.releaseConnection(); } } return response; }
From source file:org.apache.kylin.tool.JobTaskCounterExtractor.java
private String getHttpResponse(String url) { HttpClient client = new HttpClient(); String response = null;//from w w w .ja va2s . com while (response == null) { HttpMethod get = new GetMethod(url); try { get.addRequestHeader("accept", "application/json"); client.executeMethod(get); response = get.getResponseBodyAsString(); } catch (Exception e) { logger.warn("Failed to fetch http response" + e); } finally { get.releaseConnection(); } } return response; }
From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java
/** * Checks the given link.//from w w w .j av a2s.c o m * * @param link the link to check. * @param nbRedirect the number of current redirects. * @return HttpMethod * @throws IOException if something goes wrong. */ private HttpMethod checkLink(String link, int nbRedirect) throws IOException { int max = MAX_NB_REDIRECT; if (this.http.getHttpClientParameters() != null && this.http.getHttpClientParameters().get(HttpClientParams.MAX_REDIRECTS) != null) { try { max = Integer .valueOf(this.http.getHttpClientParameters().get(HttpClientParams.MAX_REDIRECTS).toString()) .intValue(); } catch (NumberFormatException e) { if (LOG.isWarnEnabled()) { LOG.warn("HttpClient parameter '" + HttpClientParams.MAX_REDIRECTS + "' is not a number. Ignoring!"); } } } if (nbRedirect > max) { throw new HttpException("Maximum number of redirections (" + max + ") exceeded"); } HttpMethod hm; if (HEAD_METHOD.equalsIgnoreCase(this.http.getMethod())) { hm = new HeadMethod(link); } else if (GET_METHOD.equalsIgnoreCase(this.http.getMethod())) { hm = new GetMethod(link); } else { if (LOG.isErrorEnabled()) { LOG.error("Unsupported method: " + this.http.getMethod() + ", using 'get'."); } hm = new GetMethod(link); } // Default hm.setFollowRedirects(this.http.isFollowRedirects()); try { URL url = new URL(link); cl.getHostConfiguration().setHost(url.getHost(), url.getPort(), url.getProtocol()); cl.executeMethod(hm); StatusLine sl = hm.getStatusLine(); if (sl == null) { if (LOG.isErrorEnabled()) { LOG.error("Unknown error validating link : " + link); } return null; } if (hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) { Header locationHeader = hm.getResponseHeader("location"); if (locationHeader == null) { LOG.error("Site sent redirect, but did not set Location header"); return hm; } String newLink = locationHeader.getValue(); // Be careful to absolute/relative links if (!newLink.startsWith("http://") && !newLink.startsWith("https://")) { if (newLink.startsWith("/")) { URL oldUrl = new URL(link); newLink = oldUrl.getProtocol() + "://" + oldUrl.getHost() + (oldUrl.getPort() > 0 ? ":" + oldUrl.getPort() : "") + newLink; } else { newLink = link + newLink; } } HttpMethod oldHm = hm; if (LOG.isDebugEnabled()) { LOG.debug("[" + link + "] is redirected to [" + newLink + "]"); } oldHm.releaseConnection(); hm = checkLink(newLink, nbRedirect + 1); // Restore the hm to "Moved permanently" | "Moved temporarily" | "Temporary redirect" // if the new location is found to allow us to report it if (hm.getStatusCode() == HttpStatus.SC_OK && nbRedirect == 0) { return oldHm; } } } finally { hm.releaseConnection(); } return hm; }
From source file:org.apache.oodt.cas.filemgr.catalog.solr.SolrClient.java
/** * Common functionality for HTTP GET and POST requests. * @param method// w w w .j ava2 s .c om * @return * @throws Exception */ private String doHttp(HttpMethod method) throws IOException, CatalogException { StringBuilder response = new StringBuilder(); BufferedReader br = null; try { // send request HttpClient httpClient = new HttpClient(); // OODT-719 Prevent httpclient from spawning closewait tcp connections method.setRequestHeader("Connection", "close"); int statusCode = httpClient.executeMethod(method); // read response if (statusCode != HttpStatus.SC_OK) { // still consume the response method.getResponseBodyAsString(); throw new CatalogException("HTTP method failed: " + method.getStatusLine()); } else { // read the response body. br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); String readLine; while (((readLine = br.readLine()) != null)) { response.append(readLine); } } } finally { // must release the connection even if an exception occurred method.releaseConnection(); if (br != null) { try { br.close(); } catch (Exception ignored) { } } } return response.toString(); }
From source file:org.apache.openejb.arquillian.session.SessionScopeTest.java
@Test public void testShouldBeAbleToAccessServletAndEjb() throws Exception { String[] sessionResult = new String[2]; for (int i = 0; i < sessionResult.length; i++) { HttpClient client = new HttpClient(); HttpMethod get = new GetMethod(TEST_SESSION_URL); String[] contents = new String[2]; try {// ww w . j av a2 s . c o m for (int j = 0; j < contents.length; j++) { int out = client.executeMethod(get); if (out != 200) { throw new RuntimeException("get " + TEST_SESSION_URL + " returned " + out); } contents[j] = get.getResponseBodyAsString(); } assertEquals(contents[0], contents[1]); } finally { get.releaseConnection(); } sessionResult[i] = contents[0]; } assertNotSame(sessionResult[0], sessionResult[1]); }
From source file:org.apache.roller.weblogger.util.Trackback.java
/** * Sends trackback from entry to remote URL. * See Trackback spec for details: http://www.sixapart.com/pronet/docs/trackback_spec *///from w ww . j a va2 s. c o m public RollerMessages send() throws WebloggerException { RollerMessages messages = new RollerMessages(); log.debug("Sending trackback to url - " + trackbackURL); // Construct data String title = entry.getTitle(); String excerpt = StringUtils.left(Utilities.removeHTML(entry.getDisplayContent()), 255); String url = entry.getPermalink(); String blog_name = entry.getWebsite().getName(); // build trackback post parameters as query string Map params = new HashMap(); params.put("title", URLUtilities.encode(title)); params.put("excerpt", URLUtilities.encode(excerpt)); params.put("url", URLUtilities.encode(url)); params.put("blog_name", URLUtilities.encode(blog_name)); String queryString = URLUtilities.getQueryString(params); log.debug("query string - " + queryString); // prepare http request HttpClient client = new HttpClient(); client.setConnectionTimeout(45 * 1000); HttpMethod method = new PostMethod(trackbackURL); method.setQueryString(queryString); try { // execute trackback int statusCode = client.executeMethod(method); // read response byte[] response = method.getResponseBody(); String responseString = Utilities.escapeHTML(new String(response, "UTF-8")); log.debug("result = " + statusCode + " " + method.getStatusText()); log.debug("response:\n" + responseString); if (statusCode == HttpStatus.SC_OK) { // trackback request succeeded, message will give details try { messages = parseTrackbackResponse(new String(response, "UTF-8"), messages); } catch (Exception e) { // Cannot parse response, indicates failure messages.addError("weblogEdit.trackbackErrorParsing", responseString); } } else if (statusCode == HttpStatus.SC_NOT_FOUND) { // 404, invalid trackback url messages.addError("weblogEdit.trackbackError404"); } else { // some other kind of error with url, like 500, 403, etc // just provide a generic error message and give the http response text messages.addError("weblogEdit.trackbackErrorResponse", new String[] { "" + statusCode, method.getStatusText() }); } } catch (IOException e) { // some kind of transport error sending trackback post log.debug("Error sending trackback", e); messages.addError("weblogEdit.trackbackErrorTransport"); } finally { // release used connection method.releaseConnection(); } return messages; }