View Javadoc

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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.portal.portlets.viewprocessor;
17  
18  // Ecs
19  import org.apache.ecs.ElementContainer;
20  import org.apache.ecs.StringElement;
21  
22  // Jetspeed portal
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  // Turbine stuff
33  import org.apache.turbine.services.jsp.JspService;
34  
35  // Turbine util
36  import org.apache.turbine.util.RunData;
37  
38  //java stuff
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              // Allow access to portlet from .jsp template
87              request.setAttribute("portlet", portlet);
88  
89              // put context in attribute so you can get to it from .jsp template
90              request.setAttribute("context", context);
91  
92              // Add js_peid out of convenience
93              request.setAttribute("js_peid", portlet.getID());
94  
95              // Add rundata out of convenience (JspService.RUNDATA differs from GenericMVCPortlet.RUNDATA)
96              request.setAttribute(JspService.RUNDATA, data);
97  
98              // Retrieve the URL. For backward compatibility, use the URL first 
99              // and then fallback to "template" parameter
100             PortletEntry pe = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
101 
102             // Files referenced from default templates folder will be processed
103             // using JspService. Otherwise, they will be loaded using EcsServletElement
104             // from where ever they came from.
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                 //we use the template locator to translate the template
117                 String locatedTemplate = TemplateLocator.locatePortletTemplate(data, template);
118                 logger.info("JSPViewProcessor - located template: " + locatedTemplate);
119 
120                 /*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                 } */
129 
130                 JspService service = (JspService) ServiceUtil.getServiceByName(JspService.SERVICE_NAME);
131 
132                 // this is only necessary if we don't run in a JSP page environment
133                 // but better be safe than sorry...
134                 service.addDefaultObjects(data);
135 
136                 // handle request
137                 service.handleRequest(data, locatedTemplate);
138 
139             }
140             else
141             {
142                 // Build parameter list to be passed with the jsp
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                 // get the RequestDispatcher for the JSP
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 }