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.yahoo;
006:
007: import java.net.MalformedURLException;
008: import java.net.URLDecoder;
009: import java.net.URLEncoder;
010: import java.security.MessageDigest;
011: import java.security.NoSuchAlgorithmException;
012: import java.util.List;
013: import java.util.Map;
014:
015: import sun.misc.BASE64Encoder;
016:
017: import com.sun.portal.rewriter.util.Debug;
018: import com.sun.portal.rewriter.util.StringHelper;
019: import com.sun.portal.rewriter.util.collections.ListMap;
020: import com.sun.portal.rewriter.util.uri.DecoratedURI;
021: import com.sun.portal.rproxy.rewriter.SRAPTranslatorHelper;
022: import com.sun.portal.rproxy.rewriter.util.uri.SRAPGatewayURI;
023:
024: public final class YahooTranslatorHelper {
025: private static final String YPB_URL = "ypburl";
026:
027: private static final String MD5_SIGN = "sign";
028:
029: private static final String DOT_DONE = ".done";
030:
031: public static String doTranslate(SRAPGatewayURI aGatewayURI,
032: DecoratedURI aYahooURI) {
033: String lAbsoluteURIString = aYahooURI.toExternalForm();
034:
035: final String bQuery = StringHelper.normalize(aYahooURI
036: .getDecoratee().getQuery());
037: final String translatedQuery = translateQuery(bQuery,
038: aGatewayURI);
039:
040: if (translatedQuery == bQuery) {
041: return lAbsoluteURIString;
042: } else {
043: lAbsoluteURIString = aYahooURI.getFullFileURI() + "?"
044: + translatedQuery + aYahooURI.getReference();
045: }
046:
047: return lAbsoluteURIString;
048: }// doTranslate()
049:
050: /**
051: * Identifies the Yahoo URL's and parses the query string. 1. Authentication
052: * URL 2. Edit URL 3. Logout URL Rewrites the query strings and recomputes
053: * the MD5 sum for the authentication URL
054: *
055: * @see com.sun.portal.rproxy.rewriter.yahoo.QueryParser
056: * @see com.sun.portal.rproxy.rewriter.yahoo.YahooException
057: */
058: private static String translateQuery(final String aQuery,
059: final SRAPGatewayURI aGatewayURI) {
060: try {
061: ListMap bParamMap = null;
062: String urlKey = YPB_URL;
063: if (isYahooAuthURL(aQuery)) {
064: bParamMap = rewriteParam(urlKey, aQuery, aGatewayURI);
065: String newMD5 = computeYahooSign(bParamMap);
066: bParamMap.put(urlKey, URLEncoder.encode(bParamMap.get(
067: urlKey).toString()));
068: bParamMap.put(MD5_SIGN, newMD5);
069: } else if (isYahooLogoutURL(aQuery)) {
070: bParamMap = rewriteParam(urlKey, aQuery, aGatewayURI);
071: } else if (isYahooEditURL(aQuery)) {
072: urlKey = DOT_DONE;
073: bParamMap = rewriteParam(urlKey, aQuery, aGatewayURI);
074: } else {
075: return aQuery;
076: }
077:
078: // BugNo:4889146
079: return toQueryForm(bParamMap);
080: } catch (MalformedURLException e) {
081: return aQuery;
082: }
083: }// translateQuery()
084:
085: /**
086: * ressurects the query string with the new MD5 sum and the rewritten url.
087: */
088: private static String toQueryForm(final ListMap aParamMap) {
089: List lKeys = aParamMap.keyList();
090: StringBuffer lResult = new StringBuffer();
091:
092: for (int i = 0; i < lKeys.size(); i++) {
093: String bKey = lKeys.get(i).toString();
094: String bValue = aParamMap.get(bKey).toString();
095:
096: if (i != 0) {
097: lResult.append("&");
098: }
099:
100: if (bValue == null) {
101: lResult.append(bKey).append("=");
102: } else {
103: lResult.append(bKey).append("=").append(bValue);
104: }
105: }
106:
107: return lResult.toString();
108: }// toQueryForm()
109:
110: /**
111: * Scans for Yahoo Edit URL
112: */
113: private static boolean isYahooEditURL(String url) {
114: if (url.indexOf(DOT_DONE) != -1) {
115: return true;
116: }
117:
118: return false;
119: }// isYahooEditURL()
120:
121: /**
122: * Scans for Yahoo Logout URL
123: */
124: private static boolean isYahooLogoutURL(String url) {
125: if (url.indexOf(YPB_URL) != -1) {
126: return true;
127: }
128: return false;
129: }// isYahooLogoutURL()
130:
131: /**
132: * Scans for Yahoo URL in Auth URL in the query String
133: */
134: private static boolean isYahooAuthURL(String url) {
135: if (url.indexOf("&ypburl") != -1 && url.indexOf("&sign") != -1) {
136: return true;
137: }
138: return false;
139: }// isYahooAuthURL()
140:
141: /**
142: * Computes the MD5 sum for Yahoo digest authentication.
143: *
144: * TBD : Need a generic mechanism at the gateway to support any digest
145: * authentication scheme
146: *
147: * Order of entry to be passed to this function
148: *
149: * String[] signArgs = new String[] {_corporateYahooID, _login, _usernum,
150: * _ypburl, _ma, _corporateYahooPassword};
151: *
152: * Signs and appends the MD5 Sum for Yahoo Authentication
153: */
154: private static String computeYahooSign(Map aParamMap) {
155: String[] args = new String[] {
156: YahooConfigManager.getCorporateID(),
157: aParamMap.get("login").toString(),
158: aParamMap.get("usernum").toString(),
159: aParamMap.get(YPB_URL).toString(),
160: aParamMap.get("ma").toString(),
161: YahooConfigManager.getCorporatePassword(), };
162: try {
163: MessageDigest bMessageDigest = MessageDigest
164: .getInstance("MD5");
165:
166: for (int i = 0; i < args.length; i++) {
167: if (args[i] != null) {
168: bMessageDigest.update(args[i].getBytes());
169: }
170: }
171:
172: StringBuffer bSignBuffer = new StringBuffer(
173: (new BASE64Encoder()).encodeBuffer(bMessageDigest
174: .digest()));
175: // to remove the space appended at the end
176: bSignBuffer = new StringBuffer(bSignBuffer.substring(0, 24));
177:
178: int length = bSignBuffer.length();
179: for (int i = 0; i < length; i++) {
180: switch (bSignBuffer.charAt(i)) {
181: case '+': {
182: bSignBuffer.setCharAt(i, '.');
183: break;
184: }
185: case '/': {
186: bSignBuffer.setCharAt(i, '_');
187: break;
188: }
189: case '=': {
190: bSignBuffer.setCharAt(i, '-');
191: break;
192: }
193: default: {
194: break;
195: }
196: }
197: }// if
198:
199: return bSignBuffer.toString();
200: } catch (NoSuchAlgorithmException e) {
201: Debug.error("MD5 Algorithm Not Found", e);
202: }
203:
204: return "MD5AlgorithmNotFound";
205: }// computeYahooSign()
206:
207: private static ListMap rewriteParam(final String aParamName,
208: final String aQuery, final SRAPGatewayURI aGatewayURI)
209: throws MalformedURLException {
210: ListMap lParamMap = QueryParser.parseQueryString(aQuery);
211: String paramvalue = lParamMap.get(aParamName).toString();
212: // BugNo:4889146
213: DecoratedURI lURI = new DecoratedURI(URLDecoder
214: .decode(paramvalue));
215: String lModifiedURI = SRAPTranslatorHelper.prefixGateway(
216: aGatewayURI, lURI);
217: lParamMap.put(aParamName, lModifiedURI);
218: return lParamMap;
219: }// rewriteParam()
220:
221: }// class YahooTranslatorHelper
|