1 package com.opensymphony.webwork.components; 2 3 import com.opensymphony.webwork.config.Configuration; 4 import com.opensymphony.webwork.util.FastByteArrayOutputStream; 5 import com.opensymphony.xwork.util.OgnlValueStack; 6 import org.apache.commons.logging.LogFactory; 7 8 import javax.servlet.RequestDispatcher ; 9 import javax.servlet.ServletException ; 10 import javax.servlet.ServletOutputStream ; 11 import javax.servlet.ServletRequest ; 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 import javax.servlet.http.HttpServletResponseWrapper ; 15 import java.io.IOException ; 16 import java.io.OutputStreamWriter ; 17 import java.io.PrintWriter ; 18 import java.io.Writer ; 19 import java.net.URLEncoder ; 20 import java.util.*; 21 22 28 public class Include extends Component { 29 private static String encoding; 30 private static boolean encodingDefined = true; 31 32 protected String value; 33 private HttpServletRequest req; 34 private HttpServletResponse res; 35 36 public Include(OgnlValueStack stack, HttpServletRequest req, HttpServletResponse res) { 37 super(stack); 38 this.req = req; 39 this.res = res; 40 } 41 42 public void end(Writer writer) { 43 String page = findString(value); 44 StringBuffer urlBuf = new StringBuffer (); 45 46 urlBuf.append(page); 48 49 if (parameters.size() > 0) { 51 urlBuf.append('?'); 52 53 String concat = ""; 54 55 Iterator iter = parameters.entrySet().iterator(); 57 58 while (iter.hasNext()) { 59 Map.Entry entry = (Map.Entry) iter.next(); 60 Object name = entry.getKey(); 61 List values = (List) entry.getValue(); 62 63 for (int i = 0; i < values.size(); i++) { 64 urlBuf.append(concat); 65 urlBuf.append(name); 66 urlBuf.append('='); 67 68 try { 69 urlBuf.append(URLEncoder.encode(values.get(i).toString())); 70 } catch (Exception e) { 71 } 72 73 concat = "&"; 74 } 75 } 76 } 77 78 String result = urlBuf.toString(); 79 80 try { 82 include(result, writer, req, res); 83 } catch (Exception e) { 84 LogFactory.getLog(getClass()).warn("Exception thrown during include of " + result, e); 85 } 86 } 87 88 public void setValue(String value) { 89 this.value = value; 90 } 91 92 public static String getContextRelativePath(ServletRequest request, String relativePath) { 93 String returnValue; 94 95 if (relativePath.startsWith("/")) { 96 returnValue = relativePath; 97 } else if (!(request instanceof HttpServletRequest )) { 98 returnValue = relativePath; 99 } else { 100 HttpServletRequest hrequest = (HttpServletRequest ) request; 101 String uri = (String ) request.getAttribute("javax.servlet.include.servlet_path"); 102 103 if (uri == null) { 104 uri = hrequest.getServletPath(); 105 } 106 107 returnValue = uri.substring(0, uri.lastIndexOf('/')) + '/' + relativePath; 108 } 109 110 if (returnValue.indexOf("..") != -1) { 113 Stack stack = new Stack(); 114 StringTokenizer pathParts = new StringTokenizer(returnValue.replace('\\', '/'), "/"); 115 116 while (pathParts.hasMoreTokens()) { 117 String part = pathParts.nextToken(); 118 119 if (!part.equals(".")) { 120 if (part.equals("..")) { 121 stack.pop(); 122 } else { 123 stack.push(part); 124 } 125 } 126 } 127 128 StringBuffer flatPathBuffer = new StringBuffer (); 129 130 for (int i = 0; i < stack.size(); i++) { 131 flatPathBuffer.append("/" + stack.elementAt(i)); 132 } 133 134 returnValue = flatPathBuffer.toString(); 135 } 136 137 return returnValue; 138 } 139 140 public static void include(String aResult, Writer writer, ServletRequest request, HttpServletResponse response) throws ServletException , IOException { 141 String resourcePath = getContextRelativePath(request, aResult); 142 RequestDispatcher rd = request.getRequestDispatcher(resourcePath); 143 144 if (rd == null) { 145 throw new ServletException ("Not a valid resource path:" + resourcePath); 146 } 147 148 PageResponse pageResponse = new PageResponse(response); 149 150 rd.include((HttpServletRequest ) request, pageResponse); 152 153 String encoding = getEncoding(); 155 156 if (encoding != null) { 157 pageResponse.getContent().writeTo(writer, encoding); 159 } else { 160 pageResponse.getContent().writeTo(writer, null); 162 } 163 } 164 165 181 private static String getEncoding() { 182 if (encodingDefined) { 183 try { 184 encoding = Configuration.getString("webwork.i18n.encoding"); 185 } catch (IllegalArgumentException e) { 186 encoding = System.getProperty("file.encoding"); 187 encodingDefined = false; 188 } 189 } 190 191 return encoding; 192 } 193 194 195 202 static final class PageOutputStream extends ServletOutputStream { 203 205 private FastByteArrayOutputStream buffer; 206 207 209 public PageOutputStream() { 210 buffer = new FastByteArrayOutputStream(); 211 } 212 213 215 218 public FastByteArrayOutputStream getBuffer() throws IOException { 219 flush(); 220 221 return buffer; 222 } 223 224 public void close() throws IOException { 225 buffer.close(); 226 } 227 228 public void flush() throws IOException { 229 buffer.flush(); 230 } 231 232 public void write(byte[] b, int o, int l) throws IOException { 233 buffer.write(b, o, l); 234 } 235 236 public void write(int i) throws IOException { 237 buffer.write(i); 238 } 239 240 public void write(byte[] b) throws IOException { 241 buffer.write(b); 242 } 243 } 244 245 246 260 static final class PageResponse extends HttpServletResponseWrapper { 261 263 protected PrintWriter pagePrintWriter; 264 protected ServletOutputStream outputStream; 265 private PageOutputStream pageOutputStream = null; 266 267 269 272 public PageResponse(HttpServletResponse response) { 273 super(response); 274 } 275 276 278 284 public FastByteArrayOutputStream getContent() throws IOException { 285 if (pagePrintWriter != null) { 289 pagePrintWriter.flush(); 290 } 291 292 return ((PageOutputStream) getOutputStream()).getBuffer(); 293 } 294 295 299 public ServletOutputStream getOutputStream() throws IOException { 300 if (pageOutputStream == null) { 301 pageOutputStream = new PageOutputStream(); 302 } 303 304 return pageOutputStream; 305 } 306 307 310 public PrintWriter getWriter() throws IOException { 311 if (pagePrintWriter == null) { 312 pagePrintWriter = new PrintWriter (new OutputStreamWriter (getOutputStream(), getCharacterEncoding())); 313 } 314 315 return pagePrintWriter; 316 } 317 } 318 } 319 | Popular Tags |