1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.chain.web.portlet; 18 19 20 import java.util.Collections; 21 import java.util.Map; 22 import javax.portlet.PortletContext; 23 import javax.portlet.PortletRequest; 24 import javax.portlet.PortletResponse; 25 import org.apache.commons.chain.web.WebContext; 26 27 28 /** 29 * <p>Concrete implementation of {@link WebContext} suitable for use in 30 * portlets. The abstract methods are mapped to the appropriate 31 * collections of the underlying portlet context, request, and response 32 * instances that are passed to the constructor (or the initialize method).</p> 33 * 34 * @author Craig R. McClanahan 35 * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $ 36 */ 37 38 public class PortletWebContext extends WebContext { 39 40 41 // ------------------------------------------------------------ Constructors 42 43 44 /** 45 * <p>Construct an uninitialized {@link PortletWebContext} instance.</p> 46 */ 47 public PortletWebContext() { 48 } 49 50 51 /** 52 * <p>Construct a {@link PortletWebContext} instance that is initialized 53 * with the specified Portlet API objects.</p> 54 * 55 * @param context The <code>PortletContext</code> for this web application 56 * @param request The <code>PortletRequest</code> for this request 57 * @param response The <code>PortletResponse</code> for this request 58 */ 59 public PortletWebContext(PortletContext context, 60 PortletRequest request, 61 PortletResponse response) { 62 63 initialize(context, request, response); 64 65 } 66 67 68 // ------------------------------------------------------ Instance Variables 69 70 71 /** 72 * <p>The lazily instantiated <code>Map</code> of application scope 73 * attributes.</p> 74 */ 75 private Map applicationScope = null; 76 77 78 /** 79 * <p>The <code>PortletContext</code> for this web application.</p> 80 */ 81 protected PortletContext context = null; 82 83 84 /** 85 * <p>The lazily instantiated <code>Map</code> of header name-value 86 * combinations (immutable).</p> 87 */ 88 private Map header = null; 89 90 91 /** 92 * <p>The lazily instantitated <code>Map</code> of header name-values 93 * combinations (immutable).</p> 94 */ 95 private Map headerValues = null; 96 97 98 /** 99 * <p>The lazily instantiated <code>Map</code> of context initialization 100 * parameters.</p> 101 */ 102 private Map initParam = null; 103 104 105 /** 106 * <p>The lazily instantiated <code>Map</code> of request 107 * parameter name-value.</p> 108 */ 109 private Map param = null; 110 111 112 /** 113 * <p>The lazily instantiated <code>Map</code> of request 114 * parameter name-values.</p> 115 */ 116 private Map paramValues = null; 117 118 119 /** 120 * <p>The <code>PortletRequest</code> for this request.</p> 121 */ 122 protected PortletRequest request = null; 123 124 125 /** 126 * <p>The lazily instantiated <code>Map</code> of request scope 127 * attributes.</p> 128 */ 129 private Map requestScope = null; 130 131 132 /** 133 * <p>The <code>PortletResponse</code> for this request.</p> 134 */ 135 protected PortletResponse response = null; 136 137 138 /** 139 * <p>The lazily instantiated <code>Map</code> of session scope 140 * attributes.</p> 141 */ 142 private Map sessionScope = null; 143 144 145 // ---------------------------------------------------------- Public Methods 146 147 148 /** 149 * <p>Return the {@link PortletContext} for this context.</p> 150 * 151 * @return The <code>PortletContext</code> for this request 152 */ 153 public PortletContext getContext() { 154 155 return (this.context); 156 157 } 158 159 160 /** 161 * <p>Return the {@link PortletRequest} for this context.</p> 162 * 163 * @return The <code>PortletRequest</code> for this context. 164 */ 165 public PortletRequest getRequest() { 166 167 return (this.request); 168 169 } 170 171 172 /** 173 * <p>Return the {@link PortletResponse} for this context.</p> 174 * 175 * @return The <code>PortletResponse</code> for this context. 176 */ 177 public PortletResponse getResponse() { 178 179 return (this.response); 180 181 } 182 183 184 /** 185 * <p>Initialize (or reinitialize) this {@link PortletWebContext} instance 186 * for the specified Portlet API objects.</p> 187 * 188 * @param context The <code>PortletContext</code> for this web application 189 * @param request The <code>PortletRequest</code> for this request 190 * @param response The <code>PortletResponse</code> for this request 191 */ 192 public void initialize(PortletContext context, 193 PortletRequest request, 194 PortletResponse response) { 195 196 // Save the specified Portlet API object references 197 this.context = context; 198 this.request = request; 199 this.response = response; 200 201 // Perform other setup as needed 202 203 } 204 205 206 /** 207 * <p>Release references to allocated resources acquired in 208 * <code>initialize()</code> of via subsequent processing. After this 209 * method is called, subsequent calls to any other method than 210 * <code>initialize()</code> will return undefined results.</p> 211 */ 212 public void release() { 213 214 // Release references to allocated collections 215 applicationScope = null; 216 header = null; 217 headerValues = null; 218 initParam = null; 219 param = null; 220 paramValues = null; 221 requestScope = null; 222 sessionScope = null; 223 224 // Release references to Portlet API objects 225 context = null; 226 request = null; 227 response = null; 228 229 } 230 231 232 233 // ------------------------------------------------------ WebContext Methods 234 235 236 /** 237 * See the {@link WebContext}'s Javadoc. 238 * 239 * @return Application scope Map. 240 */ 241 public Map getApplicationScope() { 242 243 if ((applicationScope == null) && (context != null)) { 244 applicationScope = new PortletApplicationScopeMap(context); 245 } 246 return (applicationScope); 247 248 } 249 250 251 /** 252 * See the {@link WebContext}'s Javadoc. 253 * 254 * @return Header values Map. 255 */ 256 public Map getHeader() { 257 258 if ((header == null) && (request != null)) { 259 // header = new PortletHeaderMap(request); 260 header = Collections.EMPTY_MAP; 261 } 262 return (header); 263 264 } 265 266 267 /** 268 * See the {@link WebContext}'s Javadoc. 269 * 270 * @return Header values Map. 271 */ 272 public Map getHeaderValues() { 273 274 if ((headerValues == null) && (request != null)) { 275 // headerValues = new PortletHeaderValuesMap(request); 276 headerValues = Collections.EMPTY_MAP; 277 } 278 return (headerValues); 279 280 } 281 282 283 /** 284 * See the {@link WebContext}'s Javadoc. 285 * 286 * @return Initialization parameter Map. 287 */ 288 public Map getInitParam() { 289 290 if ((initParam == null) && (context != null)) { 291 initParam = new PortletInitParamMap(context); 292 } 293 return (initParam); 294 295 } 296 297 298 /** 299 * See the {@link WebContext}'s Javadoc. 300 * 301 * @return Request parameter Map. 302 */ 303 public Map getParam() { 304 305 if ((param == null) && (request != null)) { 306 param = new PortletParamMap(request); 307 } 308 return (param); 309 310 } 311 312 313 /** 314 * See the {@link WebContext}'s Javadoc. 315 * 316 * @return Request parameter Map. 317 */ 318 public Map getParamValues() { 319 320 if ((paramValues == null) && (request != null)) { 321 paramValues = new PortletParamValuesMap(request); 322 } 323 return (paramValues); 324 325 } 326 327 328 /** 329 * Returns an empty Map - portlets don't support Cookies. 330 * 331 * @return An empty Map. 332 * @since Chain 1.1 333 */ 334 public Map getCookies() { 335 336 return Collections.EMPTY_MAP; 337 338 } 339 340 341 /** 342 * See the {@link WebContext}'s Javadoc. 343 * 344 * @return Request scope Map. 345 */ 346 public Map getRequestScope() { 347 348 if ((requestScope == null) && (request != null)) { 349 requestScope = new PortletRequestScopeMap(request); 350 } 351 return (requestScope); 352 353 } 354 355 356 /** 357 * See the {@link WebContext}'s Javadoc. 358 * 359 * @return Session scope Map. 360 */ 361 public Map getSessionScope() { 362 363 if ((sessionScope == null) && (request != null)) { 364 sessionScope = 365 new PortletSessionScopeMap(request); 366 } 367 return (sessionScope); 368 369 } 370 371 372 373 }