1 19 20 package com.sslexplorer.vfs.actions; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 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 60 public class UploadAction extends AuthenticatedAction { 61 62 static Log log = LogFactory.getLog(UploadAction.class); 63 64 67 public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 68 throws Exception { 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 ("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 ("No handler for upload type " + upload.getType()); 83 } 84 request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer (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 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 request, HttpServletResponse response) throws Exception { 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 ("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 ("No handler for upload type " + upload.getType()); 124 } 125 request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer (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 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 169 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 170 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT; 171 } 172 } | Popular Tags |