1 17 18 package org.apache.catalina.ssi; 19 20 import java.io.IOException ; 21 import java.io.OutputStreamWriter ; 22 import java.io.PrintWriter ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletOutputStream ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpServletResponseWrapper ; 29 30 import org.apache.catalina.util.DateTool; 31 39 public class ResponseIncludeWrapper extends HttpServletResponseWrapper { 40 43 private static final String CONTENT_TYPE = "content-type"; 44 private static final String LAST_MODIFIED = "last-modified"; 45 protected long lastModified = -1; 46 private String contentType = null; 47 48 51 protected ServletOutputStream captureServletOutputStream; 52 protected ServletOutputStream servletOutputStream; 53 protected PrintWriter printWriter; 54 55 private ServletContext context; 56 private HttpServletRequest request; 57 58 59 68 public ResponseIncludeWrapper(ServletContext context, 69 HttpServletRequest request, HttpServletResponse response, 70 ServletOutputStream captureServletOutputStream) { 71 super(response); 72 this.context = context; 73 this.request = request; 74 this.captureServletOutputStream = captureServletOutputStream; 75 } 76 77 78 83 public void flushOutputStreamOrWriter() throws IOException { 84 if (servletOutputStream != null) { 85 servletOutputStream.flush(); 86 } 87 if (printWriter != null) { 88 printWriter.flush(); 89 } 90 } 91 92 93 101 public PrintWriter getWriter() throws java.io.IOException { 102 if (servletOutputStream == null) { 103 if (printWriter == null) { 104 setCharacterEncoding(getCharacterEncoding()); 105 printWriter = new PrintWriter ( 106 new OutputStreamWriter (captureServletOutputStream, 107 getCharacterEncoding())); 108 } 109 return printWriter; 110 } 111 throw new IllegalStateException (); 112 } 113 114 115 123 public ServletOutputStream getOutputStream() throws java.io.IOException { 124 if (printWriter == null) { 125 if (servletOutputStream == null) { 126 servletOutputStream = captureServletOutputStream; 127 } 128 return servletOutputStream; 129 } 130 throw new IllegalStateException (); 131 } 132 133 134 142 public long getLastModified() { 143 if (lastModified == -1) { 144 return -1; 147 } 148 return lastModified; 149 } 150 151 156 public void setLastModified(long lastModified) { 157 this.lastModified = lastModified; 158 ((HttpServletResponse ) getResponse()).setDateHeader(LAST_MODIFIED, 159 lastModified); 160 } 161 162 168 public String getContentType() { 169 if (contentType == null) { 170 String url = request.getRequestURI(); 171 String mime = context.getMimeType(url); 172 if (mime != null) 173 { 174 setContentType(mime); 175 } 176 else 177 { 178 setContentType("application/x-octet-stream"); 180 } 181 } 182 return contentType; 183 } 184 185 190 public void setContentType(String mime) { 191 contentType = mime; 192 if (contentType != null) { 193 getResponse().setContentType(contentType); 194 } 195 } 196 197 198 public void addDateHeader(String name, long value) { 199 super.addDateHeader(name, value); 200 String lname = name.toLowerCase(); 201 if (lname.equals(LAST_MODIFIED)) { 202 lastModified = value; 203 } 204 } 205 206 public void addHeader(String name, String value) { 207 super.addHeader(name, value); 208 String lname = name.toLowerCase(); 209 if (lname.equals(LAST_MODIFIED)) { 210 try { 211 lastModified = DateTool.rfc1123Format.parse(value).getTime(); 212 } catch (Throwable ignore) { } 213 } else if (lname.equals(CONTENT_TYPE)) { 214 contentType = value; 215 } 216 } 217 218 public void setDateHeader(String name, long value) { 219 super.setDateHeader(name, value); 220 String lname = name.toLowerCase(); 221 if (lname.equals(LAST_MODIFIED)) { 222 lastModified = value; 223 } 224 } 225 226 public void setHeader(String name, String value) { 227 super.setHeader(name, value); 228 String lname = name.toLowerCase(); 229 if (lname.equals(LAST_MODIFIED)) { 230 try { 231 lastModified = DateTool.rfc1123Format.parse(value).getTime(); 232 } catch (Throwable ignore) { } 233 } 234 else if (lname.equals(CONTENT_TYPE)) 235 { 236 contentType = value; 237 } 238 } 239 } 240 | Popular Tags |