1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
89
90
91
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 }