Detail how to deploy projects remotely through tomcat's ManagerServlet

  • 2020-12-07 04:38:10
  • OfStack

introduce

During my previous post internship, leader asked me to read the source code of tomcat and try to implement the function of remote deployment project by myself.
There is an Manager application in Tomact, which is used to manage the deployed web application. In this application, ManagerServlet is its master servlet, through which we can obtain some metrics of tomcat and remotely manage web application, but this function is protected by the security constraints in the deployment of web application.

When you request ManagerServlet, it checks the value returned by getPathInfo() and the associated query parameters to determine the requested operation. It supports the following operations and parameters (starting with the servlet path):

请求路径 描述
/deploy?config={config-url} 根据指定的path部署并启动1个新的web应用程序(详见源码)
/deploy?config={config-url}&war={war-url}/ 根据指定的pat部署并启动1个新的web应用程序(详见源码)
/deploy?path=/xxx&war={war-url} 根据指定的path部署并启动1个新的web应用程序(详见源码)
/list 列出所有web应用程序的上下文路径。格式为path:status:sessions(活动会话数)
/reload?path=/xxx 根据指定path重新加载web应用
/resources?type=xxxx 枚举可用的全局JNDI资源,可以限制指定的java类名
/serverinfo 显示系统信息和JVM信息
/sessions 此方法已过期
/expire?path=/xxx 列出path路径下的web应用的session空闲时间信息
/expire?path=/xxx&idle=mm Expire sessions for the context path /xxx which were idle for at least mm minutes.
/sslConnectorCiphers 显示当前connector配置的SSL/TLS密码的诊断信息
/start?path=/xx 根据指定path启动web应用程序
/stop?path=/xxx 根据指定path关闭web应用程序
/threaddump Write a JVM thread dump
/undeploy?path=/xxx 关闭并删除指定path的Web应用程序,然后删除底层WAR文件或文档基目录。

We can go through ManagerServlet getPathInfo() provides the operation to deploy your project remotely to a server, my practice code is posted below, and you only need to introduce the httpclient package and commons package before you can practice it.

Encapsulates the remote request management class of Series 1

Encapsulate this class to facilitate client requests for ManagerServlet:


import java.io.File;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;

public class TomcatManager {
  private static final String MANAGER_CHARSET = "UTF-8";
  private String username;
  private URL url;
  private String password;
  private String charset;
  private boolean verbose;
  private DefaultHttpClient httpClient;
  private BasicHttpContext localContext;

  /** constructor */
  public TomcatManager(URL url, String username) {
    this(url, username, "");
  }
  public TomcatManager(URL url, String username, String password) {
    this(url, username, password, "ISO-8859-1");
  }
  public TomcatManager(URL url, String username, String password, String charset) {
    this(url, username, password, charset, true);
  }
  public TomcatManager(URL url, String username, String password, String charset, boolean verbose) {
    this.url = url;
    this.username = username;
    this.password = password;
    this.charset = charset;
    this.verbose = verbose;
    
    //  Create the configuration associated with the network request 
    PoolingClientConnectionManager poolingClientConnectionManager = new PoolingClientConnectionManager();
    poolingClientConnectionManager.setMaxTotal(5);
    this.httpClient = new DefaultHttpClient(poolingClientConnectionManager);

    if (StringUtils.isNotEmpty(username)) {
      Credentials creds = new UsernamePasswordCredentials(username, password);

      String host = url.getHost();
      int port = url.getPort() > -1 ? url.getPort() : AuthScope.ANY_PORT;
      httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), creds);

      AuthCache authCache = new BasicAuthCache();
      BasicScheme basicAuth = new BasicScheme();
      HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
      authCache.put(targetHost, basicAuth);

      localContext = new BasicHttpContext();
      localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
  }

  /**  According to path Deploy and start 1 A new application  */
  public TomcatManagerResponse deploy(String path, File war, boolean update) throws Exception {
    StringBuilder buffer = new StringBuilder("/deploy");
    buffer.append("?path=").append(URLEncoder.encode(path, charset));
    if (war != null) {
      buffer.append("&war=").append(URLEncoder.encode(war.toString(), charset));
    }
    if (update) {
      buffer.append("&update=true");
    }
    return invoke(buffer.toString());
  }

  /**  Get all deployed web The context path of the application. Format for path:status:sessions( Active sessions ) */
  public TomcatManagerResponse list() throws Exception {
    StringBuilder buffer = new StringBuilder("/list");
    return invoke(buffer.toString());
  }

  /**  Get system information and JVM information  */
  public TomcatManagerResponse serverinfo() throws Exception {
    StringBuilder buffer = new StringBuilder("/serverinfo");
    return invoke(buffer.toString());
  }

  /**  The method that actually sends the request  */
  private TomcatManagerResponse invoke(String path) throws Exception {
    HttpRequestBase httpRequestBase = new HttpGet(url + path);
    HttpResponse response = httpClient.execute(httpRequestBase, localContext);

    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
      case HttpStatus.SC_OK: // 200
      case HttpStatus.SC_CREATED: // 201
      case HttpStatus.SC_ACCEPTED: // 202
        break;
      case HttpStatus.SC_MOVED_PERMANENTLY: // 301
      case HttpStatus.SC_MOVED_TEMPORARILY: // 302
      case HttpStatus.SC_SEE_OTHER: // 303
      String redirectUrl = getRedirectUrl(response);
      this.url = new URL(redirectUrl);
      return invoke(path);
    }

    return new TomcatManagerResponse().setStatusCode(response.getStatusLine().getStatusCode())
        .setReasonPhrase(response.getStatusLine().getReasonPhrase())
        .setHttpResponseBody(IOUtils.toString(response.getEntity().getContent()));
  }
  
  /**  Extract redirection URL */
  protected String getRedirectUrl(HttpResponse response) {
    Header locationHeader = response.getFirstHeader("Location");
    String locationField = locationHeader.getValue();
    // is it a relative Location or a full ?
    return locationField.startsWith("http") ? locationField : url.toString() + '/' + locationField;
  }
}

Encapsulate the response result set


@Data
public class TomcatManagerResponse {
  private int statusCode;
  private String reasonPhrase;
  private String httpResponseBody;
}

Test remote deployment

Before testing, please put the following user permissions in the configuration file:


<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="sqdyy" password="123456" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>

Here is the code to test the successful remote deployment of the war package:


import static org.testng.AssertJUnit.assertEquals;
import java.io.File;
import java.net.URL;
import org.testng.annotations.Test;

public class TestTomcatManager {

  @Test
  public void testDeploy() throws Exception {
    TomcatManager tm = new TomcatManager(new URL("http://localhost:8080/manager/text"), "sqdyy", "123456");
    File war = new File("E:\\tomcat\\simple-war-project-1.0-SNAPSHOT.war");
    TomcatManagerResponse response = tm.deploy("/simple-war-project-1.0-SNAPSHOT", war, true);
    System.out.println(response.getHttpResponseBody());
    assertEquals(200, response.getStatusCode());
    
    // output:
    // OK - Deployed application at context path /simple-war-project-1.0-SNAPSHOT
  }

  @Test
  public void testList() throws Exception {
    TomcatManager tm = new TomcatManager(new URL("http://localhost:8080/manager/text"), "sqdyy", "123456");
    TomcatManagerResponse response = tm.list();
    System.out.println(response.getHttpResponseBody());
    assertEquals(200, response.getStatusCode());
    
    // output:
    // OK - Listed applications for virtual host localhost
    // /:running:0:ROOT
    // /simple-war-project-1.0-SNAPSHOT:running:0:simple-war-project-1.0-SNAPSHOT
    // /examples:running:0:examples
    // /host-manager:running:0:host-manager
    // /manager:running:0:manager
    // /docs:running:0:docs
  }

  @Test
  public void testServerinfo() throws Exception {
    TomcatManager tm = new TomcatManager(new URL("http://localhost:8080/manager/text"), "sqdyy", "123456");
    TomcatManagerResponse response = tm.serverinfo();
    System.out.println(response.getHttpResponseBody());
    assertEquals(200, response.getStatusCode());
    
    // output:
    // OK - Server info
    // Tomcat Version: Apache Tomcat/7.0.82
    // OS Name: Windows 10
    // OS Version: 10.0
    // OS Architecture: amd64
    // JVM Version: 1.8.0_144-b01
    // JVM Vendor: Oracle Corporation
  }
}

The resources

ManagerServlet source address


Related articles: