KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > deploy > ApplyUploadAction


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ApplyUploadAction.java,v 1.5 2005/07/25 21:03:54 pasmith Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.deploy;
27
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
45  * Upload a file to the J2EE server.
46  * @author Florent Benoit
47  */

48 public class ApplyUploadAction extends BaseDeployAction {
49
50     /**
51      * Size of Buffer
52      */

53     private static final int BUFFER_SIZE = 1024;
54
55     /**
56      * Execute the action with given params
57      * @param actionMapping The ActionMapping used to select this instance
58      * @param actionForm The optional ActionForm bean for this request (if any)
59      * @param request The HTTP request we are processing
60      * @param response The HTTP response we are creating
61      * @return a forward when action is finished
62      * @exception ServletException if business logic throws an exception
63      */

64     public ActionForward executeAction(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request,
65             HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
66         String JavaDoc sForward = "Upload Result";
67
68         if (!(actionForm instanceof UploadForm)) {
69             Throwable JavaDoc t = new Throwable JavaDoc("Action not instance of UploadForm");
70             addGlobalError(t);
71             saveErrors(request, m_Errors);
72             return (actionMapping.findForward("Global Error"));
73         }
74
75         // this line is here for when the input page is upload-utf8.jsp,
76
// it sets the correct character encoding for the response
77
String JavaDoc 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         // indicated whether to return to regular or domain deployment page
85
uploadForm.setIsDomain(isDomain());
86
87         // retrieve the file representation
88
FormFile file = uploadForm.getUploadedFile();
89
90         // retrieve the file name
91
String JavaDoc fileName = file.getFileName();
92
93         // retrieve the content type
94
String JavaDoc contentType = file.getContentType();
95
96         // retrieve the file size
97
String JavaDoc size = (file.getFileSize() + " bytes");
98
99         // J2EE server MBean
100
ObjectName JavaDoc j2eeServer = J2eeObjectName.J2EEServer(m_WhereAreYou.getCurrentDomainName(), m_WhereAreYou
101                 .getCurrentJonasServerName());
102
103         // invoke method on the MBean
104
String JavaDoc sentFile = null;
105         InputStream JavaDoc inputStream = null;
106         ByteArrayOutputStream JavaDoc baos = null;
107         int len;
108         try {
109             inputStream = file.getInputStream();
110             baos = new ByteArrayOutputStream JavaDoc();
111             byte[] buf = new byte[BUFFER_SIZE];
112             // Read bytes
113
while ((len = inputStream.read(buf)) > 0) {
114                 baos.write(buf, 0, len);
115             }
116             byte[] bytesOfFile = baos.toByteArray();
117             Object JavaDoc[] param = new Object JavaDoc[] {bytesOfFile, fileName, Boolean.valueOf(uploadForm.isOverwrite())};
118             String JavaDoc[] signature = {"[B", "java.lang.String", "boolean"};
119             sentFile = (String JavaDoc) JonasManagementRepr.invoke(j2eeServer, "sendFile", param, signature);
120         } catch (Exception JavaDoc 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 JavaDoc e) {
133                 getServlet().log("Cannot close streams", e);
134             }
135
136         }
137
138         // place the data into the request for retrieval for display
139
request.setAttribute("fileName", fileName);
140         request.setAttribute("contentType", contentType);
141         request.setAttribute("size", size);
142         request.setAttribute("data", "File uploaded in '" + sentFile + "'");
143         // destroy the temporary file created
144
file.destroy();
145
146         // Forward to the jsp.
147
return (actionMapping.findForward(sForward));
148     }
149 }
150
Popular Tags