001: /*
002: * CdmHandler.java
003: *
004: * Created on November 21, 2002, 2:59 PM
005: */
006:
007: package com.sun.portal.rproxy.configservlet.server;
008:
009: import java.io.FileWriter;
010: import java.io.PrintWriter;
011: import java.rmi.RemoteException;
012: import java.util.Hashtable;
013:
014: import com.iplanet.am.util.AMClientDetector;
015: import com.iplanet.services.cdm.Client;
016: import com.iplanet.services.cdm.ClientException;
017: import com.sun.portal.rproxy.configservlet.DummyHTTPServletRequest;
018: import com.sun.portal.rproxy.configservlet.Request;
019: import com.sun.portal.rproxy.configservlet.Response;
020: import com.sun.portal.rproxy.configservlet.ServiceHandler;
021:
022: /**
023: *
024: * @author mm132998
025: * @version
026: */
027: public class CdmHandler implements ServiceHandler {
028:
029: private static CdmHandler instance = new CdmHandler();
030:
031: private Hashtable table;
032:
033: private static final String scrlf = "\r\n";
034:
035: static final boolean doDebug = true;
036:
037: static PrintWriter log = null;
038: static {
039: if (doDebug) {
040: try {
041: log = new PrintWriter(new FileWriter(
042: "/tmp/cdm.servlet.out"));
043: } catch (Exception ex) {
044: }
045: }
046: }
047:
048: private static void writeLog(String message) {
049: try {
050: log.write(message);
051: log.flush();
052: } catch (Exception ex) {
053: }
054: }
055:
056: private CdmHandler() {
057: table = new Hashtable();
058: }
059:
060: public static CdmHandler getInstance() {
061: return instance;
062: }
063:
064: public Response handleRequest(Request request)
065: throws RemoteException {
066: String userAgent = request.getRequestObject().toString();
067: Object result;
068: Client client;
069:
070: if (doDebug) {
071: writeLog("Request for : " + userAgent + "\n");
072: }
073: if (!userAgent.endsWith(scrlf)) {
074: userAgent = userAgent + scrlf;
075: }
076:
077: client = (Client) table.get(userAgent);
078:
079: if (client == null) {
080: if (doDebug) {
081: writeLog(userAgent + " not in cache.\n");
082: }
083:
084: // Find out and add to cache.
085: AMClientDetector amCD = new AMClientDetector();
086: result = "autodetect";
087:
088: if (amCD.detectionEnabled().equalsIgnoreCase("true")) {
089: DummyHTTPServletRequest dmReq = new DummyHTTPServletRequest();
090: dmReq.addHeaderLine("GET / HTTP/1.0 \r\n");
091: dmReq.addHeaderLine("User-Agent : " + userAgent);
092: dmReq.addHeaderLine(scrlf);
093: String clientType = amCD.getClientType(dmReq);
094:
095: try {
096: client = Client.getInstance(clientType);
097: table.put(userAgent, client);
098: String str = client.getProperty("cookieSupport");
099:
100: if (str != null) {
101: if (doDebug) {
102: writeLog(userAgent + " cookieSupport : "
103: + str + ".\n");
104: }
105: result = str.trim();
106: }
107: } catch (ClientException ce) {
108: result = "autodetect";
109: }
110: } else {
111: result = "autodetect";
112: }
113: } else {
114: String str = client.getProperty("cookieSupport");
115:
116: if (str != null) {
117: result = str.trim();
118: } else {
119: result = "autodetect";
120: }
121: }
122: if (doDebug) {
123: writeLog(userAgent + " support : " + result + ".\n");
124: }
125: return new Response(request.getServiceName(), request
126: .getRequestType(), result);
127: }
128: }
|