001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.servlets;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import javax.servlet.ServletConfig;
027: import javax.servlet.ServletContext;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.roller.RollerException;
035: import org.apache.roller.business.FileManager;
036: import org.apache.roller.business.RollerFactory;
037: import org.apache.roller.business.ThemeManager;
038: import org.apache.roller.pojos.Theme;
039: import org.apache.roller.pojos.WeblogResource;
040: import org.apache.roller.pojos.WebsiteData;
041: import org.apache.roller.ui.rendering.util.ModDateHeaderUtil;
042: import org.apache.roller.ui.rendering.util.WeblogResourceRequest;
043:
044: /**
045: * Serves files uploaded by users as well as static resources in shared themes.
046: *
047: * Since we keep resources in a location outside of the webapp
048: * context we need a way to serve them up. This servlet assumes that
049: * resources are stored on a filesystem in the "uploads.dir" directory.
050: *
051: * @web.servlet name="ResourcesServlet" load-on-startup="5"
052: * @web.servlet-mapping url-pattern="/roller-ui/rendering/resources/*"
053: */
054: public class ResourceServlet extends HttpServlet {
055:
056: private static Log log = LogFactory.getLog(ResourceServlet.class);
057:
058: private ServletContext context = null;
059:
060: public void init(ServletConfig config) throws ServletException {
061:
062: super .init(config);
063:
064: log.info("Initializing ResourceServlet");
065:
066: this .context = config.getServletContext();
067: }
068:
069: /**
070: * Handles requests for user uploaded resources.
071: */
072: public void doGet(HttpServletRequest request,
073: HttpServletResponse response) throws ServletException,
074: IOException {
075:
076: WebsiteData weblog = null;
077: String context = request.getContextPath();
078: String servlet = request.getServletPath();
079: String reqURI = request.getRequestURI();
080:
081: WeblogResourceRequest resourceRequest = null;
082: try {
083: // parse the incoming request and extract the relevant data
084: resourceRequest = new WeblogResourceRequest(request);
085:
086: weblog = resourceRequest.getWeblog();
087: if (weblog == null) {
088: throw new RollerException("unable to lookup weblog: "
089: + resourceRequest.getWeblogHandle());
090: }
091:
092: } catch (Exception e) {
093: // invalid resource request or weblog doesn't exist
094: log.debug("error creating weblog resource request", e);
095: response.sendError(HttpServletResponse.SC_NOT_FOUND);
096: return;
097: }
098:
099: log.debug("Resource requested ["
100: + resourceRequest.getResourcePath() + "]");
101:
102: long resourceLastMod = 0;
103: InputStream resourceStream = null;
104:
105: // first see if resource comes from weblog's shared theme
106: if (!Theme.CUSTOM.equals(weblog.getEditorTheme())) {
107: try {
108: ThemeManager themeMgr = RollerFactory.getRoller()
109: .getThemeManager();
110: Theme weblogTheme = themeMgr.getTheme(weblog
111: .getEditorTheme());
112: File resource = weblogTheme.getResource(resourceRequest
113: .getResourcePath());
114: if (resource != null) {
115: resourceLastMod = resource.lastModified();
116: resourceStream = new FileInputStream(resource);
117: }
118: } catch (Exception ex) {
119: // hmmm, some kind of error getting theme. that's an error.
120: response
121: .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
122: return;
123: }
124: }
125:
126: // if not from theme then see if resource is in weblog's upload dir
127: if (resourceStream == null) {
128: try {
129: FileManager fileMgr = RollerFactory.getRoller()
130: .getFileManager();
131: WeblogResource resource = fileMgr.getFile(weblog,
132: resourceRequest.getResourcePath());
133: resourceLastMod = resource.getLastModified();
134: resourceStream = resource.getInputStream();
135: } catch (Exception ex) {
136: // still not found? then we don't have it, 404.
137: log.debug("Unable to get resource", ex);
138: response.sendError(HttpServletResponse.SC_NOT_FOUND);
139: return;
140: }
141: }
142:
143: // Respond with 304 Not Modified if it is not modified.
144: if (ModDateHeaderUtil.respondIfNotModified(request, response,
145: resourceLastMod)) {
146: return;
147: } else {
148: // set last-modified date
149: ModDateHeaderUtil.setLastModifiedHeader(response,
150: resourceLastMod);
151: }
152:
153: // set the content type based on whatever is in our web.xml mime defs
154: response.setContentType(this .context
155: .getMimeType(resourceRequest.getResourcePath()));
156:
157: OutputStream out = null;
158: try {
159: // ok, lets serve up the file
160: byte[] buf = new byte[8192];
161: int length = 0;
162: out = response.getOutputStream();
163: while ((length = resourceStream.read(buf)) > 0) {
164: out.write(buf, 0, length);
165: }
166:
167: // close output stream
168: out.close();
169:
170: } catch (Exception ex) {
171: if (!response.isCommitted()) {
172: response.reset();
173: response
174: .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
175: }
176: } finally {
177: // make sure stream to resource file is closed
178: resourceStream.close();
179: }
180:
181: }
182:
183: }
|