}
// redirection should be adjusted with local host header...
httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
HttpRequestBase httpRequest = null;
String method = request.getMethod();
if (HttpGet.METHOD_NAME.equals(method))
{
httpRequest = new HttpGet(proxyTargetURL);
}
else if (HttpHead.METHOD_NAME.equals(method))
{
httpRequest = new HttpHead(proxyTargetURL);
}
else if (HttpPost.METHOD_NAME.equals(method))
{
httpRequest = new HttpPost(proxyTargetURL);
long contentLength = NumberUtils.toLong(request.getHeader(HTTP.CONTENT_LEN));
if (contentLength > 0L)
{
HttpEntity entity = new InputStreamEntity(request.getInputStream(), contentLength);
((HttpPost) httpRequest).setEntity(entity);
}
}
else if (HttpPut.METHOD_NAME.equals(method))
{
httpRequest = new HttpPut(proxyTargetURL);
long contentLength = NumberUtils.toLong(request.getHeader(HTTP.CONTENT_LEN));
if (contentLength > 0L)
{
HttpEntity entity = new InputStreamEntity(request.getInputStream(), contentLength);
((HttpPost) httpRequest).setEntity(entity);
}
}
else if (HttpDelete.METHOD_NAME.equals(method))
{
httpRequest = new HttpDelete(proxyTargetURL);
}
else if (HttpOptions.METHOD_NAME.equals(method))
{
httpRequest = new HttpOptions(proxyTargetURL);
}
else if (HttpTrace.METHOD_NAME.equals(method))
{
httpRequest = new HttpHead(proxyTargetURL);
}
else
{
throw new HttpReverseProxyException("Unsupported method: " + method);
}
// set sso credentials if available
List<SSOSiteCredentials> credsList = getSSOSiteCredentials(proxyTargetURL, httpClient, request);
if (credsList != null && !credsList.isEmpty())
{
SSOSiteCredentials firstCreds = credsList.get(0);
if (firstCreds.isFormAuthentication() && StringUtils.equals(firstCreds.getBaseURL(), proxyTargetURL))
{
httpRequest = new HttpPost(proxyTargetURL);
List <NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair(firstCreds.getFormUserField(), firstCreds.getUsername()));
formParams.add(new BasicNameValuePair(firstCreds.getFormPwdField(), firstCreds.getPassword()));
((HttpPost) httpRequest).setEntity(new UrlEncodedFormEntity(formParams));
}
else
{
for (SSOSiteCredentials creds : credsList)
{
AuthScope authScope = new AuthScope(creds.getHost(), creds.getPort(), creds.getRealm(), creds.getScheme());
Credentials usernamePwdCreds = new UsernamePasswordCredentials(creds.getUsername(), creds.getPassword());
httpClient.getCredentialsProvider().setCredentials(authScope, usernamePwdCreds);
}
}
}
// pass most headers to proxy target...
for (Enumeration enumHeaderNames = request.getHeaderNames(); enumHeaderNames.hasMoreElements(); )
{
String headerName = (String) enumHeaderNames.nextElement();
if (StringUtils.equalsIgnoreCase(headerName, HTTP.CONTENT_LEN))
continue;
if (StringUtils.equalsIgnoreCase(headerName, HTTP.TARGET_HOST))
continue;
for (Enumeration enumHeaderValues = request.getHeaders(headerName); enumHeaderValues.hasMoreElements(); )
{
String headerValue = (String) enumHeaderValues.nextElement();
httpRequest.addHeader(headerName, headerValue);
}
}
Map<String, String> defaultRequestHeaders = proxyPathMapper.getDefaultRequestHeaders();
if (defaultRequestHeaders != null)
{
for (Map.Entry<String, String> entry : defaultRequestHeaders.entrySet())
{
httpRequest.setHeader(entry.getKey(), entry.getValue());
}
}
CookieStore cookieStore = httpClient.getCookieStore();
if (cookieStore != null)
{
Map<String, String> defaultRequestCookies = proxyPathMapper.getDefaultRequestCookies();
if (defaultRequestCookies != null)
{
for (Map.Entry<String, String> entry : defaultRequestCookies.entrySet())
{
cookieStore.addCookie(new BasicClientCookie(entry.getKey(), entry.getValue()));
}
}
}
HttpEntity httpEntity = null;
try
{
HttpResponse httpResponse = httpClient.execute(httpRequest);
httpEntity = httpResponse.getEntity();
String rewriterContextPath = localBaseURL;
if (rewriterContextPath == null)
{
rewriterContextPath = request.getContextPath() + request.getServletPath();
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
// Check if the proxy response is a redirect
if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
&& statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */)
{
String location = null;
Header locationHeader = httpResponse.getFirstHeader(HttpReverseProxyConstants.HTTP_HEADER_LOCATION);
if (locationHeader != null)
{
location = locationHeader.getValue();
}
if (location == null)
{
throw new HttpReverseProxyException("Recieved status code is " + statusCode + " but no " + HttpReverseProxyConstants.HTTP_HEADER_LOCATION + " header was found in the response");
}
// Modify the redirect to go to this proxy servlet rather that the proxied host
// FYI, according to rfc2616, "Location" header value must be an absolute URI.
String localPath = proxyPathMapper.getLocalPath(location);
// if the current proxy path mapper cannot map the remote location to local path, then
// try to find out a possible path mapper instead one more...
if (localPath == null)
{
HttpReverseProxyPathMapper proxyPathMapperByLocation = proxyPathMapperProvider.findMapperByRemoteURL(location);
if (proxyPathMapperByLocation != null)
{
localPath = proxyPathMapperByLocation.getLocalPath(location);
}
}
String redirectLocation = null;
if (localPath == null)
{
if (log.isWarnEnabled())
{
log.warn("Cannot translate the redirect location to local path. {}", location);
}
redirectLocation = location;
}
else
{
redirectLocation = rewriterContextPath + localPath;
}
if (!responseSetCookies.isEmpty())
{
addResponseCookies(request, response, responseSetCookies, proxyPathMapper, rewriterContextPath);
}
response.sendRedirect(redirectLocation);
return;
}
else if (statusCode == 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.
response.setIntHeader(HTTP.CONTENT_LEN, 0);
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (!responseSetCookies.isEmpty())
{
addResponseCookies(request, response, responseSetCookies, proxyPathMapper, rewriterContextPath);
}
return;
}
else
{
// Pass the response code back to the client
response.setStatus(statusCode);
if (httpEntity != null)
{
boolean rewritable = false;
Rewriter rewriter = null;
ParserAdaptor parserAdaptor = null;
RewriterController rewriterController = proxyPathMapperProvider.getRewriterController(proxyPathMapper);
if (rewriterController != null)
{
parserAdaptor = createParserAdaptor(rewriterController, httpEntity);
if (parserAdaptor != null)
{
rewriter = createRewriter(rewriterController, proxyPathMapper);
rewritable = (rewriter != null);
}
}
// Pass response headers back to the client
Header [] headerArrayResponse = httpResponse.getAllHeaders();
for (Header header : headerArrayResponse)
{
String headerName = header.getName();
if (rewritable && StringUtils.equalsIgnoreCase(headerName, HTTP.CONTENT_LEN))
continue;
if (StringUtils.startsWithIgnoreCase(headerName, "Set-Cookie"))
continue;
String headerValue = header.getValue();
if (StringUtils.equalsIgnoreCase(headerName, HTTP.TARGET_HOST))
{
response.setHeader(headerName, hostHeaderValue);
}
else
{
response.setHeader(headerName, headerValue);
}
}
if (!responseSetCookies.isEmpty())
{
addResponseCookies(request, response, responseSetCookies, proxyPathMapper, rewriterContextPath);
}
// Send the content to the client
writeHttpEntityToClient(response, httpEntity, proxyPathMapper, rewriterContextPath, localPathInfo, rewriter, parserAdaptor);
}
}
}
catch (IOException e)
{
if (log.isDebugEnabled())
{
log.error("IOException occurred during execution for " + proxyTargetURL, e);
}
else
{
log.error("IOException occurred during execution for {} {}", proxyTargetURL, e);
}
httpRequest.abort();
httpEntity = null;
throw e;
}
catch (Exception e)
{
if (log.isDebugEnabled())
{
log.error("Exception occurred during execution for " + proxyTargetURL, e);
}
else
{
log.error("Exception occurred during execution for {} {}", proxyTargetURL, e);
}
httpRequest.abort();
httpEntity = null;
throw new HttpReverseProxyException(e);
}
finally