001: /**
002: * $Id: NetFileSessionInitializer.java,v 1.20 2005/11/30 11:26:35 ss150821 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.netfile.servlet.java1;
014:
015: import java.util.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.util.logging.*;
018: import java.io.*;
019: import com.iplanet.sso.SSOToken;
020:
021: /**
022: *
023: * @author Ali Hasnain Baqri
024: * @author Suresh Yellamaraju
025: */
026: /*
027: * Created and invoked during NetFile session
028: * initialization. Replaces the NetFileLiteUIServlet.
029: */
030: public class NetFileSessionInitializer {
031:
032: public NetFileLogManager logMgr;
033: private static Logger logger = PortalLogger
034: .getLogger(NetFileSessionInitializer.class);
035: private static boolean is_first_request = true;
036: private char c_eol = '\n';
037: private String s_empty_string = "";
038: private static final String DEFAULT_TEMPORARY_DIRECTORY = "/tmp";
039:
040: public NetFileSessionInitializer(NetFileLogManager logManager) {
041: logMgr = logManager;
042: }
043:
044: /* in case of a session fail over the user session does not get initialised
045: * we just want to create the temporary directory and register the listener
046: */
047:
048: void reInitialiseUserSession(SSOToken ssoToken,
049: NetFileServlet nfs_token_listener) throws Throwable {
050: try {
051: ssoToken.addSSOTokenListener(nfs_token_listener);
052: NetFileContext nfContext = new NetFileContextImpl(ssoToken);
053:
054: Map[] prefs = nfContext.getPreferences();
055:
056: NetFileAttributeExtractor nfAttrs = new NetFileAttributeExtractor(
057: prefs[0]);
058:
059: createTemporaryDirectory(ssoToken, nfAttrs);
060: } catch (Exception e) {
061: // logger.log(Level.SEVERE, "Exception while reinitialising user session", e);
062: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1181");
063: }
064: }
065:
066: String initialiseNewUserSession(SSOToken ssoToken,
067: NetFileResource nfr_user_locale_i18n_bucket,
068: NetFileServlet nfs_token_listener, String htmlCharset)
069: throws Throwable {
070: String szDefaultDomain = s_empty_string;
071: StringBuffer returnbufstr = new StringBuffer(1000);
072:
073: try {
074: ssoToken.addSSOTokenListener(nfs_token_listener);
075: java.security.Principal princi = ssoToken.getPrincipal();
076: String userIdentity = princi.getName();
077: int startIndex = userIdentity.indexOf('=', 0);
078: int endIndex = userIdentity.indexOf(',', 0);
079: returnbufstr.append(
080: "userid="
081: + userIdentity.substring(startIndex + 1,
082: endIndex)).append(c_eol);
083:
084: NetFileContext nfContext = new NetFileContextImpl(ssoToken);
085:
086: Map[] prefs = nfContext.getPreferences();
087:
088: NetFileAttributeExtractor nfAttrs = new NetFileAttributeExtractor(
089: prefs[0]);
090:
091: returnbufstr.append(nfContext.getMailPreferences());
092:
093: returnbufstr.append("sunPortalNetFileNTDomain=").append(
094: nfAttrs.getString("sunPortalNetFileNTDomain",
095: s_empty_string)).append(c_eol);
096:
097: szDefaultDomain = nfAttrs.getString(
098: "sunPortalNetFileDefaultDomain", "");
099: returnbufstr.append("sunPortalNetFileDefaultDomain=")
100: .append(szDefaultDomain);
101:
102: returnbufstr.append("sunPortalNetFileWindowLocation=")
103: .append(
104: nfAttrs.getString(
105: "sunPortalNetFileWindowLocation",
106: "100|50")).append(c_eol);
107:
108: returnbufstr.append("sunPortalNetFileWindowSize=").append(
109: nfAttrs.getString("sunPortalNetFileWindowSize",
110: "700|400")).append(c_eol);
111:
112: returnbufstr.append("iplanet-am-user-preferredlocale=")
113: .append(nfContext.getUserLocale()).append(c_eol);
114:
115: returnbufstr.append("htmlCharset=").append(htmlCharset)
116: .append(c_eol);
117:
118: NetFileHostDataProcessor nfhdp = new NetFileHostDataProcessor(
119: logMgr, ssoToken, nfr_user_locale_i18n_bucket);
120: nfhdp.setDefaultDomain(szDefaultDomain);
121: returnbufstr = nfhdp.processHostsData(returnbufstr,
122: nfContext, nfAttrs, nfr_user_locale_i18n_bucket);
123:
124: returnbufstr
125: .append("sunPortalNetFileMaxSearchDir=")
126: .append(
127: nfAttrs
128: .getInt(
129: "sunPortalNetFileMaxSearchDir",
130: 100)).append(c_eol);
131:
132: returnbufstr.append("sunPortalNetFileAccessWin=").append(
133: nfAttrs.getBoolean("sunPortalNetFileAccessWin",
134: true)).append(c_eol);
135:
136: returnbufstr.append("sunPortalNetFileAccessNFS=").append(
137: nfAttrs.getBoolean("sunPortalNetFileAccessNFS",
138: true)).append(c_eol);
139:
140: returnbufstr.append("sunPortalNetFileAccessFTP=").append(
141: nfAttrs.getBoolean("sunPortalNetFileAccessFTP",
142: true)).append(c_eol);
143:
144: returnbufstr.append("sunPortalNetFileAccessNetware=")
145: .append(
146: nfAttrs.getBoolean(
147: "sunPortalNetFileAccessNetware",
148: true)).append(c_eol);
149:
150: returnbufstr.append("sunPortalNetFileAllowDelete=").append(
151: nfAttrs.getBoolean("sunPortalNetFileAllowDelete",
152: true)).append(c_eol);
153:
154: returnbufstr.append("sunPortalNetFileAllowRename=").append(
155: nfAttrs.getBoolean("sunPortalNetFileAllowRename",
156: true)).append(c_eol);
157:
158: returnbufstr.append("sunPortalNetFileAllowMail=").append(
159: nfAttrs.getBoolean("sunPortalNetFileAllowMail",
160: true)).append(c_eol);
161: returnbufstr.append("sunPortalNetFileAllowSearch=").append(
162: nfAttrs.getBoolean("sunPortalNetFileAllowSearch",
163: true)).append(c_eol);
164: returnbufstr.append("sunPortalNetFileAllowCompression=")
165: .append(
166: nfAttrs.getBoolean(
167: "sunPortalNetFileAllowCompression",
168: true)).append(c_eol);
169:
170: returnbufstr.append("sunPortalNetFileAllowUpload=").append(
171: nfAttrs.getBoolean("sunPortalNetFileAllowUpload",
172: true)).append(c_eol);
173: returnbufstr.append("sunPortalNetFileAllowDownload=")
174: .append(
175: nfAttrs.getBoolean(
176: "sunPortalNetFileAllowDownload",
177: true)).append(c_eol);
178:
179: returnbufstr
180: .append("sunPortalNetFileAllowChangeUserId=")
181: .append(
182: nfAttrs
183: .getBoolean(
184: "sunPortalNetFileAllowChangeUserId",
185: true)).append(c_eol);
186:
187: returnbufstr
188: .append("sunPortalNetFileAllowChangeDomain=")
189: .append(
190: nfAttrs
191: .getBoolean(
192: "sunPortalNetFileAllowChangeDomain",
193: true)).append(c_eol);
194:
195: returnbufstr.append("sunPortalNetFileFileUploadLimit=")
196: .append(
197: nfAttrs.getInt(
198: "sunPortalNetFileFileUploadLimit",
199: 5)).append(c_eol);
200:
201: returnbufstr.append("sunPortalNetFileExecute=")
202: .append(
203: nfAttrs.getBoolean(
204: "sunPortalNetFileExecute", true))
205: .append(c_eol);
206:
207: returnbufstr.append("sunPortalNetFileOSCharSet=").append(
208: nfAttrs.getString("sunPortalNetFileOSCharSet",
209: "Unicode(UTF-8)")).append(c_eol);
210:
211: returnbufstr
212: .append("iplanet-srap-netfile-application-tempdir=")
213: .append(createTemporaryDirectory(ssoToken, nfAttrs))
214: .append(c_eol);
215: logSessionStartTime(logMgr, ssoToken);
216:
217: nfAttrs.setUserAttributesMap(prefs[1]);
218:
219: returnbufstr.append("sunPortalNetFileApplicationName=")
220: .append(
221: nfAttrs.getString(
222: "sunPortalNetFileApplicationName",
223: "Sun JavaTM System Portal Server"))
224: .append(c_eol);
225:
226: returnbufstr
227: .append("sunPortalNetFileApplicationRelease=")
228: .append(
229: nfAttrs
230: .getString(
231: "sunPortalNetFileApplicationRelease",
232: "6")).append(c_eol);
233:
234: returnbufstr.append(
235: "sunPortalNetFileApplicationReleaseDate=").append(
236: nfAttrs.getString(
237: "sunPortalNetFileApplicationReleaseDate",
238: "2004Q2")).append(c_eol);
239:
240: returnbufstr.append("sunPortalNetFileCompanyName=").append(
241: nfAttrs.getString("sunPortalNetFileCompanyName",
242: "Sun Microsystems Inc.")).append(c_eol);
243:
244: } catch (Exception e) {
245: returnbufstr.append("Exception " + e.getMessage()).append(
246: c_eol);
247: // logger.log(Level.SEVERE, "Exception getting attributes data",e);
248: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1182");
249: }
250:
251: return returnbufstr.toString();
252:
253: }
254:
255: private void logSessionStartTime(NetFileLogManager logMgr,
256: SSOToken ssoToken) {
257: try {
258: FileOption file_option = new FileOption(logMgr, "UTF8",
259: ssoToken);
260: if (NetFileSessionInitializer.is_first_request) {
261: file_option.doLog(file_option
262: .getPlatformLocalisedString("netfile_starts"));
263: NetFileSessionInitializer.is_first_request = false;
264: } else {
265: file_option.doLog(file_option
266: .getPlatformLocalisedString("session_starts"));
267: }
268: } catch (Exception e) {
269: // logger.log(Level.SEVERE, "Could not log session start time in iwtPlatformLocale ",e);
270: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1183");
271: }
272: }
273:
274: String createTemporaryDirectory(SSOToken ssoToken,
275: NetFileAttributeExtractor nfAttrs) throws NetFileException {
276: String tmpdir = NetFileSessionInitializer.DEFAULT_TEMPORARY_DIRECTORY;
277: try {
278: tmpdir = nfAttrs
279: .getString(
280: "sunPortalNetFileTempDir",
281: NetFileSessionInitializer.DEFAULT_TEMPORARY_DIRECTORY);
282: } catch (Exception e) {
283: throw new NetFileException(
284: "Could not obtain temporary directory from profile");
285: }
286: /*
287: * If we are not able to retrieve the temporary directory name from the
288: * profile, then we will use the default "/tmp"
289: */
290: if (tmpdir == null || tmpdir.equals(s_empty_string)) {
291: tmpdir = NetFileSessionInitializer.DEFAULT_TEMPORARY_DIRECTORY;
292: }
293: /* Get the temporary directory for this user for this session */
294: tmpdir = getTemporaryDirectory(ssoToken, tmpdir);
295: try {
296: File temporary_directory = new File(tmpdir);
297: if (!temporary_directory.exists()) {
298: temporary_directory.mkdirs();
299: }
300: if (!temporary_directory.exists()) {
301: throw new Exception("Temporary directory " + tmpdir
302: + " could not be created");
303: }
304: if (!temporary_directory.canRead()) {
305: throw new Exception("No read permission for " + tmpdir);
306: }
307: if (!temporary_directory.canWrite()) {
308: throw new Exception("No write permission for " + tmpdir);
309: }
310: } catch (Exception e) {
311: // logger.log(Level.SEVERE, "Could not create temporary directory :"+tmpdir,e);
312: Object[] params3 = { tmpdir, e };
313: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1184", params3);
314: throw new NetFileException(
315: new String[] {
316: NetFileException.KEY_IDENTIFIER_PREFIX
317: + "unable_to_create_temp_directory_or_permission_denied",
318: tmpdir });
319: }
320: return tmpdir;
321: }
322:
323: String getTemporaryDirectory(SSOToken ssoToken,
324: String s_temporary_directory) {
325: s_temporary_directory = createTemporaryDirectoryName(ssoToken,
326: s_temporary_directory);
327: NetFileServlet.putTemporaryDirectoryForSSOToken(ssoToken,
328: s_temporary_directory);
329: NetFileServlet.putTempDirCount(ssoToken,
330: new SessionTempDirCounter());
331: // logger.info("Temp directory is "+s_temporary_directory);
332: Object[] params4 = { s_temporary_directory };
333: logger.log(Level.INFO, "PSSRNF_CSPNSJ1185", params4);
334: return s_temporary_directory;
335: }
336:
337: String createTemporaryDirectoryName(SSOToken ssoToken,
338: String s_temporary_directory) {
339: // logger.info("Original tmp directory is "+s_temporary_directory);
340: Object[] params5 = { s_temporary_directory };
341: logger.log(Level.INFO, "PSSRNF_CSPNSJ1186", params5);
342: String s_sso_token = ssoToken.getTokenID().toString();
343: int i_index = s_sso_token.indexOf('@');
344: int i_length = s_sso_token.length();
345:
346: for (int i = 0; i < i_length; ++i) {
347: if (!Character.isJavaIdentifierPart(s_sso_token.charAt(i))) {
348: if (i > 1) {
349: i_index = i;
350: }
351: break;
352: }
353: }
354: return ((new StringBuffer()).append(s_temporary_directory)
355: .append("/").append(s_sso_token.substring(0, i_index)))
356: .toString();
357: }
358: }
|