001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.driver.core;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021: import javax.servlet.ServletContext;
022:
023: import org.apache.pluto.driver.url.PortalURL;
024: import org.apache.pluto.driver.url.PortalURLParser;
025: import org.apache.pluto.driver.AttributeKeys;
026: import org.apache.pluto.driver.config.DriverConfiguration;
027:
028: /**
029: * Defines the context of the currentl portal request.
030: * Allows for the retrieval of the original request
031: * and response throughout the lifetime of the request.
032: *
033: * Provides a consistent interface for parsing/creating
034: * PortalURLs to the outside world.
035: *
036: */
037: public class PortalRequestContext {
038:
039: /**
040: * The attribute key to bind the portal environment instance to servlet
041: * request.
042: */
043: public final static String REQUEST_KEY = PortalRequestContext.class
044: .getName();
045:
046: /** The servletContext of execution. **/
047: private ServletContext servletContext;
048:
049: /** The incoming servlet request. */
050: private HttpServletRequest request;
051:
052: /** The incoming servlet response. */
053: private HttpServletResponse response;
054:
055: /** The requested portal URL. */
056: private PortalURL requestedPortalURL;
057:
058: // Constructor -------------------------------------------------------------
059:
060: /**
061: * Creates a PortalRequestContext instance.
062: * @param request the incoming servlet request.
063: * @param response the incoming servlet response.
064: */
065: public PortalRequestContext(ServletContext servletContext,
066: HttpServletRequest request, HttpServletResponse response) {
067: this .servletContext = servletContext;
068: this .request = request;
069: this .response = response;
070:
071: // Bind the instance to servlet request for later use.
072: request.setAttribute(REQUEST_KEY, this );
073: }
074:
075: /**
076: * Returns the portal environment from the servlet request. The portal
077: * envirionment instance is saved in the request scope.
078: * @param request the servlet request.
079: * @return the portal environment.
080: */
081: public static PortalRequestContext getContext(
082: HttpServletRequest request) {
083: return (PortalRequestContext) request.getAttribute(REQUEST_KEY);
084: }
085:
086: /**
087: * Returns the servlet request.
088: * @return the servlet request.
089: */
090: public HttpServletRequest getRequest() {
091: return request;
092: }
093:
094: /**
095: * Returns the servlet response.
096: * @return the servlet response.
097: */
098: public HttpServletResponse getResponse() {
099: return response;
100: }
101:
102: /**
103: * Returns the requested portal URL.
104: * @return the requested portal URL.
105: */
106: public PortalURL getRequestedPortalURL() {
107: if (requestedPortalURL == null) {
108: DriverConfiguration config = (DriverConfiguration) servletContext
109: .getAttribute(AttributeKeys.DRIVER_CONFIG);
110: PortalURLParser parser = config.getPortalUrlParser();
111: requestedPortalURL = parser.parse(request);
112: }
113: return requestedPortalURL;
114: }
115:
116: public PortalURL createPortalURL() {
117: return (PortalURL) getRequestedPortalURL().clone();
118: }
119: }
|