1 13 21 package org.jahia.services.fetchers; 22 23 import java.io.IOException ; 24 25 import javax.servlet.RequestDispatcher ; 26 import javax.servlet.ServletException ; 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 38 public class JahiaFetcherBaseService extends JahiaFetcherService { 39 40 private static JahiaFetcherBaseService theObject = null; 42 43 47 private JahiaFetcherBaseService() { 48 logger.debug("***** Starting up the Jahia Fetcher Service *****"); 49 } 51 55 public static synchronized JahiaFetcherBaseService getInstance() { 56 if (theObject == null) { 57 theObject = new JahiaFetcherBaseService(); 58 } 59 return theObject; 60 } 62 70 public String fetchServlet( ParamBean jParams, String servletPath ) { 71 72 ServletIncludeResponseWrapper responseWrapper = null; 73 74 try { 75 76 logger.debug("Dispatching to servlet path = [" + servletPath + "]"); 77 78 RequestDispatcher sessionDispatcher = jParams.getContext().getRequestDispatcher( servletPath ); 79 80 if (sessionDispatcher == null) { 81 String 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 String forceEncoding = null; 91 forceEncoding = Jahia.getSettings().getDefaultResponseBodyEncoding(); 92 responseWrapper = new ServletIncludeResponseWrapper(jParams.getResponse(), false, forceEncoding); 93 94 if (responseWrapper == null) { 95 String 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 sessionDispatcher.include(requestWrapper, responseWrapper); 106 107 } catch (ServletException se) { 108 String generatedOutput = ""; 109 try { 110 generatedOutput = responseWrapper.getStringBuffer(); 111 } catch (IOException ioe) { 112 logger.error("Error while retrieving generated output", ioe); 113 } 114 logger.error(generatedOutput, se); 115 Throwable t = se; 116 while (t instanceof ServletException ) { 117 ServletException curSe = (ServletException ) t; 118 logger.error(generatedOutput, curSe.getRootCause()); 119 t = curSe.getRootCause(); 120 } 121 return ""; 122 } catch (IOException ioe) { 123 String generatedOutput = ""; 124 try { 125 generatedOutput = responseWrapper.getStringBuffer(); 126 } catch (IOException ioe2) { 127 logger.error("Error while retrieving generated output", ioe2); 128 } 129 logger.debug(generatedOutput, ioe); 130 return ""; 131 } catch (Throwable t) { 132 String generatedOutput = ""; 133 try { 134 generatedOutput = responseWrapper.getStringBuffer(); 135 } catch (IOException ioe) { 136 logger.error("Error while retrieving generated output", ioe); 137 } 138 logger.debug(generatedOutput, t); 139 return ""; 140 } 141 String generatedOutput = ""; 142 try { 143 generatedOutput = responseWrapper.getStringBuffer(); 144 } catch (IOException ioe) { 145 logger.error("Error while retrieving generated output", ioe); 146 } 147 return generatedOutput; 148 } 150 private static Logger logger = Logger.getLogger( 151 JahiaFetcherBaseService.class); 152 153 } | Popular Tags |