001: /**
002: * $Id: NetFileApplet.java,v 1.36 2006/01/02 10:07:36 fa151985 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 javax.servlet.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import javax.servlet.http.*;
018: import java.io.*;
019: import java.util.*;
020: import java.util.logging.*;
021:
022: import com.iplanet.sso.*;
023:
024: import com.iplanet.am.util.AMClientDetector;
025: import com.iplanet.am.util.SystemProperties;
026:
027: import com.iplanet.services.cdm.Client;
028: import com.iplanet.services.cdm.ClientException;
029:
030: public class NetFileApplet extends HttpServlet {
031:
032: public static Logger logger = PortalLogger
033: .getLogger(NetFileApplet.class);
034:
035: public void init(ServletConfig config) throws ServletException {
036: super .init(config);
037: }
038:
039: public void doGet(HttpServletRequest req, HttpServletResponse res)
040: throws ServletException, IOException {
041:
042: SSOTokenManager manager = null;
043: SSOToken ssoToken = null;
044:
045: Locale userLocale = null;
046: ResourceBundle userRes = null;
047: String htmlCharset = null;
048:
049: try {
050: manager = SSOTokenManager.getInstance();
051: ssoToken = manager.createSSOToken(req);
052:
053: String qs = req.getQueryString();
054: Hashtable qsParams = HttpUtils.parseQueryString(qs);
055:
056: // No required parameters in request
057: // So show an error in JVM default locale.
058: if (qsParams == null) {
059: getDefaultResourceValues(req, userRes, userLocale,
060: htmlCharset);
061: writeErrorHTML(req, res, userRes, htmlCharset, userRes
062: .getString("noparams"));
063: return;
064: }
065:
066: NetFileContext nfSession = new NetFileContextImpl(ssoToken);
067:
068: // No NetFileContext. Return session error.
069: if (nfSession == null) {
070: getDefaultResourceValues(req, userRes, userLocale,
071: htmlCharset);
072: writeErrorHTML(req, res, userRes, htmlCharset, userRes
073: .getString("sessionerror"));
074: return;
075: }
076:
077: if (nfSession.isSessionValid(ssoToken)) {
078:
079: // Get the refering parameter
080: String[] szReferFrom = (String[]) qsParams.get("Refer");
081:
082: nfSession.determineHTMLCharset(req);
083: htmlCharset = nfSession.getHTMLCharset();
084: if ((htmlCharset == null) || (htmlCharset.equals("")))
085: htmlCharset = "ISO-8859-1";
086:
087: userRes = getUserResourceValues(req, nfSession,
088: userLocale);
089: if (szReferFrom != null) {
090:
091: // Allright, so do you have the permission?
092: if (nfSession.isExecutable(ssoToken)) {
093: if (szReferFrom[0].equals("java1"))
094: writeJava1AppletHTML(req, res, userRes,
095: htmlCharset, ssoToken.getTokenID()
096: .toString());
097: else if (szReferFrom[0].equals("java2"))
098: writeJava2AppletHTML(req, res, userRes,
099: htmlCharset, ssoToken.getTokenID()
100: .toString());
101: } else {
102: // No permission. Take a walk.
103: writeErrorHTML(req, res, userRes, htmlCharset,
104: userRes.getString("nopermission"));
105: return;
106: }
107:
108: } else {
109: // Bad Request
110: writeErrorHTML(req, res, userRes, htmlCharset,
111: userRes.getString("noparams"));
112: return;
113: }
114: } else {
115: // No valid session
116: userRes = getUserResourceValues(req, nfSession,
117: userLocale);
118: writeErrorHTML(req, res, userRes, htmlCharset, userRes
119: .getString("sessionerror"));
120: return;
121: }
122: } catch (SSOException ssoe) {
123: ssoe.printStackTrace();
124: getDefaultResourceValues(req, userRes, userLocale,
125: htmlCharset);
126: writeErrorHTML(req, res, userRes, htmlCharset, userRes
127: .getString("sessionerror"));
128: }
129: return;
130: }
131:
132: private void writeJava1AppletHTML(HttpServletRequest req,
133: HttpServletResponse res, ResourceBundle userResources,
134: String charset, String sznfID) throws IOException {
135:
136: String staticURL = getStaticUrl(req);
137: String dynaURL = getDynamicUrl(req);
138:
139: // logger.info("Charset is " + charset);
140: Object[] params0 = { charset };
141: logger.log(Level.INFO, "PSSRNF_CSPNSJ1079", params0);
142:
143: PrintWriter out = getWriter(res, charset);
144: res.setHeader("Pragma", "no-cache");
145: res.setHeader("Cache-Control", "max-age=0");
146:
147: String bgcolors = userResources.getString("bgcolorS");
148: String textcolors = userResources.getString("textcolorS");
149:
150: out.println("<html><head><title>"
151: + userResources.getString("nftitle") + "</title>");
152: out
153: .println("<script src=\""
154: + req.getContextPath()
155: + "/close-windows.jsp\" language=\"JavaScript\" type=\"text/javascript\"></script>");
156: out.println("</head>");
157: out.print("<body text='");
158: out.print(textcolors);
159: out.print("'");
160: out.print(" bgcolor='");
161: out.print(bgcolors);
162: out
163: .print("' onLoad=\"javascript:windowOpened(window.name);\">");
164:
165: out.println("<h2>" + userResources.getString("nftitle")
166: + "</h2>");
167:
168: String qs = req.getQueryString();
169:
170: if (qs != null && qs.indexOf("exit") != -1) {
171:
172: out
173: .println("<HEAD><SCRIPT LANGUAGE=\"Javascript\">window.close();</SCRIPT></HEAD>");
174: } else {
175:
176: out.println("<p><b>" + userResources.getString("nftext")
177: + "</p></b>");
178: String agent = req.getHeader("user-agent");
179:
180: if (agent.indexOf("Mac_PowerPC") != -1
181: || agent.indexOf("Macintosh") != -1) {
182: // Mac-MSIE gets special JAR, along with cookie
183: out
184: .println("<applet code=NetFileApplet.class codebase="
185: + staticURL
186: + "/netfile archive=nfuijava1.mac.jar alt=\"NetFile Java1\" height=2 width=1 MAYSCRIPT>");
187: out.println("<param name=\"cookiename\" value=\""
188: + SystemProperties.get(
189: "com.iplanet.am.cookie.name",
190: "iPlanetDirectoryPro") + "\">");
191: } else {
192: out
193: .println("<applet code=NetFileApplet.class codebase="
194: + staticURL
195: + "/netfile archive=nfuijava1.jar alt=\"NetFile Java1\" height=2 width=1 MAYSCRIPT>");
196: }
197:
198: out.println("<param name=nfid value=\"" + sznfID + "\">");
199: out.println("<param name=agent value=\"" + agent + "\">");
200: out.println("<param name=uploadURL value=" + dynaURL
201: + "/NetFileUploadServlet>");
202: out.println("<param name=NetFileURL value=" + dynaURL
203: + "/NetFileServlet>");
204: out.println("<param name=InitURL value=" + dynaURL
205: + "/NetFileServlet>");
206: out.println("<param name=exitURL value=" + dynaURL
207: + "/NetFileApplet?exitP=exit>");
208: out.println("<param name=OpenFileURL value=" + dynaURL
209: + "/NetFileOpenFileServlet>");
210: out.println("</applet>");
211: }
212:
213: out.println("</html>");
214: out.close();
215:
216: }
217:
218: private void writeJava2AppletHTML(HttpServletRequest req,
219: HttpServletResponse res, ResourceBundle userResources,
220: String charset, String sznfID) throws IOException {
221:
222: String szDynaURL = getDynamicUrl(req);
223: String szStaticURL = getStaticUrl(req);
224:
225: String agent = req.getHeader("user-agent");
226: // logger.info("User agent is " + agent);
227: Object[] params1 = { agent };
228: logger.log(Level.INFO, "PSSRNF_CSPNSJ1080", params1);
229:
230: if ((agent.indexOf("MSIE") > 0)
231: || (agent.indexOf("Netscape") > 0)) {
232: } else if (agent.indexOf("Gecko") > 0) {
233: if (!isSupportedGeckoVersion(agent)) {
234: writeErrorHTML(req, res, userResources, charset,
235: userResources.getString("NoSupMsg"));
236: return;
237: }
238: } else {
239: writeErrorHTML(req, res, userResources, charset,
240: userResources.getString("NoSupMsg"));
241: return;
242: }
243: // write the Applet html page.
244: PrintWriter out = getWriter(res, charset);
245: res.setHeader("Pragma", "no-cache");
246: res.setHeader("Cache-Control", "max-age=0");
247:
248: String bgcolors = userResources.getString("bgcolorS");
249: String textcolors = userResources.getString("textcolorS");
250:
251: out.println("<html><head><title>"
252: + userResources.getString("nftitle") + "</title>");
253: out
254: .println("<script src=\""
255: + req.getContextPath()
256: + "/close-windows.jsp\" language=\"JavaScript\" type=\"text/javascript\"></script>");
257: out.println("</head>");
258: out.print("<body text='");
259: out.print(textcolors);
260: out.print("'");
261: out.print(" bgcolor='");
262: out.print(bgcolors);
263: out
264: .print("' onLoad=\"javascript:windowOpened(window.name);\">");
265:
266: String qs = req.getQueryString();
267: if (qs != null && qs.indexOf("exit") != -1) {
268: out.println("<h2>" + userResources.getString("nfexit")
269: + "</h2>");
270: out
271: .println("<SCRIPT LANGUAGE=\"Javascript\">window.close();</SCRIPT>");
272: } else {
273: out.println("<h2>" + userResources.getString("nfmsg")
274: + "</h2>");
275: out.println("<p><b>" + userResources.getString("nftext")
276: + "</p></b>");
277: out
278: .println("<applet code=NetFileApplet.class codebase="
279: + szStaticURL
280: + "/netfile archive=nfuijava2.jar alt=\"NetFile Java2\" height=2 width=1 MAYSCRIPT>");
281: out.println("<param name=nfid value=\"" + sznfID + "\">");
282: out.println("<param name=agent value=\"" + agent + "\">");
283: out.println("<param name=UploadURL value=" + szDynaURL
284: + "/NetFileController?action=upload&stream=plain>");
285: out.println("<param name=NetFileURL value=" + szDynaURL
286: + "/NetFileController>");
287: out.println("<param name=OpenFileURL value=" + szDynaURL
288: + "/NetFileController?action=open&stream=object>");
289: out
290: .println("<param name=NFProto value=HTTP_JAVA_SERIALIZE>");
291: out.println("<param name=NoVMMessage value=\""
292: + userResources.getString("NoVMMessage") + "\">");
293: out.println("<param name=exitURL value=" + szDynaURL
294: + "/NetFileApplet?Refer=java2&exitP=exit>");
295: out.println("</applet>");
296: }
297: out.println("</body>");
298: out.println("</html>");
299: out.close();
300: }
301:
302: private void writeErrorHTML(HttpServletRequest req,
303: HttpServletResponse res, ResourceBundle userResources,
304: String charset, String szError) throws IOException {
305:
306: PrintWriter out = getWriter(res, charset);
307: res.setHeader("Cache-Control", "max-age=0");
308:
309: String bgcolors = userResources.getString("bgcolorS");
310: String textcolors = userResources.getString("textcolorS");
311:
312: out.println("<html><head><title>"
313: + userResources.getString("nftitle")
314: + "</title></head>");
315: out.print("<body text='");
316: out.print(textcolors);
317: out.print("'");
318: out.print(" bgcolor='");
319: out.print(bgcolors);
320: out.print("'>");
321: out.println("<h2>" + userResources.getString("nftitle")
322: + "</h2>");
323: out.println("<p><b> " + szError + " </p></b>");
324: out.println("</html>");
325: out.close();
326:
327: }
328:
329: private String getStaticUrl(HttpServletRequest req) {
330: return req.getContextPath();
331: }
332:
333: private String getDynamicUrl(HttpServletRequest req) {
334: String s = req.getServletPath();
335: int i = s.lastIndexOf("/");
336: return req.getContextPath() + s.substring(0, i);
337: }
338:
339: private PrintWriter getWriter(HttpServletResponse res,
340: String charset) throws IOException {
341: if (charset == null) {
342: res.setContentType("text/html");
343: return res.getWriter();
344: } else {
345: res.setContentType("text/html; charset=" + charset);
346: return new PrintWriter(new BufferedWriter(
347: new OutputStreamWriter(res.getOutputStream(),
348: charset)));
349: }
350: }
351:
352: private ResourceBundle getUserResourceValues(
353: HttpServletRequest req, NetFileContext nfSession,
354: Locale locale) {
355: // Obtain the user locale.
356: // If no locale, then get the platform default
357: locale = nfSession.getUserLocale();
358: if (locale == null) {
359: locale = Locale.getDefault();
360: }
361:
362: // Get the resource bundle based on locale
363: return ResourceBundle.getBundle(FileOption.SERVLET_PROPERTIES,
364: locale);
365: }
366:
367: private void getDefaultResourceValues(HttpServletRequest req,
368: ResourceBundle bundle, Locale locale, String charset) {
369:
370: // Obtain the platform default locale
371: locale = Locale.getDefault();
372:
373: // Detect the HTML character set using the client type
374: AMClientDetector amCD = new AMClientDetector();
375: String clientType = amCD.getClientType(req);
376:
377: try {
378: Client clientObj = Client.getInstance(clientType);
379: charset = clientObj.getCharset(locale);
380: } catch (ClientException ce) {
381: }
382:
383: if ((charset == null) || (charset.equals("")))
384: charset = "ISO-8859-1";
385:
386: // Finally, get the resource bundle based on locale
387: bundle = ResourceBundle.getBundle(
388: FileOption.SERVLET_PROPERTIES, locale);
389: }
390:
391: private boolean isSupportedGeckoVersion(String agent) {
392: int verIndex = agent.indexOf("rv:");
393:
394: if (verIndex > 0) {
395: String version = agent.substring(verIndex);
396: int closeIndex = version.indexOf(')');
397: if (closeIndex <= 0)
398: return false;
399:
400: version = version.substring(3, closeIndex);
401: // logger.info("Mozilla version is " + version);
402: Object[] params2 = { version };
403: logger.log(Level.INFO, "PSSRNF_CSPNSJ1081", params2);
404:
405: Integer major = new Integer(new String(new char[] { version
406: .charAt(0) }));
407: if (major.intValue() <= 0) {
408: } else if (major.intValue() == 1) {
409: if (version.length() >= 3) {
410: Integer minor = new Integer(new String(
411: new char[] { version.charAt(2) }));
412: if (minor.intValue() >= 2)
413: return true;
414: }
415: }
416: } else {
417: return false;
418: }
419: return false;
420: }
421:
422: }
|