1 24 package org.ofbiz.widget.screen; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.util.Enumeration ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.servlet.ServletContext ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import javolution.util.FastMap; 42 import javolution.util.FastSet; 43 44 import org.ofbiz.base.util.Debug; 45 import org.ofbiz.base.util.GeneralException; 46 import org.ofbiz.base.util.UtilDateTime; 47 import org.ofbiz.base.util.UtilFormatOut; 48 import org.ofbiz.base.util.UtilHttp; 49 import org.ofbiz.base.util.UtilMisc; 50 import org.ofbiz.base.util.UtilValidate; 51 import org.ofbiz.base.util.collections.MapStack; 52 import org.ofbiz.entity.GenericDelegator; 53 import org.ofbiz.entity.GenericEntity; 54 import org.ofbiz.entity.GenericValue; 55 import org.ofbiz.security.Security; 56 import org.ofbiz.service.DispatchContext; 57 import org.ofbiz.service.LocalDispatcher; 58 import org.ofbiz.webapp.control.LoginWorker; 59 import org.ofbiz.widget.html.HtmlFormRenderer; 60 import org.xml.sax.SAXException ; 61 62 import freemarker.ext.beans.BeansWrapper; 63 import freemarker.ext.jsp.TaglibFactory; 64 import freemarker.ext.servlet.HttpRequestHashModel; 65 import freemarker.ext.servlet.HttpSessionHashModel; 66 67 74 public class ScreenRenderer { 75 76 public static final String module = ScreenRenderer.class.getName(); 77 78 protected Writer writer; 79 protected MapStack context; 80 protected ScreenStringRenderer screenStringRenderer; 81 82 public ScreenRenderer(Writer writer, MapStack context, ScreenStringRenderer screenStringRenderer) { 83 this.writer = writer; 84 this.context = context; 85 if (this.context == null) this.context = MapStack.create(); 86 this.screenStringRenderer = screenStringRenderer; 87 } 88 89 97 public String render(String combinedName) throws GeneralException, IOException , SAXException , ParserConfigurationException { 98 String resourceName = ScreenFactory.getResourceNameFromCombined(combinedName); 99 String screenName = ScreenFactory.getScreenNameFromCombined(combinedName); 100 this.render(resourceName, screenName); 101 return ""; 102 } 103 104 113 public String render(String resourceName, String screenName) throws GeneralException, IOException , SAXException , ParserConfigurationException { 114 ModelScreen modelScreen = ScreenFactory.getScreenFromLocation(resourceName, screenName); 115 modelScreen.renderScreenString(writer, context, screenStringRenderer); 116 return ""; 117 } 118 119 public ScreenStringRenderer getScreenStringRenderer() { 120 return this.screenStringRenderer; 121 } 122 123 public void populateBasicContext(Map parameters, GenericDelegator delegator, LocalDispatcher dispatcher, Security security, Locale locale, GenericValue userLogin) { 124 context.put("screens", this); 127 128 context.put("globalContext", context.standAloneStack()); 130 131 context.put("nullField", GenericEntity.NULL_FIELD); 133 134 context.put("availableLocales", UtilMisc.availableLocales()); 136 137 context.put("parameters", parameters); 138 context.put("delegator", delegator); 139 context.put("dispatcher", dispatcher); 140 context.put("security", security); 141 context.put("locale", locale); 142 context.put("userLogin", userLogin); 143 } 144 145 153 public void populateContextForRequest(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) { 154 HttpSession session = request.getSession(); 155 156 Set attrNamesToSkip = FastSet.newInstance(); 158 attrNamesToSkip.add("delegator"); 159 attrNamesToSkip.add("dispatcher"); 160 attrNamesToSkip.add("security"); 161 attrNamesToSkip.add("webSiteId"); 162 163 Map parameterMap = UtilHttp.getParameterMap(request); 164 Enumeration requestAttrNames = request.getAttributeNames(); 166 while (requestAttrNames.hasMoreElements()) { 167 String attrName = (String ) requestAttrNames.nextElement(); 168 Object attrValue = request.getAttribute(attrName); 169 170 final boolean preserveRequestParameters = false; 173 if (preserveRequestParameters) { 174 Object param = parameterMap.get(attrName); 175 if (param == null) { 176 parameterMap.put(attrName, attrValue); 177 } else if (param instanceof String && ((String ) param).length() == 0) { 178 parameterMap.put(attrName, attrValue); 180 } else { 181 Debug.logInfo("Found request attribute that conflicts with parameter name, leaving request parameter in place for name: " + attrName, module); 183 } 184 } else { 185 parameterMap.put(attrName, attrValue); 186 } 187 } 188 189 Enumeration sessionAttrNames = session.getAttributeNames(); 191 while (sessionAttrNames.hasMoreElements()) { 192 String attrName = (String ) sessionAttrNames.nextElement(); 193 if (attrNamesToSkip.contains(attrName)) continue; 194 Object attrValue = session.getAttribute(attrName); 195 Object param = parameterMap.get(attrName); 196 if (param == null) { 197 parameterMap.put(attrName, attrValue); 198 } else if (param instanceof String && ((String ) param).length() == 0) { 199 parameterMap.put(attrName, attrValue); 201 } else { 202 Debug.logInfo("Found session attribute that conflicts with parameter name, leaving request parameter in place for name: " + attrName, module); 204 } 205 } 206 207 Enumeration applicationAttrNames = servletContext.getAttributeNames(); 209 while (applicationAttrNames.hasMoreElements()) { 210 String attrName = (String ) applicationAttrNames.nextElement(); 211 if (attrNamesToSkip.contains(attrName)) continue; 212 Object param = parameterMap.get(attrName); 213 Object attrValue = servletContext.getAttribute(attrName); 214 if (Debug.verboseOn()) Debug.logVerbose("Getting parameter from application attrbute with name [" + attrName + "] and value [" + attrValue + "]", module); 215 if (param == null) { 216 parameterMap.put(attrName, attrValue); 217 } else if (param instanceof String && ((String ) param).length() == 0) { 218 parameterMap.put(attrName, attrValue); 220 } else { 221 Debug.logInfo("Found servlet context (application) attribute that conflicts with parameter name, leaving request parameter in place for name: " + attrName, module); 223 } 224 } 225 226 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 227 228 this.populateBasicContext(parameterMap, (GenericDelegator) request.getAttribute("delegator"), 229 (LocalDispatcher) request.getAttribute("dispatcher"), (Security) request.getAttribute("security"), 230 UtilHttp.getLocale(request), userLogin); 231 232 context.put("autoUserLogin", session.getAttribute("autoUserLogin")); 233 context.put("person", session.getAttribute("person")); 234 context.put("partyGroup", session.getAttribute("partyGroup")); 235 236 request.setAttribute("userLogin", userLogin); 238 239 241 context.put("formStringRenderer", new HtmlFormRenderer(request, response)); 243 244 context.put("request", request); 245 context.put("response", response); 246 context.put("session", session); 247 context.put("application", servletContext); 248 if (servletContext != null) { 249 String rootDir = (String ) context.get("rootDir"); 250 String webSiteId = (String ) context.get("webSiteId"); 251 String https = (String ) context.get("https"); 252 if (UtilValidate.isEmpty(rootDir)) { 253 rootDir = servletContext.getRealPath("/"); 254 context.put("rootDir", rootDir); 255 } 256 if (UtilValidate.isEmpty(webSiteId)) { 257 webSiteId = (String ) servletContext.getAttribute("webSiteId"); 258 context.put("webSiteId", webSiteId); 259 } 260 if (UtilValidate.isEmpty(https)) { 261 https = (String ) servletContext.getAttribute("https"); 262 context.put("https", https); 263 } 264 } 265 266 BeansWrapper wrapper = BeansWrapper.getDefaultInstance(); 268 context.put("sessionAttributes", new HttpSessionHashModel(session, wrapper)); 269 context.put("requestAttributes", new HttpRequestHashModel(request, wrapper)); 270 TaglibFactory JspTaglibs = new TaglibFactory(servletContext); 271 context.put("JspTaglibs", JspTaglibs); 272 context.put("requestParameters", UtilHttp.getParameterMap(request)); 273 274 context.put("page", FastMap.newInstance()); 276 277 context.put("controlPath", request.getAttribute("_CONTROL_PATH_")); 279 context.put("contextRoot", request.getAttribute("_CONTEXT_ROOT_")); 280 context.put("serverRoot", request.getAttribute("_SERVER_ROOT_URL_")); 281 context.put("checkLoginUrl", LoginWorker.makeLoginUrl(request, "checkLogin")); 282 String externalLoginKey = LoginWorker.getExternalLoginKey(request); 283 String externalKeyParam = externalLoginKey == null ? "" : "&externalLoginKey=" + externalLoginKey; 284 context.put("externalLoginKey", externalLoginKey); 285 context.put("externalKeyParam", externalKeyParam); 286 287 List eventMessageList = (List ) request.getAttribute("eventMessageList"); 289 if (eventMessageList == null) eventMessageList = new LinkedList (); 290 List errorMessageList = (List ) request.getAttribute("errorMessageList"); 291 if (errorMessageList == null) errorMessageList = new LinkedList (); 292 293 if (request.getAttribute("_EVENT_MESSAGE_") != null) { 294 eventMessageList.add(UtilFormatOut.replaceString((String ) request.getAttribute("_EVENT_MESSAGE_"), "\n", "<br/>")); 295 request.removeAttribute("_EVENT_MESSAGE_"); 296 } 297 if (request.getAttribute("_EVENT_MESSAGE_LIST_") != null) { 298 eventMessageList.addAll((List ) request.getAttribute("_EVENT_MESSAGE_LIST_")); 299 request.removeAttribute("_EVENT_MESSAGE_LIST_"); 300 } 301 if (request.getAttribute("_ERROR_MESSAGE_") != null) { 302 errorMessageList.add(UtilFormatOut.replaceString((String ) request.getAttribute("_ERROR_MESSAGE_"), "\n", "<br/>")); 303 request.removeAttribute("_ERROR_MESSAGE_"); 304 } 305 if (session.getAttribute("_ERROR_MESSAGE_") != null) { 306 errorMessageList.add(UtilFormatOut.replaceString((String ) session.getAttribute("_ERROR_MESSAGE_"), "\n", "<br/>")); 307 session.removeAttribute("_ERROR_MESSAGE_"); 308 } 309 if (request.getAttribute("_ERROR_MESSAGE_LIST_") != null) { 310 errorMessageList.addAll((List ) request.getAttribute("_ERROR_MESSAGE_LIST_")); 311 request.removeAttribute("_ERROR_MESSAGE_LIST_"); 312 } 313 context.put("eventMessageList", eventMessageList); 314 context.put("errorMessageList", errorMessageList); 315 316 if (request.getAttribute("serviceValidationException") != null) { 317 context.put("serviceValidationException", request.getAttribute("serviceValidationException")); 318 request.removeAttribute("serviceValidationException"); 319 } 320 321 if (errorMessageList.size() > 0) { 323 context.put("isError", Boolean.TRUE); 324 } else { 325 context.put("isError", Boolean.FALSE); 326 } 327 if ("true".equals((String ) parameterMap.get("isError"))) { 329 context.put("isError", Boolean.TRUE); 330 } 331 context.put("nowTimestamp", UtilDateTime.nowTimestamp()); 332 333 context.push(); 335 } 336 337 public Map getContext() { 338 return context; 339 } 340 341 public void populateContextForService(DispatchContext dctx, Map serviceContext) { 342 this.populateBasicContext(serviceContext, dctx.getDelegator(), dctx.getDispatcher(), dctx.getSecurity(), 343 (Locale ) serviceContext.get("locale"), (GenericValue) serviceContext.get("userLogin")); 344 } 345 } 346 | Popular Tags |