001: /**
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: *
013: * Functionality:
014: * 1. Gets ths contents of the Proxylet channel. (getContent() method).
015: * 2. Gets the contents of the Proxylet provider. (getEdit() method).
016: * 3. Process the contents of the Proxylet provider and stores to the backend (processEdit() method).
017: *
018: * @ Author vr121038
019: */package com.sun.portal.providers.proxylet;
020:
021: import com.sun.portal.desktop.DesktopException;
022: import com.sun.portal.desktop.util.ParameterMap;
023: import com.sun.portal.desktop.util.Target;
024: import com.sun.portal.providers.ProfileProviderAdapter;
025: import com.sun.portal.providers.ProviderException;
026: import com.sun.portal.providers.context.ProviderContext;
027: import com.sun.portal.proxylet.util.ProxyletConstants;
028: import com.sun.portal.proxylet.util.UserAttributes;
029: import com.sun.portal.proxylet.util.ProxyletUtil;
030: import com.sun.portal.log.common.PortalLogger;
031: import com.sun.portal.providers.context.ProviderContextException;
032:
033: import java.net.URL;
034: import java.net.URLEncoder;
035: import java.util.*;
036: import java.util.Enumeration;
037: import java.util.Hashtable;
038: import java.util.ResourceBundle;
039: import java.util.logging.Logger;
040: import java.util.logging.Level;
041:
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044:
045: /*
046: * Provider class to retrieve and store the contents of Proxylet channel and provider.
047: */
048:
049: public class ProxyletProvider extends ProfileProviderAdapter implements
050: ProxyletConstants {
051:
052: private ResourceBundle bundle = null;
053: private String editContainer = null;
054: private String container = null;
055: private String statusMsg = null;
056: private String clientBindIP = null;
057: private String clientBindPort = null;
058: private int clientBindPortNum = PROXYLET_DEFAULT_CLIENT_BIND_PORT;
059: private boolean autoDownloadFlag = false;
060: boolean proxyletjwsmode = false;
061: String mode = null;
062:
063: // Create a logger for this class
064: private static Logger logger = PortalLogger
065: .getLogger(ProxyletProvider.class);
066:
067: /*
068: * Initialize the provider and get the resource bundle
069: */
070: public void init(String n, HttpServletRequest req)
071: throws ProviderException {
072: super .init(n, req);
073: bundle = getResourceBundle(PROXYLET_PROVIDER_RES_BUNDLE);
074: }
075:
076: /*
077: * Main method to get the Proxylet channel contents.
078: * This method is invoked by DesktopServlet
079: */
080: public StringBuffer getContent(HttpServletRequest req,
081: HttpServletResponse res) throws ProviderException {
082: StringBuffer content = null;
083: UserAttributes attrs = ProxyletUtil.getUserAttributes(req);
084:
085: content = checkProxyletEnabled(req, attrs);
086: if (null != content) {
087: return content;
088: }
089:
090: String launchModeStr = attrs
091: .getString(ProxyletConstants.PROXYLET_LAUNCH_MODE);
092: Hashtable tagTable = new Hashtable();
093:
094: if (launchModeStr != null
095: && launchModeStr.equalsIgnoreCase("Java Web Start")) {
096: proxyletjwsmode = true;
097: tagTable.put("jnlp", "true");
098: } else {
099: tagTable.put("jnlp", "false");
100: }
101:
102: content = new StringBuffer();
103:
104: if (attrs.getBoolean(PROXYLET_AUTO_DOWNLOAD)) {
105: StringBuffer scriptlet = new StringBuffer();
106: scriptlet.append("<script language=\"JavaScript\">\n")
107: .append("launchProxylet('");
108: if (proxyletjwsmode)
109: scriptlet.append(req.getContextPath()
110: + PROXYLET_COMMAND_JWS);
111: else
112: scriptlet.append(req.getContextPath()
113: + PROXYLET_COMMAND_APP);
114:
115: scriptlet.append("&followUp=NOURL").append("');").append(
116: "</script>\n");
117:
118: tagTable.put("scriptlet", scriptlet);
119:
120: }
121:
122: tagTable.put("content", getProxyletLink(req));
123: ProviderContext pc = getProviderContext();
124: tagTable.put("portal", pc.getDesktopURL(req));
125:
126: tagTable.put("iwtDesktop-fontFace1",
127: getStringProperty("fontFace1"));
128: content = getTemplate("display.template", tagTable);
129:
130: return content;
131: }
132:
133: /*
134: * Method to get the contents of Proxylet Edit Provider.
135: * Invoked by DesktopServlet.
136: */
137: public StringBuffer getEdit(HttpServletRequest req,
138: HttpServletResponse res) throws ProviderException {
139: StringBuffer content = new StringBuffer();
140: editContainer = req.getParameter("provider");
141: container = req.getParameter("containerName");
142: UserAttributes attrs = ProxyletUtil.getUserAttributes(req);
143: Hashtable tagTable = new Hashtable();
144:
145: tagTable.put("addNewTargetLable", bundle
146: .getString("addNewTargetLable"));
147: tagTable.put("downloadAutomaticallyLable", bundle
148: .getString("downloadAutomaticallyLable"));
149: tagTable.put("clientBindIPLable", bundle
150: .getString("clientBindIPLable"));
151: tagTable.put("clientBindPortLable", bundle
152: .getString("clientBindPortLable"));
153: tagTable.put("launchModeLable", bundle
154: .getString("launchModeLable"));
155: if ((statusMsg != null) && (statusMsg.trim().length() != 0)) {
156: tagTable.put("errorMsg", statusMsg);
157: tagTable.put("clientBindIP", clientBindIP);
158: tagTable.put("clientBindPort", clientBindPort);
159: if (autoDownloadFlag) {
160: tagTable.put("autoDownload", "Checked");
161: } else {
162: tagTable.put("autoDownload", "");
163: }
164: if (proxyletjwsmode) {
165: tagTable.put("selectedMode", "Java Web Start");
166: tagTable.put("other", "Applet");
167: } else {
168: tagTable.put("selectedMode", "Applet");
169: tagTable.put("other", "Java Web Start");
170: }
171:
172: statusMsg = null;
173: } else {
174: tagTable.put("errorMsg", "");
175: if (attrs.getBoolean(PROXYLET_AUTO_DOWNLOAD)) {
176: tagTable.put("autoDownload", "Checked");
177: } else {
178: tagTable.put("autoDownload", "");
179: }
180: tagTable.put("clientBindIP", attrs.getString(
181: PROXYLET_CLIENT_BIND_IP,
182: PROXYLET_DEFAULT_CLIENT_BIND_IP));
183: tagTable.put("clientBindPort", String.valueOf(attrs.getInt(
184: PROXYLET_CLIENT_BIND_PORT,
185: PROXYLET_DEFAULT_CLIENT_BIND_PORT)));
186: String mode = attrs.getString(PROXYLET_LAUNCH_MODE,
187: PROXYLET_LAUNCH_MODE_DEFAULT);
188:
189: if (mode != null && mode.equals("Applet")) {
190: tagTable.put("selectedMode", "Applet");
191: tagTable.put("other", "Java Web Start");
192: } else {
193: tagTable.put("selectedMode", "Java Web Start");
194: tagTable.put("other", "Applet");
195: }
196: }
197:
198: tagTable.put("iwtDesktop-fontFace1",
199: getStringProperty("fontFace1"));
200: content = getTemplate("edit.template", tagTable);
201: return content;
202: }
203:
204: /*
205: * Processes the Values of the Proxylet Edit Provider and stores the values to the backend.
206: */
207: public URL processEdit(HttpServletRequest req,
208: HttpServletResponse res) throws ProviderException {
209: ParameterMap params = getParameterMap(req);
210:
211: if (null != params) {
212: setProcessParameters(params);
213: UserAttributes attrs = ProxyletUtil.getUserAttributes(req);
214: validateClientBindIP();
215: if (null == statusMsg) {
216: validateClientBindPort();
217: }
218: if (null != statusMsg) {
219: return getProxyletProviderURL(req);
220: }
221: commitNewValues(attrs);
222: }
223:
224: return null;
225: }
226:
227: /*
228: * Returns the page parameter map
229: */
230: private ParameterMap getParameterMap(HttpServletRequest req) {
231: ParameterMap map = null;
232: try {
233: map = new ParameterMap(getProviderContext().getCharset(),
234: req, true);
235: for (Enumeration e = req.getParameterNames(); e
236: .hasMoreElements();) {
237: String name = (String) e.nextElement();
238: String[] val = req.getParameterValues(name);
239: map.put(name, val);
240: }
241: } catch (DesktopException de) {
242:
243: }
244: return map;
245: }
246:
247: /*
248: * Return the Proxylet URL link for the Proxylet provider channel
249: */
250: private StringBuffer checkProxyletEnabled(HttpServletRequest req,
251: UserAttributes attrs) throws ProviderException {
252: StringBuffer content = null;
253:
254: /**
255: * Check whether proxylet service is enabled for this user
256: */
257: boolean serviceEnabled = attrs.isAllowed();
258:
259: /**
260: * Check whether the request is via the Gateway and whether the proxylet is enabled.
261: */
262: boolean openPortal = true;
263: boolean proxyletDisabled = true;
264: boolean routedThroughProxylet = false;
265: String psProxyletHeader = req.getHeader(PROXYLET_HEADER);
266:
267: if (psProxyletHeader != null) {
268: openPortal = false;
269: if (-1 != psProxyletHeader.indexOf("enabled=true")) {
270: proxyletDisabled = false;
271: }
272: if (-1 != psProxyletHeader.indexOf("RT-Proxylet=true")) {
273: routedThroughProxylet = true;
274: }
275: }
276:
277: /*
278: * Return appropriate error messages if Proxylet service is not ebabled at gateway or uer level.
279: */
280: if (!serviceEnabled || openPortal || proxyletDisabled
281: || routedThroughProxylet) {
282: content = new StringBuffer();
283: content.append("<table><tr><td>\n").append("<font FACE=\"")
284: .append(getStringProperty("fontFace1")).append(
285: "\" size=\"-1\">\n");
286: if (openPortal) {
287: content.append(bundle.getString("noGateway"));
288: } else if (proxyletDisabled) {
289: content.append(bundle.getString("proxyletDisabled"));
290: } else if (!serviceEnabled) {
291: content.append(bundle.getString("noService"));
292: } else if (routedThroughProxylet) {
293: content.append(bundle
294: .getString("proxyletAlreadyRunning"));
295: if (proxyletjwsmode) {
296: content.append("</font></td></tr>\n");
297: StringBuffer scriptlet = new StringBuffer();
298: scriptlet
299: .append("<script language=\"JavaScript\">\n");
300: scriptlet
301: .append("\n"
302: + " function proxyletcleanup()\n"
303: + " {\n"
304: + " newwin = window.open(\"http://127.0.0.1:58081/PROXYLETCLOSE\");\n"
305: + " newwin.close();\n"
306: + " }");
307: scriptlet.append("</script>\n");
308: content.append(scriptlet);
309: content.append("</table>\n");
310:
311: return content;
312: }
313: }
314: content.append("</font></td></tr></table>\n");
315: }
316: return content;
317: }
318:
319: /*
320: * Return the Proxylet URL link for the Proxylet provider channel
321: */
322: private String getProxyletLink(HttpServletRequest req) {
323: StringBuffer href = null;
324: href = new StringBuffer().append(getProxyletURI(req));
325:
326: StringBuffer row = new StringBuffer().append(
327: "<tr><td><font FACE=\"[tag:iwtDesktop-fontFace1]\" ")
328: .append("size=\"-1\">").append(href.toString()).append(
329: "</font></td></tr>\n");
330: return row.toString();
331: }
332:
333: private String getProxyletURI(HttpServletRequest req) {
334: Map appurls = null;
335: String launchURL = "downloadProxylet";
336: StringBuffer buf = new StringBuffer();
337: buf.append("<table>");
338:
339: try {
340: try {
341: List clientAndLocaleFilters = getProviderContext()
342: .getClientAndLocalePropertiesFilters();
343: appurls = getProviderContext().getCollectionProperty(
344: getName(), "appurls", clientAndLocaleFilters);
345: } catch (Exception ignore) {
346: }
347:
348: // If no custom appurls provided, just provide a launch link for the
349: // portal dt page.
350: if (appurls == null || appurls.isEmpty()
351: || appurls.size() == 0) {
352: String desc;
353: try {
354: desc = bundle.getString(launchURL);
355: } catch (MissingResourceException me) {
356: desc = launchURL;
357: }
358: buf.append("<tr><td>");
359: buf.append("<a href=\"#").append(
360: "\" onClick=\"launchProxylet('");
361: if (proxyletjwsmode)
362: buf.append(req.getContextPath()
363: + PROXYLET_COMMAND_JWS);
364: else
365: buf.append(req.getContextPath()
366: + PROXYLET_COMMAND_APP);
367:
368: buf.append("&followUp=").append(
369: getProviderContext().getDesktopURL(req));
370: buf.append("'); return false;\">");
371: buf.append(desc).append("</a>");
372: buf.append("</tr></td>");
373: buf.append("</table>");
374: return buf.toString();
375: }
376:
377: //construct the HTML link from name and url listing
378: for (Iterator it = appurls.keySet().iterator(); it
379: .hasNext();) {
380:
381: String name = (String) it.next();
382: String link = (String) appurls.get(name);
383:
384: name = getProviderContext().escape(name);
385: link = getProviderContext().escape(link);
386:
387: buf.append("<tr><td>");
388: buf.append("<a href=\"#").append(
389: "\" onClick=\"launchProxylet('");
390: if (proxyletjwsmode)
391: buf.append(req.getContextPath()
392: + PROXYLET_COMMAND_JWS);
393: else
394: buf.append(req.getContextPath()
395: + PROXYLET_COMMAND_APP);
396:
397: buf.append("&followUp=").append(link);
398: buf.append("'); return false;\">");
399: buf.append(name).append("</a>");
400: buf.append("</tr></td>");
401:
402: }
403: } catch (Exception pce) {
404: }
405:
406: buf.append("</table>");
407:
408: return buf.toString();
409: }
410:
411: /*
412: * Return the Proxylet URI to download the applet
413: */
414: /*private String getProxyletURI(HttpServletRequest req) {
415:
416: UserAttributes attrs = ProxyletUtil.getUserAttributes(req);
417:
418: // get the list of app urls
419: StringBuffer buf = new StringBuffer();
420: List appURLs = attrs.getStringList(ProxyletConstants.PROXYLET_APP_URLS);
421:
422: if( appURLs != null && !appURLs.isEmpty()){
423: // get the desc and build the links
424: buf.append("<table>");
425: for (int x = 0; x < appURLs.size(); x++) {
426: String singleURL= (String) appURLs.get(x);
427: String desc = "";
428: int index = -1;
429: if( (index = singleURL.indexOf("=") )!= -1 ){
430: desc = singleURL.substring(0, index);
431: singleURL = singleURL.substring(index +1, singleURL.length());
432: }
433: else
434: desc = singleURL;
435:
436: //get the localized string for the description
437: try{
438: desc = bundle.getString(desc);
439: }catch(MissingResourceException me){
440: try{
441: desc = appurlbundle.getString(desc);
442: }catch(Exception e){}
443: }
444:
445: buf.append("<tr><td>");
446: buf.append("<a href=\"#").append("\" onClick=\"launchProxylet('");
447: if(proxyletjwsmode)
448: buf.append(req.getContextPath() + PROXYLET_COMMAND_JWS);
449: else
450: buf.append(req.getContextPath() + PROXYLET_COMMAND_APP);
451:
452: buf.append("&followUp=").append(singleURL);
453: buf.append("'); return false;\">");
454: buf.append(desc).append("</a>");
455: buf.append("</tr></td>");
456: }
457: buf.append("</table>");
458: return buf.toString();
459:
460: }
461: else
462: return "";
463:
464:
465: }*/
466:
467: /*
468: * Return the Proxylet Provider URL
469: */
470: private URL getProxyletProviderURL(HttpServletRequest req) {
471: URL proxyletProviderURL = null;
472: try {
473: proxyletProviderURL = new URL(getProviderContext()
474: .getDesktopURL(req)
475: + "?action=edit&provider="
476: + URLEncoder.encode(editContainer, "UTF-8")
477: + "&targetprovider="
478: + URLEncoder.encode(getName(), "UTF-8")
479: + "&containerName="
480: + URLEncoder.encode(container, "UTF-8"));
481: } catch (java.io.UnsupportedEncodingException ee) {
482: //getProviderContext().debugError("ProxyletProvider: Unsupported encoding -> " + ee);
483: } catch (java.net.MalformedURLException ue) {
484: //getProviderContext().debugError("ProxyletProvider: Unable to create proxylet provider URL -> " + ue);
485: }
486: return proxyletProviderURL;
487: }
488:
489: /*
490: * Set the parameters to local variables so that it can retain the values in the form
491: * if there is a invalid input.
492: */
493: private void setProcessParameters(ParameterMap params) {
494: String newAutoDownload = params.getString("autoDownload");
495: if (null != newAutoDownload) {
496: autoDownloadFlag = true;
497: } else {
498: autoDownloadFlag = false;
499: }
500: clientBindIP = params.getString("clientBindIP");
501: if (null != clientBindIP) {
502: clientBindIP = clientBindIP.trim();
503: } else {
504: clientBindIP = "";
505: }
506: clientBindPort = params.getString("clientBindPort");
507: if (null != clientBindPort) {
508: clientBindPort = clientBindPort.trim();
509: } else {
510: clientBindPort = "";
511: }
512: mode = params.getString("launchMode");
513: if (mode != null && mode.equalsIgnoreCase("Java Web Start")) {
514: proxyletjwsmode = true;
515: } else {
516: proxyletjwsmode = false;
517: }
518:
519: }
520:
521: /*
522: * Validate client bind IP selected by the user.
523: */
524: private void validateClientBindIP() {
525: String[] tokens = clientBindIP.split("\\.");
526: if ((null != tokens) && (tokens.length == 4)) {
527: for (int i = 0; i < 4; i++) {
528: try {
529: Integer.parseInt(tokens[i]);
530: } catch (NumberFormatException nfe) {
531: statusMsg = bundle.getString("invalidIP");
532: }
533: }
534: } else {
535: statusMsg = bundle.getString("invalidIP");
536: }
537: return;
538: }
539:
540: /*
541: * Validate client bind Port selected by the user.
542: */
543: private void validateClientBindPort() {
544: try {
545: if (clientBindPort != null) {
546: clientBindPortNum = Integer.parseInt(clientBindPort);
547: if ((clientBindPortNum < 1025)
548: || (clientBindPortNum > 65535)) {
549: statusMsg = bundle.getString("portOutOfRange");
550: }
551: }
552: } catch (NumberFormatException nfe) {
553: statusMsg = bundle.getString("invalidPort");
554: }
555: }
556:
557: /*
558: * Store all the new values selected by the user if these values are different from old values.
559: */
560: private void commitNewValues(UserAttributes attrs) {
561: boolean autoDownload = attrs.getBoolean(PROXYLET_AUTO_DOWNLOAD);
562: if (!autoDownloadFlag && autoDownload) {
563: attrs.setBoolean(PROXYLET_AUTO_DOWNLOAD, false);
564: } else if (autoDownloadFlag && !autoDownload) {
565: attrs.setBoolean(PROXYLET_AUTO_DOWNLOAD, true);
566: }
567:
568: String oldClientBindIP = attrs
569: .getString(PROXYLET_CLIENT_BIND_IP);
570: if (clientBindIP != null
571: && !clientBindIP.equals(oldClientBindIP)) {
572: attrs.setString(PROXYLET_CLIENT_BIND_IP, clientBindIP);
573: }
574:
575: int oldClientBindPort = attrs.getInt(PROXYLET_CLIENT_BIND_PORT);
576: if (clientBindPortNum != oldClientBindPort) {
577: attrs.setInt(PROXYLET_CLIENT_BIND_PORT, clientBindPortNum);
578: }
579:
580: String oldmode = attrs.getString(PROXYLET_LAUNCH_MODE);
581: if (!oldmode.equals(mode)) {
582: attrs.setString(PROXYLET_LAUNCH_MODE, mode);
583: }
584:
585: }
586:
587: }
|