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.Constants;
008: import com.sun.portal.rewriter.util.Debug;
009: import com.sun.portal.rewriter.util.crypto.CryptoHelper;
010:
011: import java.net.MalformedURLException;
012:
013: /**
014: * This is the decoration to standard URI implementations. Inspite of number
015: * of URI implementations, the decoration to all these implementation should
016: * result in consistant method return values.
017: *
018: * This could acccept either Absoulte or Relative like the std URI implements
019: *
020: * @version 1.0 12/15/2001
021: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
022: *
023: * example uri = http://raja.sun.com:87/nag/abc/index.html?abc=45&&true#summery
024: * protocol = http
025: * host = raja.sun.com
026: * dirURI = /nag/abc/
027: * fileURI = /abc/index.html
028: * file = /nag/abc/index.html?abc=45&&true#summery
029: * pathQueryReference = /nag/abc/index.html?abc=45&&true#summery
030: * query = ?abc=45&&true
031: * reference = #summery
032: * resolveURI = http://raja.sun.com:87/nag/abc/index.html
033: */
034:
035: public class DecoratedURI implements URIIntf {
036: public final static DecoratedURI EMPTY_DECORATED_URI;
037:
038: private final StandardURI uriImpl;
039: private String orgURI;
040:
041: private String portString = null;
042: private String networkURI = null;
043: private String baseHREF = null;
044: private String pathQueryReference = null;
045: private String protocol = null;
046: private String userInfo = null;
047: private String host = null;
048: private String path = null;
049: private String query = null;
050: private String reference = null;
051: private String fileURI = null;
052: private String fullFileURI = null;
053:
054: private String pathAndQuery = null;
055: private String externalForm = null;
056: private String normalForm = null;
057:
058: //Used in UBT
059: private String appendURL = null;
060:
061: static {
062: EMPTY_DECORATED_URI = createEmptyDecoratedURI();
063: }
064:
065: private DecoratedURI(final StandardURI aStandardURI,
066: final String aOrgURI) {
067: uriImpl = aStandardURI;
068: orgURI = aOrgURI;
069: }//constructor
070:
071: public DecoratedURI(final String aURIString)
072: throws MalformedURLException {
073: orgURI = CryptoHelper.decode(aURIString);
074: try {
075: uriImpl = new StandardURI(orgURI);
076: } catch (MalformedURLException me) {
077: //Debug.recordURIWarning( "Invalid URI:" + orgURI, me );
078: Debug.recordURIWarning("PSRW_CSPR_0007", me);
079: throw me;
080: }
081: }//constructor
082:
083: //Set appendURL for UBT
084: public void setAppendURL(String appendURL) {
085: this .appendURL = appendURL;
086: }
087:
088: public String getInputString() {
089: if (orgURI == null) {
090: return Constants.EMPTY_STRING;
091: }
092:
093: return orgURI;
094: }//getInputString()
095:
096: public boolean isAbsolute() {
097: return uriImpl.isAbsolute();
098: }//isAbsolute()
099:
100: public String getProtocol() {
101: if (protocol == null) {
102: protocol = uriImpl.getProtocol();
103: }
104: return protocol;
105: }//getProtocol()
106:
107: public String getUserInfo() {
108: if (userInfo == null) {
109: userInfo = ((uriImpl.getUserInfo().equals("")) ? ""
110: : uriImpl.getUserInfo() + "@");
111: if (uriImpl.getHost().length() == 0
112: && userInfo.length() == 0
113: && uriImpl.getAuthority().length() != 0) {
114: userInfo = uriImpl.getAuthority();
115: }
116: }
117:
118: return userInfo;
119: }//getUserInfo()
120:
121: public String getHost() {
122: if (host == null) {
123: host = uriImpl.getHost();
124: }
125:
126: return host;
127: }//getHost()
128:
129: public int getPort() {
130: final int lPort = uriImpl.getPort();
131: if (lPort == uriImpl.getDefaultPort()) {
132: return -1;
133: }
134:
135: return lPort;
136: }//getPort()
137:
138: public String getPortString() {
139: if (portString == null) {
140: int lPort = getPort();
141: if (lPort != -1) {
142: portString = ":" + lPort;
143: } else {
144: portString = "";
145: }
146: }
147:
148: return portString;
149: }//getPortString()
150:
151: public int getDefaultPort() {
152: return uriImpl.getDefaultPort();
153: }//getDefaultPort()
154:
155: /**
156: * This is same as StandardURI except that, if the user has given the port and
157: * that port is nothing but the default port of the protocol, do't append
158: * the port number
159: */
160: public String getNetworkURI() {
161: if (!isAbsolute()) {
162: return Constants.EMPTY_STRING;
163: }
164:
165: if (networkURI == null) {
166: networkURI = getProtocol() + "://" + getUserInfo()
167: + getHost() + getPortString();
168: }
169:
170: return networkURI;
171: }//getNetworkURI()
172:
173: public String getPath() {
174: if (path == null) {
175: path = uriImpl.getPath();
176: }
177:
178: return path;
179: }//getPath()
180:
181: public String getPathQueryAndReference() {
182: if (pathQueryReference == null) {
183: pathQueryReference = getFileURI() + getQuery()
184: + getReference();
185: }
186:
187: return pathQueryReference;
188: }//getPathQueryAndReference()
189:
190: public String getPathAndQuery() {
191: if (pathAndQuery == null) {
192: pathAndQuery = getFileURI() + getQuery();
193: }
194:
195: return pathAndQuery;
196: }//getPathAndQuery()
197:
198: public String getParameterValue(final String aParamName) {
199: return uriImpl.getParameterValue(aParamName);
200: }//getParameterValue()
201:
202: public String getFileURI() {
203: if (fileURI == null) {
204: fileURI = uriImpl.getFileURI();
205:
206: if (URIHelper.needsToAppendDirectorySeperator(fileURI)) {
207: fileURI = uriImpl.getFileURI() + "/";
208: }
209: }
210:
211: return fileURI;
212: }//getFileURI()
213:
214: public String getDirURI() {
215: return uriImpl.getDirURI();
216: }//getDirURI()
217:
218: public String getFileName() {
219: return uriImpl.getFileName();
220: }//getFileName()
221:
222: public String getFileExtension() {
223: return uriImpl.getFileExtension();
224: }//getFileExtension()
225:
226: public String getQuery() {
227: if (query == null) {
228: //BugID:4790445, 4790402
229: if (hasQuery()) {
230: query = "?" + uriImpl.getQuery();
231: } else {
232: query = "";
233: }
234: }
235:
236: return query;
237: }//getQuery()
238:
239: public String getReference() {
240: if (reference == null) {
241: //BugID:4790445, 4790402
242: if (hasReference()) {
243: reference = "#" + uriImpl.getReference();
244: } else {
245: reference = "";
246: }
247: }
248:
249: return reference;
250: }//getReference()
251:
252: public boolean hasReference() {
253: return uriImpl.hasReference();
254: }//hasReference()
255:
256: public boolean hasQuery() {
257: return uriImpl.hasQuery();
258: }//hasQuery()
259:
260: public String getBaseHREF() {
261: if (baseHREF == null) {
262: baseHREF = getNetworkURI() + uriImpl.getDirURI();
263: }
264:
265: return baseHREF;
266: }//getBaseHREF()
267:
268: public String getFullFileURI() {
269: if (fullFileURI == null) {
270: fullFileURI = getNetworkURI() + getFileURI();
271: }
272:
273: return fullFileURI;
274: }//getBaseHREF()
275:
276: public boolean isValid() {
277: return uriImpl.isValid();
278: }//isValid()
279:
280: public DecoratedURI resolve(final String aResolveURI)
281: throws MalformedURLException {
282: //BugNo:4780962
283: return new DecoratedURI(uriImpl.resolve(aResolveURI),
284: aResolveURI);
285: }//resolve()
286:
287: public boolean isOnlyReference() {
288: if (orgURI != null) {
289: return orgURI.startsWith("#");
290: }
291:
292: return false;
293: }//isReference()
294:
295: public URIIntf getDecoratee() {
296: return uriImpl;
297: }//getDecoratee()
298:
299: public final String toExternalForm() {
300: if (externalForm == null) {
301: if (!isValid()) {
302: externalForm = uriImpl.toExternalForm();
303: } else {
304: StringBuffer sb = new StringBuffer(getInputString()
305: .length());
306: sb.append(getProtocol());
307: if (getProtocol().length() != 0) {
308: sb.append("://");
309: }
310: sb.append(getUserInfo()).append(getHost()).append(
311: getPortString()).append(getPath()).append(
312: getQuery()).append(getReference());
313: externalForm = sb.toString();
314: //Done for UBT
315: try {
316: if (appendURL != null
317: && !externalForm.startsWith(appendURL
318: .substring(0, appendURL
319: .indexOf('?') - 1))) {
320: externalForm = appendURL + externalForm; //"http://sprint.india.sun.com/portal/dt/?action=ubt&url="
321: }
322: } catch (Exception e) {
323: //appendURL is not set properly.
324: }
325: }
326: }
327:
328: return externalForm;
329: }//toExternalForm()
330:
331: public String toNormalForm() {
332: if (normalForm == null) {
333: if (!isValid()) {
334: normalForm = toExternalForm();
335: } else {
336: StringBuffer sb = new StringBuffer(getInputString()
337: .length());
338: sb.append(getProtocol());
339: if (getProtocol().length() != 0) {
340: sb.append("://");
341: }
342: sb.append(getUserInfo()).append(getHost()).append(
343: getPortString()).append(getFileURI()).append(
344: getQuery()).append(getReference());
345: normalForm = sb.toString();
346: }
347: }
348:
349: return normalForm;
350: }//toNormalForm()
351:
352: public boolean isNetworkURIMatch(DecoratedURI aURIIntf) {
353: return getProtocol().equalsIgnoreCase(aURIIntf.getProtocol())
354: && getHost().equalsIgnoreCase(aURIIntf.getHost())
355: && getPort() == aURIIntf.getPort();
356: }//isNetworkURIMatch()
357:
358: public String toString() {
359: return "Contents of PageSpec Object:" + "\n\tOriginal: "
360: + getInputString() + "\n\tExternal Form: "
361: + toExternalForm() + "\n\tProtocol: " + getProtocol()
362: + "\n\tHost: " + getHost() + "\n\tPort: "
363: + getPortString() + "\n\tNetworkURI: "
364: + getNetworkURI() + "\n\tFileURI: " + getFileURI()
365: + "\n\tDirURI: " + getDirURI() + "\n\tFileName:"
366: + getFileName() + "\n\tReference: " + getReference()
367: + "\n\tQuery: " + getQuery() + "\n\tRelativeURI: "
368: + getPathQueryAndReference() + "\n\tBaseHREF: "
369: + getBaseHREF();
370: }//toString()
371:
372: public String getImplID() {
373: return "DecoratedURI";
374: }//getImplID()
375:
376: private static DecoratedURI createEmptyDecoratedURI() {
377: try {
378: return new DecoratedURI("");
379: } catch (Exception e) {
380: e.printStackTrace();
381: return null;
382: }
383: }//createEmptyDecoratedURI()
384:
385: public static void main(String[] args) throws Exception {
386: final String[] location = {
387: "https://zeus.nawab.inda.com:443/index.html",
388: "https://rajanagendra.sun.com:443/home/rule.jsp#rajesh",
389: "https://rajanagendra.sun.com:443/home/rule.jsp#rajesh?ab=12&&34=yu",
390: "https://rajanagendra.sun.com:443/home/..",
391: "http://rajanagendra.sun.com/Base/Raja/raja.html", //-1
392: "http://raja.com", //0
393: "http://rajanagendra.sun.com/avc/abc.jsp", //0
394: "http://rajanagendra.sun.com/", //1
395: "http://rajanagendra.sun.com:80/home/rule.jsp?ab=12&&34=yu", //2
396: "https://rajanagendra.sun.com:1443/home/rule.jsp#rajesh?ab=12&&34=yu",
397: //"http://rajanagendra.sun.com:80/home/rule.jsp?ab=12&&34=yu",
398: };
399:
400: for (int i = 0; i < location.length; i++) {
401: DecoratedURI uriDec = new DecoratedURI(location[i]);
402: System.out.println(uriDec.getPort());
403: }
404: }//main()
405: }//class URIDecorator
|