Author: woonsan
Date: Tue Oct  6 16:57:42 2009
New Revision: 822346

URL: http://svn.apache.org/viewvc?rev=822346&view=rev
Log:
APA-17: Allows file: or classpath: prefixed file configurations

Added:
    
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
   (with props)
Modified:
    
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java

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=822346&r1=822345&r2=822346&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
 Tue Oct  6 16:57:42 2009
@@ -66,6 +66,7 @@
 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;
+import org.apache.portals.applications.webcontent.util.WebResourceUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -394,7 +395,15 @@
             
             if (!StringUtils.isBlank(ruleMappings))
             {
-                ruleMappingsFilePath = 
getServletContext().getRealPath(ruleMappings);
+                File ruleMappingsFile = 
+                    WebResourceUtils.getResourceAsFile(ruleMappings, 
+                                                       
Thread.currentThread().getContextClassLoader(), 
+                                                       getServletContext());
+                
+                if (ruleMappingsFile != null && ruleMappingsFile.isFile())
+                {
+                    ruleMappingsFilePath = ruleMappingsFile.getCanonicalPath();
+                }
             }
             
             Configuration basicRewriterConf = rewriterConf.subset("basic");
@@ -467,9 +476,10 @@
                 }
             }
             
-            MappingRewriterController rewriterController = new 
MappingRewriterController(ruleMappingsFilePath, 
-                                                                               
          basicRewriterClass, ruleBasedRewriterClass, 
-                                                                               
          adaptorMimeTypeClassMap);
+            MappingRewriterController rewriterController = 
+                new MappingRewriterController(ruleMappingsFilePath, 
+                                              basicRewriterClass, 
ruleBasedRewriterClass, 
+                                              adaptorMimeTypeClassMap);
             
             rewriterController.setBasicRewriterProps(basicRewriterProps);
             rewriterController.setRulesetRewriterProps(basicRewriterProps);
@@ -664,22 +674,15 @@
     
     private void loadConfiguration() throws ServletException
     {
-        String configResourcePath = 
getServletConfig().getInitParameter("reverseproxy.configuration");
+        String configResourcePath = 
StringUtils.trim(getServletConfig().getInitParameter("reverseproxy.configuration"));
         
         if (configResourcePath == null)
         {
             configResourcePath = "/WEB-INF/conf/reverseproxy.properties";
         }
         
-        File configResourceFile = null;
-        
-        try
-        {
-            configResourceFile = new 
File(getServletContext().getRealPath(configResourcePath));
-        }
-        catch (Exception ignore)
-        {
-        }
+        File configResourceFile = 
+            WebResourceUtils.getResourceAsFile(configResourcePath, 
Thread.currentThread().getContextClassLoader(), getServletContext());
         
         InputStream configInput = null;
         
@@ -687,7 +690,7 @@
         {
             configuration = new PropertiesConfiguration();
             
-            if (configResourceFile != null && configResourceFile.isFile())
+            if (configResourceFile != null)
             {
                 configuration.load(configResourceFile);
                 
@@ -698,8 +701,8 @@
             }
             else
             {
-                configInput = 
getServletContext().getResourceAsStream(configResourcePath);
-                ((PropertiesConfiguration) configuration).load(configInput);
+                configInput = 
WebResourceUtils.getResourceAsStream(configResourcePath.substring(10));
+                configuration.load(configInput);
             }
         }
         catch (Exception e)
@@ -744,7 +747,7 @@
         }
     }
     
-    private Ruleset loadRewriterRuleset(RewriterController rewriterController, 
String rulesConfResourcePath)
+    private Ruleset loadRewriterRuleset(RewriterController rewriterController, 
String rulesConfResourcePath) throws IOException
     {
         Ruleset ruleset = null;
         
@@ -755,7 +758,7 @@
         {
             if (rewriterController != null && 
!StringUtils.isBlank(rulesConfResourcePath))
             {
-                is = 
getServletContext().getResourceAsStream(rulesConfResourcePath);
+                is = 
WebResourceUtils.getResourceAsStream(rulesConfResourcePath, 
Thread.currentThread().getContextClassLoader(), getServletContext());
                 bis = new BufferedInputStream(is);
                 ruleset = rewriterController.loadRuleset(bis);
             }

Added: 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
URL: 
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java?rev=822346&view=auto
==============================================================================
--- 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
 (added)
+++ 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
 Tue Oct  6 16:57:42 2009
@@ -0,0 +1,135 @@
+/*
+ * 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.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+
+import javax.servlet.ServletContext;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * WebResourceUtils
+ * 
+ * @version $Id$
+ */
+public class WebResourceUtils
+{
+    
+    private WebResourceUtils()
+    {
+    }
+    
+    public static File getResourceAsFile(String resourcePath)
+    {
+        return getResourceAsFile(resourcePath, null, null);
+    }
+    
+    public static File getResourceAsFile(String resourcePath, ClassLoader 
classloader)
+    {
+        return getResourceAsFile(resourcePath, classloader, null);
+    }
+    
+    public static File getResourceAsFile(String resourcePath, ServletContext 
servletContext)
+    {
+        return getResourceAsFile(resourcePath, null, servletContext);
+    }
+    
+    public static File getResourceAsFile(String resourcePath, ClassLoader 
classloader, ServletContext servletContext)
+    {
+        if (resourcePath == null)
+        {
+            return null;
+        }
+        
+        try
+        {
+            if (StringUtils.startsWith(resourcePath, "file:"))
+            {
+                return new File(URI.create(resourcePath));
+            }
+            else if (classloader != null && 
StringUtils.startsWith(resourcePath, "classpath:"))
+            {
+                URL resourceURL = 
classloader.getResource(resourcePath.substring(10));
+                
+                if (resourceURL != null && 
"file".equals(resourceURL.getProtocol()))
+                {
+                    return new File(resourceURL.toURI());
+                }
+            }
+            else if (servletContext != null)
+            {
+                return new File(servletContext.getRealPath(resourcePath));
+            }
+            else
+            {
+                return new File(resourcePath);
+            }
+        }
+        catch (Exception ignore)
+        {
+        }
+        
+        return null;
+    }
+    
+    public static InputStream getResourceAsStream(String resourcePath) throws 
IOException
+    {
+        return getResourceAsStream(resourcePath, null, null);
+    }
+    
+    public static InputStream getResourceAsStream(String resourcePath, 
ClassLoader classloader) throws IOException
+    {
+        return getResourceAsStream(resourcePath, classloader, null);
+    }
+    
+    public static InputStream getResourceAsStream(String resourcePath, 
ServletContext servletContext) throws IOException
+    {
+        return getResourceAsStream(resourcePath, null, servletContext);
+    }
+    
+    public static InputStream getResourceAsStream(String resourcePath, 
ClassLoader classloader, ServletContext servletContext) throws IOException
+    {
+        if (resourcePath == null)
+        {
+            return null;
+        }
+        
+        if (StringUtils.startsWith(resourcePath, "file:"))
+        {
+            return new FileInputStream(new File(URI.create(resourcePath)));
+        }
+        else if (classloader != null && StringUtils.startsWith(resourcePath, 
"classpath:"))
+        {
+            return classloader.getResourceAsStream(resourcePath.substring(10));
+        }
+        else if (servletContext != null)
+        {
+            return servletContext.getResourceAsStream(resourcePath);
+        }
+        else
+        {
+            return new FileInputStream(resourcePath);
+        }
+    }
+    
+}

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

Propchange: 
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id

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


Reply via email to