List of usage examples for org.apache.commons.httpclient HttpRecoverableException getMessage
public String getMessage()
From source file:com.bigrocksoftware.metarparser.MetarFetcher.java
public static String fetch(String station, int timeout) { metarData = null;/*from w ww . j ava 2 s .com*/ // create the http client HttpClient client = new HttpClient(); // set the timeout is specified if (timeout != 0) { log.debug("MetarFetch: setting timeout to '" + timeout + "' milliseconds"); long start = System.currentTimeMillis(); client.setConnectionTimeout(timeout); long end = System.currentTimeMillis(); if (end - start < timeout) { client.setTimeout((int) (end - start)); } else { return null; } } // create the http method we will use HttpMethod method = new GetMethod(httpMetarURL + station + ".TXT"); // connect to the NOAA site, retrying up to the specified num int statusCode = -1; //for (int attempt = 0; statusCode == -1 && attempt < 3; attempt++) { try { // execute the get method log.debug("MetarFetcher: downloading data for station '" + station + "'"); statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { log.error("a recoverable exception occurred, " + "retrying." + e.getMessage()); } catch (IOException e) { log.error("failed to download file: " + e); } //} // check that we didn't run out of retries if (statusCode != HttpStatus.SC_OK) { log.error("failed to download station data for '" + station + "'"); return null; } else { // read the response body byte[] responseBody = method.getResponseBody(); // release the connection method.releaseConnection(); // deal with the response. // FIXME - ensure we use the correct character encoding here metarData = new String(responseBody) + "\n"; log.debug("MetarFetcher: metar data: " + metarData); } return metarData; }
From source file:mesquite.tol.lib.BaseHttpRequestMaker.java
/** * Make the http request to the specified url * @return If returnArray is true, a byte array containing the response bytes, * if false, we return an ObjectArray with the first argument being an * InputStream and the second being the GetMethod that was used to make the * request. The GetMethod must have releaseConnection() called on it after * all of the InputStream has been read. *//*from w ww . j av a 2 s . c om*/ public static Object makeHttpRequest(String url, Credentials c, String realm, boolean returnArray) { GetMethod getMethod = new GetMethod(url); Object returnValue = null; HttpClient client; try { client = new HttpClient(); if (c != null) { client.getParams().setAuthenticationPreemptive(true); //client.getState().set //client.getState().setCredentials(new HttpAuthRealm(null, realm), c); //client.getState().setCredentials(realm, null, c); client.getState().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, realm, AuthScope.ANY_SCHEME), c); } int statusCode = -1; // We will retry up to 3 times. for (int attempt = 0; statusCode == -1 && attempt < 3; attempt++) { try { // execute the method. statusCode = client.executeMethod(getMethod); } catch (HttpRecoverableException e) { System.err.println("A recoverable exception occurred, retrying. " + e.getMessage()); } catch (IOException e) { System.err.println("Failed to download file."); e.printStackTrace(); break; } } if (statusCode == HttpStatus.SC_OK) { if (returnArray) { returnValue = getMethod.getResponseBodyAsStream(); InputStream stream = (InputStream) returnValue; byte[] bytes; long contentLength = getMethod.getResponseContentLength(); if (contentLength >= 0) { bytes = new byte[(int) contentLength]; } else { // it returned -1, so it doesn't know how long the // length is. We'll allocate a 5mb buffer in this case bytes = new byte[MAX_BYTES]; } int offset = 0; int nextByte = 0; try { while ((nextByte = stream.read()) != -1) { bytes[offset++] = (byte) nextByte; } } catch (IOException e) { e.printStackTrace(); } finally { if (stream != null) { stream.close(); } } byte[] newBytes = new byte[offset]; System.arraycopy(bytes, 0, newBytes, 0, offset); returnValue = newBytes; // make sure these get garbage collected bytes = null; } else { InputStream stream = getMethod.getResponseBodyAsStream(); returnValue = new Object[] { stream, getMethod }; } } else { System.err.println("bad status code is: " + statusCode); returnValue = null; } } catch (Exception e) { e.printStackTrace(); try { getMethod.getResponseBody(); } catch (Exception e2) { e2.printStackTrace(); } finally { if (returnArray) { getMethod.releaseConnection(); } } returnValue = null; } finally { if (returnArray) { getMethod.releaseConnection(); } } return returnValue; }
From source file:com.sun.syndication.feed.weather.WeatherGateway.java
protected WeatherChannel doQuery(String locationID, long allowedAge, NameValuePair[] query) throws IOException { HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(weatherServer + locationID); method.setQueryString(query);// ww w. ja v a 2 s . co m URI uri = method.getURI(); LOG.debug("Performing query with URI: " + uri); WeatherChannel channel = new WeatherChannel(); if (weatherCache.containsKey(uri)) { WeatherCacheEntry entry = (WeatherCacheEntry) weatherCache.get(uri); long lastUpdated = entry.getLastUpdated().getTimeInMillis(); LOG.debug("Cache entry last updated: " + lastUpdated); long now = new GregorianCalendar().getTimeInMillis(); LOG.debug("Cache entry current time: " + now); long diffMillis = now - lastUpdated; LOG.debug("Cache entry time difference is: " + diffMillis); if (diffMillis < allowedAge) { channel = entry.getChannel(); LOG.debug("Returning channel from cache."); return channel; } } int statusCode = -1; for (int attempt = 0; statusCode == -1 && attempt < 3; attempt++) { try { statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { LOG.warn("Retrying after recoverable exception: " + e.getMessage()); } catch (IOException e) { LOG.error("Failed to download file: " + e.getMessage()); } } if (statusCode == -1) { LOG.error("Failed to recover from exception."); throw new IOException(); } channel = buildWeather(method); WeatherCacheEntry entry = new WeatherCacheEntry(channel); weatherCache.put(uri, entry); LOG.debug("Returning channel from network."); return channel; }
From source file:org.jboss.test.cluster.apache_tomcat.HttpSessionReplicationTestCase.java
/** * Makes a http call to the jsp that retrieves the attribute stored on the * session. When the attribute values mathes with the one retrieved earlier, * we have HttpSessionReplication./*from w w w . ja v a 2s . co m*/ * Makes use of commons-httpclient library of Apache * * @param client * @param method * @return session attribute */ private String makeGet(HttpClient client, HttpMethod method) { try { client.executeMethod(method); } catch (HttpRecoverableException e) { log.debug("A recoverable exception occurred, retrying." + e.getMessage()); } catch (IOException e) { log.debug(e); e.printStackTrace(); System.exit(-1); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. method.releaseConnection(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data return new String(responseBody); }
From source file:org.jboss.test.cluster.httpsessionreplication.HttpSessionReplicationUnitTestCase.java
/** * Makes a http call to the jsp that retrieves the attribute stored on the * session. When the attribute values mathes with the one retrieved earlier, * we have HttpSessionReplication.//from w w w . jav a2 s. co m * Makes use of commons-httpclient library of Apache * @param client * @param method * @return session attribute * @throws IOException */ private String makeGet(HttpClient client, HttpMethod method) throws IOException { // Execute the method. int statusCode = -1; try { // execute the method. statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { System.err.println("A recoverable exception occurred, retrying." + e.getMessage()); } catch (IOException e) { System.err.println("Failed to download file."); e.printStackTrace(); System.exit(-1); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. method.releaseConnection(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data return new String(responseBody); }
From source file:org.josso.gl2.gateway.reverseproxy.ReverseProxyValve.java
/** * Intercepts Http request and redirects it to the configured SSO partner application. * * @param request The servlet request to be processed * @param response The servlet response to be created * @exception IOException if an input/output error occurs * @exception javax.servlet.ServletException if a servlet error occurs *//*from w ww .ja v a 2s. c o m*/ public int invoke(Request request, Response response) throws IOException, ServletException { if (debug >= 1) log("ReverseProxyValve Acting."); ProxyContextConfig[] contexts = _rpc.getProxyContexts(); // Create an instance of HttpClient. HttpClient client = new HttpClient(); HttpServletRequest hsr = (HttpServletRequest) request.getRequest(); String uri = hsr.getRequestURI(); String uriContext = null; StringTokenizer st = new StringTokenizer(uri.substring(1), "/"); while (st.hasMoreTokens()) { String token = st.nextToken(); uriContext = "/" + token; break; } if (uriContext == null) uriContext = uri; // Obtain the target host from the String proxyForwardHost = null; String proxyForwardUri = null; for (int i = 0; i < contexts.length; i++) { if (contexts[i].getContext().equals(uriContext)) { log("Proxy context mapped to host/uri: " + contexts[i].getForwardHost() + contexts[i].getForwardUri()); proxyForwardHost = contexts[i].getForwardHost(); proxyForwardUri = contexts[i].getForwardUri(); break; } } if (proxyForwardHost == null) { log("URI '" + uri + "' can't be mapped to host"); //valveContext.invokeNext(request, response); return Valve.INVOKE_NEXT; } if (proxyForwardUri == null) { // trim the uri context before submitting the http request int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = uri.substring(uriTrailStartPos); } else { int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = proxyForwardUri + uri.substring(uriTrailStartPos); } // log ("Proxy request mapped to " + "http://" + proxyForwardHost + proxyForwardUri); HttpMethod method; // to be moved to a builder which instantiates and build concrete methods. if (hsr.getMethod().equals(METHOD_GET)) { // Create a method instance. HttpMethod getMethod = new GetMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = getMethod; } else if (hsr.getMethod().equals(METHOD_POST)) { // Create a method instance. PostMethod postMethod = new PostMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); postMethod.setRequestBody(hsr.getInputStream()); method = postMethod; } else if (hsr.getMethod().equals(METHOD_HEAD)) { // Create a method instance. HeadMethod headMethod = new HeadMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = headMethod; } else if (hsr.getMethod().equals(METHOD_PUT)) { method = new PutMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); } else throw new java.lang.UnsupportedOperationException("Unknown method : " + hsr.getMethod()); // copy incoming http headers to reverse proxy request Enumeration hne = hsr.getHeaderNames(); while (hne.hasMoreElements()) { String hn = (String) hne.nextElement(); // Map the received host header to the target host name // so that the configured virtual domain can // do the proper handling. if (hn.equalsIgnoreCase("host")) { method.addRequestHeader("Host", proxyForwardHost); continue; } Enumeration hvals = hsr.getHeaders(hn); while (hvals.hasMoreElements()) { String hv = (String) hvals.nextElement(); method.addRequestHeader(hn, hv); } } // Add Reverse-Proxy-Host header String reverseProxyHost = getReverseProxyHost(request); method.addRequestHeader(Constants.JOSSO_REVERSE_PROXY_HEADER, reverseProxyHost); if (debug >= 1) log("Sending " + Constants.JOSSO_REVERSE_PROXY_HEADER + " " + reverseProxyHost); // DO NOT follow redirects ! method.setFollowRedirects(false); // By default the httpclient uses HTTP v1.1. We are downgrading it // to v1.0 so that the target server doesn't set a reply using chunked // transfer encoding which doesn't seem to be handled properly. // Check how to make chunked transfer encoding work. client.getParams().setVersion(new HttpVersion(1, 0)); // Execute the method. int statusCode = -1; try { // execute the method. statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { log("A recoverable exception occurred " + e.getMessage()); } catch (IOException e) { log("Failed to connect."); e.printStackTrace(); } // Check that we didn't run out of retries. if (statusCode == -1) { log("Failed to recover from exception."); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. method.releaseConnection(); HttpServletResponse sres = (HttpServletResponse) response.getResponse(); // First thing to do is to copy status code to response, otherwise // catalina will do it as soon as we set a header or some other part of the response. sres.setStatus(method.getStatusCode()); // copy proxy response headers to client response Header[] responseHeaders = method.getResponseHeaders(); for (int i = 0; i < responseHeaders.length; i++) { Header responseHeader = responseHeaders[i]; String name = responseHeader.getName(); String value = responseHeader.getValue(); // Adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses // This is essential to avoid by-passing the reverse proxy because of HTTP redirects on the // backend servers which stay behind the reverse proxy switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: if ("Location".equalsIgnoreCase(name) || "Content-Location".equalsIgnoreCase(name) || "URI".equalsIgnoreCase(name)) { // Check that this redirect must be adjusted. if (value.indexOf(proxyForwardHost) >= 0) { String trail = value.substring(proxyForwardHost.length()); value = getReverseProxyHost(request) + trail; if (debug >= 1) log("Adjusting redirect header to " + value); } } break; } //end of switch sres.addHeader(name, value); } // Sometimes this is null, when no body is returned ... if (responseBody != null && responseBody.length > 0) sres.getOutputStream().write(responseBody); sres.getOutputStream().flush(); if (debug >= 1) log("ReverseProxyValve finished."); return Valve.END_PIPELINE; }
From source file:org.josso.tc50.gateway.reverseproxy.ReverseProxyValve.java
/** * Intercepts Http request and redirects it to the configured SSO partner application. * * @param request The servlet request to be processed * @param response The servlet response to be created * @param valveContext The valve _context used to invoke the next valve * in the current processing pipeline/*from www . j ava2s.c om*/ * @exception IOException if an input/output error occurs * @exception javax.servlet.ServletException if a servlet error occurs */ public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, javax.servlet.ServletException { if (debug >= 1) log("ReverseProxyValve Acting."); ProxyContextConfig[] contexts = _rpc.getProxyContexts(); // Create an instance of HttpClient. HttpClient client = new HttpClient(); HttpServletRequest hsr = (HttpServletRequest) request.getRequest(); String uri = hsr.getRequestURI(); String uriContext = null; StringTokenizer st = new StringTokenizer(uri.substring(1), "/"); while (st.hasMoreTokens()) { String token = st.nextToken(); uriContext = "/" + token; break; } if (uriContext == null) uriContext = uri; // Obtain the target host from the String proxyForwardHost = null; String proxyForwardUri = null; for (int i = 0; i < contexts.length; i++) { if (contexts[i].getContext().equals(uriContext)) { log("Proxy context mapped to host/uri: " + contexts[i].getForwardHost() + contexts[i].getForwardUri()); proxyForwardHost = contexts[i].getForwardHost(); proxyForwardUri = contexts[i].getForwardUri(); break; } } if (proxyForwardHost == null) { log("URI '" + uri + "' can't be mapped to host"); valveContext.invokeNext(request, response); return; } if (proxyForwardUri == null) { // trim the uri context before submitting the http request int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = uri.substring(uriTrailStartPos); } else { int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = proxyForwardUri + uri.substring(uriTrailStartPos); } // log ("Proxy request mapped to " + "http://" + proxyForwardHost + proxyForwardUri); HttpMethod method; // to be moved to a builder which instantiates and build concrete methods. if (hsr.getMethod().equals(METHOD_GET)) { // Create a method instance. HttpMethod getMethod = new GetMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = getMethod; } else if (hsr.getMethod().equals(METHOD_POST)) { // Create a method instance. PostMethod postMethod = new PostMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); postMethod.setRequestBody(hsr.getInputStream()); method = postMethod; } else if (hsr.getMethod().equals(METHOD_HEAD)) { // Create a method instance. HeadMethod headMethod = new HeadMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = headMethod; } else if (hsr.getMethod().equals(METHOD_PUT)) { method = new PutMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); } else throw new java.lang.UnsupportedOperationException("Unknown method : " + hsr.getMethod()); // copy incoming http headers to reverse proxy request Enumeration hne = hsr.getHeaderNames(); while (hne.hasMoreElements()) { String hn = (String) hne.nextElement(); // Map the received host header to the target host name // so that the configured virtual domain can // do the proper handling. if (hn.equalsIgnoreCase("host")) { method.addRequestHeader("Host", proxyForwardHost); continue; } Enumeration hvals = hsr.getHeaders(hn); while (hvals.hasMoreElements()) { String hv = (String) hvals.nextElement(); method.addRequestHeader(hn, hv); } } // Add Reverse-Proxy-Host header String reverseProxyHost = getReverseProxyHost(request); method.addRequestHeader(Constants.JOSSO_REVERSE_PROXY_HEADER, reverseProxyHost); if (debug >= 1) log("Sending " + Constants.JOSSO_REVERSE_PROXY_HEADER + " " + reverseProxyHost); // DO NOT follow redirects ! method.setFollowRedirects(false); // By default the httpclient uses HTTP v1.1. We are downgrading it // to v1.0 so that the target server doesn't set a reply using chunked // transfer encoding which doesn't seem to be handled properly. // Check how to make chunked transfer encoding work. client.getParams().setVersion(new HttpVersion(1, 0)); // Execute the method. int statusCode = -1; try { // execute the method. statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { log("A recoverable exception occurred " + e.getMessage()); } catch (IOException e) { log("Failed to connect."); e.printStackTrace(); } // Check that we didn't run out of retries. if (statusCode == -1) { log("Failed to recover from exception."); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. method.releaseConnection(); HttpServletResponse sres = (HttpServletResponse) response.getResponse(); // First thing to do is to copy status code to response, otherwise // catalina will do it as soon as we set a header or some other part of the response. sres.setStatus(method.getStatusCode()); // copy proxy response headers to client response Header[] responseHeaders = method.getResponseHeaders(); for (int i = 0; i < responseHeaders.length; i++) { Header responseHeader = responseHeaders[i]; String name = responseHeader.getName(); String value = responseHeader.getValue(); // Adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses // This is essential to avoid by-passing the reverse proxy because of HTTP redirects on the // backend servers which stay behind the reverse proxy switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: if ("Location".equalsIgnoreCase(name) || "Content-Location".equalsIgnoreCase(name) || "URI".equalsIgnoreCase(name)) { // Check that this redirect must be adjusted. if (value.indexOf(proxyForwardHost) >= 0) { String trail = value.substring(proxyForwardHost.length()); value = getReverseProxyHost(request) + trail; if (debug >= 1) log("Adjusting redirect header to " + value); } } break; } //end of switch sres.addHeader(name, value); } // Sometimes this is null, when no body is returned ... if (responseBody != null && responseBody.length > 0) sres.getOutputStream().write(responseBody); sres.getOutputStream().flush(); if (debug >= 1) log("ReverseProxyValve finished."); return; }
From source file:org.josso.tc55.gateway.reverseproxy.ReverseProxyValve.java
/** * Intercepts Http request and redirects it to the configured SSO partner application. * * @param request The servlet request to be processed * @param response The servlet response to be created * in the current processing pipeline//from w w w.j ava 2s . c o m * @exception IOException if an input/output error occurs * @exception javax.servlet.ServletException if a servlet error occurs */ public void invoke(Request request, Response response) throws IOException, javax.servlet.ServletException { if (container.getLogger().isDebugEnabled()) container.getLogger().debug("ReverseProxyValve Acting."); ProxyContextConfig[] contexts = _rpc.getProxyContexts(); // Create an instance of HttpClient. HttpClient client = new HttpClient(); HttpServletRequest hsr = (HttpServletRequest) request.getRequest(); String uri = hsr.getRequestURI(); String uriContext = null; StringTokenizer st = new StringTokenizer(uri.substring(1), "/"); while (st.hasMoreTokens()) { String token = st.nextToken(); uriContext = "/" + token; break; } if (uriContext == null) uriContext = uri; // Obtain the target host from the String proxyForwardHost = null; String proxyForwardUri = null; for (int i = 0; i < contexts.length; i++) { if (contexts[i].getContext().equals(uriContext)) { log("Proxy context mapped to host/uri: " + contexts[i].getForwardHost() + contexts[i].getForwardUri()); proxyForwardHost = contexts[i].getForwardHost(); proxyForwardUri = contexts[i].getForwardUri(); break; } } if (proxyForwardHost == null) { log("URI '" + uri + "' can't be mapped to host"); getNext().invoke(request, response); return; } if (proxyForwardUri == null) { // trim the uri context before submitting the http request int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = uri.substring(uriTrailStartPos); } else { int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = proxyForwardUri + uri.substring(uriTrailStartPos); } // log ("Proxy request mapped to " + "http://" + proxyForwardHost + proxyForwardUri); HttpMethod method; // to be moved to a builder which instantiates and build concrete methods. if (hsr.getMethod().equals(METHOD_GET)) { // Create a method instance. HttpMethod getMethod = new GetMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = getMethod; } else if (hsr.getMethod().equals(METHOD_POST)) { // Create a method instance. PostMethod postMethod = new PostMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); postMethod.setRequestBody(hsr.getInputStream()); method = postMethod; } else if (hsr.getMethod().equals(METHOD_HEAD)) { // Create a method instance. HeadMethod headMethod = new HeadMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = headMethod; } else if (hsr.getMethod().equals(METHOD_PUT)) { method = new PutMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); } else throw new java.lang.UnsupportedOperationException("Unknown method : " + hsr.getMethod()); // copy incoming http headers to reverse proxy request Enumeration hne = hsr.getHeaderNames(); while (hne.hasMoreElements()) { String hn = (String) hne.nextElement(); // Map the received host header to the target host name // so that the configured virtual domain can // do the proper handling. if (hn.equalsIgnoreCase("host")) { method.addRequestHeader("Host", proxyForwardHost); continue; } Enumeration hvals = hsr.getHeaders(hn); while (hvals.hasMoreElements()) { String hv = (String) hvals.nextElement(); method.addRequestHeader(hn, hv); } } // Add Reverse-Proxy-Host header String reverseProxyHost = getReverseProxyHost(request); method.addRequestHeader(Constants.JOSSO_REVERSE_PROXY_HEADER, reverseProxyHost); if (container.getLogger().isDebugEnabled()) container.getLogger().debug("Sending " + Constants.JOSSO_REVERSE_PROXY_HEADER + " " + reverseProxyHost); // DO NOT follow redirects ! method.setFollowRedirects(false); // By default the httpclient uses HTTP v1.1. We are downgrading it // to v1.0 so that the target server doesn't set a reply using chunked // transfer encoding which doesn't seem to be handled properly. // Check how to make chunked transfer encoding work. client.getParams().setVersion(new HttpVersion(1, 0)); // Execute the method. int statusCode = -1; try { // execute the method. statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { log("A recoverable exception occurred " + e.getMessage()); } catch (IOException e) { log("Failed to connect."); e.printStackTrace(); } // Check that we didn't run out of retries. if (statusCode == -1) { log("Failed to recover from exception."); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. method.releaseConnection(); HttpServletResponse sres = (HttpServletResponse) response.getResponse(); // First thing to do is to copy status code to response, otherwise // catalina will do it as soon as we set a header or some other part of the response. sres.setStatus(method.getStatusCode()); // copy proxy response headers to client response Header[] responseHeaders = method.getResponseHeaders(); for (int i = 0; i < responseHeaders.length; i++) { Header responseHeader = responseHeaders[i]; String name = responseHeader.getName(); String value = responseHeader.getValue(); // Adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses // This is essential to avoid by-passing the reverse proxy because of HTTP redirects on the // backend servers which stay behind the reverse proxy switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: if ("Location".equalsIgnoreCase(name) || "Content-Location".equalsIgnoreCase(name) || "URI".equalsIgnoreCase(name)) { // Check that this redirect must be adjusted. if (value.indexOf(proxyForwardHost) >= 0) { String trail = value.substring(proxyForwardHost.length()); value = getReverseProxyHost(request) + trail; if (container.getLogger().isDebugEnabled()) container.getLogger().debug("Adjusting redirect header to " + value); } } break; } //end of switch sres.addHeader(name, value); } // Sometimes this is null, when no body is returned ... if (responseBody != null && responseBody.length > 0) sres.getOutputStream().write(responseBody); sres.getOutputStream().flush(); if (container.getLogger().isDebugEnabled()) container.getLogger().debug("ReverseProxyValve finished."); return; }
From source file:org.josso.tc60.gateway.reverseproxy.ReverseProxyValve.java
/** * Intercepts Http request and redirects it to the configured SSO partner application. * * @param request The servlet request to be processed * @param response The servlet response to be created * in the current processing pipeline// w ww. j a v a 2 s . co m * @exception IOException if an input/output error occurs * @exception javax.servlet.ServletException if a servlet error occurs */ public void invoke(Request request, Response response) throws IOException, javax.servlet.ServletException { if (container.getLogger().isDebugEnabled()) container.getLogger().debug("ReverseProxyValve Acting."); ProxyContextConfig[] contexts = _rpc.getProxyContexts(); // Create an instance of HttpClient. HttpClient client = new HttpClient(); HttpServletRequest hsr = (HttpServletRequest) request.getRequest(); String uri = hsr.getRequestURI(); String uriContext = null; StringTokenizer st = new StringTokenizer(uri.substring(1), "/"); while (st.hasMoreTokens()) { String token = st.nextToken(); uriContext = "/" + token; break; } if (uriContext == null) uriContext = uri; // Obtain the target host from the String proxyForwardHost = null; String proxyForwardUri = null; for (int i = 0; i < contexts.length; i++) { if (contexts[i].getContext().equals(uriContext)) { log("Proxy context mapped to host/uri: " + contexts[i].getForwardHost() + contexts[i].getForwardUri()); proxyForwardHost = contexts[i].getForwardHost(); proxyForwardUri = contexts[i].getForwardUri(); break; } } if (proxyForwardHost == null) { log("URI '" + uri + "' can't be mapped to host"); getNext().invoke(request, response); return; } if (proxyForwardUri == null) { // trim the uri context before submitting the http request int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = uri.substring(uriTrailStartPos); } else { int uriTrailStartPos = uri.substring(1).indexOf("/") + 1; proxyForwardUri = proxyForwardUri + uri.substring(uriTrailStartPos); } // log ("Proxy request mapped to " + "http://" + proxyForwardHost + proxyForwardUri); HttpMethod method; // to be moved to a builder which instantiates and build concrete methods. if (hsr.getMethod().equals(METHOD_GET)) { // Create a method instance. HttpMethod getMethod = new GetMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = getMethod; } else if (hsr.getMethod().equals(METHOD_POST)) { // Create a method instance. PostMethod postMethod = new PostMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); postMethod.setRequestBody(hsr.getInputStream()); method = postMethod; } else if (hsr.getMethod().equals(METHOD_HEAD)) { // Create a method instance. HeadMethod headMethod = new HeadMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); method = headMethod; } else if (hsr.getMethod().equals(METHOD_PUT)) { method = new PutMethod(proxyForwardHost + proxyForwardUri + (hsr.getQueryString() != null ? ("?" + hsr.getQueryString()) : "")); } else throw new java.lang.UnsupportedOperationException("Unknown method : " + hsr.getMethod()); // copy incoming http headers to reverse proxy request Enumeration hne = hsr.getHeaderNames(); while (hne.hasMoreElements()) { String hn = (String) hne.nextElement(); // Map the received host header to the target host name // so that the configured virtual domain can // do the proper handling. if (hn.equalsIgnoreCase("host")) { method.addRequestHeader("Host", proxyForwardHost); continue; } Enumeration hvals = hsr.getHeaders(hn); while (hvals.hasMoreElements()) { String hv = (String) hvals.nextElement(); method.addRequestHeader(hn, hv); } } // Add Reverse-Proxy-Host header String reverseProxyHost = getReverseProxyHost(request); method.addRequestHeader(Constants.JOSSO_REVERSE_PROXY_HEADER, reverseProxyHost); if (container.getLogger().isDebugEnabled()) container.getLogger().debug("Sending " + Constants.JOSSO_REVERSE_PROXY_HEADER + " " + reverseProxyHost); // DO NOT follow redirects ! method.setFollowRedirects(false); // By default the httpclient uses HTTP v1.1. We are downgrading it // to v1.0 so that the target server doesn't set a reply using chunked // transfer encoding which doesn't seem to be handled properly. client.getParams().setVersion(new HttpVersion(1, 0)); // Execute the method. int statusCode = -1; try { // execute the method. statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { log("A recoverable exception occurred " + e.getMessage()); } catch (IOException e) { log("Failed to connect."); e.printStackTrace(); } // Check that we didn't run out of retries. if (statusCode == -1) { log("Failed to recover from exception."); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Release the connection. method.releaseConnection(); HttpServletResponse sres = (HttpServletResponse) response.getResponse(); // First thing to do is to copy status code to response, otherwise // catalina will do it as soon as we set a header or some other part of the response. sres.setStatus(method.getStatusCode()); // copy proxy response headers to client response Header[] responseHeaders = method.getResponseHeaders(); for (int i = 0; i < responseHeaders.length; i++) { Header responseHeader = responseHeaders[i]; String name = responseHeader.getName(); String value = responseHeader.getValue(); // Adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses // This is essential to avoid by-passing the reverse proxy because of HTTP redirects on the // backend servers which stay behind the reverse proxy switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: if ("Location".equalsIgnoreCase(name) || "Content-Location".equalsIgnoreCase(name) || "URI".equalsIgnoreCase(name)) { // Check that this redirect must be adjusted. if (value.indexOf(proxyForwardHost) >= 0) { String trail = value.substring(proxyForwardHost.length()); value = getReverseProxyHost(request) + trail; if (container.getLogger().isDebugEnabled()) container.getLogger().debug("Adjusting redirect header to " + value); } } break; } //end of switch sres.addHeader(name, value); } // Sometimes this is null, when no body is returned ... if (responseBody != null && responseBody.length > 0) sres.getOutputStream().write(responseBody); sres.getOutputStream().flush(); if (container.getLogger().isDebugEnabled()) container.getLogger().debug("ReverseProxyValve finished."); return; }
From source file:org.methodize.nntprss.feed.Channel.java
/** * Retrieves the latest RSS doc from the remote site *//*from w ww. j a v a2 s.c o m*/ public synchronized void poll() { // Use method-level variable // Guard against change in history mid-poll polling = true; // boolean keepHistory = historical; long keepExpiration = expiration; lastPolled = new Date(); int statusCode = -1; HttpMethod method = null; String urlString = url.toString(); try { HttpClient httpClient = getHttpClient(); channelManager.configureHttpClient(httpClient); HttpResult result = null; try { connected = true; boolean redirected = false; int count = 0; do { URL currentUrl = new URL(urlString); method = new GetMethod(urlString); method.setRequestHeader("User-agent", AppConstants.getUserAgent()); method.setRequestHeader("Accept-Encoding", "gzip"); method.setFollowRedirects(false); method.setDoAuthentication(true); // ETag if (lastETag != null) { method.setRequestHeader("If-None-Match", lastETag); } // Last Modified if (lastModified != 0) { final String NAME = "If-Modified-Since"; //defend against such fun like net.freeroller.rickard got If-Modified-Since "Thu, 24 Aug 2028 12:29:54 GMT" if (lastModified < System.currentTimeMillis()) { final String DATE = httpDate.format(new Date(lastModified)); method.setRequestHeader(NAME, DATE); log.debug("channel " + this.name + " using " + NAME + " " + DATE); //ALEK } } method.setFollowRedirects(false); method.setDoAuthentication(true); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(currentUrl.getHost(), currentUrl.getPort(), currentUrl.getProtocol()); result = executeHttpRequest(httpClient, hostConfig, method); statusCode = result.getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { redirected = true; // Resolve against current URI - may be a relative URI try { urlString = new java.net.URI(urlString).resolve(result.getLocation()).toString(); } catch (URISyntaxException use) { // Fall back to just using location from result urlString = result.getLocation(); } if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY && channelManager.isObserveHttp301()) { try { url = new URL(urlString); if (log.isInfoEnabled()) { log.info("Channel = " + this.name + ", updated URL from HTTP Permanent Redirect"); } } catch (MalformedURLException mue) { // Ignore URL permanent redirect for now... } } } else { redirected = false; } // method.getResponseBody(); // method.releaseConnection(); count++; } while (count < 5 && redirected); } catch (HttpRecoverableException hre) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Temporary Http Problem - " + hre.getMessage()); } status = STATUS_CONNECTION_TIMEOUT; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (ConnectException ce) { // @TODO Might also be a connection refused - not only a timeout... if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Connection Timeout, skipping - " + ce.getMessage()); } status = STATUS_CONNECTION_TIMEOUT; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (UnknownHostException ue) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Unknown Host Exception, skipping"); } status = STATUS_UNKNOWN_HOST; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (NoRouteToHostException re) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - No Route To Host Exception, skipping"); } status = STATUS_NO_ROUTE_TO_HOST; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (SocketException se) { // e.g. Network is unreachable if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Socket Exception, skipping"); } status = STATUS_SOCKET_EXCEPTION; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } // Only process if ok - if not ok (e.g. not modified), don't do anything if (connected && statusCode == HttpStatus.SC_OK) { PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(result.getResponse()), PUSHBACK_BUFFER_SIZE); skipBOM(pbis); BufferedInputStream bis = new BufferedInputStream(pbis); DocumentBuilder db = AppConstants.newDocumentBuilder(); try { Document rssDoc = null; if (!parseAtAllCost) { try { rssDoc = db.parse(bis); } catch (InternalError ie) { // Crimson library throws InternalErrors if (log.isDebugEnabled()) { log.debug("InternalError thrown by Crimson", ie); } throw new SAXException("InternalError thrown by Crimson: " + ie.getMessage()); } } else { // Parse-at-all-costs selected // Read in document to local array - may need to parse twice ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int bytesRead = bis.read(buf); while (bytesRead > -1) { if (bytesRead > 0) { bos.write(buf, 0, bytesRead); } bytesRead = bis.read(buf); } bos.flush(); bos.close(); byte[] rssDocBytes = bos.toByteArray(); try { // Try the XML document parser first - just in case // the doc is well-formed rssDoc = db.parse(new ByteArrayInputStream(rssDocBytes)); } catch (SAXParseException spe) { if (log.isDebugEnabled()) { log.debug("XML parse failed, trying tidy"); } // Fallback to parse-at-all-costs parser rssDoc = LooseParser.parse(new ByteArrayInputStream(rssDocBytes)); } } processChannelDocument(expiration, rssDoc); // Update last modified / etag from headers // lastETag = httpCon.getHeaderField("ETag"); // lastModified = httpCon.getHeaderFieldDate("Last-Modified", 0); Header hdrETag = method.getResponseHeader("ETag"); lastETag = hdrETag != null ? hdrETag.getValue() : null; Header hdrLastModified = method.getResponseHeader("Last-Modified"); lastModified = hdrLastModified != null ? parseHttpDate(hdrLastModified.getValue()) : 0; log.debug("channel " + this.name + " parsed Last-Modifed " + hdrLastModified + " to " + (lastModified != 0 ? "" + (new Date(lastModified)) : "" + lastModified)); //ALEK status = STATUS_OK; } catch (SAXParseException spe) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Error parsing RSS document - check feed"); } status = STATUS_INVALID_CONTENT; } bis.close(); // end if response code == HTTP_OK } else if (connected && statusCode == HttpStatus.SC_NOT_MODIFIED) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - HTTP_NOT_MODIFIED, skipping"); } status = STATUS_OK; } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Proxy authentication required"); } status = STATUS_PROXY_AUTHENTICATION_REQUIRED; } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Authentication required"); } status = STATUS_USER_AUTHENTICATION_REQUIRED; } // Update channel in database... channelDAO.updateChannel(this); } catch (FileNotFoundException fnfe) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - File not found returned by web server - check feed"); } status = STATUS_NOT_FOUND; } catch (Exception e) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Exception while polling channel", e); } } catch (NoClassDefFoundError ncdf) { // Throw if SSL / redirection to HTTPS if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - NoClassDefFound", ncdf); } } finally { connected = false; polling = false; } }