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.jetspeed.dispatcher;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.RequestDispatcher;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.portlet.PortletRequestDispatcher;
026: import javax.portlet.RenderResponse;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.PortletException;
029:
030: import org.apache.jetspeed.container.PortletDispatcherIncludeAware;
031: import org.apache.pluto.core.impl.RenderRequestImpl;
032: import org.apache.pluto.core.impl.RenderResponseImpl;
033:
034: /**
035: * Implements the Portlet API Request Dispatcher to dispatch to portlets
036: *
037: * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
038: * @version $Id: JetspeedRequestDispatcher.java 516448 2007-03-09 16:25:47Z ate $
039: */
040: public class JetspeedRequestDispatcher implements
041: PortletRequestDispatcher {
042: private RequestDispatcher requestDispatcher;
043:
044: public JetspeedRequestDispatcher(RequestDispatcher requestDispatcher) {
045: if (requestDispatcher == null) {
046: throw new IllegalArgumentException(
047: "RequestDispatcher cannot be null for JetspeedRequestDispatcher.");
048: }
049: this .requestDispatcher = requestDispatcher;
050: }
051:
052: // portlet-only implementation
053:
054: public void include(RenderRequest request, RenderResponse response)
055: throws PortletException, java.io.IOException {
056: HttpServletRequest servletRequest = null;
057: HttpServletResponse servletResponse = null;
058: try {
059: servletRequest = (HttpServletRequest) ((RenderRequestImpl) request)
060: .getRequest();
061: servletResponse = (HttpServletResponse) ((RenderResponseImpl) response)
062: .getResponse();
063:
064: if (servletRequest instanceof PortletDispatcherIncludeAware) {
065: ((PortletDispatcherIncludeAware) servletRequest)
066: .setPortletDispatcherIncluded(true);
067: }
068: if (servletResponse instanceof PortletDispatcherIncludeAware) {
069: ((PortletDispatcherIncludeAware) servletResponse)
070: .setPortletDispatcherIncluded(true);
071: }
072:
073: this .requestDispatcher.include(servletRequest,
074: servletResponse);
075:
076: } catch (RuntimeException re) {
077: // PLT.16.3.4 cxlii:
078: // RuntimeExceptions must be propagated back
079: throw re;
080: } catch (IOException ioe) {
081: // PLT.16.3.4 cxlii:
082: // IOExceptions must be propagated back
083: throw ioe;
084: } catch (Exception e) {
085: // PLT.16.3.4 cxliii:
086: // All other exceptions, including ServletExceptions must be wrapped in a PortletException
087: // with the root cause set to the original exception before propagated back
088:
089: Throwable rootCause = null;
090: if (e instanceof ServletException) {
091: rootCause = ((ServletException) e).getRootCause();
092: } else {
093: rootCause = e.getCause();
094: }
095: throw new PortletException(rootCause != null ? rootCause
096: : e);
097: } finally {
098: if (servletRequest != null
099: && servletRequest instanceof PortletDispatcherIncludeAware) {
100: ((PortletDispatcherIncludeAware) servletRequest)
101: .setPortletDispatcherIncluded(false);
102: }
103: if (servletResponse != null
104: && servletResponse instanceof PortletDispatcherIncludeAware) {
105: ((PortletDispatcherIncludeAware) servletResponse)
106: .setPortletDispatcherIncluded(false);
107: }
108:
109: }
110: }
111: }
|