Author: woonsan
Date: Thu Jan 16 18:08:41 2014
New Revision: 1558868

URL: http://svn.apache.org/r1558868
Log:
APA-54: fixing NPE when proxied url is dispatched

Modified:
    
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java

Modified: 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
URL: 
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java?rev=1558868&r1=1558867&r2=1558868&view=diff
==============================================================================
--- 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
 (original)
+++ 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
 Thu Jan 16 18:08:41 2014
@@ -234,7 +234,7 @@ public class RewritableHttpReverseProxyS
         // proxyPathMapper can be injected by using request attribute.
         HttpReverseProxyPathMapper proxyPathMapper = 
(HttpReverseProxyPathMapper) 
request.getAttribute(HttpReverseProxyConstants.PATH_MAPPER);
 
-        String localPathInfo = request.getPathInfo();
+        String localPathInfo = getInvokedPathInfo(request);
 
         if (localPathInfo.indexOf('/', 1) == -1)
         {
@@ -468,7 +468,7 @@ public class RewritableHttpReverseProxyS
 
             if (rewriterContextPath == null)
             {
-                rewriterContextPath = request.getContextPath() + 
request.getServletPath();
+                rewriterContextPath = getInvokedContextPath(request) + 
getInvokedServletPath(request);
             }
 
             int statusCode = httpResponse.getStatusLine().getStatusCode();
@@ -618,8 +618,16 @@ public class RewritableHttpReverseProxyS
                         addResponseCookies(request, response, 
responseSetCookies, proxyPathMapper, rewriterContextPath);
                     }
 
-                    // Send the content to the client
-                    writeHttpEntityToClient(response, httpEntity, 
proxyPathMapper, rewriterContextPath, localPathInfo, rewriter, parserAdaptor);
+                    if (isDispatched(request))
+                    {
+                        // Send the content to the client
+                        writeHttpEntityToDispatcher(response, httpEntity, 
proxyPathMapper, rewriterContextPath, localPathInfo, rewriter, parserAdaptor);
+                    }
+                    else
+                    {
+                        // Send the content to the client
+                        writeHttpEntityToClient(response, httpEntity, 
proxyPathMapper, rewriterContextPath, localPathInfo, rewriter, parserAdaptor);
+                    }
                 }
             }
         }
@@ -739,13 +747,6 @@ public class RewritableHttpReverseProxyS
                         gzipEncoded = StringUtils.equalsIgnoreCase("gzip", 
contentEncodingHeader.getValue());
                     }
 
-                    if (parserAdaptor instanceof 
ReverseProxyRewritingContextAware)
-                    {
-                        ReverseProxyRewritingContext rewritingContext =
-                            new 
DefaultReverseProxyRewritingContext(proxyPathMapper, proxyPathMapperProvider, 
rewriterContextPath);
-                        ((ReverseProxyRewritingContextAware) 
parserAdaptor).setReverseProxyRewritingContext(rewritingContext);
-                    }
-
                     String responseCharSet = 
EntityUtils.getContentCharSet(httpEntity);
 
                     if (responseCharSet != null)
@@ -760,6 +761,14 @@ public class RewritableHttpReverseProxyS
                     }
 
                     rewriter.setBaseUrl(rewriterContextPath + localPathInfo);
+
+                    if (parserAdaptor instanceof 
ReverseProxyRewritingContextAware)
+                    {
+                        ReverseProxyRewritingContext rewritingContext =
+                            new 
DefaultReverseProxyRewritingContext(proxyPathMapper, proxyPathMapperProvider, 
rewriterContextPath);
+                        ((ReverseProxyRewritingContextAware) 
parserAdaptor).setReverseProxyRewritingContext(rewritingContext);
+                    }
+
                     rewriter.rewrite(parserAdaptor, reader, writer);
                     writer.flush();
                 }
@@ -786,6 +795,107 @@ public class RewritableHttpReverseProxyS
         }
     }
 
+    private void writeHttpEntityToDispatcher(HttpServletResponse response,
+                                         HttpEntity httpEntity,
+                                         HttpReverseProxyPathMapper 
proxyPathMapper,
+                                         String rewriterContextPath,
+                                         String localPathInfo,
+                                         Rewriter rewriter,
+                                         ParserAdaptor parserAdaptor) throws 
Exception
+    {
+        InputStream in = null;
+        Reader reader = null;
+        OutputStream out = null;
+        Writer writer = null;
+
+        try
+        {
+            in = httpEntity.getContent();
+
+            // According to javadoc of httpclient, getResponseBodyAsStream() 
can return null
+            // if the response has no body.
+            if (in != null)
+            {
+                try
+                {
+                    out = response.getOutputStream();
+                }
+                catch (IllegalStateException e)
+                {
+                    writer = response.getWriter();
+                }
+
+                if (out != null && (rewriter == null || parserAdaptor == null))
+                {
+                    IOUtils.copy(in, out);
+                    out.flush();
+                }
+                else
+                {
+                    boolean gzipEncoded = false;
+                    Header contentEncodingHeader = 
httpEntity.getContentEncoding();
+
+                    if (contentEncodingHeader != null)
+                    {
+                        gzipEncoded = StringUtils.equalsIgnoreCase("gzip", 
contentEncodingHeader.getValue());
+                    }
+
+                    String responseCharSet = 
EntityUtils.getContentCharSet(httpEntity);
+
+                    if (responseCharSet != null)
+                    {
+                        reader = new InputStreamReader(gzipEncoded ? new 
GZIPInputStream(in) : in, responseCharSet);
+
+                        if (writer == null && out != null)
+                        {
+                            writer = new OutputStreamWriter(gzipEncoded ? new 
GZIPOutputStream(out) : out, responseCharSet);
+                        }
+                    }
+                    else
+                    {
+                        reader = new InputStreamReader(gzipEncoded ? new 
GZIPInputStream(in) : in);
+
+                        if (writer == null && out != null)
+                        {
+                            writer = new OutputStreamWriter(gzipEncoded ? new 
GZIPOutputStream(out) : out);
+                        }
+                    }
+
+                    if (rewriter != null && parserAdaptor != null)
+                    {
+                        rewriter.setBaseUrl(rewriterContextPath + 
localPathInfo);
+    
+                        if (parserAdaptor instanceof 
ReverseProxyRewritingContextAware)
+                        {
+                            ReverseProxyRewritingContext rewritingContext =
+                                new 
DefaultReverseProxyRewritingContext(proxyPathMapper, proxyPathMapperProvider, 
rewriterContextPath);
+                            ((ReverseProxyRewritingContextAware) 
parserAdaptor).setReverseProxyRewritingContext(rewritingContext);
+                        }
+    
+                        rewriter.rewrite(parserAdaptor, reader, writer);
+                        writer.flush();
+                    }
+                    else
+                    {
+                        IOUtils.copy(reader, writer);
+                        writer.flush();
+                    }
+                }
+            }
+        }
+        finally
+        {
+            if (reader != null)
+            {
+                IOUtils.closeQuietly(reader);
+            }
+            if (in != null)
+            {
+                IOUtils.closeQuietly(in);
+            }
+        }
+    }
+
     private Rewriter createRewriter(RewriterController rewriterController, 
HttpReverseProxyPathMapper proxyPathMapper) throws Exception
     {
         Ruleset rewriterRuleset = 
proxyPathMapperProvider.getRewriterRuleset(proxyPathMapper);
@@ -873,5 +983,46 @@ public class RewritableHttpReverseProxyS
 
         return defaultReverseProxyRequestContextProvider;
     }
+
+    private boolean isDispatched(final HttpServletRequest request)
+    {
+        return request.getAttribute("javax.servlet.include.servlet_path") != 
null;
+    }
+
+    private String getInvokedContextPath(final HttpServletRequest request)
+    {
+        String path = (String) 
request.getAttribute("javax.servlet.include.context_path");
+
+        if (path != null)
+        {
+            return path;
+        }
+
+        return request.getContextPath();
+    }
+
+    private String getInvokedServletPath(final HttpServletRequest request)
+    {
+        String path = (String) 
request.getAttribute("javax.servlet.include.servlet_path");
+
+        if (path != null)
+        {
+            return path;
+        }
+
+        return request.getServletPath();
+    }
+
+    private String getInvokedPathInfo(final HttpServletRequest request)
+    {
+        String path = (String) 
request.getAttribute("javax.servlet.include.path_info");
+
+        if (path != null)
+        {
+            return path;
+        }
+
+        return request.getPathInfo();
+    }
 }
 


Reply via email to