001: /**
002: * $Id: NetFileServlet.java,v 1.24 2006/10/03 14:03:13 vk162573 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 java.io.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.util.Vector;
018: import java.util.Hashtable;
019: import java.util.Locale;
020: import java.util.ResourceBundle;
021: import java.util.logging.*;
022: import java.util.Enumeration;
023:
024: import java.net.*;
025:
026: import javax.servlet.*;
027: import javax.servlet.http.*;
028:
029: import com.sun.portal.netfile.shared.NetFileException;
030: import com.sun.portal.netfile.servlet.java2.RequestProcessor;
031:
032: import com.iplanet.sso.SSOToken;
033: import com.iplanet.sso.SSOTokenListener;
034: import com.iplanet.sso.SSOTokenEvent;
035:
036: /**
037: * @author Suresh Yellamaraju
038: * @version 1.0
039: */
040: public class NetFileServlet extends HttpServlet {
041:
042: private static Vector interface_ip = null;
043: private static final String LOCAL_HOST_IP = "127.0.0.1";
044: private static final String DOWN_INTERFACE_IP = "0.0.0.0";
045: private static Hashtable interface_server_cache = new java.util.Hashtable();
046:
047: public static Hashtable logManagerCache = new Hashtable();
048: public static Hashtable uploadProcCache = new Hashtable();
049:
050: public static Hashtable tempDirCache = null;
051: public static NetFileSessionClean nfSessClean = null;
052: private static Logger logger = PortalLogger
053: .getLogger(NetFileServlet.class);
054:
055: public void init(ServletConfig config) throws ServletException {
056: super .init(config);
057:
058: tempDirCache = new Hashtable();
059: runIfconfigA();
060:
061: nfSessClean = new NetFileServlet.NetFileSessionClean();
062: }
063:
064: /**
065: * To be invoked by NetFile classes to get the list of interface IPs.
066: */
067: public static Vector getInterfaceIPs() {
068: return interface_ip;
069: }
070:
071: /**
072: * Returns the interface on which the given server can be contacted
073: * If no match found, returns null.
074: * The calling method should cast the returned value to String after
075: * checking for null.
076: */
077: public static Object getInterface(String serverName) {
078: if (serverName == null)
079: return null;
080: return interface_server_cache.get(serverName);
081: }
082:
083: /**
084: * Sets the servername as key and interface as value
085: * in the interface_server_cache
086: */
087: public static void setInterface(String nameOfServer,
088: String nameOfInterface) {
089: interface_server_cache.put(nameOfServer, nameOfInterface);
090: }
091:
092: /**
093: * Must be called from init() ONLY.
094: * Handles the data in thread-unsafe fashion.
095: */
096: private static final void runIfconfigA() {
097:
098: try {
099:
100: Enumeration interEnum = NetworkInterface
101: .getNetworkInterfaces();
102: Enumeration ipaddEnum;
103: NetworkInterface netInter;
104: String ip;
105: interface_ip = new Vector();
106:
107: for (; interEnum.hasMoreElements();) {
108: netInter = (NetworkInterface) interEnum.nextElement();
109: ipaddEnum = netInter.getInetAddresses();
110: for (; ipaddEnum.hasMoreElements();) {
111: ip = ((InetAddress) ipaddEnum.nextElement())
112: .getHostAddress();
113: interface_ip.add(ip);
114: }
115: }
116:
117: } catch (Exception ioe) {
118: ioe.printStackTrace();
119: }
120: }
121:
122: public void doGet(HttpServletRequest request,
123: HttpServletResponse response) throws ServletException,
124: java.io.IOException {
125: writeDebug("\n Reached doGet() \n");
126: handleRequest(request, response);
127:
128: }
129:
130: public void doPost(HttpServletRequest request,
131: HttpServletResponse response) throws ServletException,
132: java.io.IOException {
133: writeDebug("\n Reached doPost() \n");
134: handleRequest(request, response);
135: }
136:
137: public void destroy() {
138: logManagerCache = null;
139: interface_server_cache = null;
140: }
141:
142: void handleRequest(HttpServletRequest req, HttpServletResponse res)
143: throws ServletException, IOException {
144:
145: RequestProcessor reqProc = null;
146:
147: try {
148: writeDebug("Reached Handle Request \n");
149: try {
150: // Obtain the stream
151: reqProc = fetchProtocolHandler(req);
152: if (reqProc == null) {
153: //TBD: get the string from resource bundle. Here just pass key.
154: //writeErrorResponse(req, res, "nfs.2");
155: writeErrorResponse(req, res, "Invalid Request.");
156: return;
157: }
158:
159: } catch (Exception e) {
160: writeDebug(
161: "NetFileServlet::Exception in request processor",
162: e);
163: throw new NetFileException(
164: NetFileException.NETFILE_NULLVALUE, e
165: .getMessage());
166: }
167:
168: reqProc.processRequest(req, res, null);
169:
170: return;
171:
172: } catch (NetFileException nfe) {
173: writeErrorDebug(
174: "NetFileException in handleRequest of NetFileServlet",
175: nfe);
176: if (reqProc != null) {
177: reqProc = null;
178: }
179: } catch (Exception e) {
180: writeErrorDebug(
181: "Exception in handleRequest of NetFileServlet", e);
182: if (reqProc != null) {
183: reqProc = null;
184: }
185: }
186: }
187:
188: RequestProcessor fetchProtocolHandler(HttpServletRequest request)
189: throws NetFileException {
190:
191: String version;
192: try {
193: version = request.getHeader("NetFileProtocolVersion");
194: writeDebug("\n Protocol Version is:" + version);
195: if (version == null) {
196: /*
197: * Get the query string and create appropriate request processor
198: * based on the query parameters.
199: *
200: * In case of upload, only plain streams are used.
201: */
202: String streamType[];
203: String action[];
204: String queryString = request.getQueryString();
205: Hashtable queryData = HttpUtils
206: .parseQueryString(queryString);
207: streamType = (String[]) queryData.get("stream");
208: action = (String[]) queryData.get("action");
209: String id[] = (String[]) queryData.get("id");
210:
211: if (action[0].equals("upload")) {
212: if (streamType[0].equals("plain")) {
213: Object object = NetFileServlet.uploadProcCache
214: .get(id[0]);
215: if (object == null) {
216: object = new FileUploadProcessor(
217: "HTTP_JAVA2_PLAIN" + id[0]);
218: NetFileServlet.uploadProcCache.put(id[0],
219: object);
220: }
221: return (RequestProcessor) object;
222: }
223: } else if (action[0].equals("open")) {
224: if (streamType[0].equals("object")) {
225: return new FileOpenProcessor(
226: "HTTP_JAVA2_OBJECT");
227: }
228: }
229: }
230:
231: if (version.endsWith("HTTP_JAVA_SERIALIZE")) {
232: return new SerializedRequestProcessor(version);
233: }
234:
235: } catch (Exception e) {
236: writeErrorDebug("NetFileServlet: Invalid request", e);
237: throw new NetFileException(
238: NetFileException.NETFILE_NULLVALUE,
239: "Error in getting version information."
240: + e.getMessage());
241:
242: }
243: return null;
244: }
245:
246: private void writeErrorResponse(HttpServletRequest request,
247: HttpServletResponse response, String errMsg)
248: throws IOException {
249: Locale defaultLocale = Locale.getDefault();
250: ResourceBundle userResources = ResourceBundle.getBundle(
251: "srapNetFileServletJava2", defaultLocale);
252:
253: //TBD: Fetch the keys from resource bundle.
254: //userResources.getString(errMsg);
255: }
256:
257: protected void writeDebug(String szMsg) {
258: writeDebug(szMsg, null);
259: }
260:
261: protected void writeDebug(String szMsg, Exception e) {
262: if (e != null) {
263: // logger.log(Level.INFO, szMsg, e);
264: logger.log(Level.INFO, "PSSRNF_CSPNSJ2097");
265: } else {
266: // logger.info(szMsg);
267: logger.info("PSSRNF_CSPNSJ2098");
268: }
269: }
270:
271: protected void writeErrorDebug(String szError, Exception e) {
272: if (e != null)
273: // logger.log(Level.SEVERE, szError, e);
274: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2099");
275: else
276: // logger.severe(szError);
277: logger.severe("PSSRNF_CSPNSJ2100");
278: }
279:
280: public static void putTempDirCache(String ssoToken, String szTempDir) {
281: NetFileServlet.tempDirCache.put(ssoToken, szTempDir);
282: }
283:
284: public static NetFileSessionClean getNetFileSessionCleaner() {
285: return nfSessClean;
286: }
287:
288: class NetFileSessionClean implements SSOTokenListener {
289: private boolean doCleanUp(java.lang.String szLocalFileName) {
290: if (szLocalFileName == null)
291: return false;
292: try {
293: File localFile = new File(szLocalFileName);
294: File tempFile = null;
295: if (localFile.exists()) {
296: File[] filesList = localFile.listFiles();
297: for (int i = 0; i < filesList.length; i++) {
298: //tempFile = new File(filesList[i]);
299: tempFile = filesList[i];
300: if (tempFile.exists())
301: tempFile.delete();
302: tempFile = null;
303: }
304: localFile.delete();
305: }
306: return true;
307: } catch (Exception e) {
308: // logger.log(Level.INFO, "Exception in deleting temp file", e);
309: logger.log(Level.INFO, "PSSRNF_CSPNSJ2101");
310: }
311: return false;
312: }
313:
314: public void ssoTokenChanged(SSOTokenEvent event) {
315: String szTempDir = "";
316: try {
317: SSOToken token = event.getToken();
318: String tokenId = token.getTokenID().toString();
319: int type = event.getType();
320:
321: switch (type) {
322: case SSOTokenEvent.SSO_TOKEN_IDLE_TIMEOUT:
323: case SSOTokenEvent.SSO_TOKEN_MAX_TIMEOUT:
324: case SSOTokenEvent.SSO_TOKEN_DESTROY:
325: // remove the logManager from the cache for this user.
326: try {
327: logManagerCache.remove(tokenId);
328: } catch (Exception e) {
329: }
330:
331: szTempDir = NetFileServlet.tempDirCache
332: .get(tokenId).toString();
333: if ((szTempDir != null)
334: || (szTempDir.trim().length() == 0)
335: || (!szTempDir.equals("null"))) {
336: doCleanUp(szTempDir);
337: // logger.info("Deleting temporary directory location " + szTempDir + " for " + tokenId);
338: Object[] params5 = { szTempDir, " for ",
339: tokenId };
340: logger.log(Level.INFO, "PSSRNF_CSPNSJ2102",
341: params5);
342: }
343: break;
344: }
345: } catch (Exception e) {
346: // logger.log(Level.INFO, "Exception in deleting temporary directory ", e);
347: logger.log(Level.INFO, "PSSRNF_CSPNSJ2103");
348: }
349: }
350:
351: }
352:
353: }
|