1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16packageorg.apache.jetspeed.portal.portlets.viewprocessor;
1718// Ecs19import org.apache.ecs.ElementContainer;
20import org.apache.ecs.StringElement;
2122// Jetspeed portal23import org.apache.jetspeed.portal.Portlet;
24import org.apache.jetspeed.portal.portlets.GenericMVCContext;
25import org.apache.jetspeed.services.TemplateLocator;
26import org.apache.jetspeed.services.Registry;
27import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28import org.apache.jetspeed.services.logging.JetspeedLogger;
29import org.apache.jetspeed.om.registry.PortletEntry;
30import org.apache.jetspeed.util.ServiceUtil;
3132// Turbine stuff33import org.apache.turbine.services.jsp.JspService;
3435// Turbine util36import org.apache.turbine.util.RunData;
3738//java stuff39import java.util.Iterator;
40import javax.servlet.http.HttpServletRequest;
41import javax.servlet.RequestDispatcher;
4243/***44 * <b>JspViewProcessor</b> - MVC processor for serving jsp files.45 * <p>46 * The .jsp file location may be specified in two different ways:47 * <li><b>using the "template" parameter</b> - the JspTemplateService will search portlets and then screens 48 * folder to locate the appropriate template. The template must be specifed in the "template" 49 * portlet parameter.50 * <li><b>using relative url</b> - the .jsp template will be served directly bypassing the 51 * JspTemplateService. The template must be specifed in the portlet url property. 52 * Example: /html/welcome.jsp.53 * <P>54 * 55 * @author <a href="mailto:tkuebler@cisco.com">Tod Kuebler</a>56 * @author <a href="mailto:weaver@apache.org">Scott Weaver</a>57 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>58 * @version $Id: $59 */60publicclassJSPViewProcessor61 implements ViewProcessor62 {
6364/***65 * Static initialization of the logger for this class66 */67privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(JSPViewProcessor.class.getName());
6869/*** Creates a new instance of JSPViewProcessor */70publicJSPViewProcessor()
71 {
72 }
7374public Object processView(GenericMVCContext context)
75 {
7677Portlet portlet = (Portlet) context.get("portlet");
78 RunData data = (RunData) context.get("data");
79 HttpServletRequest request = data.getRequest();
80 String template = (String) context.get("template");
81 logger.info("JSPViewProcessor - processing template " + template);
8283try84 {
8586// Allow access to portlet from .jsp template87 request.setAttribute("portlet", portlet);
8889// put context in attribute so you can get to it from .jsp template90 request.setAttribute("context", context);
9192// Add js_peid out of convenience93 request.setAttribute("js_peid", portlet.getID());
9495// Add rundata out of convenience (JspService.RUNDATA differs from GenericMVCPortlet.RUNDATA)96 request.setAttribute(JspService.RUNDATA, data);
9798// Retrieve the URL. For backward compatibility, use the URL first 99// and then fallback to "template" parameter100PortletEntry pe = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
101102// Files referenced from default templates folder will be processed103// using JspService. Otherwise, they will be loaded using EcsServletElement104// from where ever they came from.105if (pe.getURL() == null || pe.getURL().trim().length() == 0)
106 {
107108if (template != null && -1 == template.indexOf(".jsp"))
109 {
110 template = template + ".jsp";
111 }
112113 logger.info("JSPViewProcessor - locating template - " + data.toString()
114 + " - " + template);
115116//we use the template locator to translate the template117 String locatedTemplate = TemplateLocator.locatePortletTemplate(data, template);
118 logger.info("JSPViewProcessor - located template: " + locatedTemplate);
119120/*if (locatedTemplate == null)121 {122 locatedTemplate = TemplateLocator.locateScreenTemplate(data, template);123 if (locatedTemplate != null)124 {125 locatedTemplate = "/screens" + locatedTemplate;126 }127 logger.debug("JSPViewProcessor - located screen template: " + locatedTemplate);128 } */129130 JspService service = (JspService) ServiceUtil.getServiceByName(JspService.SERVICE_NAME);
131132// this is only necessary if we don't run in a JSP page environment133// but better be safe than sorry...134 service.addDefaultObjects(data);
135136// handle request137 service.handleRequest(data, locatedTemplate);
138139 }
140else141 {
142// Build parameter list to be passed with the jsp143 Iterator names = portlet.getPortletConfig().getInitParameterNames();
144while (names.hasNext())
145 {
146 String name = (String) names.next();
147 String value = (String) portlet.getPortletConfig().getInitParameter(name);
148 data.getParameters().setString(name, value);
149 }
150151 template = pe.getURL();
152153if (logger.isDebugEnabled())
154 {
155 logger.debug("JSPViewProcessor - serving jsp directly using: " + template);
156 }
157158// get the RequestDispatcher for the JSP159 RequestDispatcher dispatcher = data.getServletContext().getRequestDispatcher(template);
160 data.getOut().flush();
161 dispatcher.include(data.getRequest(), data.getResponse());
162 }
163164 }
165catch (Exception e)
166 {
167168 String message = "JSPViewProcessor: Could not include the following JSP Page: [" + template + "] :\n\t"169 + e.getMessage();
170 logger.error(message, e);
171172returnnew StringElement(message);
173 }
174175returnnew ElementContainer();
176 }
177178/*** Process the template passed in the context179 * (context.get("template")). Invoked by the GenericMVCPortlet180 * after action handling to process the template type181 * in question.182 *183 */184publicvoid init(Portlet portlet)
185 {
186 }
187 }