KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > internal > PageFlowTagUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.internal;
19
20 import org.apache.beehive.netui.core.urls.URLType;
21 import org.apache.beehive.netui.pageflow.FlowController;
22 import org.apache.beehive.netui.pageflow.PageFlowConstants;
23 import org.apache.beehive.netui.pageflow.PageFlowUtils;
24 import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping;
25 import org.apache.beehive.netui.pageflow.internal.InternalConstants;
26 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.config.ActionConfig;
30 import org.apache.struts.util.TokenProcessor;
31
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import javax.servlet.jsp.PageContext JavaDoc;
37 import java.net.URISyntaxException JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * This is a utility class for the beehive tags with routines for helping with URL rewriting.
42  *
43  * <p> Includes methods to create a fully-rewritten url based on an initial url with query
44  * parameters and an anchor (location on page), checking if it needs to be secure and
45  * rewriting. There's also a method to check if a url is an action. </p>
46  */

47 public class PageFlowTagUtils
48 {
49     /**
50      * Create a fully-rewritten url from an initial action url with query parameters
51      * and an anchor (location on page), checking if it needs to be secure then call
52      * the rewriter service using a type of {@link org.apache.beehive.netui.core.urls.URLType#ACTION}.
53      * @param pageContext the current PageContext.
54      * @param action the action url to rewrite.
55      * @param params the query parameters for this url.
56      * @param location the location (anchor or fragment) for this url.
57      * @return a uri that has been run through the URL rewriter service.
58      */

59     public static String JavaDoc rewriteActionURL(PageContext JavaDoc pageContext, String JavaDoc action, Map JavaDoc params, String JavaDoc location)
60             throws URISyntaxException JavaDoc
61     {
62         ServletContext JavaDoc servletContext = pageContext.getServletContext();
63         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
64         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
65         boolean forXML = TagRenderingBase.Factory.isXHTML(request);
66         if (action.length() > 0 && action.charAt(0) == '/') action = action.substring(1);
67         return PageFlowUtils.getRewrittenActionURI(servletContext, request, response, action, params, location, forXML);
68     }
69
70     /**
71      * Create a fully-rewritten url from an initial href url with query parameters
72      * and an anchor (location on page), checking if it needs to be secure then call
73      * the rewriter service using a type of {@link org.apache.beehive.netui.core.urls.URLType#ACTION}.
74      * @param pageContext the current PageContext.
75      * @param url the href url to rewrite.
76      * @param params the query parameters for this url.
77      * @param location the location (anchor or fragment) for this url.
78      * @return a url that has been run through the URL rewriter service.
79      * @see PageFlowUtils#getRewrittenHrefURI(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest,
80      * javax.servlet.http.HttpServletResponse, String, java.util.Map, String, boolean)
81      */

82     public static String JavaDoc rewriteHrefURL(PageContext JavaDoc pageContext, String JavaDoc url, Map JavaDoc params, String JavaDoc location)
83             throws URISyntaxException JavaDoc
84     {
85         ServletContext JavaDoc servletContext = pageContext.getServletContext();
86         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
87         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
88         boolean forXML = TagRenderingBase.Factory.isXHTML(request);
89         return PageFlowUtils.getRewrittenHrefURI(servletContext, request, response, url, params, location, forXML);
90     }
91
92     /**
93      * Create a fully-rewritten url from an initial resource url with query parameters
94      * and an anchor (location on page), checking if it needs to be secure then call
95      * the rewriter service using a type of {@link org.apache.beehive.netui.core.urls.URLType#RESOURCE}.
96      * @param pageContext the current PageContext.
97      * @param url the resource url to rewrite.
98      * @param params the query parameters for this url.
99      * @param location the location (anchor or fragment) for this url.
100      * @return a url that has been run through the URL rewriter service.
101      * @see PageFlowUtils#getRewrittenResourceURI(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest,
102      * javax.servlet.http.HttpServletResponse, String, java.util.Map, String, boolean)
103      */

104     public static String JavaDoc rewriteResourceURL(PageContext JavaDoc pageContext, String JavaDoc url, Map JavaDoc params, String JavaDoc location)
105             throws URISyntaxException JavaDoc
106     {
107         ServletContext JavaDoc servletContext = pageContext.getServletContext();
108         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
109         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
110         boolean forXML = TagRenderingBase.Factory.isXHTML(request);
111         return PageFlowUtils.getRewrittenResourceURI(servletContext, request, response, url, params, location, forXML);
112     }
113
114     /**
115      * Determine whether a given URI is an Action.
116      * @param request the current HttpServletRequest.
117      * @param action the URI to check.
118      * @return <code>true</code> if the action is defined in the current page flow
119      * or in a shared flow. Otherwise, return <code>false</code>.
120      */

121     public static boolean isAction(HttpServletRequest JavaDoc request, String JavaDoc action)
122     {
123         FlowController flowController = PageFlowUtils.getCurrentPageFlow(request);
124
125         if (flowController != null) {
126             if (action.endsWith(PageFlowConstants.ACTION_EXTENSION)) {
127                 action = action.substring(0, action.length() - PageFlowConstants.ACTION_EXTENSION.length());
128             }
129
130             if (getActionMapping(request, flowController, action) != null) return true;
131             FlowController globalApp = PageFlowUtils.getSharedFlow(InternalConstants.GLOBALAPP_CLASSNAME, request);
132             return getActionMapping(request, globalApp, action) != null;
133         }
134
135         return true;
136     }
137
138     /**
139      * Get or generate a token used to prevent double submits to an action. The token is stored in the session,
140      * and checked (and removed) when processing an action with the <code>preventDoubleSubmit</code> attribute
141      * set to <code>true</code>.
142      */

143     public static String JavaDoc getToken(HttpServletRequest JavaDoc request, String JavaDoc action)
144     {
145         FlowController flowController = PageFlowUtils.getCurrentPageFlow(request);
146
147         if (flowController != null) {
148             MappingAndController mac = getActionMapping(request, flowController, action);
149             if (mac != null) return getToken(request, mac.mapping);
150         }
151
152         return null;
153     }
154
155     /**
156      * Get or generate a token used to prevent double submits to an action. The token is stored in the session,
157      * and checked (and removed) when processing an action with the <code>preventDoubleSubmit</code> attribute
158      * set to <code>true</code>.
159      */

160     public static String JavaDoc getToken(HttpServletRequest JavaDoc request, ActionMapping mapping)
161     {
162         if (mapping instanceof PageFlowActionMapping && ((PageFlowActionMapping) mapping).isPreventDoubleSubmit()) {
163             HttpSession JavaDoc session = request.getSession();
164             String JavaDoc token = (String JavaDoc) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
165             if (token != null) return token;
166             token = TokenProcessor.getInstance().generateToken(request);
167             request.getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, token);
168             return token;
169         }
170
171         return null;
172     }
173
174     public static class MappingAndController
175     {
176         public ActionMapping mapping;
177         public FlowController controller;
178     }
179
180     public static MappingAndController getActionMapping(HttpServletRequest JavaDoc request, FlowController flowController, String JavaDoc action)
181     {
182         ActionConfig mapping = null;
183         FlowController fc = null;
184
185         if (flowController != null) {
186             //
187
// If there's a '.' delimiter, it's a shared flow action.
188
//
189
int dot = action.indexOf('.');
190
191             if (dot == -1) {
192                 //
193
// It's an action in the current page flow, or in the (deprecated) Global.app.
194
//
195
if (action.charAt(0) != '/') action = '/' + action;
196                 mapping = flowController.getModuleConfig().findActionConfig(action);
197                 fc = flowController;
198                 
199                 //
200
// If we don't find it in the current page flow, look in Global.app.
201
//
202
if (mapping == null) {
203                     FlowController globalApp =
204                             PageFlowUtils.getSharedFlow(InternalConstants.GLOBALAPP_CLASSNAME, request);
205                     if (globalApp != null) {
206                         mapping = globalApp.getModuleConfig().findActionConfig(action);
207                         fc = globalApp;
208                     }
209                 }
210             }
211             else if (dot < action.length() - 1) {
212                 //
213
// It's an action in a shared flow.
214
//
215
String JavaDoc sharedFlowName = action.substring(0, dot);
216                 if (sharedFlowName.length() > 0 && sharedFlowName.charAt(0) == '/') {
217                     sharedFlowName = sharedFlowName.substring(1);
218                 }
219
220                 FlowController sharedFlow = (FlowController) PageFlowUtils.getSharedFlows(request).get(sharedFlowName);
221
222                 if (sharedFlow != null) {
223                     String JavaDoc actionPath = '/' + action.substring(dot + 1);
224                     mapping = sharedFlow.getModuleConfig().findActionConfig(actionPath);
225                     fc = sharedFlow;
226                 }
227             }
228         }
229
230         assert mapping == null || mapping instanceof ActionMapping : mapping.getClass().getName();
231
232         if (mapping != null) {
233             MappingAndController mac = new MappingAndController();
234             mac.mapping = (ActionMapping) mapping;
235             mac.controller = fc;
236             return mac;
237         }
238
239         return null;
240     }
241 }
242
Popular Tags