KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > packtag > servlet > PackServlet


1 /**
2  * Project pack:tag >> http://packtag.sf.net
3  *
4  * This software is published under the terms of the LGPL
5  * License version 2.1, a copy of which has been included with this
6  * distribution in the 'lgpl.txt' file.
7  *
8  * Last author: $Author: danielgalan $
9  * Last modified: $Date: 2007/07/12 22:10:40 $
10  * Revision: $Revision: 1.3 $
11  *
12  * $Log: PackServlet.java,v $
13  * Revision 1.3 2007/07/12 22:10:40 danielgalan
14  * removed system.out
15  *
16  * Revision 1.2 2007/07/11 23:01:30 danielgalan
17  * - 2.2
18  * - enhancement: caching header improved (servlet)
19  * - servlet url-mapping can be changed now (you have to set "packtag.cache.servlet.path" to the same value)
20  * - enhancement: polished the reference at http://www.galan.de/projects/packtag
21  *
22  * Revision 1.1 2007/04/22 19:04:25 danielgalan
23  * pack.tag moved from subversion to good old CVS
24  *
25  */

26 package net.sf.packtag.servlet;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Date JavaDoc;
30
31 import javax.servlet.Servlet JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServlet JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import net.sf.packtag.cache.PackCache;
38 import net.sf.packtag.cache.Resource;
39 import net.sf.packtag.util.ContextConfiguration;
40
41
42
43 /**
44  * Servlet implementation class for Servlet: PackServlet
45  * This Servlet return the packed resources, identified thru the hashcode.
46  * Only needed when Servlet is choosen as cachetype (default).
47  * The output is gzip-compressed.
48  *
49  * @author Daniel Galán y Martins
50  * @version $Revision: 1.3 $
51  */

52 public class PackServlet extends HttpServlet JavaDoc implements Servlet JavaDoc {
53
54     private static final long serialVersionUID = 6588877416667767264L;
55
56     private static final String JavaDoc QUESTION_MARK = "?";
57     private static final String JavaDoc CONTENT_ENCODING = "Content-Encoding";
58     private static final String JavaDoc ACCEPTED_ENCODING = "Accept-Encoding";
59     private static final String JavaDoc GZIP = "gzip";
60     
61     private static final String JavaDoc CACHE_CONTROL = "Cache-Control";
62     private static final String JavaDoc CACHE_CONTROL_PRIVATE = "private";
63     private static final String JavaDoc EXPIRES = "Expires";
64     private static final long ONE_YEAR = 31536000000L;
65     private static final String JavaDoc ETAG = "ETag";
66     private static final String JavaDoc ETAG_PREFIX = "pack";
67
68
69     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
70         StringBuffer JavaDoc fqp = new StringBuffer JavaDoc();
71         // ContextPath is "" on root, elswise "/contextpath"
72
fqp.append(request.getContextPath());
73         fqp.append(ContextConfiguration.getCacheServletPath(request.getSession().getServletContext()));
74         fqp.append(QUESTION_MARK);
75         fqp.append(request.getQueryString());
76         Resource resource = PackCache.getResourceByFqp(fqp.toString());
77         //Configuration.getCacheType();
78

79         // It won't be cached by proxies, because of the Cache-Control: private header. Let the Browser cache the resource
80
response.setHeader(CACHE_CONTROL, CACHE_CONTROL_PRIVATE);
81
82         // Don't let the resource expire
83
response.setDateHeader(EXPIRES, new Date JavaDoc().getTime() + ONE_YEAR); // Expires after a year
84

85         // Funny header, see here: http://en.wikipedia.org/wiki/HTTP_ETag
86
response.setHeader(ETAG, ETAG_PREFIX + Integer.toString(resource.getMinifiedHashcode()));
87
88         // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html - Section 14.44 "Vary"
89
// This needs to be set to work right with proxies (and apache)
90
//response.setHeader("Vary", "Accept-Encoding");
91
// Thats not true, the hashcodes identify a resource unique, make it possible to the proxy to cache it
92
// Force caching, experimental, therefor disabled
93
//response.setHeader("Vary", "Accept-Encoding"); // this needs to be set to work right with proxies (and apache)
94

95         //response.setHeader("Pragma", "cache"); // HTTP 1.0
96
//response.setHeader("Cache-Control", "public"); // HTTP 1.1
97

98         String JavaDoc acceptEncoding = request.getHeader(ACCEPTED_ENCODING);
99         if ((acceptEncoding != null) && (acceptEncoding.indexOf(GZIP) != -1)) {
100             response.setHeader(CONTENT_ENCODING, GZIP);
101             response.getOutputStream().write(resource.getGzippedResource());
102         }
103         else {
104             response.getWriter().write(resource.getMinifedResource());
105         }
106     }
107
108 }
109
Popular Tags