1 /* 2 * $Id: VelocityTilesRequestContext.java 769961 2009-04-29 22:07:34Z 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 22 package org.apache.tiles.velocity.context; 23 24 import java.io.IOException; 25 import java.io.PrintWriter; 26 import java.io.Writer; 27 28 import javax.servlet.RequestDispatcher; 29 import javax.servlet.ServletException; 30 import javax.servlet.http.HttpServletRequest; 31 import javax.servlet.http.HttpServletResponse; 32 33 import org.apache.tiles.context.TilesRequestContext; 34 import org.apache.tiles.context.TilesRequestContextWrapper; 35 import org.apache.tiles.servlet.context.ExternalWriterHttpServletResponse; 36 import org.apache.tiles.servlet.context.ServletUtil; 37 import org.apache.velocity.context.Context; 38 39 /** 40 * The implementation of the Tiles request context specific for Velocity. 41 * 42 * @version $Rev: 769961 $ $Date: 2009-04-30 00:07:34 +0200 (gio, 30 apr 2009) $ 43 * @since 2.2.0 44 */ 45 public class VelocityTilesRequestContext extends TilesRequestContextWrapper { 46 47 /** 48 * The Velocity current context. 49 */ 50 private final Context ctx; 51 52 /** 53 * The request objects. 54 */ 55 private Object[] requestObjects; 56 57 /** 58 * The writer to use to render the response. It may be null, if not necessary. 59 */ 60 private Writer writer; 61 62 /** 63 * Constructor. 64 * 65 * @param enclosedRequest The request that exposes non-Velocity specific properties 66 * @param ctx The Velocity current context. 67 * @param writer The writer to use to render the response. It may be null, if not necessary. 68 * @since 2.2.0 69 */ 70 public VelocityTilesRequestContext( 71 TilesRequestContext enclosedRequest, Context ctx, Writer writer) { 72 super(enclosedRequest); 73 this.ctx = ctx; 74 this.writer = writer; 75 } 76 77 /** {@inheritDoc} */ 78 public void dispatch(String path) throws IOException { 79 include(path); 80 } 81 82 /** {@inheritDoc} */ 83 @Override 84 public void include(String path) throws IOException { 85 Object[] requestObjects = super.getRequestObjects(); 86 HttpServletRequest request = (HttpServletRequest) requestObjects[0]; 87 HttpServletResponse response = (HttpServletResponse) requestObjects[1]; 88 ServletUtil.setForceInclude(request, true); 89 RequestDispatcher rd = request.getRequestDispatcher(path); 90 91 if (rd == null) { 92 throw new IOException("No request dispatcher returned for path '" 93 + path + "'"); 94 } 95 96 PrintWriter printWriter = getPrintWriter(); 97 try { 98 rd.include(request, new ExternalWriterHttpServletResponse(response, 99 printWriter)); 100 } catch (ServletException ex) { 101 throw ServletUtil.wrapServletException(ex, "ServletException including path '" 102 + path + "'."); 103 } 104 } 105 106 /** {@inheritDoc} */ 107 @Override 108 public PrintWriter getPrintWriter() throws IOException { 109 if (writer == null) { 110 throw new IllegalStateException( 111 "A writer-less Tiles request has been created, cannot return a PrintWriter"); 112 } 113 if (writer instanceof PrintWriter) { 114 return (PrintWriter) writer; 115 } else { 116 return new PrintWriter(writer); 117 } 118 } 119 120 /** {@inheritDoc} */ 121 @Override 122 public Writer getWriter() throws IOException { 123 if (writer == null) { 124 throw new IllegalStateException( 125 "A writer-less Tiles request has been created, cannot return a PrintWriter"); 126 } 127 return writer; 128 } 129 130 /** {@inheritDoc} */ 131 @Override 132 public Object[] getRequestObjects() { 133 if (requestObjects == null) { 134 Object[] parentRequestObjects = super.getRequestObjects(); 135 if (writer == null) { 136 requestObjects = new Object[parentRequestObjects.length + 1]; 137 } else { 138 requestObjects = new Object[parentRequestObjects.length + 2]; 139 } 140 requestObjects[0] = ctx; 141 for (int i = 0; i < parentRequestObjects.length; i++) { 142 requestObjects[i + 1] = parentRequestObjects[i]; 143 } 144 if (writer != null) { 145 requestObjects[parentRequestObjects.length + 1] = writer; 146 } 147 } 148 return requestObjects; 149 } 150 }