KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fetchers > JahiaFetcherBaseService


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// JahiaFetcherBaseServices
15
// EV 11.01.2001
16
//
17
//
18
// fetchServlet( jParams, servletPath )
19
//
20

21 package org.jahia.services.fetchers;
22
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.RequestDispatcher JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.jahia.exceptions.JahiaException;
30 import org.jahia.params.ParamBean;
31 import org.jahia.services.applications.ServletIncludeRequestWrapper;
32 import org.jahia.services.applications.ServletIncludeResponseWrapper;
33 import org.jahia.bin.Jahia;
34
35 /**
36   * Class JahiaFetcherBaseService
37   */

38 public class JahiaFetcherBaseService extends JahiaFetcherService {
39
40     //private static String serviceName;
41
private static JahiaFetcherBaseService theObject = null;
42
43     /***
44         * constructor
45         *
46         */

47     private JahiaFetcherBaseService() {
48         logger.debug("***** Starting up the Jahia Fetcher Service *****");
49     } // end constructor
50

51     /***
52         * returns an instance of the fetcher service
53         *
54         */

55     public static synchronized JahiaFetcherBaseService getInstance() {
56         if (theObject == null) {
57             theObject = new JahiaFetcherBaseService();
58         }
59         return theObject;
60     } // end getInstance
61

62     /***
63         * fetches a servlet or JSP output
64         *
65         * @param jParams a ParamBean object (with request and response)
66         * @param servletPath the servlet / jsp context path
67         * @return the servlet / jsp output
68         *
69         */

70     public String JavaDoc fetchServlet( ParamBean jParams, String JavaDoc servletPath ) {
71
72         ServletIncludeResponseWrapper responseWrapper = null;
73
74         try {
75
76             logger.debug("Dispatching to servlet path = [" + servletPath + "]");
77
78             RequestDispatcher JavaDoc sessionDispatcher = jParams.getContext().getRequestDispatcher( servletPath );
79
80             if (sessionDispatcher == null) {
81                 String JavaDoc errorMsg = "Error in fetchServlet : sessionDispatcher is null - error getting request dispatcher";
82                 JahiaException je = new JahiaException("Error while fetching a servlet output",
83                         errorMsg, JahiaException.SERVLET_ERROR, JahiaException.WARNING_SEVERITY );
84                 logger.debug(errorMsg, je);
85                 return "";
86             }
87
88             // Let's initialize the wrapper, that includes our specific PrintWriter, allowing us to redirect
89
// output to a string object.
90
String JavaDoc forceEncoding = null;
91             forceEncoding = Jahia.getSettings().getDefaultResponseBodyEncoding();
92             responseWrapper = new ServletIncludeResponseWrapper(jParams.getResponse(), false, forceEncoding);
93
94             if (responseWrapper == null) {
95                 String JavaDoc errorMsg = "Error in fetchServlet : responseWrapper is null - error getting response warpper";
96                 JahiaException je = new JahiaException( "Error while fetching a servlet output",
97                                         errorMsg, JahiaException.SERVLET_ERROR, JahiaException.WARNING_SEVERITY );
98                 logger.debug(errorMsg, je);
99                 return "";
100             }
101
102             ServletIncludeRequestWrapper requestWrapper = jParams.getRequestWrapper();
103
104             // Let's now call the servlet and retrieve the result of it's processing
105
sessionDispatcher.include(requestWrapper, responseWrapper);
106
107         } catch (ServletException JavaDoc se) {
108             String JavaDoc generatedOutput = "";
109             try {
110                 generatedOutput = responseWrapper.getStringBuffer();
111             } catch (IOException JavaDoc ioe) {
112                 logger.error("Error while retrieving generated output", ioe);
113             }
114             logger.error(generatedOutput, se);
115             Throwable JavaDoc t = se;
116             while (t instanceof ServletException JavaDoc) {
117                 ServletException JavaDoc curSe = (ServletException JavaDoc) t;
118                 logger.error(generatedOutput, curSe.getRootCause());
119                 t = curSe.getRootCause();
120             }
121             return "";
122         } catch (IOException JavaDoc ioe) {
123             String JavaDoc generatedOutput = "";
124             try {
125                 generatedOutput = responseWrapper.getStringBuffer();
126             } catch (IOException JavaDoc ioe2) {
127                 logger.error("Error while retrieving generated output", ioe2);
128             }
129             logger.debug(generatedOutput, ioe);
130             return "";
131         } catch (Throwable JavaDoc t) {
132             String JavaDoc generatedOutput = "";
133             try {
134                 generatedOutput = responseWrapper.getStringBuffer();
135             } catch (IOException JavaDoc ioe) {
136                 logger.error("Error while retrieving generated output", ioe);
137             }
138             logger.debug(generatedOutput, t);
139             return "";
140         }
141         String JavaDoc generatedOutput = "";
142         try {
143             generatedOutput = responseWrapper.getStringBuffer();
144         } catch (IOException JavaDoc ioe) {
145             logger.error("Error while retrieving generated output", ioe);
146         }
147         return generatedOutput;
148     } // end fetchServlet
149

150     private static Logger logger = Logger.getLogger(
151       JahiaFetcherBaseService.class);
152
153 } // end JahiaFetcherBaseService
154
Popular Tags