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.List;
008:
009: import com.sun.portal.rewriter.util.StringHelper;
010: import com.sun.portal.rewriter.util.collections.ListMap;
011: import com.sun.portal.rewriter.util.re.Pattern;
012: import com.sun.portal.rewriter.util.re.RegExp;
013: import com.sun.portal.rewriter.util.uri.PageSpec;
014:
015: public class URI2RuleSetMap {
016: private final ListMap uriPatternsMap;
017:
018: private final ListMap domainMap;
019:
020: private final String starRuleSet;
021:
022: URI2RuleSetMap(List aRuleSetMap) {
023: final ListMap lPatternMap = new ListMap();
024: final ListMap lDomainMap = new ListMap();
025: String lStarRuleSet = null;
026:
027: for (int i = 0; i < aRuleSetMap.size(); i++) {
028: String line = StringHelper.normalize(
029: (String) aRuleSetMap.get(i)).toString();
030: String[] splited = StringHelper.tokenize(line, "|", false,
031: 2);
032: splited[0] = splited[0].trim();// uri pattern
033: splited[1] = splited[1].trim();// ruleset name
034: if (splited[0].length() != 0 && splited[1].length() != 0) {
035: if (splited[0].length() == 1 && splited[0].equals("*")) {
036: lStarRuleSet = splited[1]; // star ruleset name
037: } else {
038: String lRulesetName = splited[1].toLowerCase();
039: if (splited[0].indexOf('*') == -1
040: && splited[0].indexOf('/') == -1
041: && splited[0].indexOf(':') == -1) {
042: // domain and sub domain spec
043: lDomainMap.put(splited[0].toLowerCase(),
044: lRulesetName);
045: } else {
046: // URI spec
047: lPatternMap.put(new Pattern(splited[0], true),
048: lRulesetName);
049: }
050: }
051: }
052: }
053:
054: uriPatternsMap = ListMap.unmodifiableListMap(lPatternMap);
055: domainMap = ListMap.unmodifiableListMap(lDomainMap);
056: starRuleSet = lStarRuleSet;
057: }// constructor
058:
059: public String selectRuleset4Page(PageSpec aPageSpec) {
060: {
061: // BugNo:4769581, 4865961, 4871781
062: // use uri pattern rules
063: String lResult = matchURIPatterns(aPageSpec);
064: if (lResult != null) {
065: return lResult;
066: }
067:
068: // use domain rules
069: lResult = matchDomains(aPageSpec);
070: if (lResult != null) {
071: return lResult;
072: }
073: }
074:
075: // star ruleset would be null if not star defined else it would have
076: // some value
077: return starRuleSet;
078: }// selectRuleset4Page()
079:
080: private String matchURIPatterns(PageSpec aPageSpec) {
081: // BugNo:4769581, 4865961, 4871781
082: String[] lResult = new String[] { "", "" };
083: List lList = uriPatternsMap.keyList();
084:
085: for (int i = 0; i < uriPatternsMap.size(); i++) {
086: Pattern key = (Pattern) lList.get(i);
087: if (RegExp.match(key, aPageSpec.getBaseURI()
088: .getFullFileURI())) {
089: if (key.getInput().length() > lResult[0].length()) {
090: lResult[0] = key.getInput();
091: lResult[1] = uriPatternsMap.get(key).toString();
092: }
093: }
094: }
095:
096: if (lResult[0].length() > 0) {
097: return lResult[1];
098: }
099:
100: return null;
101: }// matchURIPatterns()
102:
103: private String matchDomains(PageSpec aPageSpec) {
104: String hostName = aPageSpec.getBaseURI().getHost()
105: .toLowerCase();
106: while (true) {
107: Object ruleSetID = domainMap.get(hostName);
108: if (ruleSetID != null) {
109: return ruleSetID.toString();
110: }
111:
112: int dotIndex = hostName.indexOf('.');
113: if (dotIndex == -1) {
114: break;
115: }
116:
117: hostName = hostName.substring(dotIndex + 1);
118: }// while true
119:
120: return null;
121: }// matchDomains()
122: }// class URI2RuleSetMap
|