You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ri...@apache.org on 2005/06/07 19:45:12 UTC

svn commit: r188831 - /incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urls/DefaultTemplatedURLFormatter.java /incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urltemplates/URLTemplatesFactory.java

Author: rich
Date: Tue Jun  7 10:45:11 2005
New Revision: 188831

URL: http://svn.apache.org/viewcvs?rev=188831&view=rev
Log:
This is a contribution from Carlin Rogers to address http://issues.apache.org/jira/browse/BEEHIVE-739 : A url template ref without a key element causes an NPE in URLTemplatesFactory.

tests: drt in trunk (WinXP)
BB: self (linux)


Modified:
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urls/DefaultTemplatedURLFormatter.java
    incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urltemplates/URLTemplatesFactory.java

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urls/DefaultTemplatedURLFormatter.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urls/DefaultTemplatedURLFormatter.java?rev=188831&r1=188830&r2=188831&view=diff
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urls/DefaultTemplatedURLFormatter.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urls/DefaultTemplatedURLFormatter.java Tue Jun  7 10:45:11 2005
@@ -19,8 +19,6 @@
 
 import org.apache.beehive.netui.core.urltemplates.URLTemplate;
 import org.apache.beehive.netui.core.urltemplates.URLTemplateDescriptor;
-import org.apache.beehive.netui.util.config.bean.UrlConfig;
-import org.apache.beehive.netui.util.config.ConfigUtil;
 
 import javax.servlet.ServletRequest;
 
@@ -51,11 +49,16 @@
         // Look for the template config and get the right template.
         // If it is found, apply the value to the template.
         String result = null;
+        URLTemplate template = null;
         String templateName = URLTemplateDescriptor.getInstance().getURLTemplateRef( DEFAULT_TEMPLATE_REF, key );
 
         if ( templateName != null )
         {
-            URLTemplate template = URLTemplateDescriptor.getInstance().getURLTemplate( templateName );
+            template = URLTemplateDescriptor.getInstance().getURLTemplate( templateName );
+        }
+
+        if ( template != null )
+        {
             result = formatURIWithTemplate( request, uri, uriContext, template );
         }
         else

Modified: incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urltemplates/URLTemplatesFactory.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urltemplates/URLTemplatesFactory.java?rev=188831&r1=188830&r2=188831&view=diff
==============================================================================
--- incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urltemplates/URLTemplatesFactory.java (original)
+++ incubator/beehive/trunk/netui/src/util/org/apache/beehive/netui/core/urltemplates/URLTemplatesFactory.java Tue Jun  7 10:45:11 2005
@@ -19,18 +19,20 @@
 
 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateConfigDocument.UrlTemplateConfig;
 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateDocument;
-import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefGroupDocument;
 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefDocument;
+import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefGroupDocument;
+import org.apache.beehive.netui.util.internal.InternalStringBuilder;
 import org.apache.beehive.netui.util.logging.Logger;
-import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlError;
 import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlOptions;
 
 import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
 import java.io.InputStream;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.ArrayList;
 
 /**
  * Methods for configuring and retrieving the URLTemplates object.
@@ -107,9 +109,10 @@
      * Allow clients to define a set of required tokens for the
      * template verification. Tokens are expected to be qualified
      * in braces. E.g. {url:path}
-     * <p/>
+     * <p>
      * The template verification will ensure the URL template conforms to
      * a valid format for known tokens and contains the required tokens.
+     * </p>
      *
      * @param requiredTokens The set of required tokens in a valid template.
      */
@@ -141,7 +144,16 @@
             else
             {
                 // No descriptor
-                _log.info( "Running without URL template descriptor, " + _configFilePath );
+                String contextName = servletContext.getServletContextName();
+                InternalStringBuilder message = new InternalStringBuilder();
+                if ( contextName != null )
+                {
+                    message.append( contextName ).append( " - ");
+                }
+
+                message.append( "Running without URL template descriptor, " );
+                message.append( _configFilePath );
+                _log.info( message.toString() );
             }
         }
         catch ( XmlException xe )
@@ -154,6 +166,11 @@
             // Bad descriptor
             _log.error( "Problem parsing URL template descriptor in " + _configFilePath, ioe );
         }
+        catch ( Exception e )
+        {
+            // Bad descriptor
+            _log.error( "Problem loading URL template descriptor file " + _configFilePath, e );
+        }
         finally
         {
             // Close the stream
@@ -186,8 +203,19 @@
         UrlTemplateDocument.UrlTemplate[] templates = urlTemplateConfig.getUrlTemplateArray();
         for ( int i = 0; i < templates.length; i++ )
         {
-            String name = templates[i].getName().trim();
-            String value = templates[i].getValue().trim();
+            String name = templates[i].getName();
+            if ( name != null )
+            {
+                name = name.trim();
+            }
+            else
+            {
+                _log.error( "Malformed URL template descriptor in " + _configFilePath
+                            + ". The url-template name is missing." );
+                continue;
+            }
+
+            String value = templates[i].getValue();
             if ( value != null )
             {
                 value = value.trim();
@@ -199,6 +227,11 @@
                 urlTemplate.verify();
                 urlTemplates.addTemplate( name, urlTemplate );
             }
+            else
+            {
+                _log.error( "Malformed URL template descriptor in " + _configFilePath
+                            + ". The url-template value is missing." );
+            }
         }
 
         // Load template refs
@@ -206,19 +239,75 @@
         for ( int i = 0; i < templateRefGroups.length; i++ )
         {
             HashMap refGroup = new HashMap();
-            String refGroupName = templateRefGroups[i].getName().trim();
+            String refGroupName = templateRefGroups[i].getName();
+            if ( refGroupName != null )
+            {
+                refGroupName = refGroupName.trim();
+            }
+            else
+            {
+                _log.error( "Malformed URL template descriptor in " + _configFilePath
+                            + ". The url-template-ref-group name is missing." );
+                continue;
+            }
+
             UrlTemplateRefDocument.UrlTemplateRef[] templateRefs = templateRefGroups[i].getUrlTemplateRefArray();
             for ( int j = 0; j < templateRefs.length; j++ )
             {
-                String key = templateRefs[j].getKey().toString().trim();
-                String name = templateRefs[j].getTemplateName().trim();
-                if ( _log.isDebugEnabled() )
+                // Validate the XML.
+                ArrayList errorList = new ArrayList();
+                XmlOptions validateOptions = new XmlOptions();
+                validateOptions.setErrorListener( errorList );
+                UrlTemplateRefDocument.UrlTemplateRef.Key keyObj = templateRefs[j].xgetKey();
+
+                if ( keyObj != null )
+                {
+                    // Check that the key has a valid value.
+                    boolean isValid = templateRefs[j].xgetKey().validate( validateOptions );
+
+                    // If not, loop through the listener's contents, logging contained messages.
+                    if ( !isValid )
+                    {
+                        InternalStringBuilder message = new InternalStringBuilder( "Malformed URL template descriptor in " );
+                        message.append( _configFilePath );
+                        for ( int k = 0; k < errorList.size(); k++ )
+                        {
+                            XmlError error = ( XmlError ) errorList.get( k );
+                            message.append( ": ").append( error.getMessage() );
+                        }
+                        _log.error( message.toString() );
+                        continue;
+                    }
+                }
+                else
                 {
-                    _log.debug( "[" + refGroupName + " URLTemplate] " + key + " = " + name );
+                    _log.error( "Malformed URL template descriptor in " + _configFilePath
+                                + ". The url-template-ref key is missing." );
+                    continue;
                 }
-                refGroup.put( key, name );
+
+                String key = templateRefs[j].getKey().toString();
+                String name = templateRefs[j].getTemplateName();
+                if ( name != null )
+                {
+                    name = name.trim();
+                    refGroup.put( key, name );
+                    if ( _log.isDebugEnabled() )
+                    {
+                        _log.debug( "[" + refGroupName + " URLTemplate] " + key + " = " + name );
+                    }
+                }
+                else
+                {
+                    _log.error( "Malformed URL template descriptor in " + _configFilePath
+                                + ". The url-template-ref template-name is missing." );
+                }
+            }
+
+            if ( refGroup.size() != 0 )
+            {
+                urlTemplates.addTemplateRefGroup( refGroupName, refGroup );
             }
-            urlTemplates.addTemplateRefGroup( refGroupName, refGroup );
         }
 
         return urlTemplates;