📄 servletincluderequestwrapper.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////////package org.jahia.services.applications;import java.io.PrintWriter;import java.io.StringWriter;import java.security.Principal;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.Vector;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpSession;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.JahiaSessionExpirationException;import org.jahia.params.ParamBean;import org.jahia.registries.ServicesRegistry;import org.jahia.services.usermanager.GenericPrincipal;import org.jahia.services.usermanager.JahiaGroup;import org.jahia.services.usermanager.JahiaUser;import org.jahia.security.license.LicenseConstants;import org.jahia.utils.JahiaConsole;/** * This wrapper around the request object has multiple responsabilities, such as * making sure that the application gets only it's local parameter set, and not * Jahia's. * @author Serge Huber * @version 1.0 * @todo This emulation is not complete, mostly for development time reasons. * @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. * @todo FIXME It seems that the hasRequestURI() handling is not correct, * notably for the following methods : getParameter* should return a combination * of the original request parameters and the include/forward parameters, * getQueryString() should be concatenated with the original query (is this * correct) */public class ServletIncludeRequestWrapper extends HttpServletRequestWrapper { private String emulatedContextPath; private java.net.URL emulatedURL; private String emulatedMethod; private Map parameters = new Hashtable(); private boolean parsed = false; private HttpSessionWrapper sessionWrapper; private PersistantServletRequest appRequest; private ParamBean jahiaParams; private String contextIdentifier; // an application is instantied only private String emulatedServletPath = ""; private String emulatedPathInfo = ""; private String initialRequestURI = ""; // used to detect forward between servlet private boolean applicationCacheOn = true; private long applicationCacheExpirationDelay = -1; private boolean inheritJahiaSessionAttributes = false; /** * This constructor creates a new emulated request object by substituting Jahia's URL with * the application's URL. This way the application will have the impression it is executing * itself normally within it's context. It remains to be seen if it seems reasonable to undertake * the full emulation of this object, or if only parts will suffice (let's hope so !) * A good test of this emulation would be to run Jahia within Jahia. * * @param httpServletRequest Jahia's original request object * @param emulURL The new URL to be substituted with Jahia's. * @param emulContextPath The path of the emulated context * @param emulMethod the method (GET, POST, etc...) that the wrapper emulates * @param applicationRequest * @param contextID identifier containing the string that represents the context * ID of the running application (usually the fieldID) * @param jParams Jahia's param bean object */ public ServletIncludeRequestWrapper(HttpServletRequest httpServletRequest, String emulURL, String emulContextPath, String emulMethod, PersistantServletRequest applicationRequest, String contextID, boolean fullScreenActivated, boolean inheritJahiaSessionAttributes, ParamBean jParams) { super(httpServletRequest); initialRequestURI = super.getRequestURI(); emulatedContextPath = emulContextPath; emulatedMethod = emulMethod; appRequest = applicationRequest; jahiaParams = jParams; contextIdentifier = contextID; this.inheritJahiaSessionAttributes = inheritJahiaSessionAttributes; parsed = false; // here we should add code that will decode the Jahia parameters and construct // the emulated request body <-- try { String URLPort; if (jahiaParams.getRequest().getServerPort() == 80) { URLPort = ""; } else { Integer conv = new Integer(jahiaParams.getRequest().getServerPort()); URLPort = ":" + conv.toString(); } java.net.URL contextURL = new java.net.URL (jahiaParams.getRequest().getScheme() + "://" + jahiaParams.getRequest().getServerName() + URLPort + "/"); /* // STRUTS ISSUE : // Struts use Servlet Action with extension, // i.e /strutsApp/myAction.do // so we have to give the emulated URL , the appearance of an URL instead of a file if ( appRequest.getServletMappingPattern().startsWith("*.") && emulURL.endsWith(appRequest.getServletMappingPattern().substring(2)) ){ emulURL += "?jahiastrutsaction=true"; appRequest.setURL(emulURL); } */ emulatedURL = new java.net.URL(contextURL, emulURL); httpServletRequest.setAttribute("org.portletapi.portal", "true"); /** @todo Add a security check before enabling this attribute */ httpServletRequest.setAttribute("org.portletapi.userlist", applicationRequest.getAppContextUsers()); httpServletRequest.setAttribute("org.portletapi.contextid", contextID); if (fullScreenActivated) { httpServletRequest.setAttribute("org.portletapi.fullscreen", "true"); } else { httpServletRequest.setAttribute("org.portletapi.fullscreen", "false"); } httpServletRequest.setAttribute("org.jahia.sitekey", jParams.getSiteKey()); httpServletRequest.setAttribute("org.jahia.siteurl", jParams.getSiteURL()); httpServletRequest.setAttribute("org.jahia.siteid", Integer.toString(jParams.getSiteID())); httpServletRequest.setAttribute("org.jahia.pageid", Integer.toString(jParams.getPageID())); httpServletRequest.setAttribute("org.jahia.operationmode", jParams.getOperationMode()); // can be used by the application to perform some license check /** @todo FIXME this is here in case some legacy applications still use this, but should be removed ! */ httpServletRequest.setAttribute("org.jahia.license.licenseType", new Integer(LicenseConstants.PROJODA_LICENSE_TYPE)); parseParameters(httpServletRequest); parseEmulatedURL(URLDecode(emulURL)); } catch (java.net.MalformedURLException mue) { JahiaConsole.printe ("Malformed URL", mue); emulatedURL = null; } catch (JahiaException je) { JahiaConsole.printe ("JahiaException in constructor of request wrapper", je); emulatedURL = null; } JahiaConsole.println("RequestWrapper", "emulatedURL value : [" + emulatedURL + "]"); JahiaConsole.println("RequestWrapper", "initial requestURI value : [" + initialRequestURI + "]"); } /** * Returns true if the initial requestURI and the value returned by super.getRequestURI() * is different. If true, this means a servlet include or forward has been performed. * When true, any emulated value should be ignored and the value returned by * wrapped methods like getServletPath(), getPathInfo(), getRequestURI(), getRequestURL() * should be the real value ! ( calling super.getServletPath(), super.getPathInfo(), ... ) */ public boolean hasRequestURIChanged() { //return ( super.getContentLength() != this.initiaContentLength ); if ( !super.getRequestURI().equals(initialRequestURI) ){ JahiaConsole.println("RequestWrapper", "Request URI Has changed !"); return true; } return false; } /** * Parses the emulated URL using servlet mapping to correctly emulate Servlet Path, Path Info... */ private void parseEmulatedURL(String url) throws JahiaException { if ( url == null ){ String errMsg = "App emulated URL is null !"; throw new JahiaException(errMsg,errMsg,JahiaException.ERROR,JahiaException.APPLICATION_ERROR); } int pos = url.indexOf(appRequest.getContext()); if ( pos == -1 ){ String errMsg = "App emulated URL doesn't contains the application context path !"; throw new JahiaException(errMsg,errMsg,JahiaException.ERROR,JahiaException.APPLICATION_ERROR); } String path = url.substring(pos+appRequest.getContext().length()); int pos2 = path.indexOf("?"); // Servlet mapping check String servletMappingPattern = appRequest.getServletMappingPattern(); if ( servletMappingPattern != null ){ if ( servletMappingPattern.equals("/") ) { // default servlet this.emulatedServletPath = ""; this.emulatedPathInfo = ""; } else if ( servletMappingPattern.startsWith("/") && servletMappingPattern.endsWith("/*") ) { // path mapping this.emulatedServletPath = servletMappingPattern.substring(0,servletMappingPattern.length()-2); if ( this.emulatedServletPath != null ) { pos = path.indexOf(this.emulatedServletPath); if ( pos != -1 ){ if ( pos2 == -1 ){ this.emulatedPathInfo = path.substring(this.emulatedServletPath.length()); } else { this.emulatedPathInfo = path.substring(this.emulatedServletPath.length(),pos2); } } } } else if ( servletMappingPattern.startsWith("*.") ){ // extension mapping /** @todo This looks buggy... emulatedServletPath is not set initially ! And what about path info ? */ pos = path.indexOf(this.emulatedServletPath); if ( pos != -1 ){ if ( pos2 == -1 ){ this.emulatedServletPath = path.substring(this.emulatedServletPath.length()); } else { this.emulatedServletPath = path.substring(this.emulatedServletPath.length(),pos2); } } } else if ( !servletMappingPattern.equals("") ){ // exact mapping this.emulatedServletPath = servletMappingPattern; if ( this.emulatedServletPath != null ) { pos = path.indexOf(this.emulatedServletPath); if ( pos != -1 ){ if ( pos2 == -1 ){ this.emulatedPathInfo = path.substring(this.emulatedServletPath.length()); } else { this.emulatedPathInfo = path.substring(this.emulatedServletPath.length(),pos2); } } } } else { if ( appRequest.getWebAppType() == PersistantServletRequest.JSP_TYPE ){ // we got a welcome file this.emulatedServletPath = appRequest.getservletsrc(); } } } JahiaConsole.println ("RequestWrapper", "url = [" + url + "]"); JahiaConsole.println ("RequestWrapper", "emulated servlet path = [" + this.emulatedServletPath + "]"); JahiaConsole.println ("RequestWrapper", "emulated path info = [" + this.emulatedPathInfo + "]"); } /** * This is a version of the wrapper for dispatcher's that don't need to emulate URLs * @param httpServletRequest Jahia's original request object */ public ServletIncludeRequestWrapper(HttpServletRequest httpServletRequest) { super(httpServletRequest); emulatedContextPath = super.getContextPath(); emulatedMethod = null; emulatedURL = null; } public String getRemoteUser() { if (emulatedURL != null) { JahiaUser user = jahiaParams.getUser(); if (user != null) { // Hollis , cos getName return the user key and not the user login name //return user.getName(); return user.getUsername(); } else { return ""; } } else { return super.getRemoteUser(); } } /** * */ public Principal getUserPrincipal() { if (emulatedURL != null) { JahiaUser user = jahiaParams.getUser(); if (user != null) { GenericPrincipal gPrincipal = new GenericPrincipal(user); return gPrincipal; } else { return null; } } else { return super.getUserPrincipal(); } } public RequestDispatcher getRequestDispatcher(String path) { JahiaConsole.println("RequestWrapper", "path = [" + path + "]"); if (emulatedURL != null) { ServletContext dispatchedContext = jahiaParams.getContext().getContext(emulatedContextPath); if (dispatchedContext == null) { JahiaConsole.println("RequestWrapper", "Error getting request context " + emulatedContextPath + " for app " + appRequest.getName()); } // Now let's retrieve that context's dispatcher object. RequestDispatcher sessionDispatcher = dispatchedContext.getRequestDispatcher(path); return sessionDispatcher; } else { return super.getRequestDispatcher(path); } } public boolean isUserInRole(String role) { // This method maps servlet roles on Jahia's groups if (emulatedURL != null) { if (jahiaParams == null) { JahiaConsole.println("RequestWrapper", "Jahia Params is null, exiting immediately..."); return false; } JahiaUser user = jahiaParams.getUser();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -