1 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 49 public class UploadServlet extends HttpServlet { 50 private static final String CFG_MAX_UPLOAD_SIZE = "maxUploadSize"; 53 private static final int MAX_UPLOAD_SIZE = 10 * 1024 * 1024; private static final String CURRENT = "current"; 55 private static final String NEXT = "next"; 56 int maxUploadSize = MAX_UPLOAD_SIZE; 57 ServletContext ctxt = null; 58 String mesg = null; 59 String 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 uploadType = req.getParameter("uploadType"); 69 String appType = req.getParameter("appType"); 70 String which = req.getParameter("which"); 71 String cancelPage = req.getParameter("cancelPage"); 72 73 res.setContentType("text/html; charset=UTF-8"); 74 PrintWriter out = res.getWriter(); 75 String contentType = req.getContentType(); 76 String uploadDir = null; 77 String uploadDirErrMsg = ""; 78 String dispatchRequest = null; 79 80 81 82 if("mbean".equals(appType) && !(which.equals("client"))){ 83 }else{ 85 try { 86 uploadDir = FileUtil.getUploadDir(); 87 } catch(SecurityException se) { 88 uploadDirErrMsg = Util.getMessage("upload.permission"); 89 se.printStackTrace(); 90 } catch(Exception 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 name = (String )fileEnum.nextElement(); 123 File appFile = multiReq.getFile(name); 124 String appFileName = appFile.getAbsolutePath(); 125 long len = appFile.length(); 126 if (len > 0) { 127 dispatchRequest = getDispatchRequest(uploadType, appType, NEXT); 128 String filePath = appFile.getAbsolutePath(); 129 String fileName = getFileName(filePath); 130 if (dispatchRequest != null) { 131 if (uploadType.equals(FileUtil.DEPLOY)) { 132 133 String 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 e) { 192 ctxt.log(e.getMessage()+" "+e.getClass()); 193 } 194 } 195 196 private String getDispatchRequest(String uploadType, String appType, String page) { 197 String 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 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 uploadFile) { 287 boolean isRedeploySuccessful = false; 288 String location = req.getParameter("AppFile"); 289 String type = req.getParameter("appType"); 290 String contextRoot = req.getParameter("contextRoot"); 291 if (isUpload) { 292 location = uploadFile; 293 } else { 294 location = req.getParameter("DirectoryDeployField"); 295 } 296 String 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 errMsg = null; 304 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 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 uploadDir) { 329 boolean isAddRuleSuccessful = false; 330 String clientLocation = req.getParameter("RuleFile"); 331 File f = new File(clientLocation); 332 String errMsg = null; 333 String ruleName = req.getParameter("ruleName"); 334 String webServiceKey = (String )req.getParameter("webServiceKey"); 335 webServiceKey = webServiceKey.replaceAll("[@]","#"); 336 String 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 e) { 343 isAddRuleSuccessful = false; 344 Throwable 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 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 private boolean testMbeanExist(HttpServletRequest req, String jarFileName){ 367 368 String errMsg = ""; 369 try { 370 String which = req.getParameter("which"); 371 String implClass = req.getParameter("implClass"); 372 req.setAttribute("implClass", implClass); 373 String 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 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 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(bundle, "common.NA", null)); 393 String 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 [] params = {implClass}; 403 String [] 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 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 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 getFileName(String filePath) { 428 String 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 |