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 java.util.Map;
024: import javax.servlet.ServletConfig;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServlet;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029: import javax.servlet.jsp.JspFactory;
030: import javax.servlet.jsp.PageContext;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.roller.RollerException;
034: import org.apache.roller.config.RollerConfig;
035: import org.apache.roller.config.RollerRuntimeConfig;
036: import org.apache.roller.business.RollerFactory;
037: import org.apache.roller.business.UserManager;
038: import org.apache.roller.pojos.Template;
039: import org.apache.roller.pojos.WebsiteData;
040: import org.apache.roller.ui.rendering.Renderer;
041: import org.apache.roller.ui.rendering.RendererManager;
042: import org.apache.roller.ui.rendering.model.Model;
043: import org.apache.roller.ui.rendering.model.ModelLoader;
044: import org.apache.roller.ui.rendering.model.SearchResultsModel;
045: import org.apache.roller.ui.rendering.util.WeblogPageRequest;
046: import org.apache.roller.ui.rendering.util.WeblogSearchRequest;
047: import org.apache.roller.util.cache.CachedContent;
048:
049: /**
050: * Handles search queries for weblogs.
051: *
052: * @web.servlet name="SearchServlet" load-on-startup="5"
053: * @web.servlet-mapping url-pattern="/roller-ui/rendering/search/*"
054: */
055: public class SearchServlet extends HttpServlet {
056:
057: private static Log log = LogFactory.getLog(SearchServlet.class);
058:
059: /**
060: * Init method for this servlet
061: */
062: public void init(ServletConfig servletConfig)
063: throws ServletException {
064:
065: super .init(servletConfig);
066:
067: log.info("Initializing SearchServlet");
068: }
069:
070: /**
071: * Handle GET requests for weblog pages.
072: */
073: public void doGet(HttpServletRequest request,
074: HttpServletResponse response) throws ServletException,
075: IOException {
076:
077: log.debug("Entering");
078:
079: WebsiteData weblog = null;
080: WeblogSearchRequest searchRequest = null;
081:
082: // first off lets parse the incoming request and validate it
083: try {
084: searchRequest = new WeblogSearchRequest(request);
085:
086: // now make sure the specified weblog really exists
087: UserManager userMgr = RollerFactory.getRoller()
088: .getUserManager();
089: weblog = userMgr.getWebsiteByHandle(searchRequest
090: .getWeblogHandle(), Boolean.TRUE);
091:
092: } catch (Exception e) {
093: // invalid search request format or weblog doesn't exist
094: log.debug("error creating weblog search request", e);
095: response.sendError(HttpServletResponse.SC_NOT_FOUND);
096: return;
097: }
098:
099: // get their default page template to use for rendering
100: Template page = null;
101: try {
102: page = weblog.getDefaultPage();
103: if (page == null) {
104: throw new RollerException(
105: "Could not lookup default page "
106: + "for weblog " + weblog.getHandle());
107: }
108: } catch (Exception e) {
109: log.error("Error getting weblogs default page", e);
110: }
111:
112: // set the content type
113: response.setContentType("text/html; charset=utf-8");
114:
115: // looks like we need to render content
116: Map model = new HashMap();
117: try {
118: PageContext pageContext = JspFactory.getDefaultFactory()
119: .getPageContext(this , request, response, "", false,
120: 8192, true);
121:
122: // populate the rendering model
123: Map initData = new HashMap();
124: initData.put("request", request);
125: initData.put("pageContext", pageContext);
126:
127: // this is a little hacky, but nothing we can do about it
128: // we need the 'weblogRequest' to be a pageRequest so other models
129: // are properly loaded, which means that searchRequest needs its
130: // own custom initData property aside from the standard weblogRequest.
131: // possible better approach is make searchRequest extend pageRequest.
132: WeblogPageRequest pageRequest = new WeblogPageRequest();
133: pageRequest
134: .setWeblogHandle(searchRequest.getWeblogHandle());
135: pageRequest.setWeblogCategoryName(searchRequest
136: .getWeblogCategoryName());
137: initData.put("weblogRequest", pageRequest);
138: initData.put("searchRequest", searchRequest);
139:
140: // Load models for pages
141: String searchModels = RollerConfig
142: .getProperty("rendering.searchModels");
143: ModelLoader.loadModels(searchModels, model, initData, true);
144:
145: // Load special models for site-wide blog
146: if (RollerRuntimeConfig
147: .isSiteWideWeblog(weblog.getHandle())) {
148: String siteModels = RollerConfig
149: .getProperty("rendering.siteModels");
150: ModelLoader.loadModels(siteModels, model, initData,
151: true);
152: }
153:
154: // Load weblog custom models
155: ModelLoader.loadCustomModels(weblog, model, initData);
156:
157: // ick, gotta load pre-3.0 model stuff as well :(
158: ModelLoader.loadOldModels(model, request, response,
159: pageContext, pageRequest);
160:
161: // manually add search model again to support pre-3.0 weblogs
162: Model searchModel = new SearchResultsModel();
163: searchModel.init(initData);
164: model.put("searchResults", searchModel);
165:
166: } catch (RollerException ex) {
167: log.error("Error loading model objects for page", ex);
168:
169: if (!response.isCommitted())
170: response.reset();
171: response
172: .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
173: return;
174: }
175:
176: // lookup Renderer we are going to use
177: Renderer renderer = null;
178: try {
179: log.debug("Looking up renderer");
180: renderer = RendererManager.getRenderer(page);
181: } catch (Exception e) {
182: // nobody wants to render my content :(
183: log.error("Couldn't find renderer for rsd template", e);
184:
185: if (!response.isCommitted())
186: response.reset();
187: response.sendError(HttpServletResponse.SC_NOT_FOUND);
188: return;
189: }
190:
191: // render content
192: CachedContent rendererOutput = new CachedContent(4096);
193: try {
194: log.debug("Doing rendering");
195: renderer.render(model, rendererOutput.getCachedWriter());
196:
197: // flush rendered output and close
198: rendererOutput.flush();
199: rendererOutput.close();
200: } catch (Exception e) {
201: // bummer, error during rendering
202: log.error("Error during rendering for rsd template", e);
203:
204: if (!response.isCommitted())
205: response.reset();
206: response.sendError(HttpServletResponse.SC_NOT_FOUND);
207: return;
208: }
209:
210: // post rendering process
211:
212: // flush rendered content to response
213: log.debug("Flushing response output");
214: response.setContentLength(rendererOutput.getContent().length);
215: response.getOutputStream().write(rendererOutput.getContent());
216:
217: log.debug("Exiting");
218: }
219:
220: }
|