View Javadoc

1   /*
2    * Copyright 2005 Jenia org.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author srecinto
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          // search the resource in classloader
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) { //Use custom path to FCKeditor
65          	this.getServletContext().getRequestDispatcher(getCustomResourcePath() + path).forward(request,response);
66          } else {  //Use default FCKeditor bundled up in the jar
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  	        // if no resource found in classloader return nothing
85  	        if (is==null) return;
86  	        // resource found, copying on output stream
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 }