KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > resource > ResourceController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.resource;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.net.SocketException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.activation.FileTypeMap JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.riotfamily.common.util.FormatUtils;
41 import org.springframework.core.io.Resource;
42 import org.springframework.util.FileCopyUtils;
43 import org.springframework.web.context.support.WebApplicationObjectSupport;
44 import org.springframework.web.servlet.ModelAndView;
45 import org.springframework.web.servlet.mvc.Controller;
46 import org.springframework.web.servlet.mvc.LastModified;
47
48 /**
49  * Controller that serves an internal resource.
50  * <p>
51  * Note: This will only work when a prefix mapping is used for the
52  * DispatcherServlet (like <tt>/riot/*</tt>) since
53  * <code>request.getPathInfo()</code> is used.
54  * </p>
55  */

56 public class ResourceController extends WebApplicationObjectSupport
57         implements Controller, LastModified {
58
59     private static final String JavaDoc HEADER_EXPIRES = "Expires";
60     
61     private Log log = LogFactory.getLog(ResourceController.class);
62     
63     private FileTypeMap JavaDoc fileTypeMap = FileTypeMap.getDefaultFileTypeMap();
64     
65     private List JavaDoc mappings;
66     
67     private List JavaDoc filters;
68     
69     private long expiresAfter = 1000 * 60 * 60 * 24;
70     
71     private long lastModified = System.currentTimeMillis();
72     
73     private String JavaDoc pathAttribute;
74     
75     public void setExpiresAfter(String JavaDoc s) {
76         this.expiresAfter = FormatUtils.parseMillis(s);
77     }
78             
79     /**
80      * @param pathAttribute The pathAttribute to set.
81      */

82     public void setPathAttribute(String JavaDoc pathAttribute) {
83         this.pathAttribute = pathAttribute;
84     }
85     
86     public void setFileTypeMap(FileTypeMap JavaDoc fileTypeMap) {
87         this.fileTypeMap = fileTypeMap;
88     }
89
90     public void setMappings(List JavaDoc resourceMappings) {
91         this.mappings = resourceMappings;
92     }
93
94     public void setFilters(List JavaDoc filters) {
95         this.filters = filters;
96     }
97
98     public long getLastModified(HttpServletRequest JavaDoc request) {
99         return lastModified;
100     }
101
102     public ModelAndView handleRequest(HttpServletRequest JavaDoc request,
103             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
104         
105         String JavaDoc path;
106         if (pathAttribute != null) {
107             path = "/" + request.getAttribute(pathAttribute);
108         }
109         else {
110             path = request.getPathInfo();
111         }
112         log.debug("Looking up resource " + path);
113         Iterator JavaDoc it = mappings.iterator();
114         while (it.hasNext()) {
115             ResourceMapping mapping = (ResourceMapping) it.next();
116             Resource res = mapping.getResource(path);
117             if (res != null) {
118                 response.addDateHeader(HEADER_EXPIRES,
119                         System.currentTimeMillis() + expiresAfter);
120
121                 String JavaDoc contentType = fileTypeMap.getContentType(
122                         res.getFilename());
123                 
124                 response.setContentType(contentType);
125                 if (contentType.startsWith("text/")) {
126                     serveText(path, res, request, response);
127                 }
128                 else {
129                     try {
130                         FileCopyUtils.copy(res.getInputStream(),
131                                 response.getOutputStream());
132                     }
133                     catch (IOException JavaDoc e) {
134                         if (!SocketException JavaDoc.class.isInstance(e.getCause())) {
135                             throw e;
136                         }
137                     }
138                 }
139                 return null;
140             }
141         }
142         response.sendError(HttpServletResponse.SC_NOT_FOUND);
143         return null;
144     }
145     
146     protected void serveText(String JavaDoc path, Resource res,
147             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
148             throws IOException JavaDoc {
149         
150         log.debug("Serving text resource: " + path);
151         Reader JavaDoc in = new InputStreamReader JavaDoc(res.getInputStream());
152         Writer JavaDoc out = response.getWriter();
153         
154         if (filters != null) {
155             Iterator JavaDoc it = filters.iterator();
156             while (it.hasNext()) {
157                 ResourceFilter filter = (ResourceFilter) it.next();
158                 if (filter.matches(path)) {
159                     log.debug("Filter " + filter + " matches.");
160                     in = filter.createFilterReader(in, request);
161                     break;
162                 }
163                 log.debug("Filter " + filter + " does not match.");
164             }
165         }
166         FileCopyUtils.copy(in, out);
167     }
168
169 }
170
Popular Tags