1 25 26 package org.objectweb.jonas.webapp.jonasadmin.deploy; 27 28 import java.io.ByteArrayOutputStream ; 29 import java.io.InputStream ; 30 31 import javax.management.ObjectName ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.struts.action.ActionForm; 37 import org.apache.struts.action.ActionForward; 38 import org.apache.struts.action.ActionMapping; 39 import org.apache.struts.upload.FormFile; 40 41 import org.objectweb.jonas.jmx.J2eeObjectName; 42 import org.objectweb.jonas.jmx.JonasManagementRepr; 43 44 48 public class ApplyUploadAction extends BaseDeployAction { 49 50 53 private static final int BUFFER_SIZE = 1024; 54 55 64 public ActionForward executeAction(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, 65 HttpServletResponse response) throws ServletException { 66 String sForward = "Upload Result"; 67 68 if (!(actionForm instanceof UploadForm)) { 69 Throwable t = new Throwable ("Action not instance of UploadForm"); 70 addGlobalError(t); 71 saveErrors(request, m_Errors); 72 return (actionMapping.findForward("Global Error")); 73 } 74 75 String encoding = request.getCharacterEncoding(); 78 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) { 79 response.setContentType("text/html; charset=utf-8"); 80 } 81 82 UploadForm uploadForm = (UploadForm) actionForm; 83 84 uploadForm.setIsDomain(isDomain()); 86 87 FormFile file = uploadForm.getUploadedFile(); 89 90 String fileName = file.getFileName(); 92 93 String contentType = file.getContentType(); 95 96 String size = (file.getFileSize() + " bytes"); 98 99 ObjectName j2eeServer = J2eeObjectName.J2EEServer(m_WhereAreYou.getCurrentDomainName(), m_WhereAreYou 101 .getCurrentJonasServerName()); 102 103 String sentFile = null; 105 InputStream inputStream = null; 106 ByteArrayOutputStream baos = null; 107 int len; 108 try { 109 inputStream = file.getInputStream(); 110 baos = new ByteArrayOutputStream (); 111 byte[] buf = new byte[BUFFER_SIZE]; 112 while ((len = inputStream.read(buf)) > 0) { 114 baos.write(buf, 0, len); 115 } 116 byte[] bytesOfFile = baos.toByteArray(); 117 Object [] param = new Object [] {bytesOfFile, fileName, Boolean.valueOf(uploadForm.isOverwrite())}; 118 String [] signature = {"[B", "java.lang.String", "boolean"}; 119 sentFile = (String ) JonasManagementRepr.invoke(j2eeServer, "sendFile", param, signature); 120 } catch (Exception e) { 121 addGlobalError(e); 122 saveErrors(request, m_Errors); 123 return (actionMapping.findForward("Upload")); 124 } finally { 125 try { 126 if (inputStream != null) { 127 inputStream.close(); 128 } 129 if (baos != null) { 130 baos.close(); 131 } 132 } catch (Exception e) { 133 getServlet().log("Cannot close streams", e); 134 } 135 136 } 137 138 request.setAttribute("fileName", fileName); 140 request.setAttribute("contentType", contentType); 141 request.setAttribute("size", size); 142 request.setAttribute("data", "File uploaded in '" + sentFile + "'"); 143 file.destroy(); 145 146 return (actionMapping.findForward(sForward)); 148 } 149 } 150 | Popular Tags |