1 16 19 20 package org.apache.pluto.core.impl; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.UnsupportedEncodingException ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javax.portlet.PortletResponse; 29 import javax.servlet.ServletOutputStream ; 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.apache.pluto.core.InternalPortletResponse; 33 import org.apache.pluto.om.window.PortletWindow; 34 import org.apache.pluto.services.information.InformationProviderAccess; 35 import org.apache.pluto.services.information.ResourceURLProvider; 36 import org.apache.pluto.services.property.PropertyManager; 37 import org.apache.pluto.util.PrintWriterServletOutputStream; 38 39 public abstract class PortletResponseImpl extends javax.servlet.http.HttpServletResponseWrapper 40 implements InternalPortletResponse, PortletResponse 41 { 42 43 PortletWindow portletWindow; 44 45 49 private javax.servlet.http.HttpServletRequest webModuleServletRequest; 50 51 private boolean usingWriter; 52 private boolean usingStream; 53 54 private ServletOutputStream wrappedWriter; 55 private Map properties; 56 57 60 private boolean included; 61 62 public PortletResponseImpl(PortletWindow portletWindow, 63 javax.servlet.http.HttpServletRequest servletRequest, 64 javax.servlet.http.HttpServletResponse servletResponse) 65 { 66 super(servletResponse); 67 68 this.webModuleServletRequest = servletRequest; 69 this.portletWindow = portletWindow; 70 } 71 72 public void addProperty(String key, String value) 74 { 75 if (key == null) 76 { 77 throw new IllegalArgumentException ("Property key == null"); 78 } 79 80 Map props = getProperties(); 81 82 String [] oldValues = (String []) props.get(key); 83 String [] newValues = null; 84 if (oldValues == null) 85 { 86 newValues = new String []{value}; 87 } 88 else 89 { 90 int len = oldValues.length; 91 newValues = new String [len+1]; 92 System.arraycopy(oldValues, 0, newValues, 0, len); 93 newValues[len] = value; 94 } 95 props.put(key, newValues); 96 97 PropertyManager.setResponseProperties(portletWindow, this.getHttpServletRequest(), _getHttpServletResponse(), props); 98 99 } 100 101 public void setProperty(String key, String value) 102 { 103 if (key == null) 104 { 105 throw new IllegalArgumentException ("Property key == null"); 106 } 107 108 Map props = getProperties(); 109 110 String [] newValues = new String []{value}; 111 props.put(key, newValues); 112 113 PropertyManager.setResponseProperties(portletWindow, this.getHttpServletRequest(), _getHttpServletResponse(), props); 114 } 115 116 public String encodeURL(String path) 117 { 118 if (path.indexOf("://") == -1 && !path.startsWith("/")) 119 { 120 throw new IllegalArgumentException ("only absolute URLs or full path URIs are allowed"); 121 } 122 ResourceURLProvider provider = InformationProviderAccess.getDynamicProvider(getHttpServletRequest()).getResourceURLProvider(portletWindow); 123 if (path.indexOf("://") != -1) 124 { 125 provider.setAbsoluteURL(path); 126 } 127 else 128 { 129 provider.setFullPath(path); 130 } 131 return this._getHttpServletResponse().encodeURL(provider.toString()); 132 } 133 135 public void lateInit(javax.servlet.http.HttpServletRequest webModuleServletRequest, 137 javax.servlet.http.HttpServletResponse webModuleServletResponse) 138 { 139 this.webModuleServletRequest = webModuleServletRequest; 140 this.setResponse(webModuleServletResponse); 141 } 142 144 protected javax.servlet.http.HttpServletResponse _getHttpServletResponse() 146 { 147 return(javax.servlet.http.HttpServletResponse ) super.getResponse(); 148 } 149 150 protected javax.servlet.http.HttpServletRequest getHttpServletRequest() 151 { 152 return webModuleServletRequest; 153 } 154 155 private Map getProperties() { 156 if (properties == null) 157 properties = new HashMap (); 158 return properties; 159 } 160 162 public void addCookie(javax.servlet.http.Cookie cookie) 166 { 167 this._getHttpServletResponse().addCookie(cookie); 168 } 169 170 public boolean containsHeader(String name) 171 { 172 return this._getHttpServletResponse().containsHeader(name); 173 } 174 175 178 public String encodeRedirectUrl(String url) 179 { 180 return included ? null : this._getHttpServletResponse().encodeRedirectUrl(url); 181 } 182 183 public String encodeRedirectURL(String url) 184 { 185 return included ? null : this._getHttpServletResponse().encodeRedirectURL(url); 186 } 187 188 public void sendRedirect(String location) throws java.io.IOException 189 { 190 this._getHttpServletResponse().sendRedirect(location); 191 } 192 193 public void setDateHeader(String name, long date) 194 { 195 this._getHttpServletResponse().setDateHeader(name,date); 196 } 197 198 public void sendError(int sc, String msg) throws java.io.IOException 199 { 200 this._getHttpServletResponse().sendError(sc,msg); 201 } 202 203 public void sendError(int sc) throws java.io.IOException 204 { 205 this._getHttpServletResponse().sendError(sc); 206 } 207 208 public void addHeader(String name, String value) 209 { 210 this._getHttpServletResponse().addHeader(name,value); 211 } 212 213 public void setIntHeader(String name, int value) 214 { 215 this._getHttpServletResponse().setIntHeader(name,value); 216 } 217 218 public void addDateHeader(String name, long date) 219 { 220 this._getHttpServletResponse().addDateHeader(name,date); 221 } 222 223 public void setHeader(String name, String value) 224 { 225 this._getHttpServletResponse().setHeader(name,value); 226 } 227 228 public void setStatus(int sc) 229 { 230 this._getHttpServletResponse().setStatus(sc); 231 } 232 233 240 public void setStatus(int sc, String sm) 241 { 242 this._getHttpServletResponse().setStatus(sc,sm); 243 } 244 245 public void addIntHeader(String name, int value) 246 { 247 this._getHttpServletResponse().addIntHeader(name,value); 248 } 249 250 public void setContentLength(int len) 251 { 252 this._getHttpServletResponse().setContentLength(len); 253 } 254 255 public String encodeUrl(String url) 256 { 257 return this.encodeURL(url); 258 } 259 260 public void setLocale(java.util.Locale loc) 261 { 262 this._getHttpServletResponse().setLocale(loc); 263 } 264 265 public ServletOutputStream getOutputStream() throws IllegalStateException , IOException { 266 if (usingWriter) 267 { 268 throw new IllegalStateException ("getPortletOutputStream can't be used after getWriter was invoked"); 269 } 270 271 if (wrappedWriter == null) 272 { 273 wrappedWriter = new PrintWriterServletOutputStream( 274 _getHttpServletResponse().getWriter(), 275 _getHttpServletResponse().getCharacterEncoding() 276 ); 277 } 278 279 usingStream = true; 280 281 return wrappedWriter; 282 } 283 284 public PrintWriter getWriter() throws UnsupportedEncodingException , IllegalStateException , IOException { 285 if (usingStream) 286 { 287 throw new IllegalStateException ("getWriter can't be used after getOutputStream was invoked"); 288 } 289 290 usingWriter = true; 291 292 return _getHttpServletResponse().getWriter(); 293 } 294 295 public PortletWindow getInternalPortletWindow() 297 { 298 return portletWindow; 299 } 300 301 303 HttpServletRequest getHttpDServletRequest() 304 { 305 return webModuleServletRequest; 306 } 307 308 public void setIncluded(boolean included) 309 { 310 this.included = included; 311 } 312 313 public boolean isIncluded() 314 { 315 return included; 316 } 317 } 318 | Popular Tags |