1 13 package com.tonbeller.wcf.component; 14 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpSession ; 22 23 import com.tonbeller.wcf.controller.RequestContext; 24 25 33 public class RendererParameters { 34 private static final String WEBKEY = RendererParameters.class.getName(); 35 36 public static final String MODE = "mode"; 37 public static final String EXCEL = "excel"; 38 public static final String PRINT = "print"; 39 40 41 public static boolean isRenderActions(RequestContext context) { 42 return isRenderActions(context.getRequest()); 43 } 44 45 public static boolean isExcelMode(RequestContext context) { 46 return isExcelMode(context.getRequest()); 47 } 48 49 public static boolean isPrintMode(RequestContext context) { 50 return isPrintMode(context.getRequest()); 51 } 52 53 54 public static boolean isRenderActions(HttpServletRequest request) { 55 return getParameterMap(request).get(MODE) == null; 56 } 57 58 public static boolean isExcelMode(HttpServletRequest request) { 59 return EXCEL.equals(getParameterMap(request).get(MODE)); 60 } 61 62 public static boolean isPrintMode(HttpServletRequest request) { 63 return PRINT.equals(getParameterMap(request).get(MODE)); 64 } 65 66 public static void setParameter(HttpServletRequest req, String name, Object value, String scope) { 67 Map map = getMap(req, scope, true); 68 map.put(name, value); 69 } 70 71 public static void removeParameter(HttpServletRequest req, String name, Object value, String scope) { 72 Map map = getMap(req, scope, false); 73 if (map != null) 74 map.remove(name); 75 } 76 77 private static Map getMap(HttpServletRequest req, String scope, boolean create) { 78 if ("request".equals(scope)) 79 return getRequestMap(req, create); 80 if ("session".equals(scope)) 81 return getSessionMap(req, create); 82 if ("application".equals(scope)) 83 return getContextMap(req, create); 84 throw new IllegalArgumentException ("scope must be one of 'request', 'session', 'application'"); 85 86 } 87 88 private static Map getRequestMap(HttpServletRequest req, boolean create) { 89 Map map = (Map ) req.getAttribute(WEBKEY); 90 if (map != null || !create) 91 return map; 92 map = new HashMap (); 93 req.setAttribute(WEBKEY, map); 94 return map; 95 } 96 97 private static Map getSessionMap(HttpServletRequest req, boolean create) { 98 HttpSession session = req.getSession(true); 99 Map map = (Map ) session.getAttribute(WEBKEY); 100 if (map != null || !create) 101 return map; 102 map = new HashMap (); 103 session.setAttribute(WEBKEY, map); 104 return map; 105 } 106 107 private static Map getContextMap(HttpServletRequest req, boolean create) { 108 ServletContext context = req.getSession(true).getServletContext(); 109 Map map = (Map ) context.getAttribute(WEBKEY); 110 if (map != null || !create) 111 return map; 112 map = new HashMap (); 113 context.setAttribute(WEBKEY, map); 114 return map; 115 } 116 117 120 public static Map getParameterMap(HttpServletRequest request) { 121 HttpSession session = request.getSession(true); 122 ServletContext context = session.getServletContext(); 123 124 Map map = new HashMap (); 125 Map m = (Map ) context.getAttribute(WEBKEY); 126 if (m != null) 127 map.putAll(m); 128 m = (Map ) session.getAttribute(WEBKEY); 129 if (m != null) 130 map.putAll(m); 131 m = (Map ) request.getAttribute(WEBKEY); 132 if (m != null) 133 map.putAll(m); 134 return Collections.unmodifiableMap(map); 135 } 136 137 } | Popular Tags |