KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ssi > SSIServlet


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation. Licensed under the
3  * Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the License
5  * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
6  * law or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */

11 package org.apache.catalina.ssi;
12
13
14 import java.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServlet JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import org.apache.catalina.Globals;
28 /**
29  * Servlet to process SSI requests within a webpage. Mapped to a path from
30  * within web.xml.
31  *
32  * @author Bip Thelin
33  * @author Amy Roh
34  * @author Dan Sandberg
35  * @author David Becker
36  * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
37  */

38 public class SSIServlet extends HttpServlet JavaDoc {
39     /** Debug level for this servlet. */
40     protected int debug = 0;
41     /** Should the output be buffered. */
42     protected boolean buffered = false;
43     /** Expiration time in seconds for the doc. */
44     protected Long JavaDoc expires = null;
45     /** virtual path can be webapp-relative */
46     protected boolean isVirtualWebappRelative = false;
47     /** Input encoding. If not specified, uses platform default */
48     protected String JavaDoc inputEncoding = null;
49     /** Output encoding. If not specified, uses platform default */
50     protected String JavaDoc outputEncoding = "UTF-8";
51
52
53     //----------------- Public methods.
54
/**
55      * Initialize this servlet.
56      *
57      * @exception ServletException
58      * if an error occurs
59      */

60     public void init() throws ServletException JavaDoc {
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     /**
85      * Process and forward the GET request to our <code>requestHandler()</code>*
86      *
87      * @param req
88      * a value of type 'HttpServletRequest'
89      * @param res
90      * a value of type 'HttpServletResponse'
91      * @exception IOException
92      * if an error occurs
93      * @exception ServletException
94      * if an error occurs
95      */

96     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
97             throws IOException JavaDoc, ServletException JavaDoc {
98         if (debug > 0) log("SSIServlet.doGet()");
99         requestHandler(req, res);
100     }
101
102
103     /**
104      * Process and forward the POST request to our
105      * <code>requestHandler()</code>.
106      *
107      * @param req
108      * a value of type 'HttpServletRequest'
109      * @param res
110      * a value of type 'HttpServletResponse'
111      * @exception IOException
112      * if an error occurs
113      * @exception ServletException
114      * if an error occurs
115      */

116     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
117             throws IOException JavaDoc, ServletException JavaDoc {
118         if (debug > 0) log("SSIServlet.doPost()");
119         requestHandler(req, res);
120     }
121
122
123     /**
124      * Process our request and locate right SSI command.
125      *
126      * @param req
127      * a value of type 'HttpServletRequest'
128      * @param res
129      * a value of type 'HttpServletResponse'
130      */

131     protected void requestHandler(HttpServletRequest JavaDoc req,
132             HttpServletResponse JavaDoc res) throws IOException JavaDoc, ServletException JavaDoc {
133         ServletContext JavaDoc servletContext = getServletContext();
134         String JavaDoc path = SSIServletRequestUtil.getRelativePath(req);
135         if (debug > 0)
136             log("SSIServlet.requestHandler()\n" + "Serving "
137                     + (buffered?"buffered ":"unbuffered ") + "resource '"
138                     + path + "'");
139         // Exclude any resource in the /WEB-INF and /META-INF subdirectories
140
// (the "toUpperCase()" avoids problems on Windows systems)
141
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 JavaDoc 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 JavaDoc 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 JavaDoc()).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 JavaDoc req, HttpServletResponse JavaDoc res,
168             URL JavaDoc resource) throws IOException JavaDoc {
169         SSIExternalResolver ssiExternalResolver =
170             new SSIServletExternalResolver(getServletContext(), req, res,
171                     isVirtualWebappRelative, debug, inputEncoding);
172         SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver,
173                 debug);
174         PrintWriter JavaDoc printWriter = null;
175         StringWriter JavaDoc stringWriter = null;
176         if (buffered) {
177             stringWriter = new StringWriter JavaDoc();
178             printWriter = new PrintWriter JavaDoc(stringWriter);
179         } else {
180             printWriter = res.getWriter();
181         }
182
183         URLConnection JavaDoc resourceInfo = resource.openConnection();
184         InputStream JavaDoc resourceInputStream = resourceInfo.getInputStream();
185         String JavaDoc encoding = resourceInfo.getContentEncoding();
186         if (encoding == null) {
187             encoding = inputEncoding;
188         }
189         InputStreamReader JavaDoc isr;
190         if (encoding == null) {
191             isr = new InputStreamReader JavaDoc(resourceInputStream);
192         } else {
193             isr = new InputStreamReader JavaDoc(resourceInputStream, encoding);
194         }
195         BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(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 JavaDoc text = stringWriter.toString();
205             res.getWriter().write(text);
206         }
207     }
208 }
Popular Tags