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.im.portal.servlet;
006:
007: import java.io.*;
008: import java.net.*;
009: import javax.servlet.http.*;
010: import javax.servlet.*;
011: import com.iplanet.am.util.*;
012: import com.iplanet.sso.*;
013:
014: import com.sun.portal.log.common.PortalLogger;
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017:
018: // This is basically a URL scraper. It pulls data from the im provider and returns it to the client.
019: // During the read it checks for a xml descriptor. If found it will set the content type to
020: // application/x-java-jnlp-file. Needed because desktop providers are unable to set content type.
021: public class LaunchServlet extends HttpServlet {
022:
023: private static Logger debugLogger = PortalLogger
024: .getLogger("com.sun.portal.im.servlet");
025:
026: public void doGet(HttpServletRequest request,
027: HttpServletResponse response) throws ServletException,
028: IOException {
029: // Passing thru the improvider's getLaunchContent
030: // The following attributes are required:
031: // launch - Type of launch. jnlp for webstart, plugin for java applet
032: // provider - Name of the provider to use
033: //
034: // The following attributes are optional:
035: // username - User to start dialoge with
036: // msg - Message to send user on starting dialoge, ignored when username is missing
037: //
038: String provider = request.getParameter("provider");
039: String launch = request.getParameter("launch");
040: if ((provider == null) || (launch == null))
041: throw new ServletException(
042: "Null values for required attributes");
043: String username = request.getParameter("username");
044: String msg = request.getParameter("msg");
045:
046: String host = request.getServerName();
047: int port = request.getServerPort();
048: String proto = request.getScheme();
049: String cookieName = SystemProperties.get(
050: "com.iplanet.am.cookie.name", "iPlanetDirectoryPro");
051: String encoding = "UTF-8";
052: StringBuffer ncURI = new StringBuffer(request.getContextPath());
053: ncURI.append("/dt?provider=");
054: ncURI.append(URLEncoder.encode(provider, encoding));
055: ncURI.append("&launch=");
056: ncURI.append(URLEncoder.encode(launch, encoding));
057: if (username != null) {
058: ncURI.append("&username=");
059: ncURI.append(URLEncoder.encode(username, encoding));
060: if (msg != null) {
061: ncURI.append("&msg=");
062: ncURI.append(URLEncoder.encode(msg, encoding));
063: }
064: }
065: // Pass thru the gateway header info if available
066: String gateway_url = request.getHeader("x-ps-gw-url");
067: if (gateway_url != null) {
068: debugLogger.log(Level.FINEST,
069: "LaunchServlet.doGet: Found gateway url: {0}",
070: gateway_url);
071: ncURI.append("&gateway_url=");
072: ncURI.append(URLEncoder.encode(gateway_url, encoding));
073: }
074: ncURI.append("&last=false"); // Needed so that the provider doesn't consume the desktop
075: URL nc = new URL(proto, host, port, ncURI.toString());
076: HttpURLConnection ncc = (HttpURLConnection) nc.openConnection();
077: ncc.setDoOutput(false);
078: ncc.setDoInput(true);
079: ncc.setUseCaches(false);
080: try {
081: ncc.setRequestProperty("Cookie", cookieName
082: + "="
083: + URLEncoder.encode(SSOTokenManager.getInstance()
084: .createSSOToken(request).getTokenID()
085: .toString(), encoding));
086: } catch (SSOException se) {
087: throw new ServletException("LaunchServlet", se);
088: }
089:
090: debugLogger.log(Level.FINEST,
091: "LaunchServlet.doGet: Finished creating url: {0}", nc
092: .toString());
093: InputStream in = ncc.getInputStream();
094: BufferedReader br = new BufferedReader(
095: new InputStreamReader(in));
096: StringBuffer sb = new StringBuffer();
097: String line;
098: boolean isJnlp = false;
099: debugLogger.log(Level.FINEST,
100: "LaunchServlet.doGet: Reading input stream");
101: while ((line = br.readLine()) != null) {
102: if (line.startsWith("<?xml")) {
103: // Assume this is a jnlp file
104: debugLogger
105: .log(Level.FINEST,
106: "LaunchServlet.doGet: Found xml descriptor string");
107: isJnlp = true;
108: }
109: sb.append(line);
110: sb.append('\n');
111: }
112: debugLogger
113: .log(Level.FINEST,
114: "LaunchServlet.doGet: Finished reading to StringBuffer");
115: if (isJnlp) {
116: response
117: .setContentType("application/x-java-jnlp-file;charset=UTF-8");
118: } else {
119: response.setContentType("text/html;charset=UTF-8");
120: }
121: // Must get writer after setting content type
122: debugLogger.log(Level.FINEST,
123: "LaunchServlet.doGet: Now encoding is: {0}", response
124: .getCharacterEncoding());
125: PrintWriter writer = response.getWriter();
126: writer.write(sb.toString());
127: debugLogger.log(Level.FINEST,
128: "LaunchServlet.doGet: Finished writing to PrintWriter");
129: }
130: }
|