1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.jetspeed.portal.portlets.viewprocessor;
17
18
19 import org.apache.ecs.ElementContainer;
20 import org.apache.ecs.StringElement;
21
22
23 import org.apache.jetspeed.portal.Portlet;
24 import org.apache.jetspeed.portal.portlets.GenericMVCContext;
25 import org.apache.jetspeed.services.TemplateLocator;
26 import org.apache.jetspeed.services.Registry;
27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28 import org.apache.jetspeed.services.logging.JetspeedLogger;
29 import org.apache.jetspeed.om.registry.PortletEntry;
30 import org.apache.jetspeed.util.ServiceUtil;
31
32
33 import org.apache.turbine.services.jsp.JspService;
34
35
36 import org.apache.turbine.util.RunData;
37
38
39 import java.util.Iterator;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.RequestDispatcher;
42
43 /***
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 */
60 public class JSPViewProcessor
61 implements ViewProcessor
62 {
63
64 /***
65 * Static initialization of the logger for this class
66 */
67 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JSPViewProcessor.class.getName());
68
69 /*** Creates a new instance of JSPViewProcessor */
70 public JSPViewProcessor()
71 {
72 }
73
74 public Object processView(GenericMVCContext context)
75 {
76
77 Portlet 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);
82
83 try
84 {
85
86
87 request.setAttribute("portlet", portlet);
88
89
90 request.setAttribute("context", context);
91
92
93 request.setAttribute("js_peid", portlet.getID());
94
95
96 request.setAttribute(JspService.RUNDATA, data);
97
98
99
100 PortletEntry pe = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
101
102
103
104
105 if (pe.getURL() == null || pe.getURL().trim().length() == 0)
106 {
107
108 if (template != null && -1 == template.indexOf(".jsp"))
109 {
110 template = template + ".jsp";
111 }
112
113 logger.info("JSPViewProcessor - locating template - " + data.toString()
114 + " - " + template);
115
116
117 String locatedTemplate = TemplateLocator.locatePortletTemplate(data, template);
118 logger.info("JSPViewProcessor - located template: " + locatedTemplate);
119
120
121
122
123
124
125
126
127
128
129
130 JspService service = (JspService) ServiceUtil.getServiceByName(JspService.SERVICE_NAME);
131
132
133
134 service.addDefaultObjects(data);
135
136
137 service.handleRequest(data, locatedTemplate);
138
139 }
140 else
141 {
142
143 Iterator names = portlet.getPortletConfig().getInitParameterNames();
144 while (names.hasNext())
145 {
146 String name = (String) names.next();
147 String value = (String) portlet.getPortletConfig().getInitParameter(name);
148 data.getParameters().setString(name, value);
149 }
150
151 template = pe.getURL();
152
153 if (logger.isDebugEnabled())
154 {
155 logger.debug("JSPViewProcessor - serving jsp directly using: " + template);
156 }
157
158
159 RequestDispatcher dispatcher = data.getServletContext().getRequestDispatcher(template);
160 data.getOut().flush();
161 dispatcher.include(data.getRequest(), data.getResponse());
162 }
163
164 }
165 catch (Exception e)
166 {
167
168 String message = "JSPViewProcessor: Could not include the following JSP Page: [" + template + "] :\n\t"
169 + e.getMessage();
170 logger.error(message, e);
171
172 return new StringElement(message);
173 }
174
175 return new ElementContainer();
176 }
177
178 /*** Process the template passed in the context
179 * (context.get("template")). Invoked by the GenericMVCPortlet
180 * after action handling to process the template type
181 * in question.
182 *
183 */
184 public void init(Portlet portlet)
185 {
186 }
187 }