KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > NetworkPlaceUploadHandler


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.networkplaces;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.upload.FormFile;
31
32 import com.sslexplorer.core.CoreServlet;
33 import com.sslexplorer.core.UploadHandler;
34 import com.sslexplorer.policyframework.LaunchSession;
35 import com.sslexplorer.policyframework.LaunchSessionFactory;
36 import com.sslexplorer.security.LogonControllerFactory;
37 import com.sslexplorer.security.SessionInfo;
38 import com.sslexplorer.vfs.UploadDetails;
39 import com.sslexplorer.vfs.VFSResource;
40 import com.sslexplorer.vfs.webdav.DAVServlet;
41
42
43 /**
44  * Upload handler that uploads to a <i>VFS</i> resource.
45  *
46  * @author James Robinson <a HREF="mailto: james@3sp.com">&lt;james@3sp.com&gt;</a>
47  */

48 public class NetworkPlaceUploadHandler implements UploadHandler {
49     
50     public static final String JavaDoc TYPE_VFS = "VFS";
51
52     public ActionForward performUpload(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, UploadDetails upload,
53                                        FormFile uploadFile) throws Exception JavaDoc {
54
55         /*
56          * If an upload was attempted after session timeout, the login screen
57          * would have interrupted the upload. In this case just go straight back
58          * to the home page.
59          */

60         if (upload.getResourcePath() == null || upload.getResourcePath().length() == 0) {
61             return upload.getUploadedForward();
62         }
63
64         if (uploadFile == null || uploadFile.getFileName() == null || uploadFile.getFileName().trim().equals("")) {
65             return upload.getUploadedForward();
66         }
67
68         LaunchSession launchSession = null;
69         VFSResource res = null;
70         try {
71             
72             // Get the launch session
73
SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request);
74             launchSession = session == null ? null : LaunchSessionFactory.getInstance().getLaunchSession(session, upload.getExtraAttribute2());
75             if(launchSession == null) {
76                 throw new Exception JavaDoc("No launch session.");
77             }
78             launchSession.checkAccessRights(null, session);
79
80             res = DAVServlet.getDAVResource(launchSession, request, response, upload.getResourcePath() + "/" + uploadFile.getFileName());
81             
82             res.getFile().exists();
83             InputStream JavaDoc in = uploadFile.getInputStream();
84             OutputStream JavaDoc out = res.getOutputStream();
85             try {
86                 byte[] buf = new byte[4096];
87                 int read;
88                 while (true) {
89                     read = in.read(buf, 0, buf.length);
90                     if (read == -1) {
91                         break;
92                     }
93                     out.write(buf, 0, read);
94                 }
95             } finally {
96                 in.close();
97                 out.close();
98             }
99             if (res.getMount().getStore().getProvider().isFireEvents()) {
100                 CoreServlet.getServlet().fireCoreEvent(
101                     NetworkPlaceResourceType.getResourceAccessUploadEvent(this, launchSession, request, res.getFullPath(), res.getFile().getName().getURI(), uploadFile
102                                     .getFileName(), null));
103             }
104             return upload.getUploadedForward();
105         } catch (Exception JavaDoc e) {
106             if (res != null && res.getMount().getStore().getProvider().isFireEvents()) {
107                 CoreServlet.getServlet().fireCoreEvent(
108                     NetworkPlaceResourceType.getResourceAccessUploadEvent(this, launchSession, request, res.getFullPath(), res.getFile().getName().getURI(), uploadFile
109                                     .getFileName(), e));
110             }
111             /*
112              * Close the stream so the client gets an error straight away rather
113              * than having to wait for the file to upload
114              */

115             try {
116                 request.getInputStream().close();
117             } catch (IOException JavaDoc ioe) {
118                 throw ioe;
119             }
120             throw e;
121         }
122     }
123
124     public boolean checkFileToUpload(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, UploadDetails fileUpload, FormFile file) throws IOException JavaDoc, Exception JavaDoc {
125
126         if (fileUpload.getResourcePath() == null || fileUpload.getResourcePath().length() == 0) {
127             return false;
128         }
129
130         if (file==null || file.getFileName() == null || file.getFileName().trim().equals("")) {
131             return false;
132         }
133         
134         // Get the launch session
135
SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request);
136         LaunchSession launchSession = session == null ? null : LaunchSessionFactory.getInstance().getLaunchSession(session, fileUpload.getExtraAttribute2());
137         if(launchSession == null) {
138             throw new Exception JavaDoc("No launch session.");
139         }
140
141         VFSResource res = DAVServlet.getDAVResource(launchSession, request, response, fileUpload.getResourcePath() + "/" + file.getFileName());
142
143         return res.getFile().exists();
144     }
145     
146 }
147
Popular Tags