1 18 package org.apache.beehive.netui.script.common; 19 20 import java.util.Map ; 21 import java.util.Collections ; 22 import javax.servlet.ServletRequest ; 23 import javax.servlet.ServletResponse ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.jsp.JspContext ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.el.VariableResolver ; 30 31 import org.apache.beehive.netui.pageflow.FacesBackingBean; 32 import org.apache.beehive.netui.pageflow.GlobalApp; 33 import org.apache.beehive.netui.pageflow.PageFlowController; 34 import org.apache.beehive.netui.pageflow.PageFlowUtils; 35 import org.apache.beehive.netui.pageflow.SharedFlowController; 36 import org.apache.beehive.netui.pageflow.internal.AnyBeanActionForm; 37 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 38 import org.apache.beehive.netui.pageflow.internal.FacesBackingBeanFactory; 39 import org.apache.beehive.netui.script.el.NetUIUpdateVariableResolver; 40 import org.apache.beehive.netui.util.logging.Logger; 41 42 45 public class ImplicitObjectUtil { 46 47 private static final Logger LOGGER = Logger.getInstance(ImplicitObjectUtil.class); 48 49 private static final String PAGE_FLOW_IMPLICIT_OBJECT_KEY = "pageFlow"; 50 private static final String SHARED_FLOW_IMPLICIT_OBJECT_KEY = "sharedFlow"; 51 private static final String GLOBAL_APP_IMPLICIT_OBJECT_KEY = "globalApp"; 52 private static final String BUNDLE_IMPLICIT_OBJECT_KEY = "bundle"; 53 private static final String BACKING_IMPLICIT_OBJECT_KEY = "backing"; 54 private static final String PAGE_INPUT_IMPLICIT_OBJECT_KEY = "pageInput"; 55 private static final String ACTION_FORM_IMPLICIT_OBJECT_KEY = "actionForm"; 56 private static final String OUTPUT_FORM_BEAN_OBJECT_KEY = "outputFormBean"; 57 58 59 private ImplicitObjectUtil() {} 60 61 public static final void loadActionForm(JspContext jspContext, Object form) { 62 jspContext.setAttribute(ACTION_FORM_IMPLICIT_OBJECT_KEY, unwrapForm(form)); 63 } 64 65 public static final void unloadActionForm(JspContext jspContext) { 66 jspContext.removeAttribute(ACTION_FORM_IMPLICIT_OBJECT_KEY); 67 } 68 69 public static final void loadPageFlow(ServletRequest request, PageFlowController pageFlow, FacesBackingBean fbb) { 70 if(pageFlow != null) 71 request.setAttribute(PAGE_FLOW_IMPLICIT_OBJECT_KEY, pageFlow); 72 if(fbb != null) 73 request.setAttribute(BACKING_IMPLICIT_OBJECT_KEY, fbb); 74 75 Map map = InternalUtils.getPageInputMap(request); 76 request.setAttribute(PAGE_INPUT_IMPLICIT_OBJECT_KEY, map != null ? map : Collections.EMPTY_MAP); 77 } 78 79 public static final void loadSharedFlow(ServletRequest request, Map sharedFlows) { 80 if(sharedFlows != null) 81 request.setAttribute(SHARED_FLOW_IMPLICIT_OBJECT_KEY, sharedFlows); 82 } 83 84 public static final void loadGlobalApp(ServletRequest request, GlobalApp globalApp) { 85 if(globalApp != null) 86 request.setAttribute(GLOBAL_APP_IMPLICIT_OBJECT_KEY, globalApp); 87 } 88 89 public static final void loadBundleMap(ServletRequest servletRequest, BundleMap bundleMap) { 90 servletRequest.setAttribute(BUNDLE_IMPLICIT_OBJECT_KEY, bundleMap); 91 } 92 93 public static final Object unwrapForm(Object form) { 94 if(LOGGER.isDebugEnabled() && form instanceof AnyBeanActionForm) 95 LOGGER.debug("using form of type: " + ((AnyBeanActionForm)form != null ? ((AnyBeanActionForm)form).getClass().getName() : "null")); 96 97 if(form instanceof AnyBeanActionForm) 98 return ((AnyBeanActionForm)form).getBean(); 99 else 100 return form; 101 } 102 103 public static final Map getSharedFlow(ServletRequest request) { 104 return (Map )request.getAttribute(SHARED_FLOW_IMPLICIT_OBJECT_KEY); 105 } 106 107 public static final PageFlowController getPageFlow(ServletRequest request, ServletResponse response) { 108 assert request instanceof HttpServletRequest ; 109 110 PageFlowController jpf = PageFlowUtils.getCurrentPageFlow((HttpServletRequest )request); 111 if(jpf != null) 112 return jpf; 113 else { 114 RuntimeException re = new RuntimeException ("There is no current PageFlow for the expression."); 116 if(LOGGER.isErrorEnabled()) LOGGER.error("", re); 117 throw re; 118 } 119 } 120 121 public static final GlobalApp getGlobalApp(ServletRequest request, ServletResponse response) { 122 assert request instanceof HttpServletRequest ; 123 124 GlobalApp ga = PageFlowUtils.getGlobalApp((HttpServletRequest )request); 125 126 if(ga == null) { 127 RuntimeException re = new RuntimeException ("Can not create the globalApp binding context; the GlobalApp object is null."); 129 if(LOGGER.isErrorEnabled()) LOGGER.error("", re); 130 throw re; 131 } 132 133 return ga; 134 } 135 136 139 public static final VariableResolver getUpdateVariableResolver(ServletRequest request, ServletResponse response, boolean isHandlingPost) { 140 Object form = ImplicitObjectUtil.unwrapForm(request.getAttribute(org.apache.struts.taglib.html.Constants.BEAN_KEY)); 141 142 143 return new NetUIUpdateVariableResolver(form, request, response, isHandlingPost); 144 } 145 146 public static final VariableResolver getUpdateVariableResolver(Object form, ServletRequest request, ServletResponse response, boolean isHandlingPost) { 147 Object realForm = ImplicitObjectUtil.unwrapForm(form); 148 149 150 return new NetUIUpdateVariableResolver(realForm, request, response, isHandlingPost); 151 } 152 153 public static final VariableResolver getReadVariableResolver(PageContext pageContext) { 154 assert pageContext != null; 155 return pageContext.getVariableResolver(); 156 } 157 158 public static final void loadImplicitObjects(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, PageFlowController curJpf) { 159 FacesBackingBean fbb = 162 FacesBackingBeanFactory.getFacesBackingBeanForRequest(request, response, servletContext); 163 loadPageFlow(request, curJpf, fbb); 164 165 BundleMap bundleMap = new BundleMap(request, servletContext); 167 loadBundleMap(request, bundleMap); 168 } 169 170 public static final void loadOutputFormBean(ServletRequest request, Object bean) { 171 if(bean != null) 172 request.setAttribute(OUTPUT_FORM_BEAN_OBJECT_KEY, bean); 173 } 174 } 175 | Popular Tags |