List of usage examples for org.apache.commons.httpclient HttpMethod setFollowRedirects
public abstract void setFollowRedirects(boolean paramBoolean);
From source file:org.eclipsetrader.yahoojapan.internal.core.connector.SnapshotConnector.java
protected void fetchLatestSnapshot(HttpClient client, String[] symbols, boolean isStaleUpdate) { HttpMethod method = null; BufferedReader in = null;/*from w ww . j a v a 2 s . c om*/ String line = ""; //$NON-NLS-1$ try { for (int page = 1; page < 11; page++) { StringBuilder work = new StringBuilder(); boolean isNextPage = false; method = Util.getSnapshotFeedMethod(symbols, page); method.setFollowRedirects(true); client.executeMethod(method); in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((line = in.readLine()) != null) { if (line.indexOf("<!-- .pfListView -->") >= 0) { break; } } while ((line = in.readLine()) != null) { if (line.indexOf("<!---->") >= 0) { isNextPage = (line.indexOf("?</a>") >= 0); break; } if (line.startsWith("<a href=\"http://stocks.finance.yahoo.co.jp/stocks/detail/?code=")) { //CODE // work.append(line.substring(63, 67)); // work.append(","); work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>"))); work.append(","); //LAST, DATE while ((line = in.readLine()) != null) { if (line.equals("<td nowrap>")) { break; } } if ((line = in.readLine()) != null) { if (line.indexOf("<td>") >= 0) { work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")) .replaceAll(",", "")); work.append(","); String date = line.substring(0, line.indexOf("</td>")); if (date.indexOf("/") >= 0) { work.append(date); work.append(",00:00,,"); } else { Calendar c = Calendar.getInstance(); work.append(String.valueOf(c.get(Calendar.MONTH) + 1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH))); work.append(","); work.append(date); work.append(",,"); } } } //OPEN while ((line = in.readLine()) != null) { if (line.equals("<td nowrap>")) { break; } } if ((line = in.readLine()) != null) { if (line.indexOf("</td>") >= 0) { work.append(line.substring(0, line.indexOf("</td>")).replaceAll(",", "")); } } work.append(","); //HIGH while ((line = in.readLine()) != null) { if (line.equals("<td nowrap>")) { break; } } if ((line = in.readLine()) != null) { if (line.indexOf("</td>") >= 0) { work.append(line.substring(0, line.indexOf("</td>")).replaceAll(",", "")); } } work.append(","); //LOW while ((line = in.readLine()) != null) { if (line.equals("<td nowrap>")) { break; } } if ((line = in.readLine()) != null) { if (line.indexOf("</td>") >= 0) { work.append(line.substring(0, line.indexOf("</td>")).replaceAll(",", "")); } } work.append(",,,,,,,@"); processSnapshotData(work.toString(), isStaleUpdate); work = new StringBuilder(); } } FeedSubscription[] subscriptions; synchronized (symbolSubscriptions) { Collection<FeedSubscription> c = symbolSubscriptions.values(); subscriptions = c.toArray(new FeedSubscription[c.size()]); } for (int i = 0; i < subscriptions.length; i++) { subscriptions[i].fireNotification(); } if (!isNextPage) { break; } } } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error reading data", e); YahooJapanActivator.log(status); } finally { try { if (in != null) { in.close(); } if (method != null) { method.releaseConnection(); } } catch (Exception e) { Status status = new Status(IStatus.WARNING, YahooJapanActivator.PLUGIN_ID, 0, "Connection wasn't closed cleanly", e); YahooJapanActivator.log(status); } } }
From source file:org.eclipsetrader.yahoojapan.internal.core.connector.SnapshotConnector.java
protected void fetchLatestSnapshot1(HttpClient client, String[] symbols, boolean isStaleUpdate) { HttpMethod method = null; BufferedReader in = null;/*w ww . j a v a 2s.co m*/ String line = ""; //$NON-NLS-1$ try { StringBuilder work = new StringBuilder(); method = Util.getSnapshotFeedMethod(symbols, 1); method.setFollowRedirects(true); client.executeMethod(method); in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); while ((line = in.readLine()) != null) { if (line.indexOf("<!-- NEW STOCKS DETAIL -->") >= 0) { break; } } //CODE while ((line = in.readLine()) != null) { if (line.indexOf("<dl class=\"stocksInfo\">") >= 0) { break; } } if ((line = in.readLine()) != null) { if (line.length() > 8) { work.append(line.substring(4, 8)); } } work.append(","); //DATE String date = ""; while ((line = in.readLine()) != null) { if (line.indexOf("<dd class=\"yjSb real\">") >= 0) { break; } } if (line.length() > 33) { date = line.substring(28, line.indexOf("</span>")); } //LAST while ((line = in.readLine()) != null) { if (line.indexOf("<td class=\"stoksPrice\">") >= 0) { break; } } if (line.length() > 23) { work.append(line.substring(line.indexOf("<td class=\"stoksPrice\">") + 23, line.indexOf("</td>")) .replaceAll(",", "")); } work.append(","); if (date.indexOf("/") >= 0) { work.append(date); work.append(",00:00,,"); } else { Calendar c = Calendar.getInstance(); work.append(String.valueOf(c.get(Calendar.MONTH) + 1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH))); work.append(","); work.append(date); work.append(",,"); } //YESTERDAY LAST while ((line = in.readLine()) != null) { if (line.indexOf("<div class=\"innerDate\">") >= 0) { break; } } while ((line = in.readLine()) != null) { if (line.indexOf("<strong>") >= 0) { break; } } //OPEN while ((line = in.readLine()) != null) { if (line.indexOf("<strong>") >= 0) { break; } } if (line.length() > 16) { work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",", "")); } work.append(","); //HIGH while ((line = in.readLine()) != null) { if (line.indexOf("<strong>") >= 0) { break; } } if (line.length() > 16) { work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",", "")); } work.append(","); //LOW while ((line = in.readLine()) != null) { if (line.indexOf("<strong>") >= 0) { break; } } if (line.length() > 16) { work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",", "")); } work.append(","); //VOLUME while ((line = in.readLine()) != null) { if (line.indexOf("<strong>") >= 0) { break; } } if (line.length() > 16) { work.append(line.substring(line.indexOf("<strong>") + 8, line.indexOf("</strong>")).replaceAll(",", "")); } work.append(",,,,,,@"); processSnapshotData(work.toString(), isStaleUpdate); FeedSubscription[] subscriptions; synchronized (symbolSubscriptions) { Collection<FeedSubscription> c = symbolSubscriptions.values(); subscriptions = c.toArray(new FeedSubscription[c.size()]); } for (int i = 0; i < subscriptions.length; i++) { subscriptions[i].fireNotification(); } } catch (Exception e) { Status status = new Status(IStatus.ERROR, YahooJapanActivator.PLUGIN_ID, 0, "Error reading data", e); YahooJapanActivator.log(status); } finally { try { if (in != null) { in.close(); } if (method != null) { method.releaseConnection(); } } catch (Exception e) { Status status = new Status(IStatus.WARNING, YahooJapanActivator.PLUGIN_ID, 0, "Connection wasn't closed cleanly", e); YahooJapanActivator.log(status); } } }
From source file:org.entando.entando.plugins.jpoauthclient.aps.system.httpclient.OAuthHttpClient.java
@Override public HttpResponseMessage execute(HttpMessage request, Map<String, Object> parameters) throws IOException { final String method = request.method; final String url = request.url.toExternalForm(); final InputStream body = request.getBody(); final boolean isDelete = DELETE.equalsIgnoreCase(method); final boolean isPost = POST.equalsIgnoreCase(method); final boolean isPut = PUT.equalsIgnoreCase(method); byte[] excerpt = null; HttpMethod httpMethod; if (isPost || isPut) { EntityEnclosingMethod entityEnclosingMethod = isPost ? new PostMethod(url) : new PutMethod(url); if (body != null) { ExcerptInputStream e = new ExcerptInputStream(body); String length = request.removeHeaders(HttpMessage.CONTENT_LENGTH); entityEnclosingMethod.setRequestEntity((length == null) ? new InputStreamRequestEntity(e) : new InputStreamRequestEntity(e, Long.parseLong(length))); excerpt = e.getExcerpt();/* ww w . j a va2 s. com*/ } httpMethod = entityEnclosingMethod; } else if (isDelete) { httpMethod = new DeleteMethod(url); } else { httpMethod = new GetMethod(url); } for (Map.Entry<String, Object> p : parameters.entrySet()) { String name = p.getKey(); String value = p.getValue().toString(); if (FOLLOW_REDIRECTS.equals(name)) { httpMethod.setFollowRedirects(Boolean.parseBoolean(value)); } else if (READ_TIMEOUT.equals(name)) { httpMethod.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, Integer.parseInt(value)); } } for (Map.Entry<String, String> header : request.headers) { httpMethod.addRequestHeader(header.getKey(), header.getValue()); } HttpClient client = this._clientPool.getHttpClient(new URL(httpMethod.getURI().toString())); client.executeMethod(httpMethod); return new HttpMethodResponse(httpMethod, excerpt, request.getContentCharset()); }
From source file:org.infoglue.cms.applications.managementtool.actions.UploadPortletAction.java
/** * Report to deliver engines that a portlet has been uploaded * * @param contentId//from w ww . j av a2 s .com * contentId of portlet */ private void updateDeliverEngines(Integer digitalAssetId) { List allUrls = CmsPropertyHandler.getInternalDeliveryUrls(); allUrls.addAll(CmsPropertyHandler.getPublicDeliveryUrls()); Iterator urlIterator = allUrls.iterator(); while (urlIterator.hasNext()) { String url = (String) urlIterator.next() + "/DeployPortlet.action"; try { HttpClient client = new HttpClient(); // establish a connection within 5 seconds client.setConnectionTimeout(5000); // set the default credentials HttpMethod method = new GetMethod(url); method.setQueryString("digitalAssetId=" + digitalAssetId); method.setFollowRedirects(true); // execute the method client.executeMethod(method); StatusLine status = method.getStatusLine(); if (status != null && status.getStatusCode() == 200) { log.info("Successfully deployed portlet at " + url); } else { log.warn("Failed to deploy portlet at " + url + ": " + status); } //clean up the connection resources method.releaseConnection(); } catch (Exception e) { e.printStackTrace(); } } /* Properties props = CmsPropertyHandler.getProperties(); for (Enumeration keys = props.keys(); keys.hasMoreElements();) { String key = (String) keys.nextElement(); if (key.startsWith(PORTLET_DEPLOY_PREFIX)) { String url = props.getProperty(key); try { HttpClient client = new HttpClient(); //establish a connection within 5 seconds client.setConnectionTimeout(5000); //set the default credentials HttpMethod method = new GetMethod(url); method.setQueryString("digitalAssetId=" + digitalAssetId); method.setFollowRedirects(true); //execute the method client.executeMethod(method); StatusLine status = method.getStatusLine(); if (status != null && status.getStatusCode() == 200) { log.info("Successfully deployed portlet at " + url); } else { log.warn("Failed to deploy portlet at " + url + ": " + status); } //clean up the connection resources method.releaseConnection(); } catch (Throwable e) { log.error(e.getMessage(), e); } } } */ }
From source file:org.infoscoop.request.filter.ProxyFilterContainer.java
public final int invoke(HttpClient client, HttpMethod method, ProxyRequest request) throws Exception { int preStatus = prepareInvoke(client, method, request); switch (preStatus) { case 0://from www . j av a2s . com break; case EXECUTE_POST_STATUS: doFilterChain(request, request.getResponseBody()); default: return preStatus; } // copy headers sent target server List ignoreHeaderNames = request.getIgnoreHeaders(); List allowedHeaderNames = request.getAllowedHeaders(); boolean allowAllHeader = false; Proxy proxy = request.getProxy(); if (proxy != null) { allowAllHeader = proxy.isAllowAllHeader(); if (!allowAllHeader) allowedHeaderNames.addAll(proxy.getAllowedHeaders()); } AuthenticatorUtil.doAuthentication(client, method, request); StringBuffer headersSb = new StringBuffer(); for (String name : request.getRequestHeaders().keySet()) { String value = request.getRequestHeader(name); String lowname = name.toLowerCase(); if (!allowAllHeader && !allowedHeaderNames.contains(lowname)) continue; if (ignoreHeaderNames.contains(lowname)) continue; if ("cookie".equalsIgnoreCase(name)) { if (proxy.getSendingCookies() != null) { value = RequestUtil.removeCookieParam(value, proxy.getSendingCookies()); } } if ("if-modified-since".equalsIgnoreCase(name) && "Thu, 01 Jun 1970 00:00:00 GMT".equals(value)) continue; method.addRequestHeader(new Header(name, value)); headersSb.append(name + "=" + value + ", "); } int cacheStatus = getCache(client, method, request); if (cacheStatus != 0) return cacheStatus; if (log.isInfoEnabled()) log.info("RequestHeader: " + headersSb); // execute http method and process redirect method.setFollowRedirects(false); client.executeMethod(method); int statusCode = method.getStatusCode(); for (int i = 0; statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT; i++) { // connection release method.releaseConnection(); if (i == 5) { log.error("The circular redirect is limited by five times."); return 500; } Header location = method.getResponseHeader("Location"); String redirectUrl = location.getValue(); // According to 2,068 1.1 rfc http spec, we cannot appoint the relative URL, // but microsoft.com gives back the relative URL. if (redirectUrl.startsWith("/")) { URI baseURI = method.getURI(); baseURI.setPath(redirectUrl); redirectUrl = baseURI.toString(); } //method.setURI(new URI(redirectUrl, false)); Header[] headers = method.getRequestHeaders(); method = new GetMethod(redirectUrl); for (int j = 0; j < headers.length; j++) { String headerName = headers[j].getName(); if (!headerName.equalsIgnoreCase("content-length") && !headerName.equalsIgnoreCase("authorization")) method.setRequestHeader(headers[j]); } AuthenticatorUtil.doAuthentication(client, method, request); method.setRequestHeader("authorization", request.getRequestHeader("Authorization")); method.setFollowRedirects(false); client.executeMethod(method); statusCode = method.getStatusCode(); request.setRedirectURL(redirectUrl); if (log.isInfoEnabled()) log.info("Redirect " + request.getTargetURL() + " to " + location + "."); } // copy response headers to proxyReqeust Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { request.putResponseHeader(headers[i].getName(), headers[i].getValue()); } if (log.isInfoEnabled()) log.info("Original Status:" + statusCode); // check response code if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { log.error("Proxy Authentication Required. Confirm ajax proxy setting."); throw new Exception( "Http Status 407, Proxy Authentication Required. Please contuct System Administrator."); } if (statusCode == HttpStatus.SC_NOT_MODIFIED || statusCode == HttpStatus.SC_RESET_CONTENT) { return statusCode; } else if (statusCode < 200 || statusCode >= 300) { request.setResponseBody(method.getResponseBodyAsStream()); return statusCode; } // process response body InputStream responseStream = null; if (statusCode != HttpStatus.SC_NO_CONTENT) { if (request.allowUserPublicCache()) { byte[] responseBody = method.getResponseBody(); Map<String, List<String>> responseHeaders = request.getResponseHeaders(); if (request.getRedirectURL() != null) responseHeaders.put("X-IS-REDIRECTED-FROM", Arrays.asList(new String[] { request.getRedirectURL() })); if (method instanceof GetMethod) { putCache(request.getOriginalURL(), new ByteArrayInputStream(responseBody), responseHeaders); } responseStream = new ByteArrayInputStream(responseBody); } else { responseStream = method.getResponseBodyAsStream(); } } doFilterChain(request, responseStream); return statusCode != HttpStatus.SC_NO_CONTENT ? method.getStatusCode() : 200; }
From source file:org.j2free.http.HttpCallable.java
public HttpCallResult call() throws IOException { HttpMethod method; if (task.method == HttpCallTask.Method.GET) method = new GetMethod(task.toString()); else {/* www . j a v a 2s . c o m*/ method = new PostMethod(task.url); String postBody = task.getExplicitPostBody(); if (postBody != null) { ((PostMethod) method).setRequestEntity(new StringRequestEntity(postBody, "text/xml", null)); } else { List<KeyValuePair<String, String>> params = task.getQueryParams(); NameValuePair[] data = new NameValuePair[params.size()]; int i = 0; for (KeyValuePair<String, String> param : params) { data[i] = new NameValuePair(param.key, param.value); i++; } ((PostMethod) method).setRequestBody(data); } } for (Header header : task.getRequestHeaders()) { method.setRequestHeader(header); } method.setFollowRedirects(task.followRedirects); try { if (log.isDebugEnabled()) log.debug("Making HTTP call [url=" + task.toString() + "]"); client.executeMethod(method); if (log.isDebugEnabled()) log.debug("Call returned [status=" + method.getStatusCode() + "]"); return new HttpCallResult(method); } finally { // ALWAYS release the connection!!! method.releaseConnection(); } }
From source file:org.jboss.orion.openshift.server.proxy.JsonProxyServlet.java
/** * Executes the {@link HttpMethod} passed in and sends the proxy response * back to the client via the given {@link javax.servlet.http.HttpServletResponse} * * @param proxyDetails//w w w . j a va 2 s. com * @param httpMethodProxyRequest An object representing the proxy request to be made * @param httpServletResponse An object by which we can send the proxied * response back to the client * @throws java.io.IOException Can be thrown by the {@link HttpClient}.executeMethod * @throws javax.servlet.ServletException Can be thrown to indicate that another error has occurred */ private void executeProxyRequest(ProxyDetails proxyDetails, HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { httpMethodProxyRequest.setDoAuthentication(false); httpMethodProxyRequest.setFollowRedirects(false); // Create a default HttpClient HttpClient httpClient = proxyDetails.createHttpClient(httpMethodProxyRequest); // Execute the request int intProxyResponseCode = 500; try { intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest); } catch (Exception e) { e.printStackTrace(); } // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect // Hooray for open source software if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { String stringStatusCode = Integer.toString(intProxyResponseCode); String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue(); if (stringLocation == null) { throw new ServletException("Received status code: " + stringStatusCode + " but no " + STRING_LOCATION_HEADER + " header was found in the response"); } // Modify the redirect to go to this proxy servlet rather that the proxied host String stringMyHostName = httpServletRequest.getServerName(); if (httpServletRequest.getServerPort() != 80) { stringMyHostName += ":" + httpServletRequest.getServerPort(); } stringMyHostName += httpServletRequest.getContextPath(); httpServletResponse.sendRedirect(stringLocation .replace(proxyDetails.getProxyHostAndPort() + proxyDetails.getProxyPath(), stringMyHostName)); return; } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) { // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0); httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } // Pass the response code back to the client httpServletResponse.setStatus(intProxyResponseCode); // Pass response headers back to the client Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders(); for (Header header : headerArrayResponse) { if (!ProxySupport.isHopByHopHeader(header.getName())) { if (ProxySupport.isSetCookieHeader(header)) { HttpProxyRule proxyRule = proxyDetails.getProxyRule(); String setCookie = ProxySupport.replaceCookieAttributes(header.getValue(), proxyRule.getCookiePath(), proxyRule.getCookieDomain()); httpServletResponse.setHeader(header.getName(), setCookie); } else { httpServletResponse.setHeader(header.getName(), header.getValue()); } } } // check if we got data, that is either the Content-Length > 0 // or the response code != 204 int code = httpMethodProxyRequest.getStatusCode(); boolean noData = code == HttpStatus.SC_NO_CONTENT; if (!noData) { String length = httpServletRequest.getHeader(STRING_CONTENT_LENGTH_HEADER_NAME); if (length != null && "0".equals(length.trim())) { noData = true; } } if (!noData) { // Send the content to the client InputStream inputStreamProxyResponse = httpMethodProxyRequest.getResponseBodyAsStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamProxyResponse); OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream(); int intNextByte; while ((intNextByte = bufferedInputStream.read()) != -1) { outputStreamClientResponse.write(intNextByte); } } }
From source file:org.jboss.web.loadbalancer.Loadbalancer.java
protected HttpClient prepareServerRequest(HttpServletRequest request, HttpServletResponse response, HttpMethod method) { // clear state HttpClient client = new HttpClient(connectionManager); client.setStrictMode(false);/*from ww w . j av a 2 s . co m*/ client.setTimeout(connectionTimeout); method.setFollowRedirects(false); method.setDoAuthentication(false); client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); Enumeration reqHeaders = request.getHeaderNames(); while (reqHeaders.hasMoreElements()) { String headerName = (String) reqHeaders.nextElement(); String headerValue = request.getHeader(headerName); if (!ignorableHeader.contains(headerName.toLowerCase())) { method.setRequestHeader(headerName, headerValue); } } //Cookies Cookie[] cookies = request.getCookies(); HttpState state = client.getState(); for (int i = 0; cookies != null && i < cookies.length; ++i) { Cookie cookie = cookies[i]; org.apache.commons.httpclient.Cookie reqCookie = new org.apache.commons.httpclient.Cookie(); reqCookie.setName(cookie.getName()); reqCookie.setValue(cookie.getValue()); if (cookie.getPath() != null) { reqCookie.setPath(cookie.getPath()); } else { reqCookie.setPath("/"); } reqCookie.setSecure(cookie.getSecure()); reqCookie.setDomain(method.getHostConfiguration().getHost()); state.addCookie(reqCookie); } return client; }
From source file:org.jivesoftware.util.HttpClientWithTimeoutFeedFetcher.java
/** * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL) *//*from w w w . ja v a 2s . co m*/ public SyndFeed retrieveFeed(URL feedUrl) throws IllegalArgumentException, IOException, FeedException, FetcherException { if (feedUrl == null) { throw new IllegalArgumentException("null is not a valid URL"); } // TODO Fix this //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); HttpClient client = new HttpClient(); HttpConnectionManager conManager = client.getHttpConnectionManager(); conManager.getParams().setParameter("http.connection.timeout", 3000); conManager.getParams().setParameter("http.socket.timeout", 3000); if (getCredentialSupplier() != null) { client.getState().setAuthenticationPreemptive(true); // TODO what should realm be here? Credentials credentials = getCredentialSupplier().getCredentials(null, feedUrl.getHost()); if (credentials != null) { client.getState().setCredentials(null, feedUrl.getHost(), credentials); } } System.setProperty("httpclient.useragent", "Openfire Admin Console: v" + XMPPServer.getInstance().getServerInfo().getVersion().getVersionString()); String urlStr = feedUrl.toString(); FeedFetcherCache cache = getFeedInfoCache(); if (cache != null) { // retrieve feed HttpMethod method = new GetMethod(urlStr); method.addRequestHeader("Accept-Encoding", "gzip"); try { if (isUsingDeltaEncoding()) { method.setRequestHeader("A-IM", "feed"); } // get the feed info from the cache // Note that syndFeedInfo will be null if it is not in the cache SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl); if (syndFeedInfo != null) { method.setRequestHeader("If-None-Match", syndFeedInfo.getETag()); if (syndFeedInfo.getLastModified() instanceof String) { method.setRequestHeader("If-Modified-Since", (String) syndFeedInfo.getLastModified()); } } method.setFollowRedirects(true); int statusCode = client.executeMethod(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr); handleErrorCodes(statusCode); SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode); syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode); cache.setFeedInfo(new URL(urlStr), syndFeedInfo); // the feed may have been modified to pick up cached values // (eg - for delta encoding) feed = syndFeedInfo.getSyndFeed(); return feed; } finally { method.releaseConnection(); } } else { // cache is not in use HttpMethod method = new GetMethod(urlStr); try { method.setFollowRedirects(true); int statusCode = client.executeMethod(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr); handleErrorCodes(statusCode); return getFeed(null, urlStr, method, statusCode); } finally { method.releaseConnection(); } } }
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 *///w ww . j a va 2 s . co 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; }