Thanks for the contribution, Carlin -- I looked this over and submitted it. I made some minor changes to the javadoc, mostly just so we're explicit about what we mean by "rewriting the URL yourself" (using org.apache.beehive.netui.core.urls.URLRewriterService) -- let me know what you think.

Thanks,
Rich

Carlin Rogers wrote:

I have a request to check in a change to...

- 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.

Thanks,
Carlin

------------------------------------------------------------------------

Index: src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java
===================================================================
--- src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java    
(revision 125448)
+++ src/tags-html/org/apache/beehive/netui/tags/html/RewriteURL.java    
(working copy)
@@ -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;
            }

Index: 
src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java
===================================================================
--- src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java  
(revision 125448)
+++ src/tags-html/org/apache/beehive/netui/tags/internal/PageFlowTagUtils.java  
(working copy)
@@ -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);

@@ -280,56 +278,4 @@

        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;
-    }
}
Index: src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java
===================================================================
--- src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java   
(revision 125448)
+++ src/pageflow/org/apache/beehive/netui/pageflow/PageFlowUtils.java   
(working copy)
@@ -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.*;
@@ -1128,4 +1132,180 @@
    {
        InternalUtils.setCurrentActionResolver( resolver, request );
    }
+
+    /**
+     * Gives you the raw action URI, which you can modify as needed,
+     * then run through the URL rewriter yourself.
+     *
+     * @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's a problem converting the action 
url (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;
+    }
+
+    /**
+     * Provides 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 "&amp;" entity, rather than the character, 
'&'.
+     * @return a fully-rewritten URI for the given action.
+     * @throws URISyntaxException if there's a problem converting the action 
url (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();
+    }
+
+    /**
+     * Provides a fully-rewritten URI given a url and parameters.
+     *
+     * @param servletContext the current ServletContext.
+     * @param request the current HttpServletRequest.
+     * @param response the current HttpServletResponse.
+     * @param url the url 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 url.
+     * @param asValidXml flag indicating that the query of the uri should be 
written
+     *                   using the "&amp;" entity, rather than the character, 
'&'.
+     * @return a fully-rewritten URI for the given action.
+     * @throws URISyntaxException if there's a problem converting the action 
url (derived
+     *                            from processing the given action name) into 
a MutableURI.
+     */
+    public static String getRewrittenResourceURI( ServletContext 
servletContext, HttpServletRequest request,
+                                                  HttpServletResponse 
response, String url, Map params,
+                                                  String fragment, boolean 
asValidXml )
+            throws URISyntaxException
+    {
+        MutableURI uri = new MutableURI( url );
+        uri.setEncoding( response.getCharacterEncoding() );
+
+        if ( params != null )
+        {
+            uri.addParameters( params, false );
+        }
+
+        if ( fragment != null )
+        {
+            uri.setFragment( fragment );
+        }
+
+        if ( !uri.isAbsolute() )
+        {
+            if ( !url.startsWith( "/" ) && !url.equals( "" ) )
+            {
+                String reqUri = request.getRequestURI();
+                String path = reqUri.substring( 0, reqUri.lastIndexOf( '/' ) + 
1 );
+                uri.setPath( path + 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;
+    }
}
Index: src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java
===================================================================
--- src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java  
(revision 125448)
+++ src/pageflow/org/apache/beehive/netui/pageflow/FlowController.java  
(working copy)
@@ -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;
@@ -1838,4 +1840,61 @@
assert rp == null || rp instanceof PageFlowRequestProcessor : rp.getClass().getName();
return ( PageFlowRequestProcessor ) rp;
}
+
+ /**
+ * Gives you the raw action URI, which you can modify as needed,
+ * then run through the URL rewriter yourself.
+ *
+ * @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's a problem converting the action url (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 );
+ }
+
+ /**
+ * Provides 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 "&amp;" entity, rather than the character, '&'.
+ * @return a fully-rewritten URI for the given action.
+ * @throws URISyntaxException if there's 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 );
+ }
}


Reply via email to