📄 applicationcontext.java
字号:
// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////////// NK 18.06.2002////package org.jahia.data.applications;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import org.jahia.utils.JahiaConsole;import org.jahia.services.applications.ServletIncludeRequestWrapper;/** * Holds information for a given web application context. * Some of them are: servlets, roles, servlet mapping, welcome files. * * @author Khue Nguyen <a href="mailto:khue@jahia.com">khue@jahia.com</a> */public class ApplicationContext{ private static final String CLASS_NAME = ApplicationContext.class.getName(); /** the application display name **/ private String displayName = ""; /** the application context **/ private String context = ""; /** the application description **/ private String descr = ""; /** The HashMap of servlet bean , keyed by the servlet name **/ private HashMap servlets = new HashMap(); /** * The hashMap of servlet mapping, keyed with the pattern used to map a servlet. * The value is the servlet name. **/ private HashMap servletMappings = new HashMap(); /** vector of security roles **/ private Vector roles = new Vector(); /** The list of Welcome files **/ private Vector welcomeFiles = new Vector(); //-------------------------------------------------------------------------- /** * Constructor * */ public ApplicationContext(String context){ this.context = context; } //-------------------------------------------------------------------------- /** * Constructor * * @param context , the application context * @param displayName , the application display name * @param descr, the application description * @param servlets, a vector ServletBean * @param servletMappings, a map of servletMappings keyed with the url pappern and value = the servlet name * @param welcomeFiles, a vector of welcome files (String) */ public ApplicationContext( String context, String displayName, String descr, Vector servlets, HashMap servletMappings, Vector roles, Vector welcomeFiles ) { this.context = context; if ( displayName != null ){ this.displayName = displayName; } if ( descr != null ){ this.descr = descr; } addServlets(servlets); setServletMappings(servletMappings); setRoles(roles); setWelcomeFiles(welcomeFiles); } //-------------------------------------------------------------------------- /** * Add a Vector of ServletBean. * * @param servlets */ public void addServlets(Vector servlets) { synchronized (this.servlets) { if ( servlets!=null ){ int size = servlets.size(); ServletBean servlet = null; for ( int i=0 ; i<size ; i++ ){ servlet = (ServletBean)servlets.get(i); if ( servlet!=null && servlet.getServletName() != null ){ this.servlets.put(servlet.getServletName(), servlet); } } } } } //-------------------------------------------------------------------------- /** * Add a new servlet bean. * * @param ServletBean */ public void addServlet(ServletBean servlet) { synchronized (servlets) { if ( servlet!=null && servlet.getServletName() != null ){ servlets.put(servlet.getServletName(), servlet); } } } //-------------------------------------------------------------------------- /** * Get a servlet looking at it name. * * @param name , the servlet name */ public ServletBean getServlet(String name) { synchronized (servlets) { if ( name!=null){ return (ServletBean)servlets.get(name); } } return null; } //-------------------------------------------------------------------------- /** * Set the map of servlet mapping. * * @param servletMappings */ public void setServletMappings(HashMap servletMappings) { synchronized (this.servletMappings) { if ( servletMappings != null ){ this.servletMappings = servletMappings; } } } //-------------------------------------------------------------------------- /** * Add a new servlet mapping and replace old mapping with same pattern. * * @param pattern * @param name , the servlet name */ public void addServletMapping(String pattern, String name) { synchronized (servletMappings) { servletMappings.put(pattern, name); } } //-------------------------------------------------------------------------- /** * Return the servlet name mapped by the given pattern * otherwise return null. * * @param pattern the url-pattern */ public String findServletMapping(String pattern) { synchronized (servletMappings) { return ((String) servletMappings.get(pattern)); } } //-------------------------------------------------------------------------- /** * Return the vector of servlets. * * @return servlets */ public HashMap getServlets() { return servlets; } //-------------------------------------------------------------------------- /** * Return a servlet mapping pattern that matches the given request URI. * * @param pattern the request URI * @return pattern , the servlet mapping pattern or null if not found. */ public String findServletMappingFromRequestURI(String requestURI) { JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI", "Started for requestURI [" + requestURI + "]"); if ( requestURI == null ){ return null; } String decRequestURI = ServletIncludeRequestWrapper.URLDecode(requestURI); if ( !decRequestURI.startsWith(context) ){ // invalid request URI return null; } int pos = decRequestURI.indexOf(context); String str = decRequestURI.substring(context.length()); pos = str.indexOf("?"); if ( pos != -1 ){ str = str.substring(0,pos); } pos = str.indexOf(";"); if ( pos != -1 ){ str = str.substring(0,pos); } JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI", "Servlet Path + PathInfo [" + str + "]"); // try matching with exact mapping String pattern = findServletExactMapping(str); if ( pattern == null ){ // try matching with path mapping pattern = findServletPathMapping(str); } if ( pattern == null ){ // try matching with extension mapping pattern = findServletExtensionMapping(str); } if ( pattern == null && findServletMapping("/")!=null ){ // the default servlet if any pattern = "/"; } JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI", "Result [" + pattern + "]"); return pattern; } //-------------------------------------------------------------------------- /** * Set roles. * * @param roles a vector of security roles */ public void setRoles(Vector roles) { synchronized (this.roles) { if ( roles != null ){ this.roles = roles; } } } //-------------------------------------------------------------------------- /** * Add a new security role. * * @param role New security role */ public void addRole(String role) { synchronized (roles) { roles.add(role); } } //-------------------------------------------------------------------------- /** * Return true if a given role is already in the list. * * @param role the security role to look for */ public boolean findRole(String role) { if ( role == null ){ return false; } synchronized (roles) { return roles.contains(role); } } //-------------------------------------------------------------------------- /** * Return the security roles defined for this application. */ public Vector getRoles() { return roles; } //-------------------------------------------------------------------------- /** * Set the welcome files. * * @param welcomeFiles */ public void setWelcomeFiles(Vector welcomeFiles) { synchronized (this.welcomeFiles) { if ( welcomeFiles != null ){ this.welcomeFiles = welcomeFiles; } } } //-------------------------------------------------------------------------- /** * Add a new welcome file. * * @param filename New welcome file name */ public void addWelcomeFile(String filename) { synchronized (welcomeFiles) { welcomeFiles.add(filename); } } //-------------------------------------------------------------------------- /** * Return the list of welcome file. * * @return welcomeFiles */ public Vector getWelcomeFiles() { return welcomeFiles; } //-------------------------------------------------------------------------- /** * Return the context * */ public String getContext(){ return context; } //-------------------------------------------------------------------------- /** * Set the context * * @param String val */ public void setContext(String val){ context = val; } //-------------------------------------------------------------------------- /** * Return the display name * */ public String getDisplayName(){ return displayName; } //-------------------------------------------------------------------------- /** * Set the display name * * @param String val */ public void setDisplayName(String val){ displayName = val; } //-------------------------------------------------------------------------- /** * Return the descr * */ public String getDescr(){ return descr; } //-------------------------------------------------------------------------- /** * Set the descr * * @param String val */ public void setDescr(String val){ descr = val; } //-------------------------------------------------------------------------- /** * Find an exact servlet mapping pattern matching the given path if any * * @param String path ( servlet pat + path info + querystring ) */ private String findServletExactMapping(String path){ if ( path == null ){ return null; } Iterator iterator = servletMappings.keySet().iterator(); String pattern = null; String result = ""; while ( iterator.hasNext() ) { pattern = (String)iterator.next(); if ( !pattern.equals("/") || !pattern.startsWith("*.") || !(pattern.startsWith("/") && pattern.endsWith("/*")) ) { // we've got an exact mapping pattern if ( path.startsWith(pattern) ){ boolean match = false; if ( pattern.length() == path.length() ){ match = true; } else if ( pattern.endsWith("/") ){ match = true; } else if ( path.charAt(pattern.length())=='/' ) { match = true; } if ( match && (pattern.length() > result.length()) ){ result = pattern; } } } } JahiaConsole.println( CLASS_NAME+".findServletExactMapping", "result [" + result + "]"); if ( result.length() == 0 ){ return null; } return result; } //-------------------------------------------------------------------------- /** * Find a servlet path mapping pattern matching the given path if any * * @param String path ( servlet pat + path info + querystring ) */ private String findServletPathMapping(String path){ if ( path == null ){ return null; } Iterator iterator = servletMappings.keySet().iterator(); String pattern = null; String result = ""; while ( iterator.hasNext() ) { pattern = (String)iterator.next(); if ( pattern.startsWith("/") && pattern.endsWith("/*") ) { // we've got a path mapping pattern if ( path.startsWith(pattern.substring(0,pattern.length()-1)) && pattern.length() > result.length() ) { result = pattern; } } } JahiaConsole.println( CLASS_NAME+".findServletPathMapping", "result [" + result + "]"); if ( result.length() == 0 ){ return null; } return result; } //-------------------------------------------------------------------------- /** * Find a servlet extension mapping pattern matching the given path if any * * @param String path ( servlet pat + path info + querystring ) */ private String findServletExtensionMapping(String path){ if ( path == null ){ return null; } int pos = path.indexOf("?"); String str = path; if ( pos != -1 ){ str = path.substring(pos); } Iterator iterator = servletMappings.keySet().iterator(); String pattern = null; String result = ""; while ( iterator.hasNext() ) { pattern = (String)iterator.next(); if ( pattern.startsWith("*.") ) { // we've got a path mapping pattern if ( path.indexOf(pattern.substring(2)) != -1 && pattern.length() > result.length() ) { result = pattern; } } } JahiaConsole.println( CLASS_NAME+".findServletExtensionMapping", "result [" + result + "]"); if ( result.length() == 0 ){ return null; } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -