001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.theme;
023:
024: import org.jboss.portal.server.servlet.FilterCommand;
025: import org.jboss.portal.theme.render.RendererContext;
026: import org.jboss.portal.theme.render.renderer.PageRendererContext;
027: import org.jboss.portal.web.RequestDispatchCallback;
028: import org.jboss.portal.web.ServletContextDispatcher;
029:
030: import javax.servlet.RequestDispatcher;
031: import javax.servlet.ServletContext;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletRequestWrapper;
035: import javax.servlet.http.HttpServletResponse;
036: import java.io.IOException;
037:
038: /**
039: * Dispatches the request to the target layout. The major side effect is to change the context path returned by the
040: * request to the value returned by <code>PortalLayout#getContextPath()</code> so the layout can safely use the
041: * getContextPath in order to designates resources located in the same web application.
042: *
043: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
044: * @version $Revision: 8784 $
045: */
046: public final class LayoutDispatcher implements RequestDispatchCallback {
047:
048: private final PageRendererContext markupResult;
049: private final RendererContext rendererContext;
050: private final String layoutURI;
051: private final LayoutInfo layoutInfo;
052:
053: /**
054: * @param rendererContext
055: * @throws IllegalArgumentException if the layout is null
056: */
057: public LayoutDispatcher(RendererContext rendererContext,
058: PageRendererContext result, String layoutURI,
059: LayoutInfo layoutInfo) throws IllegalArgumentException {
060:
061: if (result == null) {
062: throw new IllegalArgumentException(
063: "No null response allowed here");
064: }
065:
066: if (rendererContext == null) {
067: throw new IllegalArgumentException(
068: "No render context provided");
069: }
070: if (layoutURI == null) {
071: throw new IllegalArgumentException(
072: "No null layout allowed here");
073: }
074:
075: this .markupResult = result;
076: this .rendererContext = rendererContext;
077: this .layoutURI = layoutURI;
078: this .layoutInfo = layoutInfo;
079: }
080:
081: public void include() throws IOException, ServletException {
082: try {
083: ServletContextDispatcher dispatcher = rendererContext
084: .getDispatcher();
085: dispatcher.include(layoutInfo.getServletContext(), this ,
086: null);
087: } catch (Exception e) {
088: e.printStackTrace();
089: }
090: }
091:
092: public Object doCallback(ServletContext dispatchedServletContext,
093: HttpServletRequest dispatchedRequest,
094: HttpServletResponse dispatchedResponse, Object handback)
095: throws ServletException, IOException {
096: try {
097: RequestDispatcher dispatcher = dispatchedServletContext
098: .getRequestDispatcher(layoutURI);
099:
100: //
101: dispatchedRequest
102: .setAttribute(LayoutConstants.ATTR_RENDERCONTEXT,
103: rendererContext);
104: dispatchedRequest.setAttribute(FilterCommand.REQ_ATT_KEY,
105: this );
106: dispatchedRequest.setAttribute(LayoutConstants.ATTR_PAGE,
107: markupResult);
108:
109: //
110: HttpServletRequest requestWrapper = new HttpServletRequestWrapper(
111: dispatchedRequest) {
112: public String getContextPath() {
113: return layoutInfo.getContextPath();
114: }
115: };
116:
117: //
118: dispatcher.include(requestWrapper, dispatchedResponse);
119: } finally {
120: dispatchedRequest
121: .removeAttribute(LayoutConstants.ATTR_PAGE);
122: dispatchedRequest
123: .removeAttribute(LayoutConstants.ATTR_RENDERCONTEXT);
124: }
125:
126: //
127: return null;
128: }
129: }
|