Merge pull request #3454 from adrianprecub/BAEL-1267-programmatic-tomcat

BAEL-1267: programmatically create, configure and run a tomcat server
This commit is contained in:
daoire 2018-02-05 12:15:30 +00:00 committed by GitHub
commit 08dbab446f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 189 additions and 0 deletions

View File

@ -716,6 +716,13 @@
<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.milyn</groupId>
<artifactId>milyn-smooks-all</artifactId>
@ -802,6 +809,7 @@
<kafka.version>1.0.0</kafka.version>
<smooks.version>1.7.0</smooks.version>
<docker.version>3.0.14</docker.version>
<tomcat.version>8.5.24</tomcat.version>
<async.http.client.version>2.2.0</async.http.client.version>
</properties>
</project>

View File

@ -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() {
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}