📄 servletincluderesponsewrapper.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.applications;import java.io.IOException;import java.io.File;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.Properties;import javax.servlet.ServletContext;import javax.servlet.ServletOutputStream;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletResponseWrapper;import org.apache.regexp.RE;import org.jahia.bin.Jahia;import org.jahia.data.applications.ApplicationBean;import org.jahia.engines.applications.FullScreenDispatcherEngine;import org.jahia.exceptions.JahiaException;import org.jahia.params.ParamBean;import org.jahia.registries.ServicesRegistry;import org.jahia.utils.JahiaConsole;/** * This response wrapper allows the use of a StringPrinterWriter instead of the * regular output stream-based ones. This allows Jahia to get a copy of the output * to enable further processing before sending it back to the browser. * Another important feature of this wrapper is to modify the behaviour of the * encodeURL function call to enable the routing of action URLs to the correct * application. * * Note : the implementation of the getWriter and getOutputStream could be * re-written by using a combination like this one PrintWriter(StringWriter(StringBuffer)) * but this would probably involve a small performance hit (to be tested !). It * would have the advantage of probably removing some code that is a little * unnecessary. (for implementation see org.jahia.bin.JahiaErrorDisplay where * this has been used but performance is not an issue there). * * @author Serge Huber * * @todo We should really split this class into two classes : one with * emulation, one without. It would make a lot of the code much simpler to * maintain. * */public class ServletIncludeResponseWrapper extends HttpServletResponseWrapper { // private StringPrintWriter strPrintWriter; private PrintWriter strPrintWriter = null; private StringServletOutputStream strOutputStream = null; private String appUniqueIDStr; // application ID in String format private ParamBean jahiaParams; private String emulatedURL; private String contentType; private boolean outputStreamCalled = false; private String redirectLocation = null; private boolean isPassThrough = false; private String encoding = "ISO-8859-1"; private String forceEncoding = null; private static final String APP_ID_SEPARATOR = "_"; /** * Pattern used to extract the second id part * **/ private static String mFieldIDPart = "(^\\d+)_\\d+"; /** * The RExp used to extract the role name part in the Application role group name pattern * **/ private static org.apache.regexp.RE mRExpFieldID = null; static { try{ mRExpFieldID = new RE(mFieldIDPart); } catch ( Throwable t ){ JahiaConsole.printe ("Error while creating regular expression ", t ) ; } }// public ServletIncludeResponseWrapper (HttpServletResponse httpServletResponse) {// super (httpServletResponse);//// JahiaConsole.println("ResponseWrapper", "Initializing using normal mode");// isPassThrough = false;// appUniqueIDStr = null;// forceEncoding = null;// if (forceEncoding != null) {// this.contentType = "text/html; charset=" + forceEncoding;// }// } /** * Simple constructor without support for URL redirecting. */ public ServletIncludeResponseWrapper (HttpServletResponse httpServletResponse, boolean isPassThrough, String forceEncoding) { super(httpServletResponse); JahiaConsole.println("ResponseWrapper", "Initializing using normal mode"); appUniqueIDStr = null; this.isPassThrough = isPassThrough; this.forceEncoding = forceEncoding; if (forceEncoding != null) { this.contentType = "text/html; charset=" + forceEncoding; } } public ServletIncludeResponseWrapper(HttpServletResponse httpServletResponse, String appUniqueID, String emulURL, ParamBean jParams, boolean isPassThrough, String forceEncoding) { super(httpServletResponse); JahiaConsole.println("ResponseWrapper", "Initializing using emulation mode. appID = [" + appUniqueID + "]"); appUniqueIDStr = appUniqueID; jahiaParams = jParams; emulatedURL = emulURL; this.isPassThrough = isPassThrough; this.forceEncoding = forceEncoding; if (forceEncoding != null) { this.contentType = "text/html; charset=" + forceEncoding; } } public PrintWriter getWriter() { JahiaConsole.println("ResponseWrapper", "Using a print writer for output"); checkStreams(); if (outputStreamCalled) { JahiaConsole.println("ResponseWrapper", "Servlet compliance warning, OutputStream has already been called, the response output will be reset !"); } return strPrintWriter; } public ServletOutputStream getOutputStream() throws IOException { JahiaConsole.println("ResponseWrapper", "Using an output stream for output"); checkStreams(); this.outputStreamCalled = true; return strOutputStream; } public String getStringBuffer() throws IOException{ return getStringBuffer(true); } public String getStringBuffer(boolean checkWriterError) throws IOException { try { if (outputStreamCalled) { // logger.debug("buffer=[" + strOutputStream.getBuffer(encoding) + "]"); return strOutputStream.getBuffer(); } else { // logger.debug("buffer=[" + strOutputStream.getBuffer(encoding) + "]"); if (strOutputStream == null) { return null; } else { if ( checkWriterError && strPrintWriter.checkError()) { throw new IOException("An error has occured while writing to output"); } return strOutputStream.getBuffer(); } } } catch (UnsupportedEncodingException uee) { JahiaConsole.printe ("Error in encoding [" + encoding + " ], returning empty buffer", uee); return ""; } } public String getRedirectLocation() { return redirectLocation; } public void flushBuffer() { /* if (strPrintWriter != null) { strPrintWriter.flush(); } if (strOutputStream != null) { try { strOutputStream.flush(); } catch (IOException ioe) { JahiaConsole.println("", "Error while flushing output stream", ioe); } } */ JahiaConsole.println("ResponseWrapper", "flushBuffer()"); } public void resetBuffer() { JahiaConsole.println("ResponseWrapper", "resetBuffer()"); } public void finishResponse() { JahiaConsole.println("ResponseWrapper", "finishResponse()"); } public boolean isCommitted() { JahiaConsole.println("ResponseWrapper", "isCommitted()"); return false; } /** * The purpose of this function is to transform relative URL for a Jahia aggregation. * * The issue here is that Jahia uses it's own parameters, and aggregates applications that * also use their own parameters. Therefore Jahia must encode the URL of the application so * that it is able to dispatch the call to the correct app and not misinterpret it for one * of it's own parameters. * * <pre> * Example: * Let's say the application generates a URL like the following one : * /servlet/Test?arg1=2&arg3=4 * Jahia must encode it into something like this : * ?context=appid&appargs=%2Fservlet%2FTest%3Farg1%3D2%26arg3%3D * </pre> * */ public String encodeURL(String URL) { JahiaConsole.println("ResponseWrapper", "encodeURL(" + URL + ")"); if ( URL == null ){ return null; } String ServletIncludeURL; if ((jahiaParams != null) && (appUniqueIDStr != null)) { if ( URL.toLowerCase().indexOf(".jsp") == -1 ) { try { // Check if we should add Jahia extra URL encoding or not. // We don't if the resource is not a servlet ( or jsp ) ApplicationBean appBean = ServicesRegistry.getInstance() .getJahiaApplicationsManagerService() .getApplication( Integer.parseInt(appUniqueIDStr.substring(appUniqueIDStr.indexOf(APP_ID_SEPARATOR)+1)) ); if (appBean == null) { JahiaConsole.println("ResponseWrapper", "The requested application [" + appUniqueIDStr + "] is not available"); } ServletContext dispatchedContext = Jahia.getThreadParamBean() .getContext().getContext(appBean.getContext()); if (dispatchedContext == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -