KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > actions > UploadAction


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.vfs.actions;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessages;
32
33 import com.sslexplorer.core.BundleActionMessage;
34 import com.sslexplorer.core.CoreException;
35 import com.sslexplorer.core.CoreUtil;
36 import com.sslexplorer.core.RedirectWithMessages;
37 import com.sslexplorer.core.UploadHandler;
38 import com.sslexplorer.core.actions.AuthenticatedAction;
39 import com.sslexplorer.security.Constants;
40 import com.sslexplorer.security.LogonControllerFactory;
41 import com.sslexplorer.security.SessionInfo;
42 import com.sslexplorer.vfs.UploadDetails;
43 import com.sslexplorer.vfs.forms.UploadForm;
44 import com.sslexplorer.vfs.utils.UploadHandlerFactory;
45
46 /**
47  * Implementation of {@link AuthenticatedAction} that accepts an HTTP upload
48  * and stores it somewhere.
49  * <p>
50  * The upload itself is delegated to an {@link UploadHandler}. This is created by
51  * the {@link UploadHandlerFactory} using the type attribute provided by the
52  * current {@link UploadDetails}.
53  * <p>
54  * The upload details themselves are retrieved using the <i>uploadId</i>
55  * attribute of the associated form. This should have been set by the previous
56  * action {@link ShowUploadAction}.
57  *
58  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
59  */

60 public class UploadAction extends AuthenticatedAction {
61
62     static Log log = LogFactory.getLog(UploadAction.class);
63
64     /* (non-Javadoc)
65      * @see com.sslexplorer.core.actions.AuthenticatedAction#onExecute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
66      */

67     public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68                     throws Exception JavaDoc {
69         if (log.isDebugEnabled())
70             log.debug("Execute file upload.");
71         UploadForm uploadForm = (UploadForm) form;
72         int uploadId = uploadForm.getUploadId();
73         UploadDetails upload = CoreUtil.getUpload(request.getSession(), uploadId);
74         if (upload == null) {
75             throw new Exception JavaDoc("No file upload details configured for upload id " + uploadId + ".");
76         }
77         int sessionTimeoutBlockId = LogonControllerFactory.getInstance().addSessionTimeoutBlock(request.getSession(),
78             "Upload request.");
79         try {
80             UploadHandler handler = UploadHandlerFactory.getInstance().getUploader(upload.getType());
81             if (handler == null) {
82                 throw new Exception JavaDoc("No handler for upload type " + upload.getType());
83             }
84             request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer JavaDoc(uploadId));
85             
86             boolean isFileExist = handler.checkFileToUpload(request, response, upload, uploadForm.getUploadFile());
87             if (isFileExist) {
88                 request.getSession().setAttribute("uploadForm", uploadForm);
89                 return new ActionForward("/confirmFileUpload.do?arg0=" + uploadForm.getUploadFile().getFileName());
90             } else {
91                 return confirmUpload(mapping, form, request, response);
92             }
93
94         } catch (Exception JavaDoc e) {
95             log.error("Failed to upload.", e);
96             ActionMessages msgs = new ActionMessages();
97             msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("vfs", "upload.info.fileNotUploaded", e.getMessage()));
98             saveErrors(request, msgs);
99             if (null != upload.getUploadedForward())
100                 return upload.getUploadedForward();
101             else
102                 throw e;
103         } finally {
104             LogonControllerFactory.getInstance().removeSessionTimeoutBlock(request.getSession(), sessionTimeoutBlockId);
105         }
106     }
107     
108     public ActionForward confirmUpload(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
109         if (log.isDebugEnabled())
110             log.debug("Execute file upload.");
111         UploadForm uploadForm = (UploadForm) form;
112         int uploadId = uploadForm.getUploadId();
113         UploadDetails upload = CoreUtil.getUpload(request.getSession(), uploadId);
114         if (upload == null) {
115             throw new Exception JavaDoc("No file upload details configured for upload id " + uploadId + ".");
116         }
117         int sessionTimeoutBlockId = LogonControllerFactory.getInstance().addSessionTimeoutBlock(request.getSession(),
118             "Upload request.");
119         
120         try {
121             UploadHandler handler = UploadHandlerFactory.getInstance().getUploader(upload.getType());
122             if (handler == null) {
123                 throw new Exception JavaDoc("No handler for upload type " + upload.getType());
124             }
125             request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer JavaDoc(uploadId));
126
127             ActionForward fwd = handler.performUpload(request, response, upload, uploadForm.getUploadFile());
128             ActionMessages msgs = new ActionMessages();
129             if(upload.getResourcePath()==null) {
130                 msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("vfs", "upload.info.fileUploadedNoPath", uploadForm.getUploadFile()
131                         .getFileName()));
132             } else {
133                 msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("vfs", "upload.info.fileUploaded", uploadForm.getUploadFile()
134                             .getFileName(), upload.getResourcePath()));
135             }
136             saveMessages(request, msgs);
137             request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, uploadId);
138             uploadForm.setUploadId(uploadId);
139             return fwd;
140         }
141         catch (CoreException ce) {
142             log.error("Failed to upload.", ce);
143             ActionMessages errs = getErrors(request);
144             errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
145             saveErrors(request, errs);
146             if (null != upload.getUploadedForward())
147                 return new RedirectWithMessages(upload.getUploadedForward(), request);
148             else
149                 throw ce;
150         }
151         catch (Exception JavaDoc e) {
152             log.error("Failed to upload.", e);
153             ActionMessages msgs = new ActionMessages();
154             msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("vfs", "upload.info.fileNotUploaded", e.getMessage()));
155             saveErrors(request, msgs);
156             if (null != upload.getUploadedForward())
157                 return upload.getUploadedForward();
158             else
159                 throw e;
160         } finally {
161             LogonControllerFactory.getInstance().removeSessionTimeoutBlock(request.getSession(), sessionTimeoutBlockId);
162         }
163         
164     }
165
166     /* (non-Javadoc)
167      * @see com.sslexplorer.core.actions.AuthenticatedAction#getNavigationContext(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
168      */

169     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
170         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT;
171     }
172 }
Popular Tags