1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fckfaces.util;
17
18 import java.io.BufferedInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.Locale;
25 import java.util.TimeZone;
26
27 import javax.servlet.ServletConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServlet;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33
34
35
36 public class Servlet extends HttpServlet {
37
38 private static final long serialVersionUID = 7260045528613530636L;
39
40 private static final String modify=calcModify();
41
42 private volatile String customResourcePath;
43
44 private static final String calcModify() {
45 Date mod = new Date(System.currentTimeMillis());
46 SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z",Locale.ENGLISH);
47 sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
48 return sdf.format(mod);
49 }
50
51 public void init(ServletConfig config) throws ServletException {
52 super.init(config);
53 setCustomResourcePath(config.getInitParameter("customResourcePath"));
54 }
55
56 public void doGet(HttpServletRequest request, HttpServletResponse response)
57 throws ServletException, IOException {
58
59
60 ClassLoader cl = this.getClass().getClassLoader();
61 String uri = request.getRequestURI();
62 String path = uri.substring(uri.indexOf(Util.FCK_FACES_RESOURCE_PREFIX)+Util.FCK_FACES_RESOURCE_PREFIX.length()+1);
63
64 if(getCustomResourcePath() != null) {
65 this.getServletContext().getRequestDispatcher(getCustomResourcePath() + path).forward(request,response);
66 } else {
67 if (uri.endsWith(".jsf") || uri.endsWith(".html")) {
68 response.setContentType("text/html;charset=UTF-8");
69 } else {
70 response.setHeader("Cache-Control", "public");
71 response.setHeader("Last-Modified", modify);
72 }
73 if (uri.endsWith(".css")) {
74 response.setContentType("text/css;charset=UTF-8");
75 } else if (uri.endsWith(".js")) {
76 response.setContentType("text/javascript;charset=UTF-8");
77 } else if (uri.endsWith(".gif")) {
78 response.setContentType("image/gif;");
79 } else if (uri.endsWith(".xml")) {
80 response.setContentType("text/xml;charset=UTF-8");
81 }
82
83 InputStream is = cl.getResourceAsStream(path);
84
85 if (is==null) return;
86
87 OutputStream out = response.getOutputStream();
88 byte[] buffer = new byte[2048];
89 BufferedInputStream bis = new BufferedInputStream(is);
90 try {
91 int read = 0;
92 read = bis.read(buffer);
93 while (read!=-1) {
94 out.write(buffer,0,read);
95 read = bis.read(buffer);
96 }
97 } finally {
98 bis.close();
99 }
100 out.flush();
101 out.close();
102 }
103 }
104
105 public String getCustomResourcePath() {
106 return customResourcePath;
107 }
108
109 public void setCustomResourcePath(String customResourcePath) {
110 synchronized (this) {
111 this.customResourcePath = customResourcePath;
112 }
113 }
114
115 }