001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util.uri;
006:
007: import com.sun.portal.rewriter.util.ConfigManager;
008: import com.sun.portal.rewriter.util.StringHelper;
009:
010: import java.net.MalformedURLException;
011:
012: /**
013: * Accepts any URI Absolute or Relative
014: */
015: public final class StandardURI extends AbstractURI implements URIIntf {
016: public static String PROPERTY_IS_NORMALIZE_URI_LIKE_BROWSER = "IS_NORMALIZE_URI_LIKE_BROWSER";
017: private static boolean NORMALIZE_URI_LIKE_BROWSER = ConfigManager
018: .getBoolean(PROPERTY_IS_NORMALIZE_URI_LIKE_BROWSER);
019:
020: private URI jdkURI;
021:
022: private StandardURI(final String aURIString, final URI aJDKURI)
023: throws MalformedURLException {
024: super (aURIString);
025:
026: try {
027: if (aJDKURI != null) {
028: jdkURI = aJDKURI;
029: } else {
030: jdkURI = new URI(uriString);
031: }
032:
033: if (jdkURI.isOpaque()) {
034: throw new URISyntaxException("Opaque URI:" + uriString,
035: "");
036: }
037:
038: String protocol = getProtocol();
039: if (jdkURI.isAbsolute()) {
040: int i = uriString.indexOf(protocol);
041: int j = uriString.indexOf(protocol + "://");
042:
043: if (i != j) {
044: throw new URISyntaxException(uriString,
045: "Absolute URI does not contain pattern '://' after the protocol");
046: }
047: }
048:
049: setValid(true);
050: } catch (URISyntaxException e) {
051: String scheme = "";
052: setValid(false);
053: if (jdkURI != null && "mailto".equals(jdkURI.getScheme())) {
054: scheme = jdkURI.getScheme();
055: }
056: throw new MalformedURLException(scheme
057: + "\nInput to StandardURI:" + aURIString
058: + "\nCause StackTrace:"
059: + StringHelper.exceptionStack2String(e)
060: + "\nWraped Exception:");
061: }
062: }//constuctor
063:
064: /**
065: * Accept relative and absolute URI's and not OpaqueURI.
066: * Where as CustomURI only accepts Absolute URI
067: */
068: public StandardURI(final String aURIString)
069: throws MalformedURLException {
070: this (aURIString, null);
071: }//constuctor
072:
073: public String getProtocol() {
074: return StringHelper.normalize(jdkURI.getScheme());
075: }//getProtocol()
076:
077: public String getHost() {
078: return StringHelper.normalize(jdkURI.getHost());
079: }//getHost()
080:
081: public int getPort() {
082: return jdkURI.getPort();
083: }//getPort()
084:
085: public String getAuthority() {
086: return StringHelper.normalize(jdkURI.getAuthority());
087: }//getAuthString()
088:
089: public String getUserInfo() {
090: return StringHelper.normalize(jdkURI.getUserInfo());
091: }//getAuthString()
092:
093: public String getPath() {
094: String lPath = URI.normalize(StringHelper.normalize(jdkURI
095: .getPath()));
096: //BugNo:4855960
097: if (NORMALIZE_URI_LIKE_BROWSER) {
098: if (isAbsolute()) {
099: while (lPath.startsWith("/..")) {
100: lPath = lPath.substring(3);
101: }
102: }
103: }
104:
105: return lPath;
106: }//getPath()
107:
108: public String getReference() {
109: return StringHelper.normalize(jdkURI.getFragment());
110: }//getReference()
111:
112: public String getQuery() {
113: return StringHelper.normalize(jdkURI.getQuery());
114: }//getQuery()
115:
116: public boolean hasReference() {
117: if (jdkURI.getFragment() == null) {
118: return false;
119: }
120:
121: return true;
122: }//hasReference()
123:
124: public boolean hasQuery() {
125: if (jdkURI.getQuery() == null) {
126: return false;
127: }
128:
129: return true;
130: }//hasQuery()
131:
132: public boolean isAbsolute() {
133: return jdkURI.isAbsolute();
134: }//isAbsoulte()
135:
136: public String getImplID() {
137: return "StandardURI";
138: }//getImplID()
139:
140: public StandardURI resolve(final String aResolveURI)
141: throws MalformedURLException {
142: //BugNo:4780962
143: if (aResolveURI == null) {
144: return null;
145: }
146:
147: try {
148: URI lURI = jdkURI.resolve(new URI(aResolveURI));
149: return new StandardURI(lURI.toString(), lURI);
150: } catch (URISyntaxException e) {
151: throw new MalformedURLException("\nInvalid Resolve URI:"
152: + aResolveURI + "\nCause StackTrace:"
153: + StringHelper.exceptionStack2String(e));
154: }
155: }//resolve()
156:
157: public static void main(String[] args) throws Exception {
158: final String[] location = { "http://user@password@srap.india.sun.com",
159: /*"http://raja.com//base/raja/Image/i%20c%20on.jpg?ab\\e=eess",
160: "Image/i c on.jpg?ab\\e=eess",
161: "Image/icon.jpg?ab\\e=eess",
162: "Image\\icon.jpg",
163: "Image\\\\icon.jpg",
164: "https://rajanagendra.sun.com:443/home/rule.jsp#rajesh?ab=12&&34=yu",
165: "https://rajanagendra.sun.com:443/home/?"+LanguageConstants.SCRIPT_REFERRER+"=https://scripts.sun.com/home/scripts/abc.html",
166: "ftp://www.vlc.com.au?query",
167: "http://raja.com/?re=http://kawa.com/",
168: "http://raja/",
169: "file:///c:/something/blah.txt",
170: "http://justin:password@www.vlc.com.au:8080/",
171: "https://rajanagendra.sun.com:443/home/rule.jsp#rajesh",
172: "http://rajanagendra.sun.com/Base/Raja/raja.html", //-1
173: "http://raja.com", //0
174: "http://rajanagendra.sun.com/avc/abc.jsp", //0
175: "http://rajanagendra.sun.com/", //1
176: "http://rajanagendra.sun.com:80/home/rule.jsp?ab=12&&34=yu", //2*/
177: };
178:
179: for (int i = 0; i < location.length; i++) {
180: StandardURI pageSpec = new StandardURI(location[i]);
181: System.out.println("Original: " + location[i]);
182: System.out.println("ExternalForm: "
183: + pageSpec.toExternalForm());
184: System.out
185: .println("ToString " + pageSpec.jdkURI.toString());
186: System.out.println("Host: " + pageSpec.getHost());
187: //System.out.println( "File: " + pageSpec.getFile() );
188: System.out.println("Path: " + pageSpec.getPath());
189: System.out.println("Normalized: "
190: + new URI("HTTP://RAJA.COM/?abc=10").resolve(
191: new URI(pageSpec.getPath())).toString());
192: System.out.println("Port: " + pageSpec.getPort() + "\n\n");
193: }//for loop
194: }//main()
195: }//class StandardURI
|