BAEL-1267: programmatically create, configure and run a tomcat server
This commit is contained in:
parent
c8d11b402f
commit
369ec4e181
|
@ -705,6 +705,38 @@
|
|||
<classifier>test</classifier>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- tomcat -->
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-catalina</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jasper</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jasper-el</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jsp-api</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
@ -786,5 +818,6 @@
|
|||
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
|
||||
<kafka.version>1.0.0</kafka.version>
|
||||
<docker.version>3.0.14</docker.version>
|
||||
<tomcat.version>8.5.24</tomcat.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.tomcat;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by adi on 1/14/18.
|
||||
*/
|
||||
@WebFilter(urlPatterns = "/my-servlet/*")
|
||||
public class MyFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
System.out.println("Filtering stuff...");
|
||||
HttpServletResponse httpResponse = (HttpServletResponse) response;
|
||||
httpResponse.addHeader("myHeader", "myHeaderValue");
|
||||
chain.doFilter(request, httpResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.tomcat;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by adi on 1/10/18.
|
||||
*/
|
||||
@WebServlet(
|
||||
name = "com.baeldung.tomcat.programmatic.MyServlet",
|
||||
urlPatterns = {"/my-servlet"}
|
||||
)
|
||||
public class MyServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
resp.getWriter().write("test");
|
||||
resp.getWriter().flush();
|
||||
resp.getWriter().close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.tomcat;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.tomcat.util.descriptor.web.FilterDef;
|
||||
import org.apache.tomcat.util.descriptor.web.FilterMap;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by adi on 1/10/18.
|
||||
*/
|
||||
public class ProgrammaticTomcat {
|
||||
|
||||
private Tomcat tomcat = null;
|
||||
|
||||
//uncomment for live test
|
||||
// public static void main(String[] args) throws LifecycleException, ServletException, URISyntaxException, IOException {
|
||||
// startTomcat();
|
||||
// }
|
||||
|
||||
public void startTomcat() throws LifecycleException {
|
||||
tomcat = new Tomcat();
|
||||
tomcat.setPort(8080);
|
||||
tomcat.setHostname("localhost");
|
||||
String appBase = ".";
|
||||
tomcat
|
||||
.getHost()
|
||||
.setAppBase(appBase);
|
||||
|
||||
File docBase = new File(System.getProperty("java.io.tmpdir"));
|
||||
Context context = tomcat.addContext("", docBase.getAbsolutePath());
|
||||
|
||||
//add a servlet
|
||||
Class servletClass = MyServlet.class;
|
||||
Tomcat.addServlet(context, servletClass.getSimpleName(), servletClass.getName());
|
||||
context.addServletMappingDecoded("/my-servlet/*", servletClass.getSimpleName());
|
||||
|
||||
//add a filter and filterMapping
|
||||
Class filterClass = MyFilter.class;
|
||||
FilterDef myFilterDef = new FilterDef();
|
||||
myFilterDef.setFilterClass(filterClass.getName());
|
||||
myFilterDef.setFilterName(filterClass.getSimpleName());
|
||||
context.addFilterDef(myFilterDef);
|
||||
|
||||
FilterMap myFilterMap = new FilterMap();
|
||||
myFilterMap.setFilterName(filterClass.getSimpleName());
|
||||
myFilterMap.addURLPattern("/my-servlet/*");
|
||||
context.addFilterMap(myFilterMap);
|
||||
|
||||
tomcat.start();
|
||||
//uncomment for live test
|
||||
// tomcat
|
||||
// .getServer()
|
||||
// .await();
|
||||
}
|
||||
|
||||
public void stopTomcat() throws LifecycleException {
|
||||
tomcat.stop();
|
||||
tomcat.destroy();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.tomcat;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Created by adi on 1/14/18.
|
||||
*/
|
||||
@RunWith(BlockJUnit4ClassRunner.class)
|
||||
public class ProgrammaticTomcatTest {
|
||||
|
||||
private ProgrammaticTomcat tomcat = new ProgrammaticTomcat();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tomcat.startTomcat();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
tomcat.stopTomcat();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTomcatStarted_whenAccessServlet_responseIsTestAndResponseHeaderIsSet() throws Exception {
|
||||
CloseableHttpClient httpClient = HttpClientBuilder
|
||||
.create()
|
||||
.build();
|
||||
HttpGet getServlet = new HttpGet("http://localhost:8080/my-servlet");
|
||||
|
||||
HttpResponse response = httpClient.execute(getServlet);
|
||||
assertEquals(HttpStatus.SC_OK, response
|
||||
.getStatusLine()
|
||||
.getStatusCode());
|
||||
|
||||
String myHeaderValue = response
|
||||
.getFirstHeader("myHeader")
|
||||
.getValue();
|
||||
assertEquals("myHeaderValue", myHeaderValue);
|
||||
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
assertNotNull(responseEntity);
|
||||
|
||||
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||
assertEquals("test", responseString);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue