You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/06/28 21:32:28 UTC

svn commit: r417854 - in /incubator/roller/branches/roller_3.0: metadata/xdoclet/ src/org/apache/roller/config/ src/org/apache/roller/pojos/ src/org/apache/roller/ui/core/ src/org/apache/roller/ui/core/filters/

Author: agilliland
Date: Wed Jun 28 12:32:28 2006
New Revision: 417854

URL: http://svn.apache.org/viewvc?rev=417854&view=rev
Log:
fixing up situation with absolute url.

- added a new InitFilter which inspects its first incoming request and sets the application absolute and relative urls in our config.

- removed context.absPath from being set in RollerConfig and moved it to RollerRuntimeConfig so that we can have a single place for extracting it.

- cleaned up getAbsoluteContextUrl() methods in RollerContext to just use RuntimeConfig absolute url property.

- cleaned up WeblogEntryData and WebsiteData to just use RunctimeConfig absolute url property.


Added:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/filters/InitFilter.java
Modified:
    incubator/roller/branches/roller_3.0/metadata/xdoclet/filter-mappings.xml
    incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerConfig.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerRuntimeConfig.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WeblogEntryData.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WebsiteData.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/RollerContext.java

Modified: incubator/roller/branches/roller_3.0/metadata/xdoclet/filter-mappings.xml
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/metadata/xdoclet/filter-mappings.xml?rev=417854&r1=417853&r2=417854&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/metadata/xdoclet/filter-mappings.xml (original)
+++ incubator/roller/branches/roller_3.0/metadata/xdoclet/filter-mappings.xml Wed Jun 28 12:32:28 2006
@@ -43,6 +43,11 @@
 </filter-mapping>
 
 <filter-mapping>
+    <filter-name>InitFilter</filter-name>
+    <url-pattern>/*</url-pattern>
+</filter-mapping>
+
+<filter-mapping>
     <filter-name>RequestMappingFilter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerConfig.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerConfig.java?rev=417854&r1=417853&r2=417854&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerConfig.java (original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerConfig.java Wed Jun 28 12:32:28 2006
@@ -1,38 +1,35 @@
 /*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-*  contributor license agreements.  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.  For additional information regarding
-* copyright in this work, please see the NOTICE file in the top level
-* directory of this distribution.
-*/
-/* RollerConfig.java */
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
 
 package org.apache.roller.config;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.roller.util.PropertyExpander;
-
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.util.Enumeration;
 import java.util.Properties;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.util.PropertyExpander;
 
 
 /**
  * This is the single entry point for accessing configuration properties in Roller.
- * @author Allen Gilliland
  */
 public class RollerConfig {
     
@@ -43,9 +40,9 @@
 
     private static Properties mConfig;
 
-    private static Log mLogger =
-            LogFactory.getFactory().getInstance(RollerConfig.class);
-
+    private static Log log = LogFactory.getLog(RollerConfig.class);
+    
+    
     /*
      * Static block run once at class loading
      *
@@ -61,15 +58,15 @@
             // first, lets load our default properties
             InputStream is = config_class.getResourceAsStream(default_config);
             mConfig.load(is);
-            mLogger.info("successfully loaded default properties.");
+            log.info("successfully loaded default properties.");
 
             // now, see if we can find our custom config
             is = config_class.getResourceAsStream(custom_config);
             if(is != null) {
                 mConfig.load(is);
-                mLogger.info("successfully loaded custom properties file from classpath");
+                log.info("successfully loaded custom properties file from classpath");
             } else {
-                mLogger.info("no custom properties file found in classpath");
+                log.info("no custom properties file found in classpath");
             }
 
             // finally, check for an external config file
@@ -81,15 +78,15 @@
                 if(custom_config_file != null && custom_config_file.exists()) {
                     is = new FileInputStream(custom_config_file);
                     mConfig.load(is);
-                    mLogger.info("successfully loaded custom properties from "+
+                    log.info("successfully loaded custom properties from "+
                             custom_config_file.getAbsolutePath());
                 } else {
-                    mLogger.warn("failed to load custom properties from "+
+                    log.warn("failed to load custom properties from "+
                             custom_config_file.getAbsolutePath());
                 }
 
             } else {
-                mLogger.info("no custom properties file specified via jvm option");
+                log.info("no custom properties file specified via jvm option");
             }
 
             // Now expand system properties for properties in the config.expandedProperties list,
@@ -103,8 +100,8 @@
                     if (initialValue != null) {
                         String expandedValue = PropertyExpander.expandSystemProperties(initialValue);
                         mConfig.put(propName,expandedValue);
-                        if (mLogger.isDebugEnabled()) {
-                            mLogger.info("Expanded value of " + propName + " from '" +
+                        if (log.isDebugEnabled()) {
+                            log.info("Expanded value of " + propName + " from '" +
                                 initialValue + "' to '" + expandedValue + "'");
                         }
                     }
@@ -112,14 +109,14 @@
             }
 
             // some debugging for those that want it
-            if(mLogger.isDebugEnabled()) {
-                mLogger.debug("RollerConfig looks like this ...");
+            if(log.isDebugEnabled()) {
+                log.debug("RollerConfig looks like this ...");
 
                 String key = null;
                 Enumeration keys = mConfig.keys();
                 while(keys.hasMoreElements()) {
                     key = (String) keys.nextElement();
-                    mLogger.debug(key+"="+mConfig.getProperty(key));
+                    log.debug(key+"="+mConfig.getProperty(key));
                 }
             }
 
@@ -140,7 +137,7 @@
      * @return    String Value of property requested, null if not found
      */
     public static String getProperty(String key) {
-        mLogger.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+"]");
+        log.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+"]");
         return mConfig.getProperty(key);
     }
 
@@ -193,17 +190,6 @@
         return mConfig.keys();
     }
    
-    /**
-     * Set the "context.absPath" property at runtime.
-     * <p />
-     * Properties are meant to be read-only, but we make this exception because 
-     * Roller POJOs and backend classes need access to the 
-     * <p />
-     * This property is *not* persisted in any way.
-     */
-    public static void setAbsoluteContextPath(String path) {
-        mConfig.setProperty("context.absPath", path);
-    }
 
     /**
      * Set the "uploads.dir" property at runtime.

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerRuntimeConfig.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerRuntimeConfig.java?rev=417854&r1=417853&r2=417854&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerRuntimeConfig.java (original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/config/RollerRuntimeConfig.java Wed Jun 28 12:32:28 2006
@@ -21,6 +21,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringWriter;
+import java.util.Properties;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.config.runtime.RuntimeConfigDefs;
@@ -39,10 +40,13 @@
  */
 public class RollerRuntimeConfig {
     
+    private static Log log = LogFactory.getLog(RollerRuntimeConfig.class);
+    
     private static String runtime_config = "/rollerRuntimeConfigDefs.xml";
     private static RuntimeConfigDefs configDefs = null;
     
-    private static Log log = LogFactory.getLog(RollerRuntimeConfig.class);
+    // for properties that we are storing locally which aren't persisted
+    private static Properties localConfig = new Properties();
     
     
     // prevent instantiations
@@ -56,11 +60,21 @@
     public static String getProperty(String name) {
         
         String value = null;
-        try {
-            PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
-            value = pmgr.getProperty(name).getValue();
-        } catch(Exception e) {
-            log.warn("Trouble accessing property: "+name, e);
+        
+        // try local config first
+        value = localConfig.getProperty(name);
+        
+        // next try db
+        // i don't like this special case for the absoluteurl, but right now
+        // i can't seem to see a better way.  at some point we should come
+        // back to this and see if there is a cleaner way to handle this  -- AG
+        if(value == null || "site.absoluteurl".equals(name)) {
+            try {
+                PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
+                value = pmgr.getProperty(name).getValue();
+            } catch(Exception e) {
+                log.warn("Trouble accessing property: "+name, e);
+            }
         }
         
         log.debug("fetched property ["+name+"="+value+"]");
@@ -159,6 +173,34 @@
         }
         
         return "";
+    }
+    
+    
+    /**
+     * Set the "site.absoluteurl" property locally.
+     *
+     * Local properties are a kind of bastardized attempt to maintain some
+     * properties which can only be determined at runtime but which should
+     * not be persisted.
+     *
+     * This property is *not* persisted in any way.
+     */
+    public static void setAbsoluteContextPath(String path) {
+        localConfig.setProperty("site.absoluteurl", path);
+    }
+    
+    
+    /**
+     * Set the "site.relativeurl" property locally.
+     *
+     * Local properties are a kind of bastardized attempt to maintain some
+     * properties which can only be determined at runtime but which should
+     * not be persisted.
+     *
+     * This property is *not* persisted in any way.
+     */
+    public static void setRelativeContextPath(String path) {
+        localConfig.setProperty("site.relativeurl", path);
     }
     
 }

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WeblogEntryData.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WeblogEntryData.java?rev=417854&r1=417853&r2=417854&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WeblogEntryData.java (original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WeblogEntryData.java Wed Jun 28 12:32:28 2006
@@ -42,6 +42,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.RollerException;
 import org.apache.roller.config.RollerConfig;
+import org.apache.roller.config.RollerRuntimeConfig;
 import org.apache.roller.model.RollerFactory;
 import org.apache.roller.model.UserManager;
 import org.apache.roller.model.WeblogEntryPlugin;
@@ -711,8 +712,8 @@
      * @roller.wrapPojoMethod type="simple"
      */
     public String getPermalink() {
-        String absContextUrl = RollerConfig.getProperty("context.absPath");
-        return absContextUrl + getPermaLink();       
+        String absPath = RollerRuntimeConfig.getProperty("site.absoluteurl");
+        return absPath + getPermaLink();       
     }
     
     /**
@@ -729,7 +730,7 @@
             // go with the "no encoding" version
         }        
         WebsiteData website = this.getWebsite();
-        return "/" + website.getHandle() + "/entry/" + lAnchor;        
+        return "/" + getWebsite().getHandle() + "/entry/" + lAnchor;        
     }
     
     /**
@@ -738,15 +739,7 @@
      * @deprecated Use commentLink() instead
      */
     public String getCommentsLink() {
-        String dayString = DateUtil.format8chars(this.getPubTime());
-        String lAnchor = this.getAnchor();
-        try {
-            lAnchor = URLEncoder.encode(anchor, "UTF-8");
-        } catch (UnsupportedEncodingException e) {
-            // go with the "no encoding" version
-        }
-        String clink = "/page/" + this.getWebsite().getHandle() + "?entry=" + lAnchor;
-        return clink;
+        return getPermaLink() + "#comments";
     }
     /** 
      * to please XDoclet 
@@ -759,8 +752,8 @@
      * @roller.wrapPojoMethod type="simple"
      */
     public String getCommentLink() {
-        String absContextUrl = RollerConfig.getProperty("context.absPath");
-        return absContextUrl + getCommentsLink(); 
+        String absPath = RollerRuntimeConfig.getProperty("site.absoluteurl");
+        return absPath + getCommentsLink(); 
     }
     /** to please XDoclet */
     public void setCommentLink(String ignored) {}

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WebsiteData.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WebsiteData.java?rev=417854&r1=417853&r2=417854&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WebsiteData.java (original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/pojos/WebsiteData.java Wed Jun 28 12:32:28 2006
@@ -1024,8 +1024,8 @@
      */
     public String getURL() {
         // TODO: ATLAS reconcile entry.getPermaLink() with new URLs
-        String absContextUrl = RollerConfig.getProperty("context.absPath");
-        return absContextUrl + "/page/" + getHandle();       
+        String relPath = RollerRuntimeConfig.getProperty("site.relativeurl");
+        return relPath + "/" + getHandle();
     }
     public void setURL(String url) {
         // noop

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/RollerContext.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/RollerContext.java?rev=417854&r1=417853&r2=417854&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/RollerContext.java (original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/RollerContext.java Wed Jun 28 12:32:28 2006
@@ -466,24 +466,7 @@
     
     /** Get absolute URL of Roller context */
     public String getAbsoluteContextUrl(HttpServletRequest request) {
-        String url = RollerConfig.getProperty("context.absPath");
-        if (url == null) {
-            url = RollerRuntimeConfig.getProperty("site.absoluteurl");
-            if (url == null || url.trim().length() == 0) {
-                try {
-                    URL absURL = RequestUtils.absoluteURL(request, "/");
-                    url = absURL.toString();
-                } catch (MalformedURLException e) {
-                    url = "/";
-                    mLogger.error("ERROR: forming absolute URL", e);
-                }
-            }
-            if (url.endsWith("/")) {
-                url = url.substring(0, url.length() - 1);
-            }   
-            RollerConfig.setAbsoluteContextPath(url);
-        }
-        return url;
+        return RollerRuntimeConfig.getProperty("site.absoluteurl");
     }
     
     
@@ -493,7 +476,7 @@
      * @return Context URL or null if not initialized yet.
      */
     public String getAbsoluteContextUrl() {
-        return (String) mContext.getAttribute("org.apache.roller.absoluteContextURL");
+        return RollerRuntimeConfig.getProperty("site.absoluteurl");
     }
     
     

Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/filters/InitFilter.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/filters/InitFilter.java?rev=417854&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/filters/InitFilter.java (added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/core/filters/InitFilter.java Wed Jun 28 12:32:28 2006
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.ui.core.filters;
+
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.config.RollerRuntimeConfig;
+
+
+/**
+ * A special initialization filter which ensures that we have an opportunity
+ * to extract a few pieces of information about the environment we are running
+ * in when the first request is sent.
+ *
+ * @web.filter name="InitFilter"
+ */
+public class InitFilter implements Filter {
+    
+    private static Log log = LogFactory.getLog(InitFilter.class);
+    
+    private boolean initialized = false;
+    
+    
+    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
+            throws IOException, ServletException {
+        
+        if(!initialized) {
+            // first request, lets do our initialization
+            HttpServletRequest request = (HttpServletRequest) req;
+            HttpServletResponse response = (HttpServletResponse) res;
+            
+            // determine absolute and relative url paths to the app
+            String relPath = request.getContextPath();
+            String absPath = this.getAbsoluteUrl(request);
+            
+            // set them in our config
+            RollerRuntimeConfig.setAbsoluteContextPath(absPath);
+            RollerRuntimeConfig.setRelativeContextPath(relPath);
+            
+            log.debug("relPath = "+relPath);
+            log.debug("absPath = "+absPath);
+            
+            this.initialized = true;
+        }
+        
+        chain.doFilter(req, res);
+    }
+    
+    
+    private String getAbsoluteUrl(HttpServletRequest request) {
+        
+        String url = null;
+        
+        String fullUrl = request.getRequestURL().toString();
+        
+        // first extract just the server portion of the url
+        url = fullUrl.substring(0, fullUrl.indexOf(request.getRequestURI()));
+        
+        // then just add on the context path
+        url += request.getContextPath();
+        
+        // make certain that we don't end with a /
+        if(url.endsWith("/")) {
+            url = url.substring(0, url.length()-1);
+        }
+        
+        return url;
+    }
+    
+    
+    public void init(FilterConfig filterConfig) throws ServletException {}
+    
+    public void destroy() {}
+    
+}