001: /*
002: * $Id: DomainWebProxyConfig.java,v 1.6 2005/11/30 11:28:14 ss150821 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/util/DomainWebProxyConfig.java,v $
004: * $Log: DomainWebProxyConfig.java,v $
005: * Revision 1.6 2005/11/30 11:28:14 ss150821
006: * 6356996 - Srap Code base needs to save files in the unix file format and not windows
007: *
008: * Revision 1.5 2005/02/23 11:39:12 ss150821
009: * RFE 6223490 - SRA Should use JDK based logging
010: *
011: * Revision 1.4 2005/02/21 08:03:15 ss150821
012: * 6223490 - JDK Logging move for SRA
013: *
014: * Revision 1.3 2005/02/17 08:27:37 ss150821
015: * RFE 6223490 - Add JDK Logging Support to SRA
016: *
017: * Revision 1.2 2004/07/29 05:03:09 vt126379
018: * RFE#5075809, CRT#102
019: *
020: * Revision 1.1 2003/11/27 12:30:55 mm132998
021: * Restructuring code for PS-IS seperation
022: *
023: * Revision 1.10 2003/05/16 15:23:13 mm132998
024: * PAC File support
025: *
026: * Revision 1.9 2002/10/25 06:04:35 sg134305
027: * CRT (26) - the tests for DomainWebProxyConfig class
028: *
029: * Revision 1.8 2002/09/23 19:32:01 bv131302
030: * CRT#2003 - for types of a.b don't add domain
031: *
032: * Revision 1.5.2.2 2002/09/17 14:11:40 mm132998
033: * CRT 1977
034: *
035: * Revision 1.5.2.1 2002/09/03 11:18:33 bv131302
036: * making all the bug fixes that went into the tip in the rtm branch also
037: *
038: * Revision 1.6 2002/08/29 12:20:40 mm132998
039: * CRT # 1929
040: *
041: *
042: */
043: package com.sun.portal.util;
044:
045: import java.lang.reflect.Constructor;
046: import java.lang.reflect.InvocationTargetException;
047: import java.util.HashMap;
048: import java.util.List;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051:
052: import sun.misc.BASE64Encoder;
053:
054: import com.sun.portal.log.common.PortalLogger;
055: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
056: import com.sun.portal.rproxy.server.GatewayContext;
057: import com.sun.portal.rproxy.server.GatewayContextFactory;
058:
059: /*
060: * @author sanjib.ghosh@sun.com
061: *
062: * This class implements the domain sub-domain logic.
063: */
064: public class DomainWebProxyConfig {
065:
066: private static boolean translateAll;
067:
068: private static IntranetInfo instance = null;
069:
070: private static final byte crlf[] = { 13, 10 };
071:
072: // Lihue PRD : 7.4.3.2
073: // private static final String scrlf = new String(crlf);
074: protected static final String scrlf = new String(crlf);
075:
076: static protected HashMap proxyPasswdList;
077:
078: static protected final String PROXY_PASSWD_LIST = "ProxyPasswdList";
079:
080: // private static Logger logger =
081: // Logger.getLogger("com.sun.portal.sra.rproxy");
082: private static Logger logger = PortalLogger
083: .getLogger(DomainWebProxyConfig.class);
084:
085: static {
086: init();
087: }
088:
089: public static void init() {
090:
091: GatewayContext gatewayContext = GatewayContextFactory
092: .getGatewayContext();
093: boolean isUsingPACFile = gatewayContext.isPACFileEnabled();
094: // boolean isUsingPACFile =
095: // SystemProperties.get("gateway.pacfilesupport" , "false")
096: // .equalsIgnoreCase("true");
097:
098: if (isUsingPACFile) {
099: String pacFileLocation = gatewayContext
100: .getPACFileLocation();
101: // String pacFileLocation =
102: // SystemProperties.get("gateway.pacfilelocation" , "");
103: boolean needReInit = false;
104: Throwable th = null;
105: try {
106: // Check if we can load the com.sun.portal.util.EvalPAC class
107: Class pacFileInstance = Class
108: .forName("com.sun.portal.util.PACFileIntranetInfo");
109: Class[] paramTypes = { java.lang.String.class };
110: Constructor constructor = pacFileInstance
111: .getConstructor(paramTypes);
112: Object[] initArgs = { pacFileLocation };
113:
114: instance = (IntranetInfo) constructor
115: .newInstance(initArgs);
116: } catch (NoClassDefFoundError err) {
117: needReInit = true;
118: th = err;
119: } catch (ClassNotFoundException cnfe) {
120: needReInit = true;
121: th = cnfe;
122: } catch (NoSuchMethodException cnfe) {
123: needReInit = true;
124: th = cnfe;
125: } catch (InstantiationException e) {
126: needReInit = true;
127: th = e;
128: } catch (IllegalAccessException e) {
129: needReInit = true;
130: th = e;
131: } catch (InvocationTargetException e) {
132: instance = new GatewayListIntranetInfo();
133: // logger.severe("Unable to load PAC File : " +
134: // pacFileLocation);
135: Object[] params0 = { pacFileLocation };
136: logger.log(Level.SEVERE, "PSSR_CSPU023", params0);
137: // logger.severe("Either Rhino is not installed or error
138: // retrieving the pac file");
139: logger.severe("PSSR_CSPU024");
140: }
141:
142: if (needReInit) {
143: // logger.severe("Unable to load File evaluator - Rhino is not
144: // installed");
145: logger.severe("PSSR_CSPU025");
146: instance = new GatewayListIntranetInfo();
147: }
148: } else {
149: instance = new GatewayListIntranetInfo();
150: }
151: translateAll = gatewayContext.isTranslateAll();
152: populatePasswordList();
153: }
154:
155: /*
156: * Lihue PRD : 7.4.3.2
157: *
158: * Synopsis - The Gateway should be able to authenticate to an external
159: * proxy @ modified Mridul The mapping between the proxy host and the
160: * Authentication info. Used when the gateway has to Authenticate to a proxy
161: * before the proxy allows connections.
162: */
163: private static void populatePasswordList() {
164: List ds = GatewayProfile.getStringList(PROXY_PASSWD_LIST);
165: /*
166: * This list is assumed to contained entries in the form -
167: * "proxy_host|username|password"
168: */
169: int index;
170: int numElements = ds.size();
171: String passwd;
172: String proxy;
173: proxyPasswdList = new HashMap();
174:
175: if (ds != null) {
176: for (int i = 0; i < numElements; ++i) {
177: proxy = ds.get(i).toString().trim();
178: index = proxy.indexOf('|');
179: if (index != -1) {
180: passwd = proxy.substring(index + 1);
181: proxy = proxy.substring(0, index).trim()
182: .toLowerCase();
183: index = passwd.indexOf('|');
184: if (index != -1) {
185: passwd = passwd.substring(0, index).trim()
186: + ":"
187: + passwd.substring(index + 1).trim();
188: } else {
189: // Only user name ??
190: passwd = passwd.trim();
191: }
192: BASE64Encoder encoder = new BASE64Encoder();
193: proxyPasswdList.put(proxy, encoder
194: .encodeBuffer(passwd.getBytes())
195: + scrlf);
196: } else {
197: proxyPasswdList.put(proxy.toLowerCase(), null);
198: }
199: }
200: }
201: }
202:
203: /*
204: * Method for getting the password for corresponding Proxy host
205: */
206: public static String getProxyPassword(String proxyHost) {
207: Object passwd = proxyPasswdList.get(proxyHost.trim()
208: .toLowerCase());
209: if (passwd != null) {
210: return passwd.toString();
211: }
212: return null;
213: }
214:
215: // End of Code - Lihue PRD : 7.4.3.2
216:
217: /*
218: * This returns the proxy for the host. @param host The host which has to be
219: * checked : If it is not an FQDN , depending on whether it is of form
220: * "host" or "host.sub-domain" , the corresponding
221: * "default-subdomain.default.domain" , "default-domain" will be appended
222: * and checked.
223: */
224: public static String getWebProxy(String protocol, String host) {
225:
226: // System.out.println("getWebProxy");
227: if (host == null) {
228: return null;
229: }
230:
231: host = host.trim().toLowerCase();
232: return instance.getWebProxy(protocol, host);
233: }
234:
235: /*
236: * This returns whether the host is part of the intranet (specified in the
237: * domain list) @param host The host which has to be checked : If it is not
238: * an FQDN , depending on whther it is of form "host" or "host.sub-domain" ,
239: * the corresponding "default-subdomain.default.domain" , "default-domain"
240: * will be appended and checked.
241: */
242: public static boolean containHost(String host) {
243:
244: if (host == null) {
245: return false;
246: }
247:
248: if (translateAll) {
249: return true;
250: }
251:
252: host = host.trim().toLowerCase();
253: return instance.containHost(host);
254: }
255: }
|