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.services;
006:
007: import java.util.HashMap;
008: import java.util.List;
009: import java.util.Map;
010:
011: import com.sun.portal.rewriter.engines.LanguageConstants;
012: import com.sun.portal.rewriter.util.Constants;
013: import com.sun.portal.rewriter.util.StringHelper;
014: import com.sun.portal.rewriter.util.collections.ListMap;
015: import com.sun.portal.rewriter.util.re.Pattern;
016:
017: public final class SRAPProcessedProfile {
018: private static SRAPContext srapContext;
019:
020: private static URI2RuleSetMap uri2RulesetMAP;
021:
022: private static ListMap mime2ParserMAP;
023:
024: private static ListMap uri2ParserMAP;
025:
026: private static Pattern[] not2RewriteURIPatterns = Pattern.EMPTY_PATTERN_ARRAY;
027:
028: private static Pattern[] not2ObscureURIPatterns = Pattern.EMPTY_PATTERN_ARRAY;
029:
030: private static String httpsPort;
031:
032: private static String httpPort;
033:
034: public static void init(final SRAPContext aSRAPContext) {
035: srapContext = aSRAPContext;
036: uri2RulesetMAP = initURI2RuleSetMap(aSRAPContext
037: .readURI2RuleSetMappings());
038: mime2ParserMAP = initMIME2ParserMap(aSRAPContext
039: .readMIME2ParserMappings());
040: uri2ParserMAP = initURI2ParserMap(aSRAPContext
041: .readURI2ParserMappings());
042: not2RewriteURIPatterns = initURIPatterns(aSRAPContext
043: .readNot2RewriteURIList());
044: not2ObscureURIPatterns = initURIPatterns(aSRAPContext
045: .readNot2ObscureURIList());
046:
047: httpPort = (aSRAPContext.getHTTPPort() == 80) ? "" : ":"
048: + aSRAPContext.getHTTPPort();
049:
050: httpsPort = (aSRAPContext.getHTTPSPort() == 443) ? "" : ":"
051: + aSRAPContext.getHTTPSPort();
052: }// init()
053:
054: public static String getInstanceName() {
055: return srapContext.getProfileName();
056: }// getInstanceName()
057:
058: public static URI2RuleSetMap getURI2RuleSetMap() {
059: return uri2RulesetMAP;
060: }// readURI2RuleSetMappings()
061:
062: public static ListMap getMIME2ParserMap() {
063: return mime2ParserMAP;
064: }// readMIME2ParserMappings()
065:
066: public static boolean isMIMEGuessingEnabled() {
067: return srapContext.isMIMEGuessingEnabled();
068: }// isMIMEGuessingEnabled()
069:
070: public static ListMap getURI2ParserMAP() {
071: return uri2ParserMAP;
072: }// getURI2ParserMAP()
073:
074: public static Pattern[] getNot2RewriteURIPatterns() {
075: return not2RewriteURIPatterns;
076: }// getNot2RewriteURIPatterns()
077:
078: public static boolean isObscureURIs() {
079: return srapContext.isURLObfuscationEnabled();
080: }// isObscureURIs()
081:
082: public static String getObfuscatorSeed() {
083: return srapContext.readObfuscatorSeed();
084: }// getObfuscatorSeed()
085:
086: public static Pattern[] getNot2ObscureURIPatterns() {
087: return not2ObscureURIPatterns;
088: }// getNot2ObscureURIPatterns()
089:
090: public static boolean isUseConsistantProtocol4Gateway() {
091: return srapContext.isUseConsistantProtocol4Gateway();
092: }// isUseConsistantProtocol4Gateway()
093:
094: private static URI2RuleSetMap initURI2RuleSetMap(List aRuleSetMap) {
095: return new URI2RuleSetMap(aRuleSetMap);
096: }// initURI2RuleSetMap()
097:
098: private static ListMap initMIME2ParserMap(List aMappingInfo) {
099: return parserMapHelper(aMappingInfo, new CustomProcess() {
100: public Object processKey(Object aObject) {
101: return aObject.toString().toLowerCase();
102: }
103: });
104: }// initMIME2ParserMap()
105:
106: private static ListMap initURI2ParserMap(List aMappingInfo) {
107: return parserMapHelper(aMappingInfo, new CustomProcess() {
108: public Object processKey(Object aObject) {
109: return new Pattern(aObject.toString(), true);
110: }
111: });
112: }// initURI2ParserMap()
113:
114: private static Pattern[] initURIPatterns(List aList) {
115: return Pattern.convert2Patterns((String[]) aList
116: .toArray(Constants.EMPTY_STRING_ARRAY), true);
117: }// initURIPatterns()
118:
119: private static ListMap parserMapHelper(List aMappingInfo,
120: CustomProcess aCustomProcess) {
121: final Map supportedContentTypes = new HashMap();
122: supportedContentTypes.put("html", LanguageConstants.HTML_MIME);
123: supportedContentTypes.put("javascript",
124: LanguageConstants.JS_MIME);
125: supportedContentTypes.put("java_script",
126: LanguageConstants.JS_MIME);
127: supportedContentTypes.put("xml", LanguageConstants.XML_MIME);
128: // BugNo:4781754
129: supportedContentTypes.put("css", LanguageConstants.CSS_MIME);
130:
131: final ListMap lMap = new ListMap();
132: for (int j = 0; j < aMappingInfo.size(); j++) {
133: String line = StringHelper.normalize(
134: (String) aMappingInfo.get(j)).toString();
135: String[] splited = StringHelper.tokenize(line, "=");
136:
137: // ignore all other lines i.e which does not have length 2
138: if (splited.length == 2) {
139: String key = splited[0].trim();
140: // check if the parser exists for this key by getting the
141: // parser MIME
142: Object parserMIME = supportedContentTypes.get(key
143: .toLowerCase());
144: if (parserMIME != null) {
145: String[] values = StringHelper.tokenize(splited[1],
146: ";:|");
147: for (int i = 0; i < values.length; i++) {
148: String value = values[i].trim();
149: if (value.length() != 0) {
150: // make value as key and parser mime as value
151: // i need to know the paser mime based on content
152: // MIME
153: lMap.put(aCustomProcess.processKey(value),
154: parserMIME);
155: }
156: }
157: } else {
158: System.out.println("No Rewriter Available for key:"
159: + key);
160: }
161: } else {
162: System.out.println("Illegal List Content:" + line);
163: }
164: }// for loop
165:
166: return lMap;
167: }// parserMapHelper()
168:
169: public static String getHTTPPort() {
170: return httpPort;
171: }// getHTTPPort()
172:
173: public static String getHTTPSPort() {
174: return httpsPort;
175: }// getHTTPSPort()
176:
177: private static interface CustomProcess {
178: Object processKey(Object aObject);
179:
180: // Object processValue( Object aObject );
181: }// interface CustomProcess
182:
183: }// class SRAPProcessedProfile
|