1 24 package org.riotfamily.components.context; 25 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletRequestWrapper ; 35 36 import org.riotfamily.common.collection.IteratorEnumeration; 37 import org.springframework.util.Assert; 38 39 public class PartialPageRequest extends HttpServletRequestWrapper { 40 41 private static final String CONTEXT_KEY = 42 PartialPageRequest.class.getName() + ".contextKey"; 43 44 private PageRequestContext context; 45 46 private HashMap attributes = new HashMap (); 47 48 public PartialPageRequest(HttpServletRequest request, 49 PageRequestContext context) { 50 51 super(request); 52 this.context = context; 53 } 54 55 public String getMethod() { 56 return context.getMethod(); 57 } 58 59 public String getPathInfo() { 60 return context.getPathInfo(); 61 } 62 63 public String getServletPath() { 64 return context.getServletPath(); 65 } 66 67 public String getQueryString() { 68 return context.getQueryString(); 69 } 70 71 public String getRequestURI() { 72 return context.getRequestURI(); 73 } 74 75 public StringBuffer getRequestURL() { 76 StringBuffer url = new StringBuffer (getScheme()); 77 url.append("://").append(getServerName()).append(':').append(getServerPort()); 78 url.append(getRequestURI()); 79 return url; 80 } 81 82 public String getPathTranslated() { 83 return (getPathInfo() != null ? getRealPath(getPathInfo()) : null); 84 } 85 86 public String getParameter(String name) { 87 Assert.notNull(name, "Parameter name must not be null"); 88 String [] arr = (String []) getParameterMap().get(name); 89 return (arr != null && arr.length > 0 ? arr[0] : null); 90 } 91 92 public Enumeration getParameterNames() { 93 return Collections.enumeration(getParameterMap().keySet()); 94 } 95 96 public String [] getParameterValues(String name) { 97 Assert.notNull(name, "Parameter name must not be null"); 98 return (String []) getParameterMap().get(name); 99 } 100 101 public Map getParameterMap() { 102 HashMap params = new HashMap (); 103 params.putAll(context.getParameters()); 104 params.putAll(super.getParameterMap()); 105 return params; 106 } 107 108 public Object getAttribute(String name) { 109 if (name.equals(CONTEXT_KEY)) { 110 return context.getKey(); 111 } 112 if (name.startsWith("javax.servlet.")) { 113 return super.getAttribute(name); 114 } 115 if (attributes.containsKey(name)) { 116 return attributes.get(name); 117 } 118 return context.getAttributes().get(name); 119 } 120 121 public Enumeration getAttributeNames() { 122 HashSet names = new HashSet (); 123 names.addAll(context.getAttributes().keySet()); 124 Iterator it = attributes.entrySet().iterator(); 125 while (it.hasNext()) { 126 Map.Entry attribute = (Map.Entry ) it.next(); 127 if (attribute.getValue() != null) { 128 names.add(attribute.getKey()); 129 } 130 else { 131 names.remove(attribute.getKey()); 134 } 135 } 136 return new IteratorEnumeration(names.iterator()); 137 } 138 139 public void setAttribute(String name, Object value) { 140 Assert.notNull(name, "Attribute name must not be null"); 141 attributes.put(name, value); 142 } 143 144 public void removeAttribute(String name) { 145 Assert.notNull(name, "Attribute name must not be null"); 146 attributes.put(name, null); 149 } 150 151 public static boolean isWrapped(HttpServletRequest request) { 152 return request.getAttribute(CONTEXT_KEY) != null; 153 } 154 155 public static boolean isWrapped(HttpServletRequest request, 156 Object contextKey) { 157 158 return contextKey.equals(request.getAttribute(CONTEXT_KEY)); 159 } 160 161 } | Popular Tags |