1 /* 2 * $Id: JspTilesRequestContext.java 736275 2009-01-21 09:58:20Z apetrelli $ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 package org.apache.tiles.jsp.context; 22 23 import org.apache.tiles.context.TilesRequestContext; 24 import org.apache.tiles.context.TilesRequestContextWrapper; 25 import org.apache.tiles.servlet.context.ServletTilesRequestContext; 26 import org.apache.tiles.servlet.context.ServletUtil; 27 28 import javax.servlet.ServletContext; 29 import javax.servlet.ServletException; 30 import javax.servlet.http.HttpServletRequest; 31 import javax.servlet.http.HttpServletResponse; 32 import javax.servlet.jsp.PageContext; 33 import java.io.IOException; 34 import java.io.PrintWriter; 35 import java.io.Writer; 36 37 /** 38 * Context implementation used for executing tiles within a 39 * jsp tag library. 40 * 41 * @version $Rev: 736275 $ $Date: 2009-01-21 10:58:20 +0100 (mer, 21 gen 2009) $ 42 */ 43 public class JspTilesRequestContext extends TilesRequestContextWrapper 44 implements TilesRequestContext { 45 46 /** 47 * The current page context. 48 */ 49 private PageContext pageContext; 50 51 /** 52 * The writer response to use. 53 */ 54 private JspWriterResponse response; 55 56 /** 57 * The request objects, lazily initialized. 58 */ 59 private Object[] requestObjects; 60 61 /** 62 * Constructor. 63 * 64 * @param enclosedRequest The request that is wrapped here. 65 * @param pageContext The page context to use. 66 */ 67 public JspTilesRequestContext(TilesRequestContext enclosedRequest, 68 PageContext pageContext) { 69 super(enclosedRequest); 70 this.pageContext = pageContext; 71 } 72 73 /** 74 * Constructor. 75 * 76 * @param context The servlet context to use. 77 * @param pageContext The page context to use. 78 * @deprecated Use 79 * {@link #JspTilesRequestContext(TilesRequestContext, PageContext)}. 80 */ 81 @Deprecated 82 public JspTilesRequestContext(ServletContext context, PageContext pageContext) { 83 this(new ServletTilesRequestContext(context, 84 (HttpServletRequest) pageContext.getRequest(), 85 (HttpServletResponse) pageContext.getResponse()), pageContext); 86 } 87 88 /** 89 * Dispatches a path. In fact it "includes" it! 90 * 91 * @param path The path to dispatch to. 92 * @throws IOException If something goes wrong during dispatching. 93 * @see org.apache.tiles.servlet.context.ServletTilesRequestContext#dispatch(java.lang.String) 94 */ 95 public void dispatch(String path) throws IOException { 96 include(path); 97 } 98 99 /** {@inheritDoc} */ 100 public void include(String path) throws IOException { 101 JspUtil.setForceInclude(pageContext, true); 102 try { 103 pageContext.include(path, false); 104 } catch (ServletException e) { 105 throw ServletUtil.wrapServletException(e, "JSPException including path '" 106 + path + "'."); 107 } 108 } 109 110 /** {@inheritDoc} */ 111 @Override 112 public PrintWriter getPrintWriter() throws IOException { 113 return new JspPrintWriterAdapter(pageContext.getOut()); 114 } 115 116 /** {@inheritDoc} */ 117 @Override 118 public Writer getWriter() throws IOException { 119 return pageContext.getOut(); 120 } 121 122 /** {@inheritDoc} */ 123 @Override 124 public Object[] getRequestObjects() { 125 if (requestObjects == null) { 126 requestObjects = new Object[1]; 127 requestObjects[0] = pageContext; 128 } 129 return requestObjects; 130 } 131 132 /** 133 * Returns the page context that originated the request. 134 * 135 * @return The page context. 136 */ 137 public PageContext getPageContext() { 138 return pageContext; 139 } 140 141 /** 142 * Returns the response object, obtained by the JSP page context. The print 143 * writer will use the object obtained by {@link PageContext#getOut()}. 144 * 145 * @return The response object. 146 * @deprecated Use {@link #getPageContext()} or {@link #getPrintWriter()}. 147 */ 148 @Deprecated 149 public HttpServletResponse getResponse() { 150 if (response == null) { 151 response = new JspWriterResponse(pageContext); 152 } 153 return response; 154 } 155 156 }