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.portlet;
006:
007: import java.io.*;
008: import java.net.*;
009: import java.util.*;
010: import javax.servlet.http.*;
011: import java.security.AccessController;
012: import com.sun.portal.providers.Provider;
013: import com.sun.portal.providers.ProviderException;
014: import com.sun.portal.providers.jsp.JSPProvider;
015: import com.sun.im.service.*;
016: import com.sun.identity.security.EncryptAction;
017: import com.sun.identity.security.DecryptAction;
018: import com.iplanet.am.sdk.*;
019: import com.iplanet.am.util.*;
020: import com.iplanet.sso.*;
021:
022: // HS imports
023: import javax.portlet.GenericPortlet;
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletContext;
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletPreferences;
030: import javax.portlet.PortletMode;
031: import javax.portlet.PortletRequest;
032: import javax.portlet.PortletSession;
033: import javax.portlet.RenderResponse;
034: import javax.portlet.PortletRequestDispatcher;
035: import javax.portlet.RenderRequest;
036: import javax.portlet.WindowState;
037: import java.util.Locale;
038: import java.text.DateFormat;
039: import java.security.Principal;
040:
041: public class IMPortlet extends GenericPortlet {
042:
043: private PortletContext pContext;
044: // Define all strings used in this Portlet
045: static public final String CONTENT_PAGE = "contentPage";
046: static public final String EDIT_PAGE = "editPage";
047: static public final String HELP_PAGE = "helpPage";
048: static public final String SSOTOKEN = "ssotoken";
049: static public final String SSOTOKEN_ATT = "javax.portlet.portletc.ssotoken";
050:
051: static public final String EDIT_FINISHED = "Finished";
052: static public final String EDIT_CANCEL = "Cancel";
053: static public final String EMPTY_STRING = "";
054: // Preference vars
055: static public final String CONTACT_GROUP = "contactGroup";
056: static public final String CONTACT_GROUP_ALL = "__ALL__";
057: static public final String SERVER = "server";
058: static public final String PORT = "port";
059: static public final String PORT_ERROR_PARAM = "portError";
060: static public final String MUX = "mux";
061: static public final String MUX_PORT = "muxport";
062: static public final String MUX_PORT_ERROR_PARAM = "muxportError";
063: static public final String CLIENT_RUN_MODE = "clientRunMode";
064: static public final String PLUGIN_RUN_MODE = "plugin";
065: static public final String JNLP_RUN_MODE = "jnlp";
066: static public final String CRM_ERROR_PARAM = "crmError";
067: static public final String USERNAME = "username";
068: static public final String PASSWORD = "password";
069: static public final String GENERIC_ERROR_PARAM = "hasError";
070:
071: public void init(PortletConfig config) throws PortletException {
072: super .init(config);
073: pContext = config.getPortletContext();
074: }
075:
076: public void doView(RenderRequest request, RenderResponse response)
077: throws PortletException, IOException {
078:
079: PortletSession ports = request.getPortletSession();
080: // Unique identifier for last renderRequest for this particular channel
081: // This will be used in the launcher jsps to pull preferences
082: PortletPreferences pref = request.getPreferences();
083: ports.setAttribute(getChannelname(response), pref,
084: PortletSession.APPLICATION_SCOPE);
085:
086: // IS Username and ssotoken cannot be determined from the launcher jsp, so must be set in session
087: String userdn = request.getRemoteUser();
088: String username = userdn.substring(userdn.indexOf('=') + 1,
089: userdn.indexOf(','));
090: // pContext.log("Username is "+username);
091: ports.setAttribute(USERNAME, username,
092: PortletSession.APPLICATION_SCOPE);
093: // Principal prin = request.getUserPrincipal();
094: // pContext.log("Prin is "+prin);
095: // ports.setAttribute(USERNAME, prin.getName(), PortletSession.APPLICATION_SCOPE);
096: ports.setAttribute(SSOTOKEN,
097: request.getAttribute(SSOTOKEN_ATT),
098: PortletSession.APPLICATION_SCOPE);
099:
100: // Code that handles the jsp render - stolen from
101: // com.sun.portal.portlet.samples.jspportlet.JSPPortlet
102: String contentPage = pref.getValue(CONTENT_PAGE, EMPTY_STRING);
103: String localizedContentPage = getLocalizedJSP(request
104: .getLocale(), contentPage);
105:
106: response.setContentType(request.getResponseContentType());
107: if (localizedContentPage != null
108: && localizedContentPage.length() != 0) {
109: PortletRequestDispatcher dispatcher = pContext
110: .getRequestDispatcher(localizedContentPage);
111: try {
112: dispatcher.include(request, response);
113: } catch (IOException e) {
114: throw new PortletException(
115: "IMPortlet.doView IOException", e);
116: } catch (Exception e) {
117: throw new PortletException(
118: "IMPortlet.doView Exception. contentPage="
119: + contentPage + " localCP="
120: + localizedContentPage + " disp="
121: + dispatcher, e);
122: }
123: }
124: }
125:
126: public void doEdit(RenderRequest request, RenderResponse response)
127: throws PortletException {
128: PortletPreferences pref = request.getPreferences();
129: String editPage = pref.getValue(EDIT_PAGE, EMPTY_STRING);
130: String localizedEditPage = getLocalizedJSP(request.getLocale(),
131: editPage);
132:
133: response.setContentType(request.getResponseContentType());
134: if (editPage != null && editPage.length() != 0) {
135: try {
136: PortletRequestDispatcher dispatcher = pContext
137: .getRequestDispatcher(localizedEditPage);
138: dispatcher.include(request, response);
139: } catch (IOException e) {
140: throw new PortletException(
141: "JSPPortlet.doEdit IOException", e);
142: }
143: }
144: }
145:
146: public void doHelp(RenderRequest request, RenderResponse response)
147: throws PortletException {
148: PortletPreferences pref = request.getPreferences();
149: String helpPage = pref.getValue(HELP_PAGE, EMPTY_STRING);
150: String localizedHelpPage = getLocalizedJSP(request.getLocale(),
151: helpPage);
152:
153: response.setContentType(request.getResponseContentType());
154: if (helpPage != null && helpPage.length() != 0) {
155: try {
156: PortletRequestDispatcher dispatcher = pContext
157: .getRequestDispatcher(localizedHelpPage);
158: dispatcher.include(request, response);
159: } catch (IOException e) {
160: throw new PortletException(
161: "JSPPortlet.doEdit IOException", e);
162: }
163: }
164: }
165:
166: public void processAction(ActionRequest request,
167: ActionResponse actionResponse) throws PortletException,
168: java.io.IOException {
169:
170: PortletPreferences pref = request.getPreferences();
171: // Code to update preferences in edit mode
172: // May need an additional check for the cancel key
173:
174: if (request.getParameter(EDIT_CANCEL) != null) {
175: actionResponse.setPortletMode(PortletMode.VIEW);
176: } else if (request.getParameter(EDIT_FINISHED) != null) {
177: boolean hasError = false;
178: String val;
179:
180: /* contactGroup */
181: val = request.getParameter(CONTACT_GROUP);
182: if (val != null && val.length() > 0) {
183: if (val.equals(CONTACT_GROUP_ALL)) {
184: val = EMPTY_STRING;
185: }
186: if (!val.equals(pref.getValue(CONTACT_GROUP,
187: EMPTY_STRING))) {
188: pref.setValue(CONTACT_GROUP, val);
189: }
190: }
191:
192: /* server */
193: val = request.getParameter(SERVER);
194: if (val != null && val.length() > 0) {
195: pref.setValue(SERVER, val);
196: }
197:
198: /* port */
199: val = request.getParameter(PORT);
200: if (val != null && val.length() > 0) {
201: try {
202: int port = Integer.parseInt(val);
203: if ((port == 0) || (port > 65535)) {
204: throw new NumberFormatException();
205: } else {
206: pref.setValue(PORT, val);
207: }
208: } catch (NumberFormatException nfe) {
209: actionResponse.setRenderParameter(PORT_ERROR_PARAM,
210: val);
211: hasError = true;
212: }
213: }
214:
215: /* mux */
216: val = request.getParameter(MUX);
217: if (val != null && val.length() > 0) {
218: pref.setValue(MUX, val);
219: }
220:
221: /* muxport */
222: val = request.getParameter(MUX_PORT);
223: if (val != null && val.length() > 0) {
224:
225: try {
226: int muxport = Integer.parseInt(val);
227: if ((muxport == 0) || (muxport > 65535)) {
228: throw new NumberFormatException();
229: } else {
230: pref.setValue(MUX_PORT, val);
231: }
232: } catch (NumberFormatException nfe) {
233: actionResponse.setRenderParameter(
234: MUX_PORT_ERROR_PARAM, val);
235: hasError = true;
236: }
237: }
238:
239: /* clientRunMode */
240: val = request.getParameter(CLIENT_RUN_MODE);
241: if (val != null && val.length() > 0) {
242: // Test for either plugin or jnlp, the only two supported run modes
243: if (val.equals(PLUGIN_RUN_MODE)
244: || val.equals(JNLP_RUN_MODE)) {
245: pref.setValue(CLIENT_RUN_MODE, val);
246: } else {
247: actionResponse.setRenderParameter(CRM_ERROR_PARAM,
248: val);
249: hasError = true;
250: }
251: }
252:
253: /* username */
254: val = request.getParameter(USERNAME);
255: if (val != null && (val.length() > 0)) {
256: pref.setValue(USERNAME, val);
257: }
258:
259: /* password */
260: val = request.getParameter(PASSWORD);
261: if (val != null && (val.length() > 0)
262: && !val.equals(getDummyPassword(pref))) {
263: val = (String) AccessController
264: .doPrivileged(new EncryptAction(val));
265: pref.setValue(PASSWORD, val);
266: }
267:
268: pref.store();
269: if (!hasError) {
270: actionResponse.setPortletMode(PortletMode.VIEW);
271: } else {
272: actionResponse.setRenderParameter(GENERIC_ERROR_PARAM,
273: EMPTY_STRING);
274: actionResponse.setPortletMode(PortletMode.EDIT);
275: }
276: }
277: }
278:
279: // Return a string of "*" characters as long as the user's password
280: public String getDummyPassword(PortletPreferences pref) {
281: StringBuffer dummy = new StringBuffer();
282: int len = ((String) AccessController
283: .doPrivileged(new DecryptAction(pref.getValue(
284: "password", "")))).length();
285: for (int i = 0; i < len; i++) {
286: dummy.append('*');
287: }
288: return dummy.toString();
289: }
290:
291: // Return full pathname to localized JSP
292: protected String getLocalizedJSP(Locale locale, String jspPath) {
293: String realJspPath = jspPath;
294:
295: if (locale != null) {
296: int separator = jspPath.lastIndexOf('/');
297: String jspBaseDir;
298: if (separator > 0) {
299: jspBaseDir = jspPath.substring(0, separator);
300: } else {
301: jspBaseDir = "/";
302: }
303: String jspFileName = jspPath.substring(separator + 1);
304: PortletContext pContext = getPortletContext();
305:
306: String searchPath = getJSPPath(jspBaseDir, locale
307: .toString(), jspFileName);
308:
309: // search the requested JSP from the following location:
310: // <ctxt_root>/<portlet_base_dir>_<language>_<country>/<jsp_file_name>
311: if (pContext.getResourceAsStream(searchPath) != null) {
312: realJspPath = searchPath;
313: } else {
314: // if the country code is not empty, try to search the
315: // requested JSP from the following location:
316: // <ctxt_root>/<portlet_base_dir>_<language>/<jsp_file_name>
317: if (locale.getCountry().length() > 0) {
318: searchPath = getJSPPath(jspBaseDir, locale
319: .getLanguage(), jspFileName);
320:
321: if (pContext.getResourceAsStream(searchPath) != null) {
322: realJspPath = searchPath;
323: }
324: }
325: }
326: }
327: return realJspPath;
328: }
329:
330: private String getJSPPath(String jspBaseDir, String localeStr,
331: String jspFileName) {
332: StringBuffer sb = new StringBuffer();
333: sb.append(jspBaseDir).append('_').append(localeStr).append('/')
334: .append(jspFileName);
335: return sb.toString();
336: }
337:
338: private String getChannelname(RenderResponse rRes) {
339: String text = rRes.getNamespace();
340: // Substitute characters that kill javascript with "_"
341: StringBuffer escaped = new StringBuffer();
342: for (int i = 0; i < text.length(); i++) {
343: char c = text.charAt(i);
344: switch (c) {
345: case '!':
346: case '#':
347: case '%':
348: case '\'':
349: case ')':
350: case '+':
351: case '-':
352: case '/':
353: case ';':
354: case '=':
355: case '?':
356: case '\\':
357: case '^':
358: case '`':
359: case '|':
360: case '~':
361: case '"':
362: case '$':
363: case '&':
364: case '(':
365: case '*':
366: case ',':
367: case '.':
368: case ':':
369: case '<':
370: case '>':
371: case '@':
372: case '[':
373: case ']':
374: case '{':
375: case '}':
376: case ' ':
377: escaped.append('_');
378: continue;
379: default:
380: escaped.append(c);
381: continue;
382: }
383: }
384: return escaped.toString();
385: }
386: }
|