3

I'm trying to deploy a RESTful web service to a web server with Tomcat 8 from Eclipse.

I tried using HttpClient using the code from the second answer of this post: Tomcat: remote programmatic deploy? but I get this exception java.net.SocketException: Connection reset by peer: socket write error.

I also tried with HttpURLConnection using the code from the first answer of this post: how to upload, download file from tomcat server with username,password in swing but I also get an error.

What could be the reason? Is there another way? Thank you.

Community
  • 1
  • 1
bloox
  • 127
  • 2
  • 9
  • Is the Tomcat install on the same machine as Eclipse? – MadConan Jun 29 '15 at 15:18
  • No, it is in a different machine. I can use Tomcat Manager to deploy the web service but I would like to do it from java code with http PUT request. – bloox Jun 30 '15 at 07:35

2 Answers2

2

... it is in a different machine. I can use Tomcat Manager to deploy the web service but I would like to do it from java code with http PUT request.

In order for that to be possible, the deployment folder would need to be accessible from the HTTP server or web application. That's generally a bad idea for security reasons.

You can still do it programmatically with Java (or other language) by calling any of the many utilities for file transfers: ftp, scp, networked file system, etc.

Note that after you have copied the artifact (eg war file) to the Tomcat host, you can then tell Tomcat to deploy it remotely via the deployment manager url. From the documentation:

In this example the web application located in the directory /path/to/foo on the Tomcat server is deployed as the web application context named /footoo.

http://localhost:8080/manager/text/deploy?path=/footoo&war=file:/path/to/foo

In this example the ".war" file /path/to/bar.war on the Tomcat server is deployed as the web application context named /bar. Notice that there is no path parameter so the context path defaults to the name of the web application archive file without the ".war" extension.

http://localhost:8080/manager/text/deploy?war=jar:file:/path/to/bar.war!/

Your code could copy the artifact via scp (or whatever) and if successful call the manager URL with appropriate arguments. A two step process in a single code run.

MadConan
  • 3,749
  • 1
  • 16
  • 27
1

Thank you very much for your answer! It works! I used sftp to upload the file in the webapps folder of Tomcat server. Since in server.xml autodeploy=true, I didn't have to do HTTP PUT request. Here is my code, based on this link:

String SFTPHOST = "1.2.3.4";
int SFTPPORT = 22;
String SFTPUSER = "root";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "/home/username/apache-tomcat-8.0.23/webapps/";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd("..");
        channelSftp.cd(SFTPWORKINGDIR);
        File f = new File("path/to/war");
        channelSftp.put(new FileInputStream(f), f.getName());
} catch (Exception ex) {
        ex.printStackTrace();

}
bloox
  • 127
  • 2
  • 9