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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util.servlet;
1819import java.io.OutputStream;
20import java.io.PrintWriter;
21import javax.servlet.RequestDispatcher;
22import javax.servlet.ServletContext;
2324import org.apache.turbine.util.RunData;
25import org.apache.ecs.ConcreteElement;
2627import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28import org.apache.jetspeed.services.logging.JetspeedLogger;
2930/***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 ecs33 * dependency any more.34 *35 * EcsServletElement encapsulates a servlet/JSP within the context of ECS36 * 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 writing40 * reaches the EcsServlet element.41 */42publicclassEcsServletElementextends ConcreteElement
43 {
44/***45 * Static initialization of the logger for this class46 */47privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(EcsServletElement.class.getName());
4849/*** RunData object to obtain HttpServletRequest/Response from. */50private RunData rundata;
5152/*** URL of servlet to include */53private String url;
5455/***56 * Construct an ECS element from a given rundata object and URL.57 *58 * @param rundata Rundata object that holds the HttpServletRequest/Response59 * objects to be used for servlet invocation.60 * @param url The URL of the servlet to invoke.61 */62publicEcsServletElement(RunData rundata, String urlString)
63 {
64this.url = urlString;
65this.rundata = rundata;
66 }
6768/*** Builds the content of this element and output it in the69 * passed OutputStream70 *71 * @param out the OutputStream to use for generating content72 */73publicvoid output(OutputStream out)
74 {
75 output(new PrintWriter(out));
76 }
7778/*** Builds the content of this element and output it in the79 * passed PrintWriter80 *81 * @param out the PrintWriter to use for generating content82 */83publicvoid output(PrintWriter out) {
84 ServletContext ctx = rundata.getServletContext();
85 RequestDispatcher dispatcher = ctx.getRequestDispatcher(url);
86try87 {
88// try to flush any data before dispatching request89// rundata.getResponse().flushBuffer();9091// call the servlet/JSP92 dispatcher.include(
93 rundata.getRequest(),
94 rundata.getResponse() );
95 }
96catch (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 }