1 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 ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.http.HttpSession ; 36 import javax.servlet.jsp.PageContext ; 37 import java.net.URISyntaxException ; 38 import java.util.Map ; 39 40 47 public class PageFlowTagUtils 48 { 49 59 public static String rewriteActionURL(PageContext pageContext, String action, Map params, String location) 60 throws URISyntaxException 61 { 62 ServletContext servletContext = pageContext.getServletContext(); 63 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 64 HttpServletResponse response = (HttpServletResponse ) 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 82 public static String rewriteHrefURL(PageContext pageContext, String url, Map params, String location) 83 throws URISyntaxException 84 { 85 ServletContext servletContext = pageContext.getServletContext(); 86 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 87 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 88 boolean forXML = TagRenderingBase.Factory.isXHTML(request); 89 return PageFlowUtils.getRewrittenHrefURI(servletContext, request, response, url, params, location, forXML); 90 } 91 92 104 public static String rewriteResourceURL(PageContext pageContext, String url, Map params, String location) 105 throws URISyntaxException 106 { 107 ServletContext servletContext = pageContext.getServletContext(); 108 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 109 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 110 boolean forXML = TagRenderingBase.Factory.isXHTML(request); 111 return PageFlowUtils.getRewrittenResourceURI(servletContext, request, response, url, params, location, forXML); 112 } 113 114 121 public static boolean isAction(HttpServletRequest request, String 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 143 public static String getToken(HttpServletRequest request, String 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 160 public static String getToken(HttpServletRequest request, ActionMapping mapping) 161 { 162 if (mapping instanceof PageFlowActionMapping && ((PageFlowActionMapping) mapping).isPreventDoubleSubmit()) { 163 HttpSession session = request.getSession(); 164 String token = (String ) 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 request, FlowController flowController, String action) 181 { 182 ActionConfig mapping = null; 183 FlowController fc = null; 184 185 if (flowController != null) { 186 int dot = action.indexOf('.'); 190 191 if (dot == -1) { 192 if (action.charAt(0) != '/') action = '/' + action; 196 mapping = flowController.getModuleConfig().findActionConfig(action); 197 fc = flowController; 198 199 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 String 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 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 |