001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ApplyUploadAction.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.deploy;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.InputStream;
028:
029: import javax.management.ObjectName;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.apache.struts.action.ActionForm;
035: import org.apache.struts.action.ActionForward;
036: import org.apache.struts.action.ActionMapping;
037: import org.apache.struts.upload.FormFile;
038:
039: import org.objectweb.jonas.jmx.J2eeObjectName;
040: import org.objectweb.jonas.jmx.JonasManagementRepr;
041:
042: /**
043: * Upload a file to the J2EE server.
044: * @author Florent Benoit
045: */
046: public class ApplyUploadAction extends BaseDeployAction {
047:
048: /**
049: * Size of Buffer
050: */
051: private static final int BUFFER_SIZE = 1024;
052:
053: /**
054: * Execute the action with given params
055: * @param actionMapping The ActionMapping used to select this instance
056: * @param actionForm The optional ActionForm bean for this request (if any)
057: * @param request The HTTP request we are processing
058: * @param response The HTTP response we are creating
059: * @return a forward when action is finished
060: * @exception ServletException if business logic throws an exception
061: */
062: public ActionForward executeAction(ActionMapping actionMapping,
063: ActionForm actionForm, HttpServletRequest request,
064: HttpServletResponse response) throws ServletException {
065: String sForward = "Upload Result";
066:
067: if (!(actionForm instanceof UploadForm)) {
068: Throwable t = new Throwable(
069: "Action not instance of UploadForm");
070: addGlobalError(t);
071: saveErrors(request, m_Errors);
072: return (actionMapping.findForward("Global Error"));
073: }
074:
075: // this line is here for when the input page is upload-utf8.jsp,
076: // it sets the correct character encoding for the response
077: String encoding = request.getCharacterEncoding();
078: if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
079: response.setContentType("text/html; charset=utf-8");
080: }
081:
082: UploadForm uploadForm = (UploadForm) actionForm;
083:
084: // indicated whether to return to regular or domain deployment page
085: uploadForm.setIsDomain(isDomain());
086:
087: // retrieve the file representation
088: FormFile file = uploadForm.getUploadedFile();
089:
090: // retrieve the file name
091: String fileName = file.getFileName();
092:
093: // retrieve the content type
094: String contentType = file.getContentType();
095:
096: // retrieve the file size
097: String size = (file.getFileSize() + " bytes");
098:
099: String serverName = m_WhereAreYou.getCurrentJonasServerName();
100:
101: // J2EE server MBean
102: ObjectName j2eeServer = J2eeObjectName.J2EEServer(m_WhereAreYou
103: .getCurrentDomainName(), serverName);
104:
105: // invoke method on the MBean
106: String sentFile = null;
107: InputStream inputStream = null;
108: ByteArrayOutputStream baos = null;
109: int len;
110: try {
111: inputStream = file.getInputStream();
112: baos = new ByteArrayOutputStream();
113: byte[] buf = new byte[BUFFER_SIZE];
114: // Read bytes
115: while ((len = inputStream.read(buf)) > 0) {
116: baos.write(buf, 0, len);
117: }
118: byte[] bytesOfFile = baos.toByteArray();
119: Object[] param = new Object[] { bytesOfFile, fileName,
120: Boolean.valueOf(uploadForm.isOverwrite()) };
121: String[] signature = { "[B", "java.lang.String", "boolean" };
122: sentFile = (String) JonasManagementRepr.invoke(j2eeServer,
123: "sendFile", param, signature, serverName);
124: } catch (Exception e) {
125: addGlobalError(e);
126: saveErrors(request, m_Errors);
127: return (actionMapping.findForward("Upload"));
128: } finally {
129: try {
130: if (inputStream != null) {
131: inputStream.close();
132: }
133: if (baos != null) {
134: baos.close();
135: }
136: } catch (Exception e) {
137: getServlet().log("Cannot close streams", e);
138: }
139:
140: }
141:
142: // place the data into the request for retrieval for display
143: request.setAttribute("fileName", fileName);
144: request.setAttribute("contentType", contentType);
145: request.setAttribute("size", size);
146: request.setAttribute("data", "File uploaded in '" + sentFile
147: + "'");
148: // destroy the temporary file created
149: file.destroy();
150:
151: // Forward to the jsp.
152: return (actionMapping.findForward(sForward));
153: }
154: }
|