1

I have inherited a Maven/Tomcat(8.5) web application that is locally hosted on different machines. I am trying to get the application to update from a war file that is either stored locally (on a USB drive) or downloaded from AWS on Windows more reliably. On the Linux version, the software is able to upload the new war file via curl using the manager-script by doing the following:

        String url = "http://admin:secret@localhost:8080/manager/text/deploy?path=/foo&update=true";
        CommandRunner.executeCommand(new String[] { "curl", "--upload-file", war, url });

For Windows, the current current method tries to copy over the war file in the /webapps directory and has tomcat auto deploy the new war file after restarting either tomcat or the host machine. The problem is that the copied war file ends up being 0kb and there is nothing to deploy. This appears to happen outside of my install function because the file size after FileUtils.copyFile() for the /webapps/foo.war is the correct size.

I have tried to implement a PUT request for the manager-script roll from reading the Tomcat manager docs and another post:

    File warfile = new File(request.getWar());
    String warpath = warfile.getAbsolutePath().replace("\\", "/");

    //closes other threads
    App.terminate();

    String url = "http://admin:secret@localhost:8080/manager/text/deploy?path=/foo&war=file:" + warpath + "&update=true";
    HttpClient client = HttpClientBuilder.create().build();
    HttpPut request;
    try {
            request = new HttpPut(url);

            HttpResponse response = client.execute(request);
            BufferedReader rd = new BufferedReader(
                            new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                    result.append(line);
            }

            System.err.println(result.toString());
        } catch (Exception e){
            LOGGER.info("war install failed");
            throw new ServiceException("Unable to upload war file.", e);
        }

However, the process still ends up with a 0kb war file to /webapps. The only error in my log is when tomcat tries to deploy the empty war. I have tried relaxing file read/write permissions in my tomcat installation and it doesn't seem to help. I also don't know if I can guarantee that all Windows installations will have access to curl if I try that implementation. Has anyone ran into similar issues?Thank you for the help!

  • Update: So I was not able to get this to work. I haven't figured out what curl does that I'm not able to replicate. I was able to dig into the company records and just determine that all of the windows machines I am installed on are past version 1803 where curl came standard in Windows. I used the Java Runtime to execute the curl command above. So if anyone stumbles along here I recommend trying that first. – Dardin-dale Apr 23 '20 at 17:25

1 Answers1

0

Will, you are openning the file for sure but you are not writing anything to it, the result string is wrote to the stdout, so you got to replace System.err.println(result.toString()); with fr = new FileWriter(file); br = new BufferedWriter(fr); br.write(result); and don't forget to close your resources at the end (br.close(); fr.close(); ) :) PS: check if there are some informations to be filtered (headers or staff like that)

bfsMed
  • 58
  • 5
  • I thought that the result was just the response to the put request right? The file overwrite and deployment should be handled by the tomcat manager-script according to the put request. In any case, I tried your suggestion with no success. I tried adding the war file as a FileEntity to the put request as well but ran into a socket write error. – Dardin-dale Apr 09 '20 at 18:53