1 package org.jahia.urls; 2 3 import java.net.MalformedURLException ; 4 import javax.servlet.http.HttpServletRequest ; 5 import org.jahia.utils.InsertionSortedMap; 6 import java.util.StringTokenizer ; 7 import java.util.NoSuchElementException ; 8 import java.util.Map.Entry; 9 10 18 19 public class ContentServletURL extends ServletURL { 20 21 private static org.apache.log4j.Logger logger = 22 org.apache.log4j.Logger.getLogger (ContentServletURL.class); 23 24 InsertionSortedMap pathInfoParameters = new InsertionSortedMap(); 25 26 public ContentServletURL (HttpServletRequest request) 27 throws MalformedURLException { 28 super(request); 29 } 30 31 public ContentServletURL (HttpServletRequest request, String url, boolean ignoringAuthorityInTest) throws MalformedURLException { 32 super(); 33 if (!ContentServletURL.isContentServletURL(request, url, 34 ignoringAuthorityInTest)) { 35 throw new MalformedURLException ("URL " + url + 36 " is not a valid content servlet URL"); 37 } 38 39 42 ContentServletURL requestURL = new ContentServletURL(request); 43 URI targetURI = new URI(url); 44 QueryMapURL targetURL = null; 45 if (targetURI.isRelative()) { 46 String curRequestURI = requestURL.getRequestURI(); 47 int lastSlashPos = curRequestURI.lastIndexOf("/"); 48 if (lastSlashPos != -1) { 49 String newURI = curRequestURI.substring(0, lastSlashPos+1) + url; 50 targetURL = new QueryMapURL(newURI); 51 } else { 52 targetURL = new QueryMapURL(url); 54 } 55 } else { 56 targetURL = new QueryMapURL(url); 57 } 58 setScheme(targetURL.getScheme()); 59 setUserInfo(targetURL.getUserInfo()); 60 setHostName(targetURL.getHostName()); 61 setPort(targetURL.getPort()); 62 String targetPath = targetURL.getPath(); 66 String targetServletPath = targetPath.substring(requestURL. 67 getContextPath().length()); 68 int queryPos = targetServletPath.indexOf("?"); 69 int fragmentPos = targetServletPath.indexOf("#"); 70 String targetQueryString = null; 71 String targetFragmentString = null; 72 String targetPathInfo; 73 if (queryPos > 0) { 74 targetPathInfo = targetServletPath.substring(requestURL. 75 getServletPath().length(), queryPos); 76 if (fragmentPos > 0) { 77 targetQueryString = targetServletPath.substring(queryPos+1, fragmentPos); 78 targetFragmentString = targetServletPath.substring(fragmentPos+1); 79 } else { 80 targetQueryString = targetServletPath.substring(queryPos+1); 81 } 82 } else if (fragmentPos > 0) { 83 targetPathInfo = targetServletPath.substring(requestURL. 84 getServletPath().length(), fragmentPos); 85 targetFragmentString = targetServletPath.substring(fragmentPos+1); 86 } else { 87 targetPathInfo = targetServletPath.substring(requestURL. 88 getServletPath().length()); 89 } 90 setPathInfo(targetPathInfo); 91 setQueryString(targetQueryString); 92 setFragmentString(targetFragmentString); 93 } 94 95 static public boolean isContentServletURL (HttpServletRequest request, 96 String url, boolean ignoringAuthorityInTest) 97 throws MalformedURLException { 98 ContentServletURL requestURL = new ContentServletURL(request); 99 String useURL = url; 100 URI targetURL = new URI(useURL); 101 if ((!targetURL.isURIStartingAtPath()) && 105 (!targetURL.isRelative()) && 106 (!ignoringAuthorityInTest)) { 107 if (!requestURL.getScheme().equals(targetURL.getScheme())) { 108 return false; 109 } 110 if (!requestURL.getHostName().equals(targetURL.getHostName())) { 111 return false; 112 } 113 if (requestURL.getPort() != targetURL.getPort()) { 114 return false; 115 } 116 } 117 if (!targetURL.isRelative()) { 118 String targetPath = targetURL.getPath(); 119 if (!targetPath.startsWith(requestURL.getContextPath())) { 120 return false; 121 } 122 String targetServletPath = targetPath.substring(requestURL. 123 getContextPath().length()); 124 if (!targetServletPath.startsWith(requestURL.getServletPath())) { 125 return false; 126 } 127 } 128 131 if (targetURL.getScheme() != null 133 && "javascript".equals(targetURL.getScheme().toLowerCase())) { 134 return false; 135 } 136 141 return true; 142 } 143 144 public String getPathInfoParameter(String name) { 145 return (String ) pathInfoParameters.get(name); 146 } 147 148 public String setPathInfoParameter(String name, String value) { 149 return (String ) pathInfoParameters.put(name, value); 150 } 151 152 public String getPathInfo() { 153 if (pathInfoParameters.size() == 0) { 154 return null; 155 } 156 StringBuffer result = new StringBuffer (); 157 java.util.Iterator parameterIter = pathInfoParameters.entrySet().iterator(); 158 while (parameterIter.hasNext()) { 159 Entry curEntry = (Entry) parameterIter.next(); 160 result.append("/"); 161 result.append(curEntry.getKey()); 162 result.append("/"); 163 result.append(curEntry.getValue()); 164 } 165 return result.toString(); 166 167 } 168 169 public void setPathInfo(String pathInfo) { 170 super.setPathInfo(pathInfo); 171 parsePathInfoParameters(pathInfo); 172 } 173 174 private void parsePathInfoParameters (String pathInfo) { 175 177 if (pathInfo != null) { 178 179 if (pathInfo.lastIndexOf(";jsessionid=") != -1) { 180 int sessionIDPos = pathInfo.lastIndexOf(";jsessionid="); 182 pathInfo = pathInfo.substring(0, sessionIDPos); 183 logger.debug("Removed session ID marker from end of path info"); 184 } 185 186 if (pathInfo.lastIndexOf(".") != -1) { 187 int lastSlash = pathInfo.lastIndexOf("/"); 189 if (lastSlash != -1) { 190 String fakeStaticName = pathInfo.substring(lastSlash + 1); 191 pathInfo = pathInfo.substring(0, lastSlash); 192 logger.debug("Removed fake static ending. pathInfo=[" + 193 pathInfo + "] fakeEnding=[" + 194 fakeStaticName + "]"); 195 } 196 } 197 198 try { 199 StringTokenizer st = new StringTokenizer (pathInfo, "/"); 200 201 while (st.hasMoreTokens()) { 202 String token = st.nextToken(); 203 pathInfoParameters.put(token, st.nextToken()); 204 } 205 206 } catch (NoSuchElementException nee) { 207 } 209 } 210 } 211 212 } 213 | Popular Tags |