Package com.simoncat.actions

Source Code of com.simoncat.actions.InstallActionServlet

package com.simoncat.actions;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.simoncat.beans.FinalScreenOutput;
import com.simoncat.Constants;

import  org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import java.util.List;


import org.apache.commons.fileupload.FileItem;
import java.io.File;
import java.util.Iterator;



import java.util.Map;


import com.simoncat.net.HttpClient;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.simoncat.beans.XMLServers;
import com.simoncat.vo.Server;


import com.simoncat.net.SendFileSFTP;


public final class InstallActionServlet extends HttpServlet {
//extends Action {




    public InstallActionServlet(){

    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Deploy Web Application</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
  //ActionForm form = new ,
  try{
    execute(null,null,request,response,out);
    out.println("<br/><br/><br/><a href=\"index.html\">Back</a>")
  }
  catch(Exception e){
    e.printStackTrace();
  }
 
        out.println("</body>");
        out.println("</html>");
    }


    public void execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response,PrintWriter out) throws Exception {
  String warName=null;
  String appName=null;
  String serName="localhost";
  File uploadedFile=null;
  boolean uploadedFileOK=false;
  boolean transferFileOK=false;
        //HttpSession session = request.getSession();

        //FinalScreenOutput fso;

        //if ( form != null ) {
 
            //DynaActionForm df =  (DynaActionForm) form;
            //String warName  =  (String) df.get("c1");
            //String serverId =  (String) df.get("c2");
      //System.out.println("WAR:"+ warName + " Server:" + serverId );


            //recovery war

    /*FileUploadUtils fu = new FileUploadUtils(request);
    Map<String,FileField> fileMap = fu. getFileFieldMap();
    Map<String,String> nfileMap = fu.getNonFileFieldMap();
    FileField ff = fileMap.get("fileControl");
    String fileName = nfileMap.get("fileName");
    fileName = "/home/repository/"+fileName;
  
    ff.write(fileName);*/
     
      System.out.println("AAA");
      if (ServletFileUpload.isMultipartContent(request)){
    System.out.println("BBB");
          // Parse the HTTP request...
        //ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
        //List fileItemsList = servletFileUpload.parseRequest(request);
       
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        diskFileItemFactory.setSizeThreshold(140960); /* the unit is bytes */

    System.out.println("CCC");
        File repositoryPath = new File("/tmp");
        diskFileItemFactory.setRepository(repositoryPath);
    System.out.println("DDD");
        ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
        servletFileUpload.setSizeMax(-1);//81920); /* the unit is bytes */
        System.out.println("EEE");
        try {
       
            List fileItemsList = servletFileUpload.parseRequest(request);
        System.out.println("FFF items#"+fileItemsList.size())
            /* Process file items... */
            Iterator it = fileItemsList.iterator();
            while (it.hasNext()){
                System.out.println("GGG");     
              FileItem fileItem = (FileItem)it.next();
              if (fileItem.isFormField()){
                /* The file item contains a simple name-value pair of a form field */
                System.out.println("CAMPO NORMAL:"+fileItem.getFieldName());
        // Process a regular form field
            String name = fileItem.getFieldName();
            String value = fileItem.getString();
        if(name.equals("Submit"))
          serName=value;
              }
              else{
                /* The file item contains an uploaded file */
                System.out.println("CAMPO FILE:"+fileItem.getFieldName());
        //Copiar el archivo al /tmp
        String fieldName = fileItem.getFieldName();
       
        String fileName = fileItem.getName();
        if(fileName.trim().length()>0){
          String contentType = fileItem.getContentType();
          boolean isInMemory = fileItem.isInMemory();
          long sizeInBytes = fileItem.getSize();
          if(fileName.trim().endsWith(".war")){
            warName=fileName.trim();
            appName=warName.substring(0,fileName.toLowerCase().indexOf(".war"));

          }
          try{
            uploadedFile = new File("/tmp/"+fileName);
                fileItem.write(uploadedFile);
            uploadedFileOK=true;
         
          }
          catch(IOException io){
            byte[] data = fileItem.get();
            System.out.println("LIOS ESCRIBIENOD EL ARCHIVO AL TMP "+
            fileName +":"+new String(data,0,10))
             
          }
        }
        else{
          // no se selecciono archivo.

          out.println("<h1> Debe seleccionar el Archivo War para deploy [Finished]</h1>");
          return;
        }
              }
            }
        System.out.println("HHH");
        }
        catch (SizeLimitExceededException ex) {
            /* The size of the HTTP request body exceeds the limit */
      System.out.println("SE DESPARRAMO POR EL TAMAÑO DEL FILE");
        }
    System.out.println("III");
       
      }
           System.out.println("JJJ");    


   

              //undeploy WAR in SERVER
    //recuperar los datos del server serName
    XMLServers xmlserver = new XMLServers();
        Server server = xmlserver.loadServerByName(serName, this.getServletContext());
    System.out.println("DatosServer:"+server);
    String ip = server.getAddress();
    String tomcatUsr = server.getUserTomcat();
    String tomcatPwd = server.getPasswordTomcat();
    int port= new Integer(server.getPort()).intValue();
    int portSSH = new Integer(server.getPortSSH()).intValue();

    //HACER SFTP AL SERVIDOR
    try{
      if(uploadedFileOK){
        SendFileSFTP sfsftp = new SendFileSFTP(serName,portSSH,server.getUser(),server.getPassword());
        if(sfsftp.connect()){
          sfsftp.send(uploadedFile,"/tmp");
          sfsftp.disconnect();
          transferFileOK=true;
        }
      }
    }
    catch(Exception e){
      e.printStackTrace();
      transferFileOK=false;
    }
   

    //verificar si la aplicasion esta corriendo?
    ////http://localhost:8080/manager/list
    HttpClient hc1 = new HttpClient(ip,port,tomcatUsr,tomcatPwd,"");
    boolean lb = hc1.appIsRunning(appName);
    boolean ub=false;
    boolean db=false;
    if(lb && transferFileOK){
      // autenticar y undeploy la aplicacion en el tomcat.
      ////http://localhost:8080/manager/undeploy?path=/Binary
      String undeploy = "http://"+ip+":"+port+"/manager/undeploy?path=/"+appName;
        HttpClient hc2 = new HttpClient(ip,port,tomcatUsr,tomcatPwd,undeploy);
       ub=hc2.execute();
    }
     
   
    if(((lb && ub) || (!lb) ) && transferFileOK ){
        //Deploy WAR       
       //enviar comando al tomcat 
      ////http://localhost:8080/manager/deploy?path=/Binary&war=file:/tmp/Binary.war 
       String deploy = "http://"+ip+":"+port+"/manager/deploy?path=/"+appName+"&war=file:/tmp/"+warName;
        HttpClient hc3 = new HttpClient(ip,port,tomcatUsr,tomcatPwd,deploy);
       db=hc3.execute();
    }

    //fso = new FinalScreenOutput();
          //fso.setPageTitle("Upload-Deploy War[Finished]");
                //fso.setMessage       ("WAR deployed sucessfully!"+ "It was Running:"+lb+" Undeploy:"+ub+" deploy:"+db);
    out.println("Upload-Deploy War[Finished]");
                out.println("<h3>WAR deployed sucessfully?"+db+"</h3>");
    out.println("<h3>It was Running?"+lb+"</h3>");
    out.println("<h3>Undeploy done?"+ub+"</h3>")
    out.println("<h3>transmision FTP OK?"+transferFileOK+"</h3>");



        //}
        // else {
  //  fso = new FinalScreenOutput();
        //  fso.setPageTitle    ("Upload-Deploy War[NOT   Finished]");
        //    fso.setMessage       ("Sorry, problem with submitted form!");
        //}

        //if (Constants.xmlOutputOnly) {
        //    request.setAttribute ("content", Constants.xmlContentType);
        //} else {
        //    request.setAttribute ("content", Constants.htmlContentType);
        //    request.setAttribute ("style", "confirmWar.xsl");
        //}
        //request.setAttribute ("form", fso);

        //return (mapping.findForward(Constants.XMLHandler));
     }

}
TOP

Related Classes of com.simoncat.actions.InstallActionServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.