1 11 package org.apache.catalina.ssi; 12 13 14 import java.io.BufferedReader ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.InputStreamReader ; 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 import java.net.URL ; 21 import java.net.URLConnection ; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServlet ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import org.apache.catalina.Globals; 28 38 public class SSIServlet extends HttpServlet { 39 40 protected int debug = 0; 41 42 protected boolean buffered = false; 43 44 protected Long expires = null; 45 46 protected boolean isVirtualWebappRelative = false; 47 48 protected String inputEncoding = null; 49 50 protected String outputEncoding = "UTF-8"; 51 52 53 60 public void init() throws ServletException { 61 62 if (getServletConfig().getInitParameter("debug") != null) 63 debug = Integer.parseInt(getServletConfig().getInitParameter("debug")); 64 65 isVirtualWebappRelative = 66 Boolean.parseBoolean(getServletConfig().getInitParameter("isVirtualWebappRelative")); 67 68 if (getServletConfig().getInitParameter("expires") != null) 69 expires = Long.valueOf(getServletConfig().getInitParameter("expires")); 70 71 buffered = Boolean.parseBoolean(getServletConfig().getInitParameter("buffered")); 72 73 inputEncoding = getServletConfig().getInitParameter("inputEncoding"); 74 75 if (getServletConfig().getInitParameter("outputEncoding") != null) 76 outputEncoding = getServletConfig().getInitParameter("outputEncoding"); 77 78 if (debug > 0) 79 log("SSIServlet.init() SSI invoker started with 'debug'=" + debug); 80 81 } 82 83 84 96 public void doGet(HttpServletRequest req, HttpServletResponse res) 97 throws IOException , ServletException { 98 if (debug > 0) log("SSIServlet.doGet()"); 99 requestHandler(req, res); 100 } 101 102 103 116 public void doPost(HttpServletRequest req, HttpServletResponse res) 117 throws IOException , ServletException { 118 if (debug > 0) log("SSIServlet.doPost()"); 119 requestHandler(req, res); 120 } 121 122 123 131 protected void requestHandler(HttpServletRequest req, 132 HttpServletResponse res) throws IOException , ServletException { 133 ServletContext servletContext = getServletContext(); 134 String path = SSIServletRequestUtil.getRelativePath(req); 135 if (debug > 0) 136 log("SSIServlet.requestHandler()\n" + "Serving " 137 + (buffered?"buffered ":"unbuffered ") + "resource '" 138 + path + "'"); 139 if (path == null || path.toUpperCase().startsWith("/WEB-INF") 142 || path.toUpperCase().startsWith("/META-INF")) { 143 res.sendError(HttpServletResponse.SC_NOT_FOUND, path); 144 log("Can't serve file: " + path); 145 return; 146 } 147 URL resource = servletContext.getResource(path); 148 if (resource == null) { 149 res.sendError(HttpServletResponse.SC_NOT_FOUND, path); 150 log("Can't find file: " + path); 151 return; 152 } 153 String resourceMimeType = servletContext.getMimeType(path); 154 if (resourceMimeType == null) { 155 resourceMimeType = "text/html"; 156 } 157 res.setContentType(resourceMimeType + ";charset=" + outputEncoding); 158 if (expires != null) { 159 res.setDateHeader("Expires", (new java.util.Date ()).getTime() 160 + expires.longValue() * 1000); 161 } 162 req.setAttribute(Globals.SSI_FLAG_ATTR, "true"); 163 processSSI(req, res, resource); 164 } 165 166 167 protected void processSSI(HttpServletRequest req, HttpServletResponse res, 168 URL resource) throws IOException { 169 SSIExternalResolver ssiExternalResolver = 170 new SSIServletExternalResolver(getServletContext(), req, res, 171 isVirtualWebappRelative, debug, inputEncoding); 172 SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, 173 debug); 174 PrintWriter printWriter = null; 175 StringWriter stringWriter = null; 176 if (buffered) { 177 stringWriter = new StringWriter (); 178 printWriter = new PrintWriter (stringWriter); 179 } else { 180 printWriter = res.getWriter(); 181 } 182 183 URLConnection resourceInfo = resource.openConnection(); 184 InputStream resourceInputStream = resourceInfo.getInputStream(); 185 String encoding = resourceInfo.getContentEncoding(); 186 if (encoding == null) { 187 encoding = inputEncoding; 188 } 189 InputStreamReader isr; 190 if (encoding == null) { 191 isr = new InputStreamReader (resourceInputStream); 192 } else { 193 isr = new InputStreamReader (resourceInputStream, encoding); 194 } 195 BufferedReader bufferedReader = new BufferedReader (isr); 196 197 long lastModified = ssiProcessor.process(bufferedReader, 198 resourceInfo.getLastModified(), printWriter); 199 if (lastModified > 0) { 200 res.setDateHeader("last-modified", lastModified); 201 } 202 if (buffered) { 203 printWriter.flush(); 204 String text = stringWriter.toString(); 205 res.getWriter().write(text); 206 } 207 } 208 } | Popular Tags |