Author: rich
Date: Mon Jan 17 23:01:54 2005
New Revision: 125465

URL: http://svn.apache.org/viewcvs?view=rev&rev=125465
Log:
This is a contribution from Carlin Rogers:

    "- expose a public utility method for creating a fully
    rewritten action URL given an action name and a parameter
    map. This can be utilized in various places such as tags."

DRT: netui (WinXP)
BB: self (linux)


Modified:
   
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java
   
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java
   
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java
   
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java

Modified: 
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java?view=diff&rev=125465&p1=incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java&r1=125464&p2=incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java&r2=125465
==============================================================================
--- 
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java
    (original)
+++ 
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java
    Mon Jan 17 23:01:54 2005
@@ -17,15 +17,16 @@
  */
 package org.apache.beehive.netui.pageflow;
 
+import org.apache.beehive.netui.core.urls.MutableURI;
+import org.apache.beehive.netui.pageflow.annotations.Jpf;
 import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping;
 import org.apache.beehive.netui.pageflow.internal.ContextCache;
-import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
-import org.apache.beehive.netui.util.FileUtils;
 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
 import org.apache.beehive.netui.pageflow.internal.InternalExpressionUtils;
-import org.apache.beehive.netui.pageflow.annotations.Jpf;
 import org.apache.beehive.netui.pageflow.handler.LoginHandler;
 import org.apache.beehive.netui.pageflow.handler.ExceptionsHandler;
+import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
+import org.apache.beehive.netui.util.FileUtils;
 import org.apache.beehive.netui.util.cache.ClassLevelCache;
 import org.apache.beehive.netui.util.logging.Logger;
 import org.apache.struts.Globals;
@@ -54,6 +55,7 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Field;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.Locale;
@@ -1837,5 +1839,62 @@
         
         assert rp == null || rp instanceof PageFlowRequestProcessor : 
rp.getClass().getName();
         return ( PageFlowRequestProcessor ) rp;
+    }
+
+    /**
+     * Create a raw action URI, which can be modified before being sent 
through the registered URL rewriting chain
+     * using [EMAIL PROTECTED] 
org.apache.beehive.netui.core.urls.URLRewriterService#rewriteURL}.
+     *
+     * @param actionName the action name to convert into a MutableURI.
+     * @return a MutableURI for the given action, suitable for URL rewriting.
+     * @throws URISyntaxException    if there is a problem converting the 
action URI (derived
+     *                               from processing the given action name) 
into a MutableURI.
+     * @throws IllegalStateException if this method is invoked outside of 
action method
+     *                               execution (i.e., outside of the call to 
[EMAIL PROTECTED] FlowController#execute},
+     *                               and outside of [EMAIL PROTECTED] 
FlowController#onCreate},
+     *                               [EMAIL PROTECTED] 
FlowController#beforeAction}, [EMAIL PROTECTED] FlowController#afterAction}.
+     */
+    public MutableURI getActionURI( String actionName )
+            throws URISyntaxException
+    {
+        if ( _perRequestState == null )
+        {
+            throw new IllegalStateException( "getActionURI was called outside 
of a valid context." );
+        }
+        ServletContext servletContext = getServletContext();
+        HttpServletRequest request = getRequest();
+        HttpServletResponse response = getResponse();
+
+        return PageFlowUtils.getActionURI( servletContext, request, response, 
actionName );
+    }
+
+    /**
+     * Create a fully-rewritten URI given an action and parameters.
+     *
+     * @param actionName the action name to convert into a fully-rewritten URI.
+     * @param parameters the additional parameters to include in the URI query.
+     * @param asValidXml flag indicating that the query of the uri should be 
written
+     *                   using the "&" entity, rather than the character, 
'&'.
+     * @return a fully-rewritten URI for the given action.
+     * @throws URISyntaxException    if there is a problem converting the 
action url (derived
+     *                               from processing the given action name) 
into a URI.
+     * @throws IllegalStateException if this method is invoked outside of 
action method
+     *                               execution (i.e., outside of the call to 
[EMAIL PROTECTED] FlowController#execute},
+     *                               and outside of [EMAIL PROTECTED] 
FlowController#onCreate},
+     *                               [EMAIL PROTECTED] 
FlowController#beforeAction}, [EMAIL PROTECTED] FlowController#afterAction}.
+     */
+    public String getRewrittenActionURI( String actionName, Map parameters, 
boolean asValidXml )
+            throws URISyntaxException
+    {
+        if ( _perRequestState == null )
+        {
+            throw new IllegalStateException( "getRewrittenActionURI was called 
outside of a valid context." );
+        }
+        ServletContext servletContext = getServletContext();
+        HttpServletRequest request = getRequest();
+        HttpServletResponse response = getResponse();
+
+        return PageFlowUtils.getRewrittenActionURI( servletContext, request, 
response,
+                                                    actionName, parameters, 
null, asValidXml );
     }
 }

Modified: 
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java?view=diff&rev=125465&p1=incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java&r1=125464&p2=incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java&r2=125465
==============================================================================
--- 
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java
     (original)
+++ 
incubator/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java
     Mon Jan 17 23:01:54 2005
@@ -17,6 +17,9 @@
  */
 package org.apache.beehive.netui.pageflow;
 
+import org.apache.beehive.netui.core.urls.MutableURI;
+import org.apache.beehive.netui.core.urls.URLRewriter.URLType;
+import org.apache.beehive.netui.core.urls.URLRewriterService;
 import org.apache.beehive.netui.pageflow.config.PageFlowActionFormBean;
 import org.apache.beehive.netui.pageflow.internal.ActionResultImpl;
 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
@@ -45,12 +48,13 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import java.io.PrintStream;
+import java.net.URISyntaxException;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Stack;
-import java.util.Collections;
 import java.util.concurrent.ConcurrentHashMap;
-import java.io.PrintStream;
 
 import static org.apache.beehive.netui.pageflow.internal.InternalConstants.*;
 import static org.apache.beehive.netui.pageflow.PageFlowConstants.*;
@@ -1127,5 +1131,181 @@
     public static void setCurrentActionResolver( ActionResolver resolver, 
HttpServletRequest request )
     {
         InternalUtils.setCurrentActionResolver( resolver, request );
+    }
+
+    /**
+     * Create a raw action URI, which can be modified before being sent 
through the registered URL rewriting chain
+     * using [EMAIL PROTECTED] 
org.apache.beehive.netui.core.urls.URLRewriterService#rewriteURL}.
+     *
+     * @param servletContext the current ServletContext.
+     * @param request the current HttpServletRequest.
+     * @param response the current HttpServletResponse.
+     * @param actionName the action name to convert into a MutableURI.
+     * @return a MutableURI for the given action, suitable for URL rewriting.
+     * @throws URISyntaxException if there is a problem converting the action 
URI (derived from processing the given
+     *             action name) into a MutableURI.
+     */
+    public static MutableURI getActionURI( ServletContext servletContext, 
HttpServletRequest request,
+                                           HttpServletResponse response, 
String actionName )
+            throws URISyntaxException
+    {
+        String qualifiedAction = InternalUtils.qualifyAction( servletContext, 
actionName );
+        String actionUrl = InternalUtils.createActionURL( request, 
qualifiedAction );
+        MutableURI uri = new MutableURI( actionUrl );
+        uri.setEncoding( response.getCharacterEncoding() );
+
+        return uri;
+    }
+
+    /**
+     * Create a fully-rewritten URI given an action name and parameters.
+     *
+     * @param servletContext the current ServletContext.
+     * @param request the current HttpServletRequest.
+     * @param response the current HttpServletResponse.
+     * @param actionName the action name to convert into a fully-rewritten URI.
+     * @param params the additional parameters to include in the URI query.
+     * @param fragment the fragment (anchor or location) for this url.
+     * @param asValidXml flag indicating that the query of the uri should be 
written
+     *                   using the "&" entity, rather than the character, 
'&'.
+     * @return a fully-rewritten URI for the given action.
+     * @throws URISyntaxException if there is a problem converting the action 
URI (derived
+     *                            from processing the given action name) into 
a MutableURI.
+     */
+    public static String getRewrittenActionURI( ServletContext servletContext, 
HttpServletRequest request,
+                                                HttpServletResponse response, 
String actionName, Map params,
+                                                String fragment, boolean 
asValidXml )
+            throws URISyntaxException
+    {
+        String qualifiedAction = InternalUtils.qualifyAction( servletContext, 
actionName );
+        String actionUrl = InternalUtils.createActionURL( request, 
qualifiedAction );
+        MutableURI uri = new MutableURI( actionUrl );
+        uri.setEncoding( response.getCharacterEncoding() );
+
+        if ( params != null )
+        {
+            uri.addParameters( params, false );
+        }
+
+        if ( fragment != null )
+        {
+            uri.setFragment( fragment );
+        }
+
+        boolean needsToBeSecure = needsToBeSecure( servletContext, request, 
actionUrl, true );
+        URLRewriterService.rewriteURL( servletContext, request, response, uri, 
URLType.ACTION, needsToBeSecure );
+
+        if ( asValidXml )
+        {
+            return uri.toXMLString();
+        }
+
+        return uri.toString();
+    }
+
+    /**
+     * Create a fully-rewritten URI given a path and parameters.
+     *
+     * @param servletContext the current ServletContext.
+     * @param request the current HttpServletRequest.
+     * @param response the current HttpServletResponse.
+     * @param path the path to process into a fully-rewritten URI.
+     * @param params the additional parameters to include in the URI query.
+     * @param fragment the fragment (anchor or location) for this URI.
+     * @param asValidXml flag indicating that the query of the uri should be 
written
+     *                   using the "&" entity, rather than the character, 
'&'.
+     * @return a fully-rewritten URI for the given action.
+     * @throws URISyntaxException if there's a problem converting the action 
URI (derived
+     *                            from processing the given action name) into 
a MutableURI.
+     */
+    public static String getRewrittenResourceURI( ServletContext 
servletContext, HttpServletRequest request,
+                                                  HttpServletResponse 
response, String path, Map params,
+                                                  String fragment, boolean 
asValidXml )
+            throws URISyntaxException
+    {
+        MutableURI uri = new MutableURI( path );
+        uri.setEncoding( response.getCharacterEncoding() );
+
+        if ( params != null )
+        {
+            uri.addParameters( params, false );
+        }
+
+        if ( fragment != null )
+        {
+            uri.setFragment( fragment );
+        }
+
+        if ( !uri.isAbsolute() )
+        {
+            if ( !path.startsWith( "/" ) && !path.equals( "" ) )
+            {
+                String reqUri = request.getRequestURI();
+                String reqPath = reqUri.substring( 0, reqUri.lastIndexOf( '/' 
) + 1 );
+                uri.setPath( reqPath + uri.getPath() );
+            }
+
+            boolean needsToBeSecure = needsToBeSecure( servletContext, 
request, uri.getPath(), true );
+            URLRewriterService.rewriteURL( servletContext, request, response, 
uri, URLType.RESOURCE, needsToBeSecure );
+        }
+
+        if ( asValidXml )
+        {
+            return uri.toXMLString();
+        }
+
+        return uri.toString();
+    }
+
+    /**
+     * Tell whether a given URI should be written to be secure.
+     * @param context          the current ServletContext.
+     * @param request          the current HttpServletRequest.
+     * @param uri              the URI to check.
+     * @param stripContextPath if <code>true</code>, strip the webapp context 
path from the URI before
+     *                         processing it.
+     * @return <code>true</code> when:
+     *         <ul>
+     *         <li>the given URI is configured in the deployment descriptor to 
be secure (according to
+     *         [EMAIL PROTECTED] 
org.apache.beehive.netui.pageflow.SecurityProtocol}), or
+     *         <li>the given URI is not configured in the deployment 
descriptor, and the current request
+     *         is secure ([EMAIL PROTECTED] 
javax.servlet.http.HttpServletRequest#isSecure} returns
+     *         <code>true</code>).
+     *         </ul>
+     *         <code>false</code> when:
+     *         <ul>
+     *         <li>the given URI is configured explicitly in the deployment 
descriptor to be unsecure
+     *         (according to [EMAIL PROTECTED] 
org.apache.beehive.netui.pageflow.SecurityProtocol}), or
+     *         <li>the given URI is not configured in the deployment 
descriptor, and the current request
+     *         is unsecure ([EMAIL PROTECTED] 
javax.servlet.http.HttpServletRequest#isSecure} returns
+     *         <code>false</code>).
+     *         </ul>
+     */
+    public static boolean needsToBeSecure(ServletContext context, 
ServletRequest request,
+                                          String uri, boolean stripContextPath)
+    {
+        // Get the web-app relative path for security check
+        String secureCheck = uri;
+        if (stripContextPath) {
+            String contextPath = ((HttpServletRequest) 
request).getContextPath();
+            if (secureCheck.startsWith(contextPath)) {
+                secureCheck = secureCheck.substring(contextPath.length());
+            }
+        }
+
+        boolean secure = false;
+        if (secureCheck.indexOf('?') > -1) {
+            secureCheck = secureCheck.substring(0, secureCheck.indexOf('?'));
+        }
+
+        SecurityProtocol sp = getSecurityProtocol(secureCheck, context, 
(HttpServletRequest) request);
+        if (sp.equals(SecurityProtocol.UNSPECIFIED)) {
+            secure = request.isSecure();
+        }
+        else {
+            secure = sp.equals(SecurityProtocol.SECURE);
+        }
+
+        return secure;
     }
 }

Modified: 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java?view=diff&rev=125465&p1=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java&r1=125464&p2=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java&r2=125465
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java
      (original)
+++ 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java
      Mon Jan 17 23:01:54 2005
@@ -20,8 +20,8 @@
 import org.apache.beehive.netui.core.urls.MutableURI;
 import org.apache.beehive.netui.core.urls.URLRewriter.URLType;
 import org.apache.beehive.netui.core.urls.URLRewriterService;
+import org.apache.beehive.netui.pageflow.PageFlowUtils;
 import org.apache.beehive.netui.tags.AbstractClassicTag;
-import org.apache.beehive.netui.tags.internal.PageFlowTagUtils;
 import org.apache.beehive.netui.util.Bundle;
 
 import javax.servlet.ServletContext;
@@ -93,7 +93,7 @@
             boolean needsToBeSecure = false;
             uri = new MutableURI(url);
             uri.setEncoding(response.getCharacterEncoding());
-            if (!uri.isAbsolute() && PageFlowTagUtils.needsSecure(context, 
request, url, true)) {
+            if (!uri.isAbsolute() && PageFlowUtils.needsToBeSecure(context, 
request, url, true)) {
                 needsToBeSecure = true;
             }
 

Modified: 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java?view=diff&rev=125465&p1=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java&r1=125464&p2=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java&r2=125465
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java
    (original)
+++ 
incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java
    Mon Jan 17 23:01:54 2005
@@ -23,7 +23,6 @@
 import org.apache.beehive.netui.pageflow.FlowController;
 import org.apache.beehive.netui.pageflow.PageFlowConstants;
 import org.apache.beehive.netui.pageflow.PageFlowUtils;
-import org.apache.beehive.netui.pageflow.SecurityProtocol;
 import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping;
 import org.apache.beehive.netui.pageflow.internal.InternalConstants;
 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
@@ -33,7 +32,6 @@
 import org.apache.struts.util.TokenProcessor;
 
 import javax.servlet.ServletContext;
-import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
@@ -64,10 +62,10 @@
             throws URISyntaxException
     {
         ServletContext servletContext = pageContext.getServletContext();
-        ServletRequest request = pageContext.getRequest();
+        HttpServletRequest request = (HttpServletRequest) 
pageContext.getRequest();
         HttpServletResponse response = (HttpServletResponse) 
pageContext.getResponse();
         String qualifiedAction = InternalUtils.qualifyAction(servletContext, 
action);
-        String actionUrl = InternalUtils.createActionURL((HttpServletRequest) 
request, qualifiedAction);
+        String actionUrl = InternalUtils.createActionURL(request, 
qualifiedAction);
         MutableURI uri = new MutableURI(actionUrl);
         uri.setEncoding(response.getCharacterEncoding());
 
@@ -79,7 +77,7 @@
             uri.setFragment(location);
         }
 
-        boolean needsToBeSecure = needsSecure(servletContext, request, 
actionUrl, true);
+        boolean needsToBeSecure = 
PageFlowUtils.needsToBeSecure(servletContext, request, actionUrl, true);
         URLRewriterService.rewriteURL(servletContext, request, response, uri,
                 URLType.ACTION, needsToBeSecure);
 
@@ -146,7 +144,7 @@
         }
 
         ServletContext servletContext = pageContext.getServletContext();
-        boolean needsToBeSecure = needsSecure(servletContext, request, url, 
true);
+        boolean needsToBeSecure = 
PageFlowUtils.needsToBeSecure(servletContext, request, uri.getPath(), true);
         URLRewriterService.rewriteURL(servletContext, request, response, uri,
                 type, needsToBeSecure);
 
@@ -279,57 +277,5 @@
         }
 
         return null;
-    }
-
-    /**
-     * Tell whether a given URI should be written to be secure.
-     * @param request          the current HttpServletRequest.
-     * @param context          the current ServletContext.
-     * @param uri              the URI to check.
-     * @param stripContextPath if <code>true</code>, strip the webapp context 
path from the URI before
-     *                         processing it.
-     * @return <code>true</code> when:
-     *         <ul>
-     *         <li>the given URI is configured in the deployment descriptor to 
be secure (according to
-     *         [EMAIL PROTECTED] 
org.apache.beehive.netui.pageflow.SecurityProtocol}), or
-     *         <li>the given URI is not configured in the deployment 
descriptor, and the current request
-     *         is secure ([EMAIL PROTECTED] 
javax.servlet.http.HttpServletRequest#isSecure} returns
-     *         <code>true</code>).
-     *         </ul>
-     *         <code>false</code> when:
-     *         <ul>
-     *         <li>the given URI is configured explicitly in the deployment 
descriptor to be unsecure
-     *         (according to [EMAIL PROTECTED] 
org.apache.beehive.netui.pageflow.SecurityProtocol}), or
-     *         <li>the given URI is not configured in the deployment 
descriptor, and the current request
-     *         is unsecure ([EMAIL PROTECTED] 
javax.servlet.http.HttpServletRequest#isSecure} returns
-     *         <code>false</code>).
-     *         </ul>
-     */
-    public static boolean needsSecure(ServletContext context, ServletRequest 
request,
-                                      String uri, boolean stripContextPath)
-    {
-        // Get the web-app relative path for security check
-        String secureCheck = uri;
-        if (stripContextPath) {
-            String contextPath = ((HttpServletRequest) 
request).getContextPath();
-            if (secureCheck.startsWith(contextPath)) {
-                secureCheck = secureCheck.substring(contextPath.length());
-            }
-        }
-
-        boolean secure = false;
-        if (secureCheck.indexOf('?') > -1) {
-            secureCheck = secureCheck.substring(0, secureCheck.indexOf('?'));
-        }
-
-        SecurityProtocol sp = PageFlowUtils.getSecurityProtocol(secureCheck, 
context, (HttpServletRequest) request);
-        if (sp.equals(SecurityProtocol.UNSPECIFIED)) {
-            secure = request.isSecure();
-        }
-        else {
-            secure = sp.equals(SecurityProtocol.SECURE);
-        }
-
-        return secure;
     }
 }

Reply via email to