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.rproxy.rewriter;
006:
007: import java.io.ByteArrayInputStream;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.UnsupportedEncodingException;
011:
012: import com.sun.portal.rewriter.Rewriter;
013: import com.sun.portal.rewriter.RewriterPool;
014: import com.sun.portal.rewriter.Translator;
015: import com.sun.portal.rewriter.engines.LanguageConstants;
016: import com.sun.portal.rewriter.rom.InvalidXMLException;
017: import com.sun.portal.rewriter.services.DataServiceException;
018: import com.sun.portal.rewriter.util.Constants;
019: import com.sun.portal.rewriter.util.Debug;
020: import com.sun.portal.rewriter.util.uri.PageSpec;
021: import com.sun.portal.rewriter.util.xml.Document;
022: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
023: import com.sun.portal.rproxy.configservlet.client.UserProfile;
024: import com.sun.portal.rproxy.connectionhandler.ErrorResponse;
025: import com.sun.portal.rproxy.connectionhandler.Request;
026: import com.sun.portal.rproxy.connectionhandler.Response;
027: import com.sun.portal.rproxy.rewriter.util.SRAPConfigManager;
028: import com.sun.portal.rproxy.rewriter.util.http.ContentInfo;
029: import com.sun.portal.rproxy.rewriter.util.http.HTTPData;
030: import com.sun.portal.util.GWLocale;
031:
032: public final class SRAPAdapter {
033: private static final String UN_SUPPORTED_ENCODING_EXCEPTION_LOCALE_KEY = "UnSupportedEncodingWarningMessage";
034:
035: private static final String INVALID_XML_EXCEPTION_LOCALE_KEY = "InvalidRuleSetWarningMessage";
036:
037: private static final String DATASERVICE_EXCEPTION_LOCALE_KEY = "DataServiceErrorMessage";
038:
039: private static String NULL_RULESET = "null_ruleset";
040:
041: public static final RewriterPool rewriterPool = RewriterPool
042: .getDefault();
043:
044: private static final String[] messages = {
045: "No Parser available for this MIME", // 0
046: "No Ruleset Mapped for this URI", // 1
047: "Invalid Encoding", // 2
048: };
049:
050: /**
051: * @return null in case not rewritting
052: */
053: public static InputStream rewrite(Request aRequest,
054: Response aResponse) throws IOException {
055: HTTPData lHTTPData = null;
056: String lRuleSet4URI = null;
057:
058: try {
059: PageSpec bPageSpec = new PageSpec(aRequest.getURL());
060:
061: lHTTPData = new HTTPData(bPageSpec, aResponse);
062: ContentInfo lContentInfo = lHTTPData.getContentInfo();
063:
064: SRAPAdapterHelper.updateMIME(bPageSpec, lContentInfo);
065: // BugNo:4798308, 4766421, 4708870
066: String lParserMIME = SRAPConfigManager
067: .selectParser4MIME(lContentInfo.getMIME());
068:
069: // send the content as is, if the parser is not available for that
070: // MIME
071: // or when the ruleset for that URI is null
072: lRuleSet4URI = SRAPConfigManager
073: .selectRuleSet4URI(bPageSpec);
074: if ((lParserMIME == null) || (lRuleSet4URI == null)
075: || (NULL_RULESET.equals(lRuleSet4URI))) {
076: recordUnTouchedPage(lHTTPData,
077: (lParserMIME == null) ? messages[0]
078: : messages[1]);
079: // BugNo:4726160, 4828181
080: if (lHTTPData.isStreamRead()) {
081: return new ByteArrayInputStream(lHTTPData
082: .getContentBytes());
083: }
084: return null;
085: }
086:
087: if (lContentInfo.isInvalidEncoding()) {
088: if (lParserMIME
089: .equalsIgnoreCase(LanguageConstants.XML_MIME)) {
090: // compare with parser mime rather than content mime, as
091: // even
092: // text/xslt would have been mapped to xml parser
093: lContentInfo.setEncoding(Document
094: .parseEncoding(lHTTPData
095: .getRawEncodedString()));
096: }
097: }
098:
099: return rewriteContent(lHTTPData, lParserMIME, lRuleSet4URI,
100: lContentInfo, aRequest);
101: } catch (UnsupportedEncodingException usException) {
102: recordUnTouchedPage(lHTTPData, messages[2]);
103: return handleException(aResponse, aRequest,
104: UN_SUPPORTED_ENCODING_EXCEPTION_LOCALE_KEY,
105: new Object[] { lHTTPData.getContentInfo()
106: .getEncoding() }, usException);
107: } catch (InvalidXMLException ixException) {
108: ixException.printStackTrace();
109: return handleException(aResponse, aRequest,
110: INVALID_XML_EXCEPTION_LOCALE_KEY,
111: new Object[] { lRuleSet4URI }, ixException);
112: } catch (DataServiceException dsException) {
113: return handleException(aResponse, aRequest,
114: DATASERVICE_EXCEPTION_LOCALE_KEY,
115: new Object[] { Constants.EMPTY_STRING },
116: dsException);
117: }
118: }// rewrite()
119:
120: private static InputStream rewriteContent(final HTTPData aHTTPData,
121: final String aParserMIME, final String aRuleSet4URI,
122: final ContentInfo aContentInfo, final Request aRequest)
123: throws InvalidXMLException, IOException {
124: PageSpec lPageSpec = new PageSpec(aRequest.getURL(),
125: aContentInfo.getMIME(), aContentInfo.getEncoding());
126:
127: // BugNo:4781742
128: Translator bSRAPTranslator = SRAPTranslatorBuilder.create(
129: aRequest, lPageSpec);
130: Rewriter lRewriter = rewriterPool.getRewriter(aRuleSet4URI,
131: aParserMIME);
132: String lResult = lRewriter.rewrite(aHTTPData.getPageContent(),
133: bSRAPTranslator);
134: if (!bSRAPTranslator.getLookAheadInfo().isContentChanged()) {
135: return new ByteArrayInputStream(aHTTPData.getContentBytes());
136: }
137: if (Constants.SYSTEM_ENCODING == aContentInfo.getEncoding()) {
138: return new ByteArrayInputStream(lResult.getBytes());
139: } else {
140: return new ByteArrayInputStream(lResult
141: .getBytes(aContentInfo.getEncoding()));
142: }
143: }// rewriteContent()
144:
145: private static void recordUnTouchedPage(final HTTPData aHTTPData,
146: final String aCause) {
147: if (Debug.isWarningEnabled() && aHTTPData != null) {
148: String warningMessage = "\n(WARNING)"
149: + aHTTPData.getPageSpec()
150: + "\n\t\tIS NOT BEING REWRITTEN..\n\n"
151: + aHTTPData.getContentInfo() + "Cause: " + aCause
152: + "\n";
153:
154: Debug.recordUnaffectedPageWarning(warningMessage);
155: }
156: }// recordUnTouchedPage()
157:
158: private static final InputStream handleException(
159: final Response aResp, final Request aReq,
160: final String aLocaleKey, final Object[] aLocaleParams,
161: final Exception e) {
162: if (Debug.isWarningEnabled()) {
163: Debug.recordOriginalPageWarning(aLocaleKey, e);
164: }
165:
166: // 4683853
167: String lSessionID = aReq.getSessionID();
168: String lUserLocale = getUserLocale(lSessionID);
169: String lMessage = GWLocale.getPFString(aLocaleKey,
170: aLocaleParams, lUserLocale);
171:
172: return (new ErrorResponse(lMessage, null, aReq
173: .getRequestHeader("Accept-Language")))
174: .getContentStream();
175: }// handleException()
176:
177: private static final String getUserLocale(final String aSessionID) {
178: try {
179: UserProfile prof = new UserProfile(aSessionID);
180: return prof.getString("preferredlocale", "en_US");
181: } catch (Exception ex) {
182: return GatewayProfile.getString("locale", "en_US");
183: }
184: }// getUserLocale()
185:
186: }// class SRAPAdapter
|