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: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.scoping.internal;
020:
021: import javax.servlet.RequestDispatcher;
022: import javax.servlet.ServletRequest;
023: import javax.servlet.ServletResponse;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import java.io.IOException;
028:
029: import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
030: import org.apache.beehive.netui.util.logging.Logger;
031:
032: /**
033: * A request dispatcher that doesn't actually forward (but keeps track of the attempted
034: * forward), and which does some extra work to do server includes into our ScopedRequest
035: * and ScopedResponse.
036: *
037: * @see ScopedRequestImpl
038: * @see ScopedResponseImpl
039: */
040: public class ScopedRequestDispatcher implements RequestDispatcher {
041: private String _uri;
042:
043: private static final String REQUEST_URI_INCLUDE = "javax.servlet.include.request_uri";
044:
045: private static final Logger logger = Logger
046: .getInstance(ScopedRequestDispatcher.class);
047:
048: /**
049: * Constructor.
050: *
051: * @param uri the URI to which we'll "forward" or include.
052: */
053: public ScopedRequestDispatcher(String uri) {
054: _uri = uri;
055: }
056:
057: /**
058: * Does not actually cause a server forward of the request, but informs the ScopedRequest
059: * object that a forward was attempted for a particular URI.
060: *
061: * @param request the ScopedRequest, or a wrapper (ServletRequestWrapper) around it.
062: * @param response the ScopedResponse, or a wrapper (ServletResponseWrapper) around it.
063: */
064: public void forward(ServletRequest request, ServletResponse response)
065: throws ServletException, IOException {
066: ScopedRequestImpl scopedRequest = (ScopedRequestImpl) ScopedServletUtils
067: .unwrapRequest(request);
068: assert scopedRequest != null : request.getClass().getName();
069: scopedRequest.setForwardedURI(_uri);
070: scopedRequest.doForward();
071: }
072:
073: /**
074: * Does a server include of the stored URI into the given ScopedRequest and ScopedResponse.
075: *
076: * @param request the ScopedRequest, or a wrapper (ServletRequestWrapper) around it.
077: * @param response the ScopedResponse, or a wrapper (ServletResponseWrapper) around it.
078: */
079: public void include(ServletRequest request, ServletResponse response)
080: throws ServletException, IOException {
081: assert request instanceof HttpServletRequest : request
082: .getClass().getName();
083: HttpServletRequest httpRequest = (HttpServletRequest) request;
084:
085: //
086: // First, unwrap the request and response, looking for our ScopedRequest and ScopedResponse.
087: //
088: HttpServletRequest outerRequest = ScopedServletUtils
089: .getOuterRequest(httpRequest);
090:
091: //
092: // Need to set the "javax.servlet.include.request_uri" attribute on the outer request
093: // before forwarding with a request dispatcher. This attribute is used to keep track of
094: // the included URI.
095: //
096: outerRequest.setAttribute(REQUEST_URI_INCLUDE, httpRequest
097: .getRequestURI());
098:
099: if (logger.isDebugEnabled()) {
100: logger.debug("Delegating to RequestDispatcher for URI "
101: + _uri);
102: }
103:
104: try {
105: RequestDispatcher realDispatcher = outerRequest
106: .getRequestDispatcher(_uri);
107:
108: if (realDispatcher == null) {
109: assert response instanceof HttpServletResponse : response
110: .getClass().getName();
111: ((HttpServletResponse) response)
112: .setStatus(HttpServletResponse.SC_NOT_FOUND);
113: logger.error("Could not get RequestDispatcher for URI "
114: + _uri);
115: } else {
116: realDispatcher.include(request, response);
117: }
118: } catch (ServletException e) {
119: logger.error(
120: "Exception during RequestDispatcher.include().", e
121: .getRootCause());
122:
123: throw e;
124: }
125: }
126: }
|