1 package org.jahia.urls; 2 3 import java.net.MalformedURLException ; 4 import javax.servlet.http.HttpServletRequest ; 5 6 14 15 public class ServletURL extends QueryMapURL { 16 17 private String contextPath; 18 private String servletPath; 19 private String pathInfo; 20 21 public ServletURL () { 22 super(); 23 } 24 25 public ServletURL (HttpServletRequest request) 26 throws MalformedURLException { 27 this(request, true); 28 } 29 30 public ServletURL (HttpServletRequest request, boolean relative) 31 throws MalformedURLException { 32 this(request.getScheme(), request.getServerName(), 33 request.getServerPort(), request.getContextPath(), 34 request.getServletPath(), request.getPathInfo(), 35 request.getQueryString(), relative); 36 } 37 38 public ServletURL (String contextPath, String servletPath, String pathInfo, 39 String queryString) throws MalformedURLException { 40 this(null, null, -1, contextPath, servletPath, pathInfo, queryString, true); 41 } 42 43 public ServletURL (String scheme, String serverName, int serverPort, 44 String contextPath, String servletPath, String pathInfo, 45 String queryString, boolean relative) 46 throws MalformedURLException { 47 48 this(); 49 setScheme(scheme); 50 setHostName(serverName); 51 setPort(serverPort); 52 53 this.contextPath = contextPath; 54 this.servletPath = servletPath; 55 this.pathInfo = pathInfo; 56 setPath(contextPath, servletPath, pathInfo); 57 setQueryString(queryString); 58 59 setURIStartingAtPath(relative); 60 61 } 62 63 73 static public boolean matchesServletPattern(String pattern, String servletPath) { 74 if ((pattern == null) || (servletPath == null)) { 75 return false; 76 } 77 if ((pattern.startsWith("/") && pattern.endsWith("/*"))) { 79 String pathToMatch = pattern.substring(0, pattern.length()-2); 81 if (servletPath.startsWith(pathToMatch)) { 82 return true; 83 } else { 84 return false; 85 } 86 } else if (pattern.startsWith("*.")) { 87 int lastPointPos = servletPath.lastIndexOf("."); 89 if (lastPointPos == -1) { 90 return false; 91 } 92 String extension = servletPath.substring(lastPointPos + 1); 93 String patternExt = pattern.substring(2); 94 if (patternExt.equals(extension)) { 95 return true; 96 } else { 97 return false; 98 } 99 } else if (!pattern.equals("/")) { 100 if (pattern.equals(servletPath)) { 102 return true; 103 } else { 104 return false; 105 } 106 } else { 107 return true; 109 } 110 } 111 112 public String getContextPath () { 113 return contextPath; 114 } 115 116 public void setContextPath(String contextPath) { 117 this.contextPath = contextPath; 118 setPath(getContextPath(), contextPath, getPathInfo()); 119 } 120 121 public String getServletPath () { 122 return servletPath; 123 } 124 125 public void setServletPath(String servletPath) { 126 this.servletPath = servletPath; 127 setPath(getContextPath(), servletPath, getPathInfo()); 128 } 129 130 public String getPathInfo () { 131 return pathInfo; 132 } 133 134 public void setPathInfo(String pathInfo) { 135 this.pathInfo = pathInfo; 136 setPath(getContextPath(), getServletPath(), pathInfo); 137 } 138 139 public String getRequestURI () { 140 return getPath(); 141 } 142 143 private void setPath(String contextPath, String servletPath, String pathInfo) { 144 StringBuffer result = new StringBuffer (); 145 result.append(contextPath); 146 if (servletPath != null) { 147 result.append(servletPath); 148 } 149 if (pathInfo != null) { 150 result.append(pathInfo); 151 } 152 super.setPath(result.toString()); 153 } 154 155 156 } | Popular Tags |