1 11 package org.apache.catalina.ssi; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStreamReader ; 18 import java.io.OutputStream ; 19 import java.io.OutputStreamWriter ; 20 import java.io.PrintWriter ; 21 import java.io.Reader ; 22 import java.io.Writer ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import javax.servlet.Filter ; 27 import javax.servlet.FilterChain ; 28 import javax.servlet.FilterConfig ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.ServletRequest ; 31 import javax.servlet.ServletResponse ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.apache.catalina.Globals; 36 44 public class SSIFilter implements Filter { 45 protected FilterConfig config = null; 46 47 protected int debug = 0; 48 49 protected Long expires = null; 50 51 protected boolean isVirtualWebappRelative = false; 52 53 protected Pattern contentTypeRegEx = null; 54 55 protected Pattern shtmlRegEx = 56 Pattern.compile("text/x-server-parsed-html(;.*)?"); 57 58 59 66 public void init(FilterConfig config) throws ServletException { 67 this.config = config; 68 69 if (config.getInitParameter("debug") != null) { 70 debug = Integer.parseInt(config.getInitParameter("debug")); 71 } 72 73 if (config.getInitParameter("contentType") != null) { 74 contentTypeRegEx = Pattern.compile(config.getInitParameter("contentType")); 75 } else { 76 contentTypeRegEx = shtmlRegEx; 77 } 78 79 isVirtualWebappRelative = 80 Boolean.parseBoolean(config.getInitParameter("isVirtualWebappRelative")); 81 82 if (config.getInitParameter("expires") != null) 83 expires = Long.valueOf(config.getInitParameter("expires")); 84 85 if (debug > 0) 86 config.getServletContext().log( 87 "SSIFilter.init() SSI invoker started with 'debug'=" + debug); 88 } 89 90 public void doFilter(ServletRequest request, ServletResponse response, 91 FilterChain chain) throws IOException , ServletException { 92 HttpServletRequest req = (HttpServletRequest )request; 94 HttpServletResponse res = (HttpServletResponse )response; 95 96 req.setAttribute(Globals.SSI_FLAG_ATTR, "true"); 98 99 ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream(); 101 ResponseIncludeWrapper responseIncludeWrapper = 102 new ResponseIncludeWrapper(config.getServletContext(),req, res, basos); 103 104 chain.doFilter(req, responseIncludeWrapper); 106 107 responseIncludeWrapper.flushOutputStreamOrWriter(); 109 byte[] bytes = basos.toByteArray(); 110 111 String contentType = responseIncludeWrapper.getContentType(); 113 114 if (contentTypeRegEx.matcher(contentType).matches()) { 116 String encoding = res.getCharacterEncoding(); 117 118 SSIExternalResolver ssiExternalResolver = 120 new SSIServletExternalResolver(config.getServletContext(), req, 121 res, isVirtualWebappRelative, debug, encoding); 122 SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, 123 debug); 124 125 Reader reader = 127 new InputStreamReader (new ByteArrayInputStream (bytes), encoding); 128 ByteArrayOutputStream ssiout = new ByteArrayOutputStream (); 129 PrintWriter writer = 130 new PrintWriter (new OutputStreamWriter (ssiout, encoding)); 131 132 long lastModified = ssiProcessor.process(reader, 134 responseIncludeWrapper.getLastModified(), writer); 135 136 writer.flush(); 138 bytes = ssiout.toByteArray(); 139 140 if (expires != null) { 142 res.setDateHeader("expires", (new java.util.Date ()).getTime() 143 + expires.longValue() * 1000); 144 } 145 if (lastModified > 0) { 146 res.setDateHeader("last-modified", lastModified); 147 } 148 res.setContentLength(bytes.length); 149 150 Matcher shtmlMatcher = 151 shtmlRegEx.matcher(responseIncludeWrapper.getContentType()); 152 if (shtmlMatcher.matches()) { 153 String enc = shtmlMatcher.group(1); 156 res.setContentType("text/html" + ((enc != null) ? enc : "")); 157 } 158 } 159 160 OutputStream out = null; 162 try { 163 out = res.getOutputStream(); 164 } catch (IllegalStateException e) { 165 } 167 if (out == null) { 168 res.getWriter().write(new String (bytes)); 169 } else { 170 out.write(bytes); 171 } 172 } 173 174 public void destroy() { 175 } 176 } 177 | Popular Tags |