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 2009/09/30 13:24:51 UTC

svn commit: r820251 - /portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/

Author: woonsan
Date: Wed Sep 30 11:24:51 2009
New Revision: 820251

URL: http://svn.apache.org/viewvc?rev=820251&view=rev
Log:
JS2-1071: Adding source code header and example link rewriting adaptor

Added:
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java   (with props)
Modified:
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java
    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/DefaultSSOSiteCredentials.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java?rev=820251&r1=820250&r2=820251&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java (original)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java Wed Sep 30 11:24:51 2009
@@ -1,3 +1,19 @@
+/*
+ * 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.webcontent.proxy.impl;
 
 import org.apache.portals.applications.webcontent.proxy.SSOSiteCredentials;

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=820251&r1=820250&r2=820251&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 Wed Sep 30 11:24:51 2009
@@ -64,6 +64,7 @@
 import org.apache.portals.applications.webcontent.proxy.HttpReverseProxyService;
 import org.apache.portals.applications.webcontent.proxy.SSOSiteCredentials;
 import org.apache.portals.applications.webcontent.proxy.SSOSiteCredentialsProvider;
+import org.apache.portals.applications.webcontent.rewriter.ParserAdaptor;
 import org.apache.portals.applications.webcontent.rewriter.Rewriter;
 import org.apache.portals.applications.webcontent.rewriter.RewriterController;
 import org.apache.portals.applications.webcontent.rewriter.rules.Ruleset;
@@ -91,7 +92,7 @@
     private String hostHeaderValue;
     
     /**
-     * forced local base url. e.g., "http://localhost:8080/jetspeed/webcontent".
+     * forced local base url. e.g., "/webcontent/rproxy".
      */
     private String localBaseURL;
     
@@ -191,11 +192,6 @@
             }
         }
         
-        if (localBaseURL == null)
-        {
-            localBaseURL = request.getScheme() + "://" + hostHeaderValue + request.getContextPath() + request.getServletPath();
-        }
-        
         String proxyTargetURL = proxyPathMapper.getRemoteURL(pathInfo);
         
         if (proxyTargetURL == null)
@@ -475,22 +471,59 @@
                         }
                     }
                     
-                    String responseCharSet = EntityUtils.getContentCharSet(httpEntity);
+                    ParserAdaptor parserAdaptor = null;
                     
-                    if (responseCharSet != null)
+                    try
                     {
-                        reader = new InputStreamReader(in, responseCharSet);
-                        writer = new OutputStreamWriter(out, responseCharSet);
+                        parserAdaptor = rewriterController.createParserAdaptor(mimeType);
                     }
-                    else
+                    catch (Exception e)
                     {
-                        reader = new InputStreamReader(in);
-                        writer = new OutputStreamWriter(out);
+                        if (log.isDebugEnabled())
+                        {
+                            log.error("Failed to create parser adaptor.", e);
+                        }
+                        else
+                        {
+                            log.error("Failed to create parser adaptor. {}", e.toString());
+                        }
                     }
                     
-                    rewriter.setBaseUrl(localBaseURL + request.getPathInfo());
-                    rewriter.rewrite(rewriterController.createParserAdaptor(mimeType), reader, writer);
-                    writer.flush();
+                    if (parserAdaptor == null)
+                    {
+                        IOUtils.copy(in, out);
+                        out.flush();
+                    }
+                    else
+                    {
+                        String responseCharSet = EntityUtils.getContentCharSet(httpEntity);
+                        
+                        if (responseCharSet != null)
+                        {
+                            reader = new InputStreamReader(in, responseCharSet);
+                            writer = new OutputStreamWriter(out, responseCharSet);
+                        }
+                        else
+                        {
+                            reader = new InputStreamReader(in);
+                            writer = new OutputStreamWriter(out);
+                        }
+                        
+                        String contentBaseURL = null;
+                        
+                        if (localBaseURL != null)
+                        {
+                            contentBaseURL = localBaseURL + request.getPathInfo();
+                        }
+                        else
+                        {
+                            contentBaseURL = request.getContextPath() + request.getServletPath() + request.getPathInfo();
+                        }
+                        
+                        rewriter.setBaseUrl(contentBaseURL);
+                        rewriter.rewrite(parserAdaptor, reader, writer);
+                        writer.flush();
+                    }
                 }
             }
         }

Added: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java?rev=820251&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java (added)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java Wed Sep 30 11:24:51 2009
@@ -0,0 +1,87 @@
+/*
+ * 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.webcontent.proxy.impl;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.LineIterator;
+import org.apache.commons.lang.StringUtils;
+import org.apache.portals.applications.webcontent.rewriter.ParserAdaptor;
+import org.apache.portals.applications.webcontent.rewriter.Rewriter;
+import org.apache.portals.applications.webcontent.rewriter.RewriterException;
+
+/**
+ * A simple reverse proxy link rewriting parser adaptor implementation.
+ * <P>
+ * You can refer to this example to implement more sophisticated rewriting parser adaptor.
+ * </P>
+ * 
+ * @version $Id$
+ */
+public class SimpleLinkRewritingParserAaptor implements ParserAdaptor
+{
+    
+    private Pattern linkBasePathPattern = Pattern.compile("\\s(href|src|action)\\s*=\\s*['\"]((\\/)[^'\"]+)['\"]", Pattern.CASE_INSENSITIVE);
+
+    public SimpleLinkRewritingParserAaptor()
+    {
+    }
+
+    public void rewrite(Rewriter rewriter, Reader reader, Writer writer) throws RewriterException
+    {
+        String baseURL = rewriter.getBaseUrl();
+        int offset = baseURL.lastIndexOf('/');
+        
+        if (StringUtils.isBlank(baseURL) || offset != -1)
+        {
+            try
+            {
+                IOUtils.copy(reader, writer);
+            }
+            catch (IOException e)
+            {
+                throw new RewriterException(e);
+            }
+        }
+        
+        baseURL = baseURL.substring(0, offset + 1);
+        
+        PrintWriter out = new PrintWriter(new BufferedWriter(writer));
+        
+        for (LineIterator lineIt = IOUtils.lineIterator(reader); lineIt.hasNext(); )
+        {
+            String line = lineIt.nextLine();
+            Matcher linkBasePathMatcher = linkBasePathPattern.matcher(line);
+            out.println(linkBasePathMatcher.replaceAll(" $1=\"" + baseURL + "$2\""));
+        }
+        
+        out.flush();
+    }
+
+    public void parse(Rewriter rewriter, Reader reader) throws RewriterException
+    {
+        throw new UnsupportedOperationException();
+    }
+    
+}

Propchange: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/SimpleLinkRewritingParserAaptor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain