You are viewing a plain text version of this content. The canonical link for it is here.
Posted to portalapps-dev@portals.apache.org by wo...@apache.org on 2014/07/13 20:38:34 UTC
svn commit: r1610277 - in
/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2:
./ chain/ impl/ servlet/
Author: woonsan
Date: Sun Jul 13 18:38:33 2014
New Revision: 1610277
URL: http://svn.apache.org/r1610277
Log:
APA-63: refines lifecycle methods of proxy command type. also adds response cookie handling
Added:
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddCookiesToResponseCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeaderToHttpRequestCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeadersToResponseCommand.java
- copied, changed from r1610247, portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/SerializeHttpEntityHeadersCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ProxyProcessingChain.java
- copied, changed from r1610247, portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ContinueProcessingChain.java
Removed:
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyNotFoundException.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ContinueProcessingChain.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/SerializeHttpEntityHeadersCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/LocationProxyMapping.java
Modified:
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ProxyMapping.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/RequestContext.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyException.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyService.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AbstractProxyCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ExecuteHttpClientCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleNotModifiedCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleRedirectionCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpClientCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpRequestCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveLocalPathCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveProxyMappingCommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveRemoteURICommand.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/DefaultReverseProxyService.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/ServletRequestContext.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/SimpleProxyMapping.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/DefaultReverseProxyServlet.java
portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/SimpleReverseProxyServlet.java
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ProxyMapping.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ProxyMapping.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ProxyMapping.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ProxyMapping.java Sun Jul 13 18:38:33 2014
@@ -20,6 +20,10 @@ package org.apache.portals.applications.
public interface ProxyMapping
{
+ public String getLocalBasePath();
+
+ public String getRemoteBaseURI();
+
public boolean matchesLocal(String localPath);
public String resolveRemoteFromLocal(String localPath);
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/RequestContext.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/RequestContext.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/RequestContext.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/RequestContext.java Sun Jul 13 18:38:33 2014
@@ -20,12 +20,16 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
+import javax.servlet.http.Cookie;
+
import org.apache.portals.applications.webcontent2.rewriter2.Sink;
public interface RequestContext
{
+ public boolean isSecure();
+
public String getServerName();
public int getServerPort();
@@ -56,6 +60,8 @@ public interface RequestContext
public void setIntHeader(String name, int value);
+ public void addCookie(Cookie cookie);
+
public Sink getSink();
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyException.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyException.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyException.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyException.java Sun Jul 13 18:38:33 2014
@@ -26,6 +26,8 @@ public class ReverseProxyException exten
private static final long serialVersionUID = 1L;
+ private int statusCode;
+
/**
* Constructs a new HttpReverseProxyException exception.
*/
@@ -34,6 +36,17 @@ public class ReverseProxyException exten
}
/**
+ * Constructs a new HttpReverseProxyException exception.
+ *
+ * @param statusCode
+ * the HTTP Response status code
+ */
+ public ReverseProxyException(int statusCode) {
+ super();
+ this.statusCode = statusCode;
+ }
+
+ /**
* Constructs a new HttpReverseProxyException exception with the given message.
*
* @param message
@@ -44,6 +57,19 @@ public class ReverseProxyException exten
}
/**
+ * Constructs a new HttpReverseProxyException exception with the given message.
+ *
+ * @param statusCode
+ * the HTTP Response status code
+ * @param message
+ * the exception message
+ */
+ public ReverseProxyException(int statusCode, String message) {
+ super(message);
+ this.statusCode = statusCode;
+ }
+
+ /**
* Constructs a new HttpReverseProxyException exception with the nested exception.
*
* @param nested
@@ -54,6 +80,19 @@ public class ReverseProxyException exten
}
/**
+ * Constructs a new HttpReverseProxyException exception with the nested exception.
+ *
+ * @param statusCode
+ * the HTTP Response status code
+ * @param nested
+ * the nested exception
+ */
+ public ReverseProxyException(int statusCode, Throwable nested) {
+ super(nested);
+ this.statusCode = statusCode;
+ }
+
+ /**
* Constructs a new HttpReverseProxyException exception when the container needs to do
* the following:
* <ul>
@@ -70,5 +109,35 @@ public class ReverseProxyException exten
public ReverseProxyException(String msg, Throwable nested) {
super(msg, nested);
}
-
+
+ /**
+ * Constructs a new HttpReverseProxyException exception when the container needs to do
+ * the following:
+ * <ul>
+ * <li>throw an exception
+ * <li>include the "nested" exception
+ * <li>include a description message
+ * </ul>
+ *
+ * @param statusCode
+ * the HTTP Response status code
+ * @param msg
+ * the exception message
+ * @param nested
+ * the nested exception
+ */
+ public ReverseProxyException(int statusCode, String msg, Throwable nested) {
+ super(msg, nested);
+ this.statusCode = statusCode;
+ }
+
+ /**
+ * Returns HTTP Response status code
+ * @return
+ */
+ public int getStatusCode()
+ {
+ return statusCode;
+ }
+
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyService.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyService.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyService.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/ReverseProxyService.java Sun Jul 13 18:38:33 2014
@@ -27,10 +27,6 @@ import javax.servlet.http.HttpServletRes
public interface ReverseProxyService
{
- public void initialize();
-
public void invoke(HttpServletRequest request, HttpServletResponse response) throws ReverseProxyException, IOException;
- public void destroy();
-
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AbstractProxyCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AbstractProxyCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AbstractProxyCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AbstractProxyCommand.java Sun Jul 13 18:38:33 2014
@@ -28,6 +28,10 @@ public abstract class AbstractProxyComma
private Boolean executionResult;
+ public void initialize()
+ {
+ }
+
public final boolean execute(Context context)
{
boolean savedResult = false;
@@ -53,6 +57,10 @@ public abstract class AbstractProxyComma
protected abstract boolean executeInternal(final ProxyContext context) throws Exception;
+ public void destroy()
+ {
+ }
+
public boolean isExecutionResult()
{
return executionResult;
Added: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddCookiesToResponseCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddCookiesToResponseCommand.java?rev=1610277&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddCookiesToResponseCommand.java (added)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddCookiesToResponseCommand.java Sun Jul 13 18:38:33 2014
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.applications.webcontent2.proxy2.chain;
+
+import java.util.Date;
+import java.util.List;
+
+import javax.servlet.http.Cookie;
+
+import org.apache.http.client.HttpClient;
+import org.apache.portals.applications.webcontent2.proxy2.ProxyMapping;
+import org.apache.portals.applications.webcontent2.proxy2.impl.ProxyHttpClient;
+
+
+public class AddCookiesToResponseCommand extends AbstractProxyCommand
+{
+
+ @Override
+ protected boolean executeInternal(final ProxyContext context) throws Exception
+ {
+ HttpClient httpClient = context.getHttpClient();
+
+ if (httpClient instanceof ProxyHttpClient)
+ {
+ List<org.apache.http.cookie.Cookie> responseCookies = ((ProxyHttpClient) httpClient).getResponseCookies();
+
+ if (!responseCookies.isEmpty())
+ {
+ boolean isSecureRequest = context.getRequestContext().isSecure();
+ ProxyMapping resolvedMapping = context.getResolvedMapping();
+ String rewrittenCookiePath = context.getRequestContext().getRequestBasePath() + resolvedMapping.getLocalBasePath();
+
+ for (org.apache.http.cookie.Cookie cookie : responseCookies)
+ {
+ String cookieName = cookie.getName();
+ Cookie responseCookie = new Cookie(cookieName, cookie.getValue());
+ responseCookie.setVersion(cookie.getVersion());
+ responseCookie.setComment(cookie.getComment());
+ Date expireDate = cookie.getExpiryDate();
+
+ if (expireDate != null)
+ {
+ int maxAgeSeconds = (int) ((expireDate.getTime() - System.currentTimeMillis()) / 1000L);
+ responseCookie.setMaxAge(maxAgeSeconds);
+ }
+
+ responseCookie.setSecure(isSecureRequest && cookie.isSecure());
+ responseCookie.setVersion(cookie.getVersion());
+
+ responseCookie.setPath(rewrittenCookiePath);
+
+ context.getRequestContext().addCookie(responseCookie);
+ }
+ }
+ }
+
+ return false;
+ }
+
+}
Added: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeaderToHttpRequestCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeaderToHttpRequestCommand.java?rev=1610277&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeaderToHttpRequestCommand.java (added)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeaderToHttpRequestCommand.java Sun Jul 13 18:38:33 2014
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.applications.webcontent2.proxy2.chain;
+
+import java.util.Enumeration;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.protocol.HTTP;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class AddHeaderToHttpRequestCommand extends AbstractProxyCommand
+{
+
+ private static Logger log = LoggerFactory.getLogger(AddHeaderToHttpRequestCommand.class);
+
+ @Override
+ protected boolean executeInternal(final ProxyContext context) throws Exception
+ {
+ HttpRequestBase httpRequest = context.getHttpRequest();
+
+ // pass most headers to remote URI...
+ for (Enumeration enumHeaderNames = context.getRequestContext().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 = context.getRequestContext().getHeaders(headerName); enumHeaderValues.hasMoreElements(); )
+ {
+ String headerValue = (String) enumHeaderValues.nextElement();
+ httpRequest.addHeader(headerName, headerValue);
+ }
+ }
+
+ return false;
+ }
+
+}
Copied: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeadersToResponseCommand.java (from r1610247, portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/SerializeHttpEntityHeadersCommand.java)
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeadersToResponseCommand.java?p2=portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeadersToResponseCommand.java&p1=portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/SerializeHttpEntityHeadersCommand.java&r1=1610247&r2=1610277&rev=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/SerializeHttpEntityHeadersCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/AddHeadersToResponseCommand.java Sun Jul 13 18:38:33 2014
@@ -25,7 +25,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.protocol.HTTP;
-public class SerializeHttpEntityHeadersCommand extends AbstractProxyCommand
+public class AddHeadersToResponseCommand extends AbstractProxyCommand
{
private String hostHeaderValue;
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ExecuteHttpClientCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ExecuteHttpClientCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ExecuteHttpClientCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ExecuteHttpClientCommand.java Sun Jul 13 18:38:33 2014
@@ -16,13 +16,9 @@
*/
package org.apache.portals.applications.webcontent2.proxy2.chain;
-import java.util.Enumeration;
-
-import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,24 +33,6 @@ public class ExecuteHttpClientCommand ex
{
HttpRequestBase httpRequest = context.getHttpRequest();
- // pass most headers to remote URI...
- for (Enumeration enumHeaderNames = context.getRequestContext().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 = context.getRequestContext().getHeaders(headerName); enumHeaderValues.hasMoreElements(); )
- {
- String headerValue = (String) enumHeaderValues.nextElement();
- httpRequest.addHeader(headerName, headerValue);
- }
- }
-
HttpClient httpClient = context.getHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
context.setHttpResponse(httpResponse);
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleNotModifiedCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleNotModifiedCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleNotModifiedCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleNotModifiedCommand.java Sun Jul 13 18:38:33 2014
@@ -36,18 +36,20 @@ public class HandleNotModifiedCommand ex
int statusCode = httpResponse.getStatusLine().getStatusCode();
- if (statusCode == HttpServletResponse.SC_NOT_MODIFIED)
+ 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.
- context.getRequestContext().setIntHeader(HTTP.CONTENT_LEN, 0);
- context.getRequestContext().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+ return false;
}
+ // 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.
+ context.getRequestContext().setIntHeader(HTTP.CONTENT_LEN, 0);
+ context.getRequestContext().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+
return false;
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleRedirectionCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleRedirectionCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleRedirectionCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/HandleRedirectionCommand.java Sun Jul 13 18:38:33 2014
@@ -23,7 +23,7 @@ import javax.servlet.http.HttpServletRes
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.portals.applications.webcontent2.proxy.HttpReverseProxyConstants;
-import org.apache.portals.applications.webcontent2.proxy.HttpReverseProxyException;
+import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,65 +41,61 @@ public class HandleRedirectionCommand ex
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 */)
+ if (statusCode < HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
+ || statusCode >= HttpServletResponse.SC_NOT_MODIFIED /* 304 */)
{
- String location = null;
- Header locationHeader = httpResponse.getFirstHeader(HttpReverseProxyConstants.HTTP_HEADER_LOCATION);
+ return false;
+ }
- if (locationHeader != null)
- {
- location = locationHeader.getValue();
- }
+ String location = null;
+ Header locationHeader = httpResponse.getFirstHeader(HttpReverseProxyConstants.HTTP_HEADER_LOCATION);
- if (location == null)
- {
- throw new HttpReverseProxyException("Recieved status code is " + statusCode + " but no " + HttpReverseProxyConstants.HTTP_HEADER_LOCATION + " header was found in the response");
- }
-
- // According to rfc2616, "Location" header value must be an absolute URI.
- // However, in case it is not, prepend the host URI of the proxy target URL.
- if (location.startsWith("/")) {
- URI targetURI = new URI(context.getRemoteURI());
- String scheme = targetURI.getScheme();
- int port = targetURI.getPort();
- StringBuilder uriBuilder = new StringBuilder(40).append(targetURI.getScheme()).append("://").append(targetURI.getHost());
-
- if (port > 0 && ((port != 80 && "http".equals(scheme)) || (port != 443 && "https".equals(scheme)))) {
- uriBuilder.append(':').append(port);
- }
+ if (locationHeader != null)
+ {
+ location = locationHeader.getValue();
+ }
- uriBuilder.append(location);
- location = uriBuilder.toString();
- }
+ if (location == null)
+ {
+ throw new ReverseProxyException(HttpServletResponse.SC_BAD_GATEWAY, "No location header.");
+ }
- // Modify the redirect to go to this proxy servlet rather that the proxied host
- String localPath = context.getResolvedMapping().resolveLocalFromRemote(location);
+ // According to rfc2616, "Location" header value must be an absolute URI.
+ // However, in case it is not, prepend the host URI of the proxy target URL.
+ if (location.startsWith("/")) {
+ URI targetURI = new URI(context.getRemoteURI());
+ String scheme = targetURI.getScheme();
+ int port = targetURI.getPort();
+ StringBuilder uriBuilder = new StringBuilder(40).append(targetURI.getScheme()).append("://").append(targetURI.getHost());
- // TODO: 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 time ...
+ if (port > 0 && ((port != 80 && "http".equals(scheme)) || (port != 443 && "https".equals(scheme)))) {
+ uriBuilder.append(':').append(port);
+ }
- String redirectLocation = null;
+ uriBuilder.append(location);
+ location = uriBuilder.toString();
+ }
- if (localPath == null)
- {
- if (log.isWarnEnabled())
- {
- log.warn("Cannot translate the redirect location to local path. {}", location);
- }
+ // Modify the redirect to go to this proxy servlet rather that the proxied host
+ String localPath = context.getResolvedMapping().resolveLocalFromRemote(location);
- redirectLocation = location;
- }
- else
- {
- redirectLocation = context.getRequestContext().getRequestBasePath() + localPath;
- }
+ // TODO: 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 time ...
- context.getRequestContext().sendRedirect(redirectLocation);
+ String redirectLocation = null;
- return true;
+ if (localPath == null)
+ {
+ log.warn("Cannot translate the redirect location to local path. {}", location);
+ redirectLocation = location;
+ }
+ else
+ {
+ redirectLocation = context.getRequestContext().getRequestBasePath() + localPath;
}
+ context.getRequestContext().sendRedirect(redirectLocation);
+
return false;
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpClientCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpClientCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpClientCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpClientCommand.java Sun Jul 13 18:38:33 2014
@@ -30,6 +30,9 @@ public class InitHttpClientCommand exten
private HttpParams httpClientParams;
+ // Note: normally redirection should be adjusted with local host header...
+ private Boolean handleRedirects = Boolean.FALSE;
+
private HttpRoutePlanner httpRoutePlanner;
@Override
@@ -37,19 +40,30 @@ public class InitHttpClientCommand exten
{
ProxyHttpClient httpClient = new ProxyHttpClient(clientConnectionManager, httpClientParams);
+ if (handleRedirects != null)
+ {
+ httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, handleRedirects.booleanValue());
+ }
+
if (httpRoutePlanner != null)
{
httpClient.setRoutePlanner(httpRoutePlanner);
}
- // redirection should be adjusted with local host header...
- httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
-
context.setHttpClient(httpClient);
return false;
}
+ @Override
+ public void destroy()
+ {
+ if (clientConnectionManager != null)
+ {
+ clientConnectionManager.shutdown();
+ }
+ }
+
public ClientConnectionManager getClientConnectionManager()
{
return clientConnectionManager;
@@ -70,6 +84,16 @@ public class InitHttpClientCommand exten
this.httpClientParams = httpClientParams;
}
+ public Boolean getHandleRedirects()
+ {
+ return handleRedirects;
+ }
+
+ public void setHandleRedirects(Boolean handleRedirects)
+ {
+ this.handleRedirects = handleRedirects;
+ }
+
public HttpRoutePlanner getHttpRoutePlanner()
{
return httpRoutePlanner;
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpRequestCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpRequestCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpRequestCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/InitHttpRequestCommand.java Sun Jul 13 18:38:33 2014
@@ -16,6 +16,8 @@
*/
package org.apache.portals.applications.webcontent2.proxy2.chain;
+import javax.servlet.http.HttpServletResponse;
+
import org.apache.commons.lang.math.NumberUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpDelete;
@@ -87,7 +89,7 @@ public class InitHttpRequestCommand exte
}
else
{
- throw new ReverseProxyException("Unsupported method: " + method);
+ throw new ReverseProxyException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Unsupported method: " + method);
}
context.setHttpRequest(httpRequest);
Copied: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ProxyProcessingChain.java (from r1610247, portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ContinueProcessingChain.java)
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ProxyProcessingChain.java?p2=portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ProxyProcessingChain.java&p1=portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ContinueProcessingChain.java&r1=1610247&r2=1610277&rev=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ContinueProcessingChain.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ProxyProcessingChain.java Sun Jul 13 18:38:33 2014
@@ -17,6 +17,8 @@
package org.apache.portals.applications.webcontent2.proxy2.chain;
import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
@@ -24,29 +26,34 @@ import org.apache.commons.chain.impl.Cha
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class ContinueProcessingChain extends ChainBase
+public class ProxyProcessingChain extends ChainBase
{
- private Logger log = LoggerFactory.getLogger(ContinueProcessingChain.class);
+ private Logger log = LoggerFactory.getLogger(ProxyProcessingChain.class);
- public ContinueProcessingChain() {
+ public ProxyProcessingChain()
+ {
super();
}
- public ContinueProcessingChain(Command command) {
+ public ProxyProcessingChain(Command command)
+ {
super(command);
}
- public ContinueProcessingChain(Command[] commands) {
+ public ProxyProcessingChain(Command[] commands)
+ {
super(commands);
}
- public ContinueProcessingChain(Collection commands) {
+ public ProxyProcessingChain(Collection commands)
+ {
super(commands);
}
@Override
- public boolean execute(Context context) throws Exception {
+ public boolean execute(Context context) throws Exception
+ {
try
{
super.execute(context);
@@ -59,4 +66,28 @@ public class ContinueProcessingChain ext
return false;
}
+ public List<AbstractProxyCommand> getAllProxyCommands()
+ {
+ List<AbstractProxyCommand> allCommands = new LinkedList<AbstractProxyCommand>();
+ findDescendantProxyCommands(allCommands);
+ return allCommands;
+ }
+
+ private void findDescendantProxyCommands(List<AbstractProxyCommand> descendantProxyCommands)
+ {
+ if (commands != null)
+ {
+ for (Command command : commands)
+ {
+ if (command instanceof AbstractProxyCommand)
+ {
+ descendantProxyCommands.add((AbstractProxyCommand) command);
+ }
+ else if (command instanceof ProxyProcessingChain)
+ {
+ ((ProxyProcessingChain) command).findDescendantProxyCommands(descendantProxyCommands);
+ }
+ }
+ }
+ }
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveLocalPathCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveLocalPathCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveLocalPathCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveLocalPathCommand.java Sun Jul 13 18:38:33 2014
@@ -24,22 +24,10 @@ public class ResolveLocalPathCommand ext
@Override
protected boolean executeInternal(final ProxyContext context) throws Exception
{
- String localPath = getInvokedPathInfo(context);
+ String localPath = context.getRequestContext().getPathInfo();
context.setLocalPath(localPath);
return false;
}
- private String getInvokedPathInfo(final ProxyContext context)
- {
- String pathInfo = (String) context.getRequestContext().getAttribute("javax.servlet.include.path_info");
-
- if (pathInfo != null)
- {
- return pathInfo;
- }
-
- return context.getRequestContext().getPathInfo();
- }
-
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveProxyMappingCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveProxyMappingCommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveProxyMappingCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveProxyMappingCommand.java Sun Jul 13 18:38:33 2014
@@ -16,8 +16,10 @@
*/
package org.apache.portals.applications.webcontent2.proxy2.chain;
+import javax.servlet.http.HttpServletResponse;
+
import org.apache.portals.applications.webcontent2.proxy2.ProxyMapping;
-import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyNotFoundException;
+import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyException;
public class ResolveProxyMappingCommand extends AbstractProxyCommand
{
@@ -45,7 +47,7 @@ public class ResolveProxyMappingCommand
if (resolvedMapping == null)
{
- throw new ReverseProxyNotFoundException("Proxy configuration is not defined for " + localPath);
+ throw new ReverseProxyException(HttpServletResponse.SC_NOT_FOUND, "No mapping for " + localPath);
}
context.setResolvedMapping(resolvedMapping);
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveRemoteURICommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveRemoteURICommand.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveRemoteURICommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/chain/ResolveRemoteURICommand.java Sun Jul 13 18:38:33 2014
@@ -16,7 +16,9 @@
*/
package org.apache.portals.applications.webcontent2.proxy2.chain;
-import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyNotFoundException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyException;
import org.apache.portals.applications.webcontent2.proxy2.URICleaner;
@@ -33,7 +35,7 @@ public class ResolveRemoteURICommand ext
if (remoteURI == null)
{
- throw new ReverseProxyNotFoundException("Cannot translate the location path info into remote URI. " + remoteURI);
+ throw new ReverseProxyException(HttpServletResponse.SC_NOT_FOUND, "No mapped URI.");
}
String queryString = context.getRequestContext().getQueryString();
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/DefaultReverseProxyService.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/DefaultReverseProxyService.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/DefaultReverseProxyService.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/DefaultReverseProxyService.java Sun Jul 13 18:38:33 2014
@@ -24,10 +24,6 @@ public class DefaultReverseProxyService
this.proxyServiceCommand = proxyServiceCommand;
}
- public void initialize()
- {
- }
-
public void invoke(HttpServletRequest request, HttpServletResponse response) throws ReverseProxyException, IOException
{
try
@@ -38,11 +34,7 @@ public class DefaultReverseProxyService
}
catch (Exception e)
{
- log.error("Failed to execute proxy service.", e);
+ log.error("Failed to execute proxy service for " + request.getRequestURI(), e);
}
}
-
- public void destroy()
- {
- }
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/ServletRequestContext.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/ServletRequestContext.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/ServletRequestContext.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/ServletRequestContext.java Sun Jul 13 18:38:33 2014
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
+import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -40,6 +41,11 @@ public class ServletRequestContext imple
this.response = response;
}
+ public boolean isSecure()
+ {
+ return request.isSecure();
+ }
+
public String getServerName()
{
return request.getServerName();
@@ -63,7 +69,21 @@ public class ServletRequestContext imple
}
else
{
- return request.getContextPath() + request.getServletPath();
+ String contextPath = (String) request.getAttribute("javax.servlet.include.context_path");
+
+ if (contextPath == null)
+ {
+ return contextPath = request.getContextPath();
+ }
+
+ String servletPath = (String) request.getAttribute("javax.servlet.include.servlet_path");
+
+ if (servletPath == null)
+ {
+ servletPath = request.getServletPath();
+ }
+
+ return contextPath + servletPath;
}
}
@@ -74,6 +94,23 @@ public class ServletRequestContext imple
public String getPathInfo()
{
+ if (requestBasePath != null)
+ {
+ String requestURI = request.getRequestURI();
+
+ if (requestURI.startsWith(requestBasePath))
+ {
+ return requestURI.substring(requestBasePath.length());
+ }
+ }
+
+ String pathInfo = (String) request.getAttribute("javax.servlet.include.path_info");
+
+ if (pathInfo != null)
+ {
+ return pathInfo;
+ }
+
return request.getPathInfo();
}
@@ -122,6 +159,11 @@ public class ServletRequestContext imple
response.setIntHeader(name, value);
}
+ public void addCookie(Cookie cookie)
+ {
+ response.addCookie(cookie);
+ }
+
public void setStatus(int sc)
{
response.setStatus(sc);
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/SimpleProxyMapping.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/SimpleProxyMapping.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/SimpleProxyMapping.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/impl/SimpleProxyMapping.java Sun Jul 13 18:38:33 2014
@@ -31,6 +31,16 @@ public class SimpleProxyMapping implemen
this.remoteBaseURI = remoteBaseURI;
}
+ public String getLocalBasePath()
+ {
+ return localBasePath;
+ }
+
+ public String getRemoteBaseURI()
+ {
+ return remoteBaseURI;
+ }
+
public boolean matchesLocal(String localPath)
{
return StringUtils.startsWith(localPath, localBasePath);
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/DefaultReverseProxyServlet.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/DefaultReverseProxyServlet.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/DefaultReverseProxyServlet.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/DefaultReverseProxyServlet.java Sun Jul 13 18:38:33 2014
@@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
-import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -35,7 +34,6 @@ import org.apache.http.client.methods.Ht
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpTrace;
import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyException;
-import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyNotFoundException;
import org.apache.portals.applications.webcontent2.proxy2.ReverseProxyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,17 +70,6 @@ public class DefaultReverseProxyServlet
}
@Override
- public void init(ServletConfig servletConfig) throws ServletException
- {
- super.init(servletConfig);
-
- if (proxyService != null)
- {
- proxyService.initialize();
- }
- }
-
- @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
if (!AVAILABLE_HTTP_METHOD_SET.contains(request.getMethod()))
@@ -95,26 +82,19 @@ public class DefaultReverseProxyServlet
{
proxyService.invoke(request, response);
}
- catch (ReverseProxyNotFoundException e)
- {
- response.sendError(404, e.getLocalizedMessage());
- }
catch (ReverseProxyException e)
{
- throw new ServletException(e);
+ if (e.getStatusCode() > 0)
+ {
+ log.error("Response proxy processing exception occurred.", e);
+ response.sendError(e.getStatusCode(), e.getLocalizedMessage());
+ }
+ else
+ {
+ throw new ServletException(e);
+ }
}
}
}
- @Override
- public void destroy()
- {
- if (proxyService != null)
- {
- proxyService.destroy();
- }
-
- proxyService = null;
- }
-
}
Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/SimpleReverseProxyServlet.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/SimpleReverseProxyServlet.java?rev=1610277&r1=1610276&r2=1610277&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/SimpleReverseProxyServlet.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy2/servlet/SimpleReverseProxyServlet.java Sun Jul 13 18:38:33 2014
@@ -16,31 +16,38 @@
*/
package org.apache.portals.applications.webcontent2.proxy2.servlet;
+import java.util.List;
+
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
-import org.apache.commons.chain.Command;
import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.params.ConnManagerParamBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
import org.apache.portals.applications.webcontent2.proxy2.ProxyMapping;
+import org.apache.portals.applications.webcontent2.proxy2.chain.AbstractProxyCommand;
+import org.apache.portals.applications.webcontent2.proxy2.chain.AddCookiesToResponseCommand;
+import org.apache.portals.applications.webcontent2.proxy2.chain.AddHeaderToHttpRequestCommand;
+import org.apache.portals.applications.webcontent2.proxy2.chain.AddHeadersToResponseCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.CleanupCommand;
-import org.apache.portals.applications.webcontent2.proxy2.chain.ContinueProcessingChain;
import org.apache.portals.applications.webcontent2.proxy2.chain.ExecuteHttpClientCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.HandleNotModifiedCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.HandleRedirectionCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.InitHttpClientCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.InitHttpRequestCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.InitializationCommand;
+import org.apache.portals.applications.webcontent2.proxy2.chain.ProxyProcessingChain;
import org.apache.portals.applications.webcontent2.proxy2.chain.ResolveContentRewriterCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.ResolveLocalPathCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.ResolveProxyMappingCommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.ResolveRemoteURICommand;
import org.apache.portals.applications.webcontent2.proxy2.chain.SerializeHttpEntityContentCommand;
-import org.apache.portals.applications.webcontent2.proxy2.chain.SerializeHttpEntityHeadersCommand;
import org.apache.portals.applications.webcontent2.proxy2.impl.DefaultReverseProxyService;
import org.apache.portals.applications.webcontent2.proxy2.impl.SimpleProxyMapping;
import org.slf4j.Logger;
@@ -56,25 +63,35 @@ public class SimpleReverseProxyServlet e
private static Logger log = LoggerFactory.getLogger(SimpleReverseProxyServlet.class);
+ private ProxyProcessingChain proxyServiceCommand;
+
@Override
public void init(ServletConfig servletConfig) throws ServletException
{
- Command proxyServiceCommand = createProxyServiceCommand(servletConfig);
+ super.init(servletConfig);
+
+ proxyServiceCommand = createProxyServiceCommand(servletConfig);
+
+ List<AbstractProxyCommand> allProxyCommands = proxyServiceCommand.getAllProxyCommands();
+
+ for (AbstractProxyCommand proxyCommand : allProxyCommands)
+ {
+ proxyCommand.initialize();
+ }
+
DefaultReverseProxyService proxyService = new DefaultReverseProxyService(proxyServiceCommand);
setProxyService(proxyService);
-
- super.init(servletConfig);
}
- private Command createProxyServiceCommand(final ServletConfig servletConfig)
+ private ProxyProcessingChain createProxyServiceCommand(final ServletConfig servletConfig)
{
- ContinueProcessingChain preprocessingChain = new ContinueProcessingChain();
+ ProxyProcessingChain preprocessingChain = new ProxyProcessingChain();
InitializationCommand initializationCommand = new InitializationCommand();
preprocessingChain.addCommand(initializationCommand);
- ContinueProcessingChain processingChain = new ContinueProcessingChain();
+ ProxyProcessingChain processingChain = new ProxyProcessingChain();
ResolveLocalPathCommand resolveLocalPathCommand = new ResolveLocalPathCommand();
@@ -88,16 +105,22 @@ public class SimpleReverseProxyServlet e
InitHttpClientCommand initHttpClientCommand = new InitHttpClientCommand();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- //schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
- ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);
+ schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
+ HttpParams connManagerParams = new BasicHttpParams();
+ ConnManagerParamBean connManagerParamBean = new ConnManagerParamBean(connManagerParams);
+ connManagerParamBean.setMaxTotalConnections(400);
+ connManagerParamBean.setTimeout(10000L);
+ ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(connManagerParams, schemeRegistry);
initHttpClientCommand.setClientConnectionManager(clientConnectionManager);
initHttpClientCommand.setHttpClientParams(new BasicHttpParams());
InitHttpRequestCommand initHttpRequestCommand = new InitHttpRequestCommand();
+ AddHeaderToHttpRequestCommand addHeaderToHttpRequestCommand = new AddHeaderToHttpRequestCommand();
ExecuteHttpClientCommand executeHttpClientCommand = new ExecuteHttpClientCommand();
HandleRedirectionCommand handleRedirectionCommand = new HandleRedirectionCommand();
HandleNotModifiedCommand handleNotModifiedCommand = new HandleNotModifiedCommand();
- SerializeHttpEntityHeadersCommand serializeHttpEntityHeadersCommand = new SerializeHttpEntityHeadersCommand();
+ AddHeadersToResponseCommand addHeadersToResponseCommand = new AddHeadersToResponseCommand();
+ AddCookiesToResponseCommand addCookiesToResponseCommand = new AddCookiesToResponseCommand();
SerializeHttpEntityContentCommand serializeHttpEntityContentCommand = new SerializeHttpEntityContentCommand();
processingChain.addCommand(resolveLocalPathCommand);
@@ -106,19 +129,21 @@ public class SimpleReverseProxyServlet e
processingChain.addCommand(resolveContentRewriterCommand);
processingChain.addCommand(initHttpClientCommand);
processingChain.addCommand(initHttpRequestCommand);
+ processingChain.addCommand(addHeaderToHttpRequestCommand);
processingChain.addCommand(executeHttpClientCommand);
processingChain.addCommand(handleRedirectionCommand);
processingChain.addCommand(handleNotModifiedCommand);
- processingChain.addCommand(serializeHttpEntityHeadersCommand);
+ processingChain.addCommand(addHeadersToResponseCommand);
+ processingChain.addCommand(addCookiesToResponseCommand);
processingChain.addCommand(serializeHttpEntityContentCommand);
- ContinueProcessingChain postprocessingChain = new ContinueProcessingChain();
+ ProxyProcessingChain postprocessingChain = new ProxyProcessingChain();
CleanupCommand cleanupCommand = new CleanupCommand();
postprocessingChain.addCommand(cleanupCommand);
- ContinueProcessingChain proxyServiceChain = new ContinueProcessingChain();
+ ProxyProcessingChain proxyServiceChain = new ProxyProcessingChain();
proxyServiceChain.addCommand(preprocessingChain);
proxyServiceChain.addCommand(processingChain);
@@ -126,4 +151,23 @@ public class SimpleReverseProxyServlet e
return proxyServiceChain;
}
+
+ @Override
+ public void destroy()
+ {
+ List<AbstractProxyCommand> allProxyCommands = proxyServiceCommand.getAllProxyCommands();
+
+ for (AbstractProxyCommand proxyCommand : allProxyCommands)
+ {
+ try
+ {
+ proxyCommand.destroy();
+ }
+ catch (Exception e)
+ {
+ log.error("Failed to destroy proxy command, " + proxyCommand, e);
+ }
+ }
+ }
+
}