001: /**
002: * $Id: NetFileApplet.java,v 1.3 2006/02/23 10:48:05 aj157941 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.java2;
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: userRes = getDefaultResourceValues(req, userRes,
060: userLocale, 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: userRes = getDefaultResourceValues(req, userRes,
071: userLocale, 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("java2"))
094: writeJava2AppletHTML(req, res, userRes,
095: htmlCharset, ssoToken.getTokenID()
096: .toString());
097: } else {
098: // No permission. Take a walk.
099: writeErrorHTML(req, res, userRes, htmlCharset,
100: userRes.getString("nopermission"));
101: return;
102: }
103:
104: } else {
105: // Bad Request
106: writeErrorHTML(req, res, userRes, htmlCharset,
107: userRes.getString("noparams"));
108: return;
109: }
110: } else {
111: // No valid session
112: userRes = getUserResourceValues(req, nfSession,
113: userLocale);
114: writeErrorHTML(req, res, userRes, htmlCharset, userRes
115: .getString("sessionerror"));
116: return;
117: }
118: } catch (SSOException ssoe) {
119: ssoe.printStackTrace();
120: userRes = getDefaultResourceValues(req, userRes,
121: userLocale, htmlCharset);
122: writeErrorHTML(req, res, userRes, htmlCharset, userRes
123: .getString("sessionerror"));
124: }
125: return;
126: }
127:
128: private void writeJava2AppletHTML(HttpServletRequest req,
129: HttpServletResponse res, ResourceBundle userResources,
130: String charset, String sznfID) throws IOException {
131:
132: String szDynaURL = getDynamicUrl(req);
133: String szStaticURL = getStaticUrl(req);
134:
135: String agent = req.getHeader("user-agent");
136: // logger.info("User agent is " + agent);
137: Object[] params1 = { agent };
138: logger.log(Level.INFO, "PSSRNF_CSPNSJ1080", params1);
139:
140: if ((agent.indexOf("MSIE") > 0)
141: || (agent.indexOf("Netscape") > 0)) {
142: } else if (agent.indexOf("Gecko") > 0) {
143: if (!isSupportedGeckoVersion(agent)) {
144: writeErrorHTML(req, res, userResources, charset,
145: userResources.getString("NoSupMsg"));
146: return;
147: }
148: } else {
149: writeErrorHTML(req, res, userResources, charset,
150: userResources.getString("NoSupMsg"));
151: return;
152: }
153: // write the Applet html page.
154: PrintWriter out = getWriter(res, charset);
155: res.setHeader("Pragma", "no-cache");
156: res.setHeader("Cache-Control", "max-age=0");
157:
158: String bgcolors = userResources.getString("bgcolorS");
159: String textcolors = userResources.getString("textcolorS");
160:
161: out.println("<html><head><title>"
162: + userResources.getString("nftitle") + "</title>");
163: out
164: .println("<script src=\""
165: + req.getContextPath()
166: + "/close-windows.jsp\" language=\"JavaScript\" type=\"text/javascript\"></script>");
167: out.println("</head>");
168: out.print("<body text='");
169: out.print(textcolors);
170: out.print("'");
171: out.print(" bgcolor='");
172: out.print(bgcolors);
173: out
174: .print("' onLoad=\"javascript:windowOpened(window.name);\">");
175:
176: String qs = req.getQueryString();
177: if (qs != null && qs.indexOf("exit") != -1) {
178: out.println("<h2>" + userResources.getString("nfexit")
179: + "</h2>");
180: out
181: .println("<SCRIPT LANGUAGE=\"Javascript\">window.close();</SCRIPT>");
182: } else {
183: out.println("<SCRIPT LANGUAGE=\"Javascript\">");
184: out
185: .println(" <!-- we need a timeout period of 3 seconds for closing the browser -->");
186: out
187: .println(" <!-- window. This allows the java method of the applet to return cleanly.. -->");
188: out
189: .println(" <!-- Otherwise, it gives an error that crashes Netscape window. -->");
190: out.println(" function closeMe()");
191: out.println(" {");
192: out
193: .println(" window.setTimeout(\"reallyCloseMe()\", 3000);");
194: out.println(" }");
195: out.println(" function reallyCloseMe()");
196: out.println(" {");
197: out.println(" try");
198: out.println(" {");
199: out.println(" document.writeln(\"<html>\");");
200: out
201: .println(" document.writeln(\" <script language=\\\"javascript\\\">window.close();<\\/script>\");");
202: out.println(" document.writeln(\"<\\/html>\");");
203: out.println(" document.close();");
204: out.println(" }");
205: out.println(" catch(e){}");
206: out.println(" }");
207: out.println("</SCRIPT>");
208: out.println("<h2>" + userResources.getString("nfmsg")
209: + "</h2>");
210: out.println("<p><b>" + userResources.getString("nftext")
211: + "</p></b>");
212: out
213: .println("<applet code=NetFileApplet.class codebase="
214: + szStaticURL
215: + "/netfile archive=nfuijava2.jar alt=\"NetFile Java2\" height=2 width=1 MAYSCRIPT>");
216: out.println("<param name=nfid value=\"" + sznfID + "\">");
217: out.println("<param name=agent value=\"" + agent + "\">");
218: out.println("<param name=UploadURL value=" + szDynaURL
219: + "/NetFileController?action=upload&stream=plain>");
220: out.println("<param name=NetFileURL value=" + szDynaURL
221: + "/NetFileController>");
222: out.println("<param name=OpenFileURL value=" + szDynaURL
223: + "/NetFileController?action=open&stream=object>");
224: out
225: .println("<param name=NFProto value=HTTP_JAVA_SERIALIZE>");
226: out.println("<param name=NoVMMessage value=\""
227: + userResources.getString("NoVMMessage") + "\">");
228: out.println("<param name=exitURL value=" + szDynaURL
229: + "/NetFileApplet?Refer=java2&exitP=exit>");
230: out.println("</applet>");
231: }
232: out.println("</body>");
233: out.println("</html>");
234: out.close();
235: }
236:
237: private void writeErrorHTML(HttpServletRequest req,
238: HttpServletResponse res, ResourceBundle userResources,
239: String charset, String szError) throws IOException {
240:
241: PrintWriter out = getWriter(res, charset);
242: res.setHeader("Cache-Control", "max-age=0");
243:
244: String bgcolors = userResources.getString("bgcolorS");
245: String textcolors = userResources.getString("textcolorS");
246:
247: out.println("<html><head><title>"
248: + userResources.getString("nftitle")
249: + "</title></head>");
250: out.print("<body text='");
251: out.print(textcolors);
252: out.print("'");
253: out.print(" bgcolor='");
254: out.print(bgcolors);
255: out.print("'>");
256: out.println("<h2>" + userResources.getString("nftitle")
257: + "</h2>");
258: out.println("<p><b> " + szError + " </p></b>");
259: out.println("</html>");
260: out.close();
261:
262: }
263:
264: private String getStaticUrl(HttpServletRequest req) {
265: return req.getContextPath();
266: }
267:
268: private String getDynamicUrl(HttpServletRequest req) {
269: String s = req.getServletPath();
270: int i = s.lastIndexOf("/");
271: return req.getContextPath() + s.substring(0, i);
272: }
273:
274: private PrintWriter getWriter(HttpServletResponse res,
275: String charset) throws IOException {
276: if (charset == null) {
277: res.setContentType("text/html");
278: return res.getWriter();
279: } else {
280: res.setContentType("text/html; charset=" + charset);
281: return new PrintWriter(new BufferedWriter(
282: new OutputStreamWriter(res.getOutputStream(),
283: charset)));
284: }
285: }
286:
287: private ResourceBundle getUserResourceValues(
288: HttpServletRequest req, NetFileContext nfSession,
289: Locale locale) {
290: // Obtain the user locale.
291: // If no locale, then get the platform default
292: locale = nfSession.getUserLocale();
293: if (locale == null) {
294: locale = Locale.getDefault();
295: }
296:
297: // Get the resource bundle based on locale
298: return ResourceBundle.getBundle("srapNetFileServletJava2",
299: locale);
300: }
301:
302: private ResourceBundle getDefaultResourceValues(
303: HttpServletRequest req, ResourceBundle bundle,
304: Locale locale, String charset) {
305:
306: // Obtain the platform default locale
307: locale = Locale.getDefault();
308:
309: // Detect the HTML character set using the client type
310: AMClientDetector amCD = new AMClientDetector();
311: String clientType = amCD.getClientType(req);
312:
313: try {
314: Client clientObj = Client.getInstance(clientType);
315: charset = clientObj.getCharset(locale);
316: } catch (ClientException ce) {
317: }
318:
319: if ((charset == null) || (charset.equals("")))
320: charset = "ISO-8859-1";
321:
322: // Finally, get the resource bundle based on locale
323: return ResourceBundle.getBundle("srapNetFileServletJava2",
324: locale);
325: }
326:
327: private boolean isSupportedGeckoVersion(String agent) {
328: int verIndex = agent.indexOf("rv:");
329:
330: if (verIndex > 0) {
331: String version = agent.substring(verIndex);
332: int closeIndex = version.indexOf(')');
333: if (closeIndex <= 0)
334: return false;
335:
336: version = version.substring(3, closeIndex);
337: // logger.info("Mozilla version is " + version);
338: Object[] params2 = { version };
339: logger.log(Level.INFO, "PSSRNF_CSPNSJ1081", params2);
340:
341: Integer major = new Integer(new String(new char[] { version
342: .charAt(0) }));
343: if (major.intValue() <= 0) {
344: } else if (major.intValue() == 1) {
345: if (version.length() >= 3) {
346: Integer minor = new Integer(new String(
347: new char[] { version.charAt(2) }));
348: if (minor.intValue() >= 2)
349: return true;
350: }
351: }
352: } else {
353: return false;
354: }
355: return false;
356: }
357:
358: }
|