1 package org.jahia.urls; 2 3 import org.apache.regexp.RE; 4 import org.apache.regexp.RESyntaxException; 5 import java.util.Map ; 6 import java.util.HashMap ; 7 import java.io.UnsupportedEncodingException ; 8 9 17 18 public class URI { 19 20 private static org.apache.log4j.Logger logger = 21 org.apache.log4j.Logger.getLogger (URI.class); 22 23 private static RE uriRegexp; 24 static { 25 try { 26 uriRegexp = new RE( 27 "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"); 28 } catch (RESyntaxException rese) { 29 logger.error("Error in regexp syntax", rese); 30 } 31 } 32 33 static private final Object [][] defaultSchemePortArray = { 34 { "http", new Integer (80) }, 35 { "https", new Integer (443) } 36 }; 37 38 private boolean uriStartingAtPath = false; 39 private boolean relative = false; 40 41 private String scheme = null; 42 private String authority = null; 43 private String userInfo = null; 44 private String hostName = null; 45 private int port = -1; 46 private String path = null; 47 private String queryString = null; 48 private String fragmentString = null; 49 50 private Map defaultSchemePorts; 51 52 private static String defaultEncoding = URICodec.getDefaultEncoding(); 53 private String encoding = defaultEncoding; 54 55 static public String getDefaultEncoding() { 56 return defaultEncoding; 57 } 58 59 static public void setDefaultEncoding(String encoding) { 60 defaultEncoding = encoding; 61 } 62 63 public URI() { 64 init(); 65 } 66 67 public URI(String uri) { 68 this(); 69 setURI(uri); 70 } 71 72 82 public void setURI(String uri) { 83 if (uri == null) { 84 return; 85 } 86 if (uri.indexOf("/") == 0) { 87 uriStartingAtPath = true; 88 parseURI(uri); 89 } else { 90 parseURI(uri); 91 } 92 } 93 94 98 public void setURI(java.net.URL javaURL) { 99 if (javaURL == null) { 100 return; 101 } 102 parseURI(javaURL.toString()); 103 } 104 105 113 public String toString(String anEncoding) { 114 StringBuffer result = new StringBuffer (); 115 try { 116 if (!uriStartingAtPath) { 117 if (getScheme() != null) { 118 result.append(URICodec.encode(getScheme(), anEncoding, 119 URICodec.ALPHANUM_CHARS)); 120 result.append(":"); 121 } 122 if (getAuthority() != null) { 123 result.append("//"); 124 result.append(URICodec.encode(getAuthority(), anEncoding, 125 URICodec. 126 AUTHORITY_AUTHORIZEDCHARS)); 127 } 128 } 129 if (getPath() != null) { 130 result.append(URICodec.encode(getPath(), anEncoding, 131 URICodec.PATH_AUTHORIZEDCHARS)); 132 } 133 134 if (getQueryString() != null) { 136 result.append("?"); 137 result.append(URICodec.encode(getQueryString(), anEncoding, URICodec.QUERY_AUTHORIZEDCHARS)); 138 } 139 140 if (getFragmentString() != null) { 141 result.append("#"); 142 result.append(URICodec.encode(getFragmentString(), anEncoding, URICodec.FRAGMENT_AUTHORIZEDCHARS)); 143 } 144 } catch (UnsupportedEncodingException uee) { 145 logger.error("Unsupported encoding: " + anEncoding, uee); 146 } 147 if (result.toString().length() == 0) { 148 return null; 149 } 150 return result.toString(); 151 } 152 153 161 public String toString(javax.servlet.http.HttpServletResponse response) { 162 if (response == null) { 163 return null; 164 } 165 return response.encodeURL(toString()); 166 } 167 168 public String toString(javax.servlet.http.HttpServletResponse response, String anEncoding) { 169 if (response == null) { 170 return null; 171 } 172 return response.encodeURL(toString(anEncoding)); 173 } 174 175 public String toString() { 176 return toString(encoding); 177 } 178 179 186 public void setURIStartingAtPath(boolean anUriStartingAtPath) { 187 this.uriStartingAtPath = anUriStartingAtPath; 188 } 189 190 194 public boolean isURIStartingAtPath() { 195 return uriStartingAtPath; 196 } 197 198 public String getScheme() { 199 return scheme; 200 } 201 202 public void setScheme(String aScheme) { 203 this.scheme = aScheme; 204 } 205 206 public String getAuthority() { 207 return authority; 208 } 209 210 public void setAuthority(String anAuthority) { 211 this.authority = anAuthority; 212 if (anAuthority != null) { 213 String curSubString = anAuthority; 214 int userInfoSepPos = anAuthority.indexOf("@"); 215 if (userInfoSepPos > 0) { 216 userInfo = anAuthority.substring(0, userInfoSepPos); 217 curSubString = anAuthority.substring(userInfoSepPos+1); 218 } 219 int portSepPos = curSubString.indexOf(":"); 220 if (portSepPos > 0) { 221 hostName = curSubString.substring(0, portSepPos); 222 port = Integer.parseInt(curSubString.substring(portSepPos+1)); 223 } else { 224 hostName = curSubString; 225 port = getSchemeDefaultPort(scheme); 226 } 227 } 228 } 229 230 public String getUserInfo() { 231 return userInfo; 232 } 233 234 public void setUserInfo(String anUserInfo) { 235 this.userInfo = anUserInfo; 236 setAuthority(anUserInfo, hostName, port); 237 } 238 239 public String getHostName() { 240 return hostName; 241 } 242 243 public void setHostName(String aHostName) { 244 this.hostName = aHostName; 245 setAuthority(userInfo, aHostName, port); 246 } 247 248 public int getPort() { 249 return port; 250 } 251 252 public void setPort(int aPort) { 253 this.port = aPort; 254 setAuthority(userInfo, hostName, aPort); 255 } 256 257 public String getPath() { 258 return path; 259 } 260 261 public void setPath(String aPath) { 262 this.path = aPath; 263 } 264 265 public String getQueryString() { 266 return queryString; 267 } 268 269 public void setQueryString(String aQueryString) { 270 this.queryString = aQueryString; 271 } 272 273 public String getFragmentString() { 274 return fragmentString; 275 } 276 277 public void setFragmentString(String aFragmentString) { 278 this.fragmentString = aFragmentString; 279 } 280 281 public String getEncoding() { 282 return encoding; 283 } 284 285 public void setEncoding(String anEncoding) { 286 this.encoding = anEncoding; 287 } 288 289 private void parseURI(String uri) { 290 synchronized (uriRegexp) { 291 if (uriRegexp.match(uri)) { 292 scheme = uriRegexp.getParen(2); 294 setAuthority(uriRegexp.getParen(4)); 295 path = uriRegexp.getParen(5); 296 queryString = uriRegexp.getParen(7); 297 fragmentString = uriRegexp.getParen(9); 298 299 if (authority == null) { 300 if (path.startsWith("/")) { 301 uriStartingAtPath = true; 302 } else { 303 uriStartingAtPath = false; 304 relative = true; 305 } 306 } else { 307 uriStartingAtPath = false; 308 } 309 } 310 } 311 } 312 313 private void setAuthority(String anUserInfo, String aHostName, int aPort) { 314 StringBuffer result = new StringBuffer (); 315 if (anUserInfo != null) { 316 result.append(anUserInfo); 317 result.append("@"); 318 } 319 if (aHostName != null) { 320 result.append(aHostName); 321 } 322 if (aPort != getSchemeDefaultPort(scheme) && aPort > 0) { 323 result.append(":"); 324 result.append(aPort); 325 } 326 if (result.toString().length() > 0) { 327 authority = result.toString(); 328 } else { 329 authority = null; 330 } 331 } 332 333 private int getSchemeDefaultPort(String aScheme) { 334 if (aScheme == null) { 335 return -1; 336 } 337 Integer defaultPortInt = (Integer ) defaultSchemePorts.get(aScheme); 338 if (defaultPortInt != null) { 339 return defaultPortInt.intValue(); 340 } else { 341 return -1; 342 } 343 } 344 345 private void init() { 346 defaultSchemePorts = new HashMap (); 347 for (int i = 0; i < defaultSchemePortArray.length; i++) { 348 defaultSchemePorts.put(defaultSchemePortArray[i][0], defaultSchemePortArray[i][1]); 349 } 350 } 351 public boolean isRelative() { 352 return relative; 353 } 354 public void setRelative(boolean isRelative) { 355 this.relative = isRelative; 356 } 357 358 359 360 } 361 | Popular Tags |