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  
17  package org.apache.jetspeed.util.servlet;
18  
19  import java.io.OutputStream;
20  import java.io.PrintWriter;
21  import javax.servlet.RequestDispatcher;
22  import javax.servlet.ServletContext;
23  
24  import org.apache.turbine.util.RunData;
25  import org.apache.ecs.ConcreteElement;
26  
27  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28  import org.apache.jetspeed.services.logging.JetspeedLogger;
29  
30  /***
31   * NOTE: The use of Ecs for aggregating portlet content is deprecated!
32   *       This utility class will be removed once we don't have the ecs
33   *       dependency any more.
34   *
35   * EcsServletElement encapsulates a servlet/JSP within the context of ECS
36   * HTML-generation.
37   *
38   * This is a workaround to allow invoking servlets from JetSpeed Portlets.
39   * The servlet will be invoked when traversal of an ECS tree during writing
40   * reaches the EcsServlet element.
41   */
42  public class EcsServletElement extends ConcreteElement
43  {
44      /***
45       * Static initialization of the logger for this class
46       */    
47      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(EcsServletElement.class.getName());
48      
49      /*** RunData object to obtain HttpServletRequest/Response from. */
50      private RunData rundata;
51  
52      /*** URL of servlet to include */
53      private String url;
54  
55      /***
56      * Construct an ECS element from a given rundata object and URL.
57      *
58      * @param rundata Rundata object that holds the HttpServletRequest/Response
59      *                objects to be used for servlet invocation.
60      * @param url     The URL of the servlet to invoke.
61      */
62      public EcsServletElement(RunData rundata, String urlString)
63      {
64          this.url = urlString;
65          this.rundata = rundata;
66      }
67  
68      /*** Builds the content of this element and output it in the
69       *  passed OutputStream
70       *
71       * @param out the OutputStream to use for generating content
72       */
73      public void output(OutputStream out)
74      {
75          output(new PrintWriter(out));
76      }
77  
78      /*** Builds the content of this element and output it in the
79       *  passed PrintWriter
80       *
81       * @param out the PrintWriter to use for generating content
82       */
83      public void output(PrintWriter out) {
84          ServletContext ctx = rundata.getServletContext();
85          RequestDispatcher dispatcher = ctx.getRequestDispatcher(url);
86          try
87          {
88              // try to flush any data before dispatching request
89             // rundata.getResponse().flushBuffer();
90  
91              // call the servlet/JSP
92              dispatcher.include(
93                      rundata.getRequest(),
94                      rundata.getResponse() );
95          }
96          catch (Exception e)
97          {
98              String message = "EcsServletElement: Could not include the following URL: "
99                                  + url + " : " + e.getMessage();
100             logger.error( message, e );
101             out.print(message);
102         }
103     }
104 }