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.IOException;
022: import java.util.HashMap;
023: import javax.servlet.ServletConfig;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.RollerException;
031: import org.apache.roller.config.RollerRuntimeConfig;
032: import org.apache.roller.pojos.StaticTemplate;
033: import org.apache.roller.pojos.Template;
034: import org.apache.roller.pojos.WebsiteData;
035: import org.apache.roller.ui.rendering.util.WeblogRequest;
036: import org.apache.roller.ui.rendering.util.ModDateHeaderUtil;
037: import org.apache.roller.ui.rendering.Renderer;
038: import org.apache.roller.ui.rendering.RendererManager;
039: import org.apache.roller.util.cache.CachedContent;
040:
041: /**
042: * Generates simple rsd feed for a given weblog.
043: *
044: * This servlet supports 304 If-Modified-Since checking, but does not do any
045: * level of content caching.
046: *
047: * @web.servlet name="RSDServlet" load-on-startup="7"
048: * @web.servlet-mapping url-pattern="/roller-ui/rendering/rsd/*"
049: */
050: public class RSDServlet extends HttpServlet {
051:
052: private static Log log = LogFactory.getLog(RSDServlet.class);
053:
054: /**
055: * Init method for this servlet
056: */
057: public void init(ServletConfig servletConfig)
058: throws ServletException {
059:
060: super .init(servletConfig);
061:
062: log.info("Initializing RSDServlet");
063: }
064:
065: /**
066: * Handle GET requests for weblog pages.
067: */
068: public void doGet(HttpServletRequest request,
069: HttpServletResponse response) throws ServletException,
070: IOException {
071:
072: log.debug("Entering");
073:
074: WebsiteData weblog = null;
075:
076: WeblogRequest weblogRequest = null;
077: try {
078: weblogRequest = new WeblogRequest(request);
079:
080: // now make sure the specified weblog really exists
081: weblog = weblogRequest.getWeblog();
082: if (weblog == null) {
083: throw new RollerException("Unable to lookup weblog: "
084: + weblogRequest.getWeblogHandle());
085: }
086:
087: } catch (Exception e) {
088: // invalid rsd request format or weblog doesn't exist
089: log.debug("error creating weblog request", e);
090: response.sendError(HttpServletResponse.SC_NOT_FOUND);
091: return;
092: }
093:
094: // Respond with 304 Not Modified if it is not modified.
095: long lastModified = System.currentTimeMillis();
096: if (weblog.getLastModified() != null) {
097: lastModified = weblog.getLastModified().getTime();
098: }
099: if (ModDateHeaderUtil.respondIfNotModified(request, response,
100: lastModified)) {
101: return;
102: }
103:
104: // set last-modified date
105: ModDateHeaderUtil.setLastModifiedHeader(response, lastModified);
106:
107: // set the content type
108: response.setContentType("application/rsd+xml; charset=utf-8");
109:
110: // populate the model
111: HashMap model = new HashMap();
112: model.put("website", weblog);
113: model.put("absBaseURL", RollerRuntimeConfig
114: .getAbsoluteContextURL());
115:
116: // lookup Renderer we are going to use
117: Renderer renderer = null;
118: try {
119: log.debug("Looking up renderer");
120: Template template = new StaticTemplate(
121: "templates/weblog/rsd.vm", null, "velocity");
122: renderer = RendererManager.getRenderer(template);
123: } catch (Exception e) {
124: // nobody wants to render my content :(
125: log.error("Couldn't find renderer for rsd template", e);
126:
127: if (!response.isCommitted())
128: response.reset();
129: response.sendError(HttpServletResponse.SC_NOT_FOUND);
130: return;
131: }
132:
133: // render content
134: CachedContent rendererOutput = new CachedContent(4096);
135: try {
136: log.debug("Doing rendering");
137: renderer.render(model, rendererOutput.getCachedWriter());
138:
139: // flush rendered output and close
140: rendererOutput.flush();
141: rendererOutput.close();
142: } catch (Exception e) {
143: // bummer, error during rendering
144: log.error("Error during rendering for rsd template", e);
145:
146: if (!response.isCommitted())
147: response.reset();
148: response.sendError(HttpServletResponse.SC_NOT_FOUND);
149: return;
150: }
151:
152: // post rendering process
153:
154: // flush rendered content to response
155: log.debug("Flushing response output");
156: response.setContentLength(rendererOutput.getContent().length);
157: response.getOutputStream().write(rendererOutput.getContent());
158:
159: log.debug("Exiting");
160: }
161:
162: }
|