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;
006:
007: import com.sun.portal.rewriter.util.ConfigManager;
008: import com.sun.portal.rewriter.util.Constants;
009: import com.sun.portal.rewriter.util.Debug;
010: import com.sun.portal.rewriter.util.StringHelper;
011: import com.sun.portal.rewriter.util.uri.DecoratedURI;
012: import com.sun.portal.rewriter.util.uri.PageSpec;
013:
014: /**
015: * Implements the Translator methods related to AbsoluteJSFunctionSpec
016: *
017: * @version 1.0 12/15/2001
018: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
019: */
020: public abstract class AbstractTranslator implements Translator {
021: public static final String PROPERTY_IS_ADD_SCRIPT_REFERRER = "IS_ADD_SCRIPT_REFERRER";
022: private static final boolean IS_ADD_SCRIPT_REFERRER = ConfigManager
023: .getBoolean(PROPERTY_IS_ADD_SCRIPT_REFERRER);
024: private static final String JAR_PROTOCOL = "jar:";
025:
026: private final LookAheadInfo lookAheadInfo = new LookAheadInfo();
027: private final PageSpec pageSpec;
028: private final JSFunctionSpec jsFunctionSpec;
029:
030: protected AbstractTranslator(final PageSpec aPageSpec) {
031: this (aPageSpec, AbsoluteJSFunctionSpec.getDefault());
032: }//constructor
033:
034: protected AbstractTranslator(final PageSpec aPageSpec,
035: final JSFunctionSpec aJSFunctionSpec) {
036: pageSpec = aPageSpec;
037: jsFunctionSpec = aJSFunctionSpec;
038: }//constructor
039:
040: public PageSpec getPageSpec() {
041: return pageSpec;
042: }//getPageSpec()
043:
044: public JSFunctionSpec getJSFunctionSpec() {
045: lookAheadInfo.enableContentChanged();
046: return jsFunctionSpec;
047: }//getJSFunctionSpec()
048:
049: public final LookAheadInfo getLookAheadInfo() {
050: return lookAheadInfo;
051: }//getLookAheadInfo()
052:
053: /**
054: * Template Method which would call the hookTraslate() method after
055: * converting the URI to absolute
056: * @param aOrgURL
057: * @return
058: */
059: public final String translate(final String aOrgURL) {
060: /**
061: * first decide which basespec to be used
062: * Base Spec is either applet codebase uri or HTML <base> Tag uri
063: */
064: lookAheadInfo.enableContentChanged();
065: final PageSpec lPageSpec;
066: if (lookAheadInfo.isAppletCodeBaseExists()) {
067: lPageSpec = lookAheadInfo.getAppletCodeBase();
068: } else {
069: lPageSpec = getPageSpec();
070: }
071:
072: return doTranslate(aOrgURL, lPageSpec, this );
073: }//translate()
074:
075: static final String doTranslate(final String aOrgURL,
076: final PageSpec aResolveSpec, final Translator aTranslator) {
077: //Do Not translated the Applet URI's if the codebase pagespec
078: //is invalid - This could happen if codebase="javascript:getCodeBase()"
079: if (!aResolveSpec.isValid()) {
080: if (Debug.isWarningEnabled()) {
081: Object param[] = { aOrgURL };
082: Debug.recordURIWarning("PSRW_CSPR_0001", param);
083: }
084: return aOrgURL;
085: }
086:
087: final String[] parts = StringHelper.regExpSplit(aOrgURL,
088: Constants.REG_EX_FOR_TRIMING_QUOTES);
089:
090: //after removing these special chars see if the remaing are spaces
091: //pre-condition, i.e it should not be null or empty string
092: //or value should not be null - this happends when function params are being rewritten
093: if (parts.length != 3 || parts[1].length() == 0) {
094: if (Debug.isWarningEnabled()) {
095: Object param[] = { aOrgURL };
096: Debug.recordURIWarning("PSRW_CSPR_0002", param);
097: }
098:
099: return aOrgURL;
100: }
101:
102: String urlPart = parts[1];
103:
104: //BugNo:4790402, 4790445, 4890520
105: if (urlPart.equals("#") || urlPart.equals("?")
106: || urlPart.startsWith("+")) {
107: if (Debug.isWarningEnabled()) {
108: Object param[] = { aOrgURL };
109: Debug.recordURIWarning("PSRW_CSPR_0003", param);
110: }
111: return aOrgURL;
112: }
113:
114: //Bug No: 4701655
115: if (urlPart.startsWith("\\")) {
116: urlPart = "/" + urlPart.substring(1);
117: }
118: //Bug No: 4701655
119:
120: //BugNo:jar protocol support
121: if (StringHelper.startsWithIgnoreCase(urlPart, JAR_PROTOCOL)) {
122: parts[0] = parts[0]
123: + urlPart.substring(0, JAR_PROTOCOL.length());
124: urlPart = urlPart.substring(JAR_PROTOCOL.length());
125: }
126:
127: try {
128: StringBuffer sb = new StringBuffer(Math.round(aOrgURL
129: .length() * 1.25f));
130: sb.append(parts[0]); //Append the first part
131: DecoratedURI lAbsoluteURI = aResolveSpec.getBaseURI()
132: .resolve(urlPart);
133:
134: if (aTranslator != null) {
135: DecoratedURI lWithScriptReffer = lAbsoluteURI;
136:
137: //Bug No: 4526286, 4890520
138: if (IS_ADD_SCRIPT_REFERRER
139: && aTranslator.getLookAheadInfo()
140: .getTagContext().isScriptSRC()) {
141: lWithScriptReffer = TranslatorHelper
142: .handleScriptSRC(aResolveSpec, lAbsoluteURI);
143: }
144: //Bug No: 4526286, 4890520
145:
146: String hookTranslatedURI = aTranslator.hook4Translate(
147: lAbsoluteURI, lWithScriptReffer);
148: sb.append(hookTranslatedURI);
149: recordURI(aTranslator.getPageSpec(), aResolveSpec,
150: aOrgURL, lAbsoluteURI, hookTranslatedURI);
151: } else {
152: sb.append(lAbsoluteURI.toExternalForm());
153: recordURI(aResolveSpec, aResolveSpec, aOrgURL,
154: lAbsoluteURI,
155: "Asked for conversion to absolute URI only");
156: }
157:
158: sb.append(parts[2]); //Append the Lastpart
159: return sb.toString();
160: } catch (Exception e) {
161: if (Debug.isWarningEnabled()) {
162: if (e.getMessage().startsWith("mailto")) {
163: Object param0[] = { aResolveSpec };
164: Object param1[] = { aOrgURL };
165: /*Debug.recordURIMessage( "mailto: not rewritten:\n\t" +
166: "Page Info:" + aResolveSpec + ",\n\t" +
167: "Original URI:" + aOrgURL + ",\n" );*/
168: Debug.recordURIMessage("PSRW_CSPR_0004");
169: Debug.recordURIMessage("PSRW_CSPR_0005", param0);
170: Debug.recordURIMessage("PSRW_CSPR_0006", param1);
171: //System.out.println("**********************\n firstPSRW_CSPR_0006\n**************");
172: } else {
173: Object param0[] = { aResolveSpec };
174: Object param1[] = { aOrgURL };
175: /*Debug.recordURIWarning( "Invalid URI:\n\t" +
176: "Page Info:" + aResolveSpec + ",\n\t" +
177: "Original URI:" + aOrgURL + ",\n",e );*/
178: Debug.recordURIWarning("PSRW_CSPR_0007");
179: Debug.recordURIWarning("PSRW_CSPR_0005", param0);
180: Debug.recordURIWarning("PSRW_CSPR_0006", param1);
181: //System.out.println("**********************\n firstPSRW_CSPR_0006\n**************");
182: }
183: }
184: return aOrgURL;
185: }
186: }//doTranslate()
187:
188: private static void recordURI(final PageSpec aPageSpec,
189: final PageSpec aResolveSpec, final String aOrgURL,
190: final DecoratedURI aAbsoluteURI,
191: final String aHookTranslatedURI) {
192: //System.out.println("**********************\nPSRW_CSPR_0010\n*************"+Debug.isMessageEnabled());
193: if (Debug.isMessageEnabled()) {
194: //{BaseURI, OrignalURI, DefaultHandling, CustomHandling},
195: //Resolve URI Could be different than page URI specially when Applet
196: //codebase translations etc..
197: /*Debug.recordURIMessage( "\n{\n\t" +
198: "Page URI: " + aPageSpec.getInputString() + ",\n\t" +
199: ( ( aPageSpec != aResolveSpec ) ?
200: "Resolve URI: " + aResolveSpec.getBaseURI().toExternalForm() + ",\n\t" : "" ) +
201: "Original URI: " + aOrgURL + ",\n\t" +
202: "Resolved Absolute URI: " + aAbsoluteURI.toExternalForm() + ",\n\t" +
203: "Result of Custom Translation: " + aHookTranslatedURI +
204: "\n},\n" ); */
205: Object[] param0 = { (aPageSpec != aResolveSpec) ? aResolveSpec
206: .getBaseURI().toExternalForm()
207: : "" };
208: Object[] param1 = { aOrgURL };
209: Object[] param2 = { aAbsoluteURI.toExternalForm() };
210: Object[] param3 = { aHookTranslatedURI };
211: //System.out.println("recordURI() method ");
212: Debug.recordURIMessage("PSRW_CSPR_0008");
213: Debug.recordURIMessage("PSRW_CSPR_0009", param0);
214: Debug.recordURIMessage("PSRW_CSPR_0010", param1);
215: Debug.recordURIMessage("PSRW_CSPR_0011", param2);
216: Debug.recordURIMessage("PSRW_CSPR_0012", param3);
217:
218: }
219: }//recordURI()
220:
221: }//class AbstractTranslator
|