KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > servlet > EcsServletElement


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 JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc out)
74     {
75         output(new PrintWriter JavaDoc(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 JavaDoc out) {
84         ServletContext JavaDoc ctx = rundata.getServletContext();
85         RequestDispatcher JavaDoc 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 JavaDoc e)
97         {
98             String JavaDoc message = "EcsServletElement: Could not include the following URL: "
99                                 + url + " : " + e.getMessage();
100             logger.error( message, e );
101             out.print(message);
102         }
103     }
104 }
105
Popular Tags