portals-portalapps-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r820217 - in /portals/applications/webcontent/trunk: webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/rewriter/ webcontent-war/src/ma...
Date Wed, 30 Sep 2009 08:56:28 GMT
Author: woonsan
Date: Wed Sep 30 08:56:28 2009
New Revision: 820217

URL: http://svn.apache.org/viewvc?rev=820217&view=rev
Log:
APA-17: Generalize more on rewriter configurations.

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/rewriter/MappingRewriterController.java
    portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties

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=820217&r1=820216&r2=820217&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 Sep 30 08:56:28 2009
@@ -150,27 +150,23 @@
                 
                 proxyPathMappers.add(new DefaultHttpProxyPathMapperImpl(localBasePath, remoteBaseURL));
                 
-                String [] rewriterClassNames = pathPassConf.getStringArray("rewriters");
-                String [] rewriterRuleConfs = pathPassConf.getStringArray("rules");
-                String [] adaptorClassNames = pathPassConf.getStringArray("adaptors");
+                Configuration rewritersConf = pathPassConf.subset("rewriter");
                 
-                int rewriterRuleConfsLength = ArrayUtils.getLength(rewriterRuleConfs);
-                
-                if (rewriterRuleConfsLength > 0)
+                if (!rewritersConf.isEmpty())
                 {
-                    RewriterController rewriterController = createRewriterController(rewriterRuleConfs[0],
rewriterClassNames, adaptorClassNames);
+                    RewriterController rewriterController = createRewriterController(rewritersConf);
                     
                     if (rewriterController != null)
                     {
                         rewriterControllerPairs.add(new DefaultKeyValue(localBasePath, rewriterController));
-                        Ruleset rewriterRuleset = null;
                         
-                        if (rewriterRuleConfsLength > 1)
+                        String rules = rewritersConf.getString("rules");
+                        Ruleset ruleset = loadRewriterRuleset(rewriterController, rules);
+                        
+                        if (ruleset != null)
                         {
-                            rewriterRuleset = loadRewriterRuleset(rewriterController, rewriterRuleConfs[1]);
+                            rewriterRulesetPairs.add(new DefaultKeyValue(localBasePath, ruleset));
                         }
-                        
-                        rewriterRulesetPairs.add(new DefaultKeyValue(localBasePath, rewriterRuleset));
                     }
                 }
             }
@@ -301,9 +297,73 @@
         doGet(request, response);
     }
     
-    private RewriterController createRewriterController(String rulesMappingResourcePath,
String [] rewriterClassNames, String [] adaptorClassNames) throws Exception
+    private RewriterController createRewriterController(final Configuration rewriterConf)
     {
-        return new MappingRewriterController(getServletContext().getRealPath(rulesMappingResourcePath),
buildClassList(rewriterClassNames), buildClassList(adaptorClassNames));
+        String ruleMappingsFilePath = null;
+        Class basicRewriterClass = null;
+        Class ruleBasedRewriterClass = null;
+        Map<String, Class> adaptorMimeTypeClassMap = new HashMap<String, Class>();
+        
+        try
+        {
+            String ruleMappings = rewriterConf.getString("ruleMappings");
+            
+            if (!StringUtils.isBlank(ruleMappings))
+            {
+                ruleMappingsFilePath = getServletContext().getRealPath(ruleMappings);
+            }
+            
+            String basicRewriter = rewriterConf.getString("basic");
+            
+            if (!StringUtils.isBlank(basicRewriter))
+            {
+                basicRewriterClass = Thread.currentThread().getContextClassLoader().loadClass(basicRewriter);
+            }
+            
+            String ruleBasedRewriter = rewriterConf.getString("rulebased");
+            
+            if (!StringUtils.isBlank(ruleBasedRewriter))
+            {
+                ruleBasedRewriterClass = Thread.currentThread().getContextClassLoader().loadClass(ruleBasedRewriter);
+            }
+            
+            Configuration parserAdaptorsConf = rewriterConf.subset("parserAdaptor");
+            
+            String [] parserAdaptorNames = parserAdaptorsConf.getStringArray("");
+            
+            if (!ArrayUtils.isEmpty(parserAdaptorNames))
+            {
+                for (String parserAdaptorName : parserAdaptorNames)
+                {
+                    Configuration parserAdaptorConf = parserAdaptorsConf.subset(parserAdaptorName);
+                    String mimeType = parserAdaptorConf.getString("mimeType");
+                    String parserAdaptor = parserAdaptorConf.getString("");
+                    
+                    if (!StringUtils.isBlank(parserAdaptor))
+                    {
+                        Class parserAdaptorClass = Thread.currentThread().getContextClassLoader().loadClass(parserAdaptor);
+                        adaptorMimeTypeClassMap.put(mimeType, parserAdaptorClass);
+                    }
+                }
+            }
+            
+            return new MappingRewriterController(ruleMappingsFilePath, 
+                                                 basicRewriterClass, ruleBasedRewriterClass,

+                                                 adaptorMimeTypeClassMap);
+        }
+        catch (Exception e)
+        {
+            if (log.isDebugEnabled())
+            {
+                log.error("Failed to initialize rewriters.", e);
+            }
+            else
+            {
+                log.error("Failed to initialize rewriters. {}", e.toString());
+            }
+        }
+        
+        return null;
     }
     
     private HttpRoute buildHttpRoute(Configuration routeConf) throws Exception
@@ -562,7 +622,7 @@
         
         try
         {
-            if (!StringUtils.isBlank(rulesConfResourcePath))
+            if (rewriterController != null && !StringUtils.isBlank(rulesConfResourcePath))
             {
                 is = getServletContext().getResourceAsStream(rulesConfResourcePath);
                 bis = new BufferedInputStream(is);

Modified: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/rewriter/MappingRewriterController.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/rewriter/MappingRewriterController.java?rev=820217&r1=820216&r2=820217&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/rewriter/MappingRewriterController.java
(original)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/rewriter/MappingRewriterController.java
Wed Sep 30 08:56:28 2009
@@ -17,7 +17,6 @@
 package org.apache.portals.applications.webcontent.rewriter;
 
 import java.io.File;
-import java.io.FileReader;
 import java.io.InputStream;
 import java.io.Reader;
 import java.util.ArrayList;
@@ -71,37 +70,85 @@
     protected Class rulesetRewriterClass = RulesetRewriterImpl.class;
 
     /** Adaptors */
-    protected Class adaptorHtmlClass = SwingParserAdaptor.class;
-    protected Class adaptorXmlClass = SaxParserAdaptor.class;
+    protected Map<String, Class> parserAdaptorMimeTypeClassMap;
 
     public MappingRewriterController( String mappingFile ) throws RewriterException
     {
         this.mappingFile = mappingFile;
-        loadMapping();
+        
+        this.parserAdaptorMimeTypeClassMap = new HashMap<String, Class>();
+        this.parserAdaptorMimeTypeClassMap.put("text/html", SwingParserAdaptor.class);
+        this.parserAdaptorMimeTypeClassMap.put("text/xml", SaxParserAdaptor.class);
+        
+        if (this.mappingFile != null)
+        {
+            loadMapping();
+        }
     }
 
+    public MappingRewriterController( String mappingFile, Class basicRewriterClass, Class
ruleBasedRewriterClass, Map<String, Class> adaptorMimeTypeClassMap )
+            throws RewriterException
+    {
+        this.mappingFile = mappingFile;
+        
+        this.parserAdaptorMimeTypeClassMap = new HashMap<String, Class>();
+        this.parserAdaptorMimeTypeClassMap.put("text/html", SwingParserAdaptor.class);
+        this.parserAdaptorMimeTypeClassMap.put("text/xml", SaxParserAdaptor.class);
+        
+        if (basicRewriterClass != null)
+        {
+            this.basicRewriterClass = basicRewriterClass;
+        }
+        
+        if (ruleBasedRewriterClass != null)
+        {
+            this.rulesetRewriterClass = ruleBasedRewriterClass;
+        }
+        
+        if (adaptorMimeTypeClassMap != null)
+        {
+            this.parserAdaptorMimeTypeClassMap.putAll(adaptorMimeTypeClassMap);
+        }
+        
+        if (this.mappingFile != null)
+        {
+            loadMapping();
+        }
+    }
+    
     public MappingRewriterController( String mappingFile, List rewriterClasses, List adaptorClasses
)
             throws RewriterException
     {
         this.mappingFile = mappingFile;
+        
+        this.parserAdaptorMimeTypeClassMap = new HashMap<String, Class>();
+        this.parserAdaptorMimeTypeClassMap.put("text/html", SwingParserAdaptor.class);
+        this.parserAdaptorMimeTypeClassMap.put("text/xml", SaxParserAdaptor.class);
+        
         if (rewriterClasses.size() > 0)
         {
             this.basicRewriterClass = (Class) rewriterClasses.get(0);
+            
             if (rewriterClasses.size() > 1)
             {
                 this.rulesetRewriterClass = (Class) rewriterClasses.get(1);
             }
         }
+        
         if (adaptorClasses.size() > 0)
         {
-            this.adaptorHtmlClass = (Class) adaptorClasses.get(0);
+            this.parserAdaptorMimeTypeClassMap.put("text/html", (Class) adaptorClasses.get(0));
+            
             if (adaptorClasses.size() > 1)
             {
-                this.adaptorXmlClass = (Class) adaptorClasses.get(1);
+                this.parserAdaptorMimeTypeClassMap.put("text/xmll", (Class) adaptorClasses.get(1));
             }
         }
 
-        loadMapping();
+        if (this.mappingFile != null)
+        {
+            loadMapping();
+        }
     }
     
     public MappingRewriterController( String mappingFile, String basicRewriterClassName,
String rulesetRewriterClassName, 
@@ -156,22 +203,18 @@
     {
         try
         {
-            if (mimeType.equals("text/html"))
-            {
-                return (ParserAdaptor) adaptorHtmlClass.newInstance();
-            }
-            else if (mimeType.equals("text/xml"))
-            {
-                return (ParserAdaptor) adaptorXmlClass.newInstance();
-            }
-            else
+            Class parserAdaptorClass = parserAdaptorMimeTypeClassMap.get(mimeType);
+            
+            if (parserAdaptorClass != null)
             {
+                return (ParserAdaptor) parserAdaptorClass.newInstance();
             }
         }
         catch (Exception e)
         {
             log.error("Error creating rewriter class", e);
         }
+        
         return null;
     }
 
@@ -181,42 +224,31 @@
      */
     protected void loadMapping() throws RewriterException
     {
+        if (this.mappingFile == null)
+        {
+            throw new RewriterException("The mapping file is not set.");
+        }
+        
+        File file = new File(this.mappingFile);
+        
+        if (!file.isFile())
+        {
+            throw new RewriterException("The mapping file is not available: " + this.mappingFile);
+        }
+        
         try
         {
-            Reader reader = getReader(this.mappingFile);
-            
             this.mapper = new Mapping();
-            InputSource is = new InputSource(reader);
-            is.setSystemId(this.mappingFile);
-            this.mapper.loadMapping(is);
+            this.mapper.loadMapping(file.toURL());
         }
         catch (Exception e)
         {
-            e.printStackTrace();
             String msg = "RewriterService: Error in castor mapping creation";
             log.error(msg, e);
             throw new RewriterException(msg, e);
         }
     }
-
-    protected Reader getReader(String resource)
-    throws RewriterException
-    {
-        File file = new File(resource);
-        if (file.exists() && file.isFile() && file.canRead())
-        {
-            try
-            {
-                return new FileReader(file);
-            }
-            catch (Exception e)
-            {
-                throw new RewriterException("could not open rewriter file " + resource, e);
-            }
-        }
-        throw new RewriterException("could not access rewriter file " + resource);
-    }
-        
+    
     public Ruleset lookupRuleset( String id )
     {
         return (Ruleset) rulesets.get(id);

Modified: portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties?rev=820217&r1=820216&r2=820217&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
(original)
+++ portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
Wed Sep 30 08:56:28 2009
@@ -20,15 +20,6 @@
 #
 # ------------------------------------------------------------------------
 
-# Constant properties
-# ... Constant properties can be used in other property values by wrapping '${}'.
-defaults.htmlRewriter = org.apache.portals.applications.webcontent.rewriter.WebContentRewriter
-defaults.xmlRewriter = org.apache.portals.applications.webcontent.rewriter.WebContentRewriter
-defaults.htmlAdaptor = org.apache.portals.applications.webcontent.rewriter.html.neko.NekoParserAdaptor
-defaults.xmlAdaptor = org.apache.portals.applications.webcontent.rewriter.xml.SaxParserAdaptor
-defaults.ruleMappings = /WEB-INF/conf/rewriter-rules-mapping.xml
-defaults.rewriterRules = /WEB-INF/conf/default-rewriter-rules.xml
-
 # Proxy Host Configurations
 # ... Set the following when you want to force HOST header value forcefully.
 #proxy.server.hostname = www.localhost.com
@@ -96,6 +87,13 @@
 # ... 'somewhere' is just an example to show the full configurable items...
 proxy.reverse.pass.somewhere.local = /somewhere/
 proxy.reverse.pass.somewhere.remote = http://somewhere.localhost.com/
-proxy.reverse.pass.somewhere.rewriters = ${defaults.htmlRewriter}, ${defaults.xmlRewriter}

-proxy.reverse.pass.somewhere.adaptors = ${defaults.htmlAdaptor}, ${defaults.xmlAdaptor}
-proxy.reverse.pass.somewhere.rules = ${defaults.ruleMappings}, ${defaults.rewriterRules}
+# ... rewriter configuration for this pass mapping on html and xml types.
+proxy.reverse.pass.somewhere.rewriter.basic = org.apache.portals.applications.webcontent.rewriter.WebContentRewriter
+proxy.reverse.pass.somewhere.rewriter.rulebased = org.apache.portals.applications.webcontent.rewriter.WebContentRewriter
+proxy.reverse.pass.somewhere.rewriter.parserAdaptor = html, xml
+proxy.reverse.pass.somewhere.rewriter.parserAdaptor.html = org.apache.portals.applications.webcontent.rewriter.html.neko.NekoParserAdaptor
+proxy.reverse.pass.somewhere.rewriter.parserAdaptor.html.mimeType = text/html
+proxy.reverse.pass.somewhere.rewriter.parserAdaptor.xml = org.apache.portals.applications.webcontent.rewriter.xml.SaxParserAdaptor
+proxy.reverse.pass.somewhere.rewriter.parserAdaptor.xml.mimeType = text/xml
+proxy.reverse.pass.somewhere.rewriter.ruleMappings = /WEB-INF/conf/rewriter-rules-mapping.xml
+proxy.reverse.pass.somewhere.rewriter.rules = /WEB-INF/conf/default-rewriter-rules.xml



Mime
View raw message