KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > servlet > UploadServlet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.admingui.servlet;
25
26 import java.io.*;
27 import java.util.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import com.sun.enterprise.deployment.util.DeploymentProperties;
32 import com.sun.enterprise.tools.admingui.ConfigProperties;
33 import com.sun.enterprise.tools.admingui.handlers.DeploymentHandler;
34 import com.sun.enterprise.tools.admingui.handlers.WebServiceHandler;
35 import com.sun.enterprise.tools.admingui.util.AMXUtil;
36 import com.sun.enterprise.tools.admingui.util.Util;
37 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
38 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
39
40 import com.sun.enterprise.tools.admingui.util.FileUtil;
41
42 import com.sun.appserv.management.ext.wsmgmt.WebServiceMgr;
43 import com.sun.appserv.management.util.misc.ExceptionUtil;
44
45
46 /**
47  *
48  */

49 public class UploadServlet extends HttpServlet {
50     //Need to remove maxUploadSize after testing MultipartHandler properly.
51
//Right now leaving it here, but not using it anywhere.
52
private static final String JavaDoc CFG_MAX_UPLOAD_SIZE = "maxUploadSize";
53     private static final int MAX_UPLOAD_SIZE = 10 * 1024 * 1024; // 10 Meg
54
private static final String JavaDoc CURRENT = "current";
55     private static final String JavaDoc NEXT = "next";
56     int maxUploadSize = MAX_UPLOAD_SIZE;
57     ServletContext ctxt = null;
58     String JavaDoc mesg = null;
59     String JavaDoc bundle = "com.sun.enterprise.tools.admingui.resources.Resources";
60     
61     public void init(ServletConfig config) throws ServletException {
62         super.init(config);
63         ctxt = getServletContext();
64     }
65     
66     protected void doPost(HttpServletRequest req, HttpServletResponse res)
67     throws ServletException, IOException {
68         String JavaDoc uploadType = req.getParameter("uploadType");
69         String JavaDoc appType = req.getParameter("appType");
70         String JavaDoc which = req.getParameter("which");
71         String JavaDoc cancelPage = req.getParameter("cancelPage");
72         
73         res.setContentType("text/html; charset=UTF-8");
74         PrintWriter out = res.getWriter();
75         String JavaDoc contentType = req.getContentType();
76         String JavaDoc uploadDir = null;
77         String JavaDoc uploadDirErrMsg = "";
78         String JavaDoc dispatchRequest = null;
79         
80         
81         
82         if("mbean".equals(appType) && !(which.equals("client"))){
83             //if its create mbean from jar in server side, no need to upload, just extract and test implClass.
84
}else{
85             try {
86                 uploadDir = FileUtil.getUploadDir();
87             } catch(SecurityException JavaDoc se) {
88                 uploadDirErrMsg = Util.getMessage("upload.permission");
89                 se.printStackTrace();
90             } catch(Exception JavaDoc e) {
91                 if (uploadType==null) {
92                     uploadDirErrMsg = Util.getMessage("upload.UploadTypeNotSpecified");
93                 }
94                 e.printStackTrace();
95             }
96         }
97         
98         if (contentType.toLowerCase().startsWith("multipart/form-data")) {
99             if ((uploadDir == null) && Util.isLoggableCONFIG()) {
100                 Util.logCONFIG("UploadServlet.doPost: uploadDir is null. " + uploadDirErrMsg);
101         }
102             if (uploadDir == null) {
103                 if (Util.isLoggableWARNING()) {
104                     Util.logWARNING(Util.getMessage("upload.ErrorUploadingFile")
105                     + Util.getMessage("upload.nullDir")
106                     + Util.getMessage(uploadDirErrMsg));
107                 }
108                 req.setAttribute("ALERT_TYPE","error");
109                 req.setAttribute("ALERT_SUMMARY", "upload.nullDir");
110                 req.setAttribute("MBEAN_MESSAGE", uploadDirErrMsg);
111                 req.setAttribute("HAS_MBEAN_MESSAGE", "true");
112                 req.setAttribute("HAS_ERROR_MESSAGE", "true");
113                 if (cancelPage!=null && !cancelPage.equals("")){
114                     req.setAttribute("cancelPage", cancelPage);
115                 }
116                 dispatchRequest = getDispatchRequest(uploadType, appType, CURRENT);
117             } else {
118                 MultipartHandler multiReq = new MultipartHandler(req, uploadDir, maxUploadSize);
119                 multiReq.parseMultipartUpload();
120             
121                 Enumeration fileEnum = multiReq.getFileNames();
122                 String JavaDoc name = (String JavaDoc)fileEnum.nextElement();
123                 File appFile = multiReq.getFile(name);
124                 String JavaDoc appFileName = appFile.getAbsolutePath();
125                 long len = appFile.length();
126                 if (len > 0) {
127                     dispatchRequest = getDispatchRequest(uploadType, appType, NEXT);
128                     String JavaDoc filePath = appFile.getAbsolutePath();
129                     String JavaDoc fileName = getFileName(filePath);
130                     if (dispatchRequest != null) {
131                         if (uploadType.equals(FileUtil.DEPLOY)) {
132                             
133                             String JavaDoc listPage = multiReq.getParameter("upload.listPage");
134                             req.setAttribute("filePath", filePath);
135                             req.setAttribute("fileName", getFileName(filePath));
136                             req.setAttribute("uploadedFileName", appFile.getName());
137                             req.setAttribute("ctxtRoot", "/"+fileName);
138                             if (cancelPage!=null && !cancelPage.equals("")){
139                                 req.setAttribute("cancelPage", cancelPage);
140                             }
141                             if (listPage != null && !listPage.equals("")){
142                                 req.setAttribute("listPage", listPage);
143                             }
144                             if ("mbean".equals(appType)){
145                                 if(!testMbeanExist(req, filePath)){
146                                     dispatchRequest = getDispatchRequest(FileUtil.DEPLOY, appType, CURRENT);
147                                 }
148                             }
149                         } else if (uploadType.equals(FileUtil.REDEPLOY)) {
150                             if (!redeploy(req, true, filePath)) {
151                                 dispatchRequest = getDispatchRequest(FileUtil.REDEPLOY, appType, CURRENT);
152                             }
153                         } else if (uploadType.equals(FileUtil.TRANSFORMATION_RULE)) {
154                             if (!addTransformationRule(req, filePath)) {
155                                 dispatchRequest = getDispatchRequest(FileUtil.TRANSFORMATION_RULE, appType, CURRENT);
156                             }
157                         }
158                     }
159                 } else {
160                     req.setAttribute("AppType", appType);
161                     req.setAttribute("ALERT_TYPE","error");
162                     req.setAttribute("ALERT_SUMMARY", "upload.ErrorUploadingFile");
163                     req.setAttribute("MBEAN_MESSAGE", "upload.FileToUploadDoesNotExist");
164                     req.setAttribute("HAS_MBEAN_MESSAGE", "true");
165                     req.setAttribute("HAS_ERROR_MESSAGE", "true");
166                     if (cancelPage!=null && !cancelPage.equals("")){
167                         req.setAttribute("cancelPage", cancelPage);
168                     }
169                     dispatchRequest = getDispatchRequest(uploadType, appType, CURRENT);
170                 }
171             }
172         } else if (contentType.toLowerCase().startsWith("application/x-www-form-urlencoded")) {
173             dispatchRequest = getDispatchRequest(uploadType, appType, NEXT);
174             if (uploadType.equals(FileUtil.REDEPLOY)) {
175                 if (!redeploy(req, false, null)) {
176                     dispatchRequest = getDispatchRequest(FileUtil.REDEPLOY, appType, CURRENT);
177                 }
178             }else
179             if ("mbean".equals(appType)){
180                 if(!testMbeanExist(req, null)){
181                     dispatchRequest = getDispatchRequest(FileUtil.DEPLOY, appType, CURRENT);
182                 }
183             }
184         } else {
185             dispatchRequest = getDispatchRequest(uploadType, appType, NEXT);
186         }
187         
188         try {
189             req.getRequestDispatcher(dispatchRequest).forward(req, res);
190         }
191         catch(Exception JavaDoc e) {
192             ctxt.log(e.getMessage()+" "+e.getClass());
193         }
194     }
195     
196     private String JavaDoc getDispatchRequest(String JavaDoc uploadType, String JavaDoc appType, String JavaDoc page) {
197         String JavaDoc dispatchRequest = null;
198         if (page.equals(CURRENT)) {
199             if (uploadType.equals(FileUtil.DEPLOY)) {
200                 dispatchRequest = "../admingui/upload";
201             } else if (uploadType.equals(FileUtil.REDEPLOY)) {
202                 dispatchRequest = "../admingui/redeploy";
203             } else if (uploadType.equals(FileUtil.TRANSFORMATION_RULE)) {
204                 dispatchRequest = "../admingui/webServiceTransformationAdd";
205             }
206         } else if (page.equals(NEXT)) {
207             boolean isEAR = false;
208             boolean isWAR = false;
209             boolean isEJB = false;
210             boolean isRAR = false;
211             boolean isAC = false;
212             boolean isMBean = false;
213             if (appType != null) {
214                 if (appType.equals("ear")) {
215                     isEAR = true;
216                 } else if (appType.equals("war")) {
217                     isWAR = true;
218                 } else if (appType.equals("jar")) {
219                     isEJB = true;
220                 } else if (appType.equals("rar")) {
221                     isRAR = true;
222                 } else if (appType.equals("all")) {
223                     isAC = true;
224                 } else if (appType.equals("mbean")) {
225                     isMBean = true;
226                 }
227             }
228             if (uploadType.equals(FileUtil.DEPLOY)) {
229                 if(isWAR) {
230                     dispatchRequest = "../admingui/deployWebModule";
231                 } else if(isEAR) {
232                     dispatchRequest = "../admingui/deployJ2EEApplication";
233                 } else if(isEJB) {
234                     dispatchRequest = "../admingui/deployEJBJarModule";
235                 } else if(isRAR) {
236                     dispatchRequest = "../admingui/deployConnectorModule";
237                 } else if(isAC) {
238                     dispatchRequest = "../admingui/deployAppclientModule";
239                 } else if(isMBean) {
240                     dispatchRequest= "../admingui/createCustomMBean";
241                 }
242             } else if (uploadType.equals(FileUtil.REDEPLOY)) {
243                 Boolean JavaDoc isTargetSupported = ConfigProperties.getInstance().getTargetSupported();
244                 if(isWAR) {
245                     if (isTargetSupported.booleanValue()) {
246                         dispatchRequest = "../admingui/eeWebApplications";
247                     } else {
248                         dispatchRequest = "../admingui/webApplications";
249                     }
250                 } else if(isEAR) {
251                     if (isTargetSupported.booleanValue()) {
252                         dispatchRequest = "../admingui/eeEnterpriseApplications";
253                     } else {
254                         dispatchRequest = "../admingui/enterpriseApplications";
255                     }
256                 } else if(isEJB) {
257                     if (isTargetSupported.booleanValue()) {
258                         dispatchRequest = "../admingui/eeEjbModules";
259                     } else {
260                         dispatchRequest = "../admingui/ejbModules";
261                     }
262                 } else if(isRAR) {
263                     if (isTargetSupported.booleanValue()) {
264                         dispatchRequest = "../admingui/eeConnectorModules";
265                     } else {
266                         dispatchRequest = "../admingui/connectorModules";
267                     }
268                 } else if(isAC) {
269                     if (isTargetSupported.booleanValue()) {
270                         dispatchRequest = "../admingui/eeAppclientModules";
271                     } else {
272                         dispatchRequest = "../admingui/appclientModules";
273                     }
274                 }
275             } else if (uploadType.equals(FileUtil.TRANSFORMATION_RULE)) {
276                 dispatchRequest = "../admingui/webServiceTransformation";
277             }
278         }
279         return dispatchRequest;
280     }
281     
282     
283    
284                             
285                             
286     private boolean redeploy(HttpServletRequest req, boolean isUpload, String JavaDoc uploadFile) {
287         boolean isRedeploySuccessful = false;
288         String JavaDoc location = req.getParameter("AppFile");
289         String JavaDoc type = req.getParameter("appType");
290         String JavaDoc contextRoot = req.getParameter("contextRoot");
291         if (isUpload) {
292             location = uploadFile;
293         } else {
294             location = req.getParameter("DirectoryDeployField");
295         }
296         String JavaDoc n = req.getParameter("name");
297         Properties props = new Properties();
298         props.setProperty(DeploymentProperties.ARCHIVE_NAME, location);
299         props.setProperty(DeploymentProperties.NAME, n);
300         props.setProperty(DeploymentProperties.FORCE, "true");
301         props.setProperty(DeploymentProperties.CONTEXT_ROOT, contextRoot);
302             
303         String JavaDoc errMsg = null;
304          //our framework needs these attrs. to display the error mesg.
305
req.setAttribute("AppType", type);
306         req.setAttribute("HAS_MBEAN_MESSAGE", "true");
307         try {
308             DeploymentHandler.invokeDeploymentFacility(null, props, location);
309             isRedeploySuccessful = true;
310             req.setAttribute("ALERT_TYPE","info");
311             req.setAttribute("ALERT_SUMMARY", "redeploy.successful");
312             req.setAttribute("MBEAN_MESSAGE", "");
313             req.setAttribute("infoAlert", "true");
314         } catch (Exception JavaDoc e) {
315             isRedeploySuccessful = false;
316             errMsg = e.getMessage();
317             if (errMsg == null) {
318                 errMsg = Util.getMessage(bundle, "alert.SeeServerLog", null);
319             }
320             req.setAttribute("ALERT_TYPE","error");
321             req.setAttribute("ALERT_SUMMARY", "alert.Summary");
322             req.setAttribute("MBEAN_MESSAGE", errMsg);
323             req.setAttribute("HAS_ERROR_MESSAGE", "true");
324         }
325         return isRedeploySuccessful;
326     }
327     
328     private boolean addTransformationRule(HttpServletRequest req, String JavaDoc uploadDir) {
329         boolean isAddRuleSuccessful = false;
330         String JavaDoc clientLocation = req.getParameter("RuleFile");
331         File f = new File(clientLocation);
332         String JavaDoc errMsg = null;
333         String JavaDoc ruleName = req.getParameter("ruleName");
334         String JavaDoc webServiceKey = (String JavaDoc)req.getParameter("webServiceKey");
335         webServiceKey = webServiceKey.replaceAll("[@]","#");
336         String JavaDoc applyTo = req.getParameter("applyTo");
337         boolean enabled = Boolean.valueOf(req.getParameter("enabled")).booleanValue();
338         try {
339             WebServiceMgr wsm = (WebServiceMgr)AMXUtil.getDomainRoot().getWebServiceMgr();
340             WebServiceHandler.createTransformationRuleConfig(webServiceKey, ruleName, enabled, applyTo, uploadDir, null);
341             isAddRuleSuccessful = true;
342         } catch (Exception JavaDoc e) {
343             isAddRuleSuccessful = false;
344             Throwable JavaDoc t = ExceptionUtil.getRootCause(e);
345             if (t != null) {
346                 errMsg = t.getLocalizedMessage();
347         if (errMsg == null) {
348             errMsg = t.getMessage();
349         }
350             }
351             if (errMsg == null) {
352                 errMsg = Util.getMessage(bundle, "alert.SeeServerLog", null);
353             }
354             //our framework needs these attrs. to display the error mesg.
355
req.setAttribute("ALERT_TYPE","error");
356             req.setAttribute("ALERT_SUMMARY", "alert.Summary");
357             req.setAttribute("MBEAN_MESSAGE", errMsg);
358             req.setAttribute("HAS_MBEAN_MESSAGE", "true");
359             req.setAttribute("HAS_ERROR_MESSAGE", "true");
360         }
361         return isAddRuleSuccessful;
362     }
363     
364     //After uploading the jar that contains the mbean class to the server, we also need to
365
//extract the jar to applications/mbean directory.
366
private boolean testMbeanExist(HttpServletRequest req, String JavaDoc jarFileName){
367         
368         String JavaDoc errMsg = "";
369         try {
370             String JavaDoc which = req.getParameter("which");
371             String JavaDoc implClass = req.getParameter("implClass");
372             req.setAttribute("implClass", implClass);
373             String JavaDoc domainRoot = Util.getDomainRoot();
374             
375             if(which.equals("server")){
376                 jarFileName = req.getParameter("DirectoryDeployField");
377                 FileUtil.extractMbeanJarFile(domainRoot, jarFileName);
378                 req.setAttribute("uploadedFileName", jarFileName);
379                 req.setAttribute("implClass", implClass);
380                 String JavaDoc nm = new File(jarFileName).getName();
381                 int index = nm.indexOf(".");
382                 if (index > 0) nm = nm.substring(0, index);
383                 req.setAttribute("fileName", nm);
384                 String JavaDoc listPage = req.getParameter("upload.listPage");
385                 if (listPage != null && !listPage.equals("")){
386                         req.setAttribute("listPage", listPage);
387                 }
388             }else
389             if (which.equals("skip")){
390                 req.setAttribute("implClass", implClass);
391 // req.setAttribute("uploadedFileName", Util.getMessage("common.NA"));
392
req.setAttribute("uploadedFileName", Util.getMessage(bundle, "common.NA", null));
393                 String JavaDoc listPage = req.getParameter("upload.listPage");
394                 if (listPage != null && !listPage.equals("")){
395                         req.setAttribute("listPage", listPage);
396                 }
397             }else
398             if(which.equals("client")){
399                 FileUtil.extractMbeanJarFile(domainRoot, jarFileName);
400             }
401             
402             Object JavaDoc[] params = {implClass};
403             String JavaDoc[] types = {"java.lang.String"};
404             MBeanUtil.invoke("com.sun.appserv:type=applications,category=config", "getMBeanInfo", params, types);
405                    
406             return true;
407         }catch(IOException ioEx){
408             errMsg = ioEx.getMessage();
409             
410         }catch (Exception JavaDoc ex){
411             if (ex.getCause() == null){
412                 errMsg = ex.getLocalizedMessage();
413             }else {
414                 errMsg = ex.getCause().getLocalizedMessage();
415             }
416         }
417         req.setAttribute("AppType", "mbean");
418         //our framework needs these attrs. to display the error mesg.
419
req.setAttribute("ALERT_TYPE","error");
420         req.setAttribute("ALERT_SUMMARY", "alert.Summary");
421         req.setAttribute("MBEAN_MESSAGE", errMsg);
422         req.setAttribute("HAS_MBEAN_MESSAGE", "true");
423         req.setAttribute("HAS_ERROR_MESSAGE", "true");
424         return false;
425     }
426     
427     private String JavaDoc getFileName(String JavaDoc filePath) {
428         String JavaDoc name = new File(filePath).getName();
429         int index = name.indexOf(".");
430         if (index > 0)
431             return name.substring(0, index);
432         else
433             return filePath;
434     }
435     
436 }
437
Popular Tags