portals-portalapps-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r1232749 - in /portals/applications/webcontent/trunk/webcontent-jar/src: main/java/org/apache/portals/applications/webcontent/proxy/ main/java/org/apache/portals/applications/webcontent/proxy/impl/ test/java/org/apache/portals/applications/...
Date Wed, 18 Jan 2012 05:22:03 GMT
Author: woonsan
Date: Wed Jan 18 05:22:02 2012
New Revision: 1232749

URL: http://svn.apache.org/viewvc?rev=1232749&view=rev
Log:
APA-47: Adding URICleaner to invoke HTTP Client Methods in order to avoid wrong URI invocations

Added:
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/URICleaner.java
  (with props)
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultURICleanerImpl.java
  (with props)
    portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestURICleaner.java
  (with props)
Modified:
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java

Added: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/URICleaner.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/URICleaner.java?rev=1232749&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/URICleaner.java
(added)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/URICleaner.java
Wed Jan 18 05:22:02 2012
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * URICleaner
+ * <P>
+ * Components implementing this interface can be used to clean the remote URI
+ * before invoking HTTP Methods by HttpReverseProxyService. 
+ * </P>
+ * 
+ * @version $Id$
+ */
+public interface URICleaner
+{
+    /**
+     * Inspects the specified uri and returns a cleaned uri.
+     * For example, this should replace disallowed characters such as space by a valid one.
+     * @param uri
+     * @return
+     */
+    public String clean(String uri);
+}

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

Modified: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java?rev=1232749&r1=1232748&r2=1232749&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
(original)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
Wed Jan 18 05:22:02 2012
@@ -68,6 +68,7 @@ import org.apache.portals.applications.w
 import org.apache.portals.applications.webcontent.proxy.HttpReverseProxyPathMapper;
 import org.apache.portals.applications.webcontent.proxy.HttpReverseProxyService;
 import org.apache.portals.applications.webcontent.proxy.ReverseProxyRequestContextProvider;
+import org.apache.portals.applications.webcontent.proxy.URICleaner;
 import org.apache.portals.applications.webcontent.rewriter.MappingRewriterController;
 import org.apache.portals.applications.webcontent.rewriter.RewriterController;
 import org.apache.portals.applications.webcontent.rewriter.rules.Ruleset;
@@ -318,11 +319,29 @@ public class DefaultHttpReverseProxyServ
         SchemeRegistry schemeRegistry = new SchemeRegistry();
         schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(),
80));
         schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(),
443));
-        
+
+        URICleaner defaultURICleaner = null;
+        String defaultUriCleanerClassName = configuration.getString("proxy.http.client.default.uri.cleaner",
DefaultURICleanerImpl.class.getName());
+
+        try
+        {
+            defaultURICleaner = (URICleaner) Thread.currentThread().getContextClassLoader().loadClass(defaultUriCleanerClassName).newInstance();
+            Configuration uriCleanerParamsConf = configuration.subset("proxy.http.client.default.uri.cleaner.param");
+            if (!uriCleanerParamsConf.isEmpty())
+            {
+                setBeanPropertiesByConfiguration(defaultURICleaner, uriCleanerParamsConf);
+            }
+        }
+        catch (Exception e)
+        {
+            throw new ServletException("Failure in instantiating the default uri cleaner:
" + defaultUriCleanerClassName, e);
+        }
+
         RewritableHttpReverseProxyServiceImpl tempProxyService = new RewritableHttpReverseProxyServiceImpl(proxyPathMapperProvider,
defaultReverseProxyRequestContextProvider);
         tempProxyService.setSchemeRegistry(schemeRegistry);
         ProxyHttpRoutePlanner httpRoutePlanner = new ProxyHttpRoutePlanner(schemeRegistry);
         tempProxyService.setHttpRoutePlanner(httpRoutePlanner);
+        tempProxyService.setDefaultURICleaner(defaultURICleaner);
         
         Configuration defaultProxiesConf = configuration.subset("proxy.http.client.default.proxy");
         

Added: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultURICleanerImpl.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultURICleanerImpl.java?rev=1232749&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultURICleanerImpl.java
(added)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultURICleanerImpl.java
Wed Jan 18 05:22:02 2012
@@ -0,0 +1,79 @@
+/*
+ * 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.util.ArrayList;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.portals.applications.webcontent.proxy.URICleaner;
+
+/**
+ * DefaultURICleanerImpl
+ * 
+ * @version $Id$
+ */
+public class DefaultURICleanerImpl implements URICleaner
+{
+    private String [] searchList = { " " };
+    private String [] replacementList = { "%20" };
+    
+    public DefaultURICleanerImpl() 
+    {
+    }
+    
+    public void setSearchList(String[] searchList)
+    {
+        ArrayList<String> searchArrayList = new ArrayList<String>();
+        
+        // Always replace spaces
+        if (!ArrayUtils.contains(searchList, " ")) {
+            searchArrayList.add(" ");
+        }
+        
+        if (searchList != null) {
+            for (String search : searchList) {
+                searchArrayList.add(search);
+            }
+        }
+        
+        this.searchList = searchArrayList.toArray(new String[searchArrayList.size()]);
+    }
+
+    public void setReplacementList(String[] replacementList)
+    {
+        ArrayList<String> replacementArrayList = new ArrayList<String>();
+        
+        // Always replace spaces
+        if (!ArrayUtils.contains(replacementList, "%20")) {
+            replacementArrayList.add("%20");
+        }
+        
+        if (replacementList != null) {
+            for (String replace : replacementList) {
+                replacementArrayList.add(replace);
+            }
+        }
+        
+        this.replacementList = replacementArrayList.toArray(new String[replacementArrayList.size()]);
+    }
+
+    public String clean(String uri)
+    {
+        return StringUtils.replaceEach(uri, searchList, replacementList);
+    }
+}

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

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=1232749&r1=1232748&r2=1232749&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 Jan 18 05:22:02 2012
@@ -85,6 +85,7 @@ import org.apache.portals.applications.w
 import org.apache.portals.applications.webcontent.proxy.ReverseProxyRewritingContextAware;
 import org.apache.portals.applications.webcontent.proxy.SSOSiteCredentials;
 import org.apache.portals.applications.webcontent.proxy.SSOSiteCredentialsProvider;
+import org.apache.portals.applications.webcontent.proxy.URICleaner;
 import org.apache.portals.applications.webcontent.rewriter.ParserAdaptor;
 import org.apache.portals.applications.webcontent.rewriter.Rewriter;
 import org.apache.portals.applications.webcontent.rewriter.RewriterController;
@@ -147,6 +148,11 @@ public class RewritableHttpReverseProxyS
      */
     private HttpRoutePlanner httpRoutePlanner;
     
+    /**
+     * URI Cleaner used to clean remote URIs before invoking HTTP Methods.
+     */
+    private URICleaner defaultURICleaner;
+    
     public RewritableHttpReverseProxyServiceImpl(HttpReverseProxyPathMapperProvider proxyPathMapperProvider,
ReverseProxyRequestContextProvider defaultReverseProxyRequestContextProvider)
     {
         this.proxyPathMapperProvider = proxyPathMapperProvider;
@@ -187,6 +193,11 @@ public class RewritableHttpReverseProxyS
         this.httpRoutePlanner = httpRoutePlanner;
     }
     
+    public void setDefaultURICleaner(URICleaner defaultURICleaner)
+    {
+        this.defaultURICleaner = defaultURICleaner;
+    }
+    
     public void initialize()
     {
         if (clientParams == null)
@@ -322,6 +333,13 @@ public class RewritableHttpReverseProxyS
         
         String method = request.getMethod();
         
+        // Before invoking HTTP Methods, let's clean the remote target URI
+        // in order to avoid invalid URI exception (e.g. space in the URI)
+        if (defaultURICleaner != null) 
+        {
+            proxyTargetURL = defaultURICleaner.clean(proxyTargetURL);
+        }
+        
         if (HttpGet.METHOD_NAME.equals(method))
         {
             httpRequest = new HttpGet(proxyTargetURL);

Added: portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestURICleaner.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestURICleaner.java?rev=1232749&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestURICleaner.java
(added)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestURICleaner.java
Wed Jan 18 05:22:02 2012
@@ -0,0 +1,44 @@
+/*
+ * 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;
+
+import junit.framework.TestCase;
+
+import org.apache.portals.applications.webcontent.proxy.impl.DefaultURICleanerImpl;
+
+/**
+ * @version $Id$
+ */
+public class TestURICleaner extends TestCase
+{
+    public void testDefaultURICleaner() throws Exception
+    {
+        String wrongUriWithSpace = "http://SERVER/0/F8C57293124EDBF9C125783200376835/$FILE/EXXO-ADDM-2011-000020-01
(p2).doc";
+        
+        DefaultURICleanerImpl uriCleaner = new DefaultURICleanerImpl();
+        uriCleaner.setSearchList(new String [] { " " });
+        uriCleaner.setReplacementList(new String [] { "%20" });
+        String cleanedUri = uriCleaner.clean(wrongUriWithSpace);
+        assertEquals("http://SERVER/0/F8C57293124EDBF9C125783200376835/$FILE/EXXO-ADDM-2011-000020-01%20(p2).doc",
cleanedUri);
+
+        wrongUriWithSpace = "http://SERVER/0/F8C57293124EDBF9C125783200376835/$FILE/EXXO-ADDM-2011-000020-01
(p2)^^^.doc";
+        uriCleaner.setSearchList(new String [] { " ", "^" });
+        uriCleaner.setReplacementList(new String [] { "%20", "%5E" });
+        cleanedUri = uriCleaner.clean(wrongUriWithSpace);
+        assertEquals("http://SERVER/0/F8C57293124EDBF9C125783200376835/$FILE/EXXO-ADDM-2011-000020-01%20(p2)%5E%5E%5E.doc",
cleanedUri);
+    }   
+}

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



Mime
View raw message