Here are the examples of the java api org.apache.catalina.startup.Tomcat.addServlet() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
423 Examples
19
Source : ConverterApplication.java
with Apache License 2.0
from InteractiveAdvertisingBureau
with Apache License 2.0
from InteractiveAdvertisingBureau
private static void registerServletAndMapping(Context ctx) {
Tomcat.addServlet(ctx, "converter", new ConverterServlet()).setLoadOnStartup(1);
ctx.addServletMapping("/*", "converter");
}
18
Source : TestMimeHeaders.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
private void setupHeadersTest(Tomcat tomcat) {
Context ctx = tomcat.addContext("", getTemporaryDirectory().getAbsolutePath());
Tomcat.addServlet(ctx, "servlet", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain; charset=ISO-8859-1");
res.getWriter().write("OK");
}
});
ctx.addServletMapping("/", "servlet");
alv = new HeaderCountLogValve();
tomcat.getHost().getPipeline().addValve(alv);
}
18
Source : CookiesBaseTest.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
public static void addServlets(Tomcat tomcat) {
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "invalid", new CookieServlet("na;me", "value"));
ctx.addServletMapping("/invalid", "invalid");
Tomcat.addServlet(ctx, "null", new CookieServlet(null, "value"));
ctx.addServletMapping("/null", "null");
Tomcat.addServlet(ctx, "blank", new CookieServlet("", "value"));
ctx.addServletMapping("/blank", "blank");
Tomcat.addServlet(ctx, "invalidFwd", new CookieServlet("na/me", "value"));
ctx.addServletMapping("/invalidFwd", "invalidFwd");
Tomcat.addServlet(ctx, "invalidStrict", new CookieServlet("na?me", "value"));
ctx.addServletMapping("/invalidStrict", "invalidStrict");
Tomcat.addServlet(ctx, "valid", new CookieServlet("name", "value"));
ctx.addServletMapping("/valid", "valid");
Tomcat.addServlet(ctx, "switch", new CookieServlet("name", "val?ue"));
ctx.addServletMapping("/switch", "switch");
}
18
Source : CookiesBaseTest.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
public static void addServlets(Tomcat tomcat) {
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.setCookieProcessor(new LegacyCookieProcessor());
Tomcat.addServlet(ctx, "invalid", new CookieServlet("na;me", "value"));
ctx.addServletMappingDecoded("/invalid", "invalid");
Tomcat.addServlet(ctx, "null", new CookieServlet(null, "value"));
ctx.addServletMappingDecoded("/null", "null");
Tomcat.addServlet(ctx, "blank", new CookieServlet("", "value"));
ctx.addServletMappingDecoded("/blank", "blank");
Tomcat.addServlet(ctx, "invalidFwd", new CookieServlet("na/me", "value"));
ctx.addServletMappingDecoded("/invalidFwd", "invalidFwd");
Tomcat.addServlet(ctx, "invalidStrict", new CookieServlet("$name", "value"));
ctx.addServletMappingDecoded("/invalidStrict", "invalidStrict");
Tomcat.addServlet(ctx, "valid", new CookieServlet("name", "value"));
ctx.addServletMappingDecoded("/valid", "valid");
Tomcat.addServlet(ctx, "switch", new CookieServlet("name", "val?ue"));
ctx.addServletMappingDecoded("/switch", "switch");
}
18
Source : TomcatFilterInstrumentationTest.java
with Apache License 2.0
from ApptuitAI
with Apache License 2.0
from ApptuitAI
private static <T extends BaseTestServlet> T registerServlet(Context context, T servlet) {
Tomcat.addServlet(context, servlet.getClreplaced().getSimpleName(), servlet);
context.addServletMappingDecoded(servlet.getPath(), servlet.getClreplaced().getSimpleName());
return servlet;
}
17
Source : TestChunkedInputFilter.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
private void doTestExtensionSizeLimit(int len, boolean ok) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
tomcat.getConnector().setProperty("maxExtensionSize", Integer.toString(EXT_SIZE_LIMIT));
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(ok));
ctx.addServletMapping("/", "servlet");
tomcat.start();
String extName = ";foo=";
StringBuilder extValue = new StringBuilder(len);
for (int i = 0; i < (len - extName.length()); i++) {
extValue.append("x");
}
String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + extName + extValue.toString() + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF };
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(request);
client.connect();
client.processRequest();
if (ok) {
replacedertTrue(client.isResponse200());
} else {
replacedertTrue(client.isResponse500());
}
}
17
Source : TestChunkedInputFilter.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
/**
* @param expectPreplaced
* If the servlet is expected to process the request
* @param expectReadWholeBody
* If the servlet is expected to fully read the body and reliably
* deliver a response
* @param chunks
* Text of chunks
* @param readLimit
* Do not read more than this many bytes
* @param expectReadCount
* Expected count of read bytes
* @throws Exception
* Unexpected
*/
private void doTestChunkSize(boolean expectPreplaced, boolean expectReadWholeBody, String chunks, int readLimit, int expectReadCount) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
BodyReadServlet servlet = new BodyReadServlet(expectPreplaced, readLimit);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMapping("/", "servlet");
tomcat.start();
String request = "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: text/plain" + SimpleHttpClient.CRLF;
if (expectPreplaced) {
request += "Connection: close" + SimpleHttpClient.CRLF;
}
request += SimpleHttpClient.CRLF + chunks + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(new String[] { request });
Exception processException = null;
client.connect();
try {
client.processRequest();
} catch (Exception e) {
// Socket was probably closed before client had a chance to read
// response
processException = e;
}
if (expectPreplaced) {
if (expectReadWholeBody) {
replacedertNull(processException);
}
if (processException == null) {
replacedertTrue(client.getResponseLine(), client.isResponse200());
replacedertEquals(String.valueOf(expectReadCount), client.getResponseBody());
}
replacedertEquals(expectReadCount, servlet.getCountRead());
} else {
if (processException == null) {
replacedertTrue(client.getResponseLine(), client.isResponse500());
}
replacedertEquals(0, servlet.getCountRead());
replacedertTrue(servlet.getExceptionDuringRead());
}
}
17
Source : TestChunkedInputFilter.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
private void doTestChunkingCRLF(boolean chunkHeaderUsesCRLF, boolean chunkUsesCRLF, boolean firstheaderUsesCRLF, boolean secondheaderUsesCRLF, boolean endUsesCRLF, boolean expectPreplaced) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Configure allowed trailer headers
tomcat.getConnector().setProperty("allowedTrailerHeaders", "X-Trailer1,X-Trailer2");
EchoHeaderServlet servlet = new EchoHeaderServlet(expectPreplaced);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMapping("/", "servlet");
tomcat.start();
String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + (chunkHeaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "a=0" + (chunkUsesCRLF ? SimpleHttpClient.CRLF : LF) + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + "x-trailer1: Test", "Value1" + (firstheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "x-trailer2: TestValue2" + (secondheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + (endUsesCRLF ? SimpleHttpClient.CRLF : LF) };
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(request);
client.connect();
Exception processException = null;
try {
client.processRequest();
} catch (Exception e) {
// Socket was probably closed before client had a chance to read
// response
processException = e;
}
if (expectPreplaced) {
replacedertTrue(client.isResponse200());
replacedertEquals("nullnull7TestValue1TestValue2", client.getResponseBody());
replacedertNull(processException);
replacedertFalse(servlet.getExceptionDuringRead());
} else {
if (processException == null) {
replacedertTrue(client.getResponseLine(), client.isResponse500());
} else {
// Use fall-back for checking the error occurred
replacedertTrue(servlet.getExceptionDuringRead());
}
}
}
17
Source : TestMaxConnections.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
private synchronized void init() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context root = tomcat.addContext("", null);
Tomcat.addServlet(root, "Simple", new SimpleServlet());
root.addServletMapping("/test", "Simple");
tomcat.getConnector().setProperty("maxKeepAliveRequests", "1");
tomcat.getConnector().setProperty("maxThreads", "10");
tomcat.getConnector().setProperty("soTimeout", "20000");
tomcat.getConnector().setProperty("keepAliveTimeout", "50000");
tomcat.getConnector().setProperty("maxConnections", Integer.toString(MAX_CONNECTIONS));
tomcat.getConnector().setProperty("acceptCount", "1");
tomcat.start();
}
17
Source : TomcatHttpServer.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
protected void initServer() throws Exception {
this.tomcatServer = new Tomcat();
this.tomcatServer.setBaseDir(baseDir);
this.tomcatServer.setHostname(getHost());
this.tomcatServer.setPort(getPort());
ServletHttpHandlerAdapter servlet = initServletAdapter();
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootContext = tomcatServer.addContext(this.contextPath, base.getAbsolutePath());
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet).setAsyncSupported(true);
rootContext.addServletMappingDecoded(this.servletMapping, "httpHandlerServlet");
if (wsListener != null) {
rootContext.addApplicationListener(wsListener.getName());
}
}
17
Source : Webserver.java
with Apache License 2.0
from supertokens
with Apache License 2.0
from supertokens
public void addAPI(WebserverAPI api) {
StandardContext context = tomcatReference.getContext();
Tomcat tomcat = tomcatReference.getTomcat();
tomcat.addServlet(CONTEXT_PATH, api.getPath(), api);
context.addServletMappingDecoded(api.getPath(), api.getPath());
}
17
Source : TomcatServer.java
with Apache License 2.0
from reactor
with Apache License 2.0
from reactor
private Wrapper addServlet(Context ctx, HttpServlet servlet, String mapping) {
String servletName = servlet.getClreplaced().getName();
Wrapper wrapper = Tomcat.addServlet(ctx, servletName, servlet);
ctx.addServletMappingDecoded(mapping, servletName);
return wrapper;
}
17
Source : 280.java
with MIT License
from masud-technope
with MIT License
from masud-technope
private void doTestChunkingCRLF(boolean chunkHeaderUsesCRLF, boolean chunkUsesCRLF, boolean firstheaderUsesCRLF, boolean secondheaderUsesCRLF, boolean endUsesCRLF, boolean expectPreplaced) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Configure allowed trailer headers
tomcat.getConnector().setProperty("allowedTrailerHeaders", "X-Trailer1,X-Trailer2");
EchoHeaderServlet servlet = new EchoHeaderServlet(expectPreplaced);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMapping("/", "servlet");
tomcat.start();
String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + (chunkHeaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "a=0" + (chunkUsesCRLF ? SimpleHttpClient.CRLF : LF) + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + "x-trailer1: Test", "Value1" + (firstheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "x-trailer2: TestValue2" + (secondheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + (endUsesCRLF ? SimpleHttpClient.CRLF : LF) };
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(request);
client.connect();
Exception processException = null;
try {
client.processRequest();
} catch (Exception e) {
processException = e;
}
if (expectPreplaced) {
replacedertTrue(client.isResponse200());
replacedertEquals("nullnull7TestValue1TestValue2", client.getResponseBody());
replacedertNull(processException);
replacedertFalse(servlet.getExceptionDuringRead());
} else {
if (processException == null) {
replacedertTrue(client.getResponseLine(), client.isResponse500());
} else {
// Use fall-back for checking the error occurred
replacedertTrue(servlet.getExceptionDuringRead());
}
}
}
17
Source : 280.java
with MIT License
from masud-technope
with MIT License
from masud-technope
/**
* @param expectPreplaced
* If the servlet is expected to process the request
* @param expectReadWholeBody
* If the servlet is expected to fully read the body and reliably
* deliver a response
* @param chunks
* Text of chunks
* @param readLimit
* Do not read more than this many bytes
* @param expectReadCount
* Expected count of read bytes
* @throws Exception
* Unexpected
*/
private void doTestChunkSize(boolean expectPreplaced, boolean expectReadWholeBody, String chunks, int readLimit, int expectReadCount) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
BodyReadServlet servlet = new BodyReadServlet(expectPreplaced, readLimit);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMapping("/", "servlet");
tomcat.start();
String request = "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: text/plain" + SimpleHttpClient.CRLF;
if (expectPreplaced) {
request += "Connection: close" + SimpleHttpClient.CRLF;
}
request += SimpleHttpClient.CRLF + chunks + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(new String[] { request });
Exception processException = null;
client.connect();
try {
client.processRequest();
} catch (Exception e) {
processException = e;
}
if (expectPreplaced) {
if (expectReadWholeBody) {
replacedertNull(processException);
}
if (processException == null) {
replacedertTrue(client.getResponseLine(), client.isResponse200());
replacedertEquals(String.valueOf(expectReadCount), client.getResponseBody());
}
replacedertEquals(expectReadCount, servlet.getCountRead());
} else {
if (processException == null) {
replacedertTrue(client.getResponseLine(), client.isResponse500());
}
replacedertEquals(0, servlet.getCountRead());
replacedertTrue(servlet.getExceptionDuringRead());
}
}
17
Source : TomcatStarter.java
with GNU Lesser General Public License v2.1
from kawansoft
with GNU Lesser General Public License v2.1
from kawansoft
/**
* Add a Servlet using properties with the index
*
* @param properties the properties than contain all servlet & configurators
* info
* @param rootCtx the tomcat root context
*/
public void addAceqlServlet(Properties properties, Context rootCtx) {
if (properties == null) {
throw new IllegalArgumentException("properties can not be null");
}
String aceQLManagerServletCallName = TomcatStarterUtil.getAceQLManagerSevletName(properties);
// Add the ServerSqlManager servlet to the context
org.apache.catalina.Wrapper wrapper = Tomcat.addServlet(rootCtx, aceQLManagerServletCallName, new ServerSqlManager());
wrapper.setAsyncSupported(true);
rootCtx.addServletMappingDecoded("/*", aceQLManagerServletCallName);
TomcatStarterUtil.setInitParametersInStore(properties);
// Unecessary because we must start at / because of ou Rest API
// String serverSqlManagerUrlPattern = serverSqlManagerServletName;
// if (!serverSqlManagerUrlPattern.startsWith("/")) {
// serverSqlManagerUrlPattern = "/" + serverSqlManagerUrlPattern;
// }
}
16
Source : TestBug49158.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
public static void addServlets(Tomcat tomcat) {
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, path, new TestBug49158Servlet());
ctx.addServletMapping("/" + path, path);
}
16
Source : TestChunkedInputFilter.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testNoTrailingHeaders() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(true));
ctx.addServletMapping("/", "servlet");
tomcat.start();
String request = "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(new String[] { request });
client.connect();
client.processRequest();
replacedertEquals("nullnull7nullnull", client.getResponseBody());
}
16
Source : TestChunkedInputFilter.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testTrailingHeadersSizeLimit() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(false));
ctx.addServletMapping("/", "servlet");
// Limit the size of the trailing header
tomcat.getConnector().setProperty("maxTrailerSize", "10");
tomcat.start();
String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + "x-trailer: Test" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF };
TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
client.setRequest(request);
client.connect();
client.processRequest();
// Expected to fail because the trailers are longer
// than the set limit of 10 bytes
replacedertTrue(client.isResponse500());
}
16
Source : TestStandardContextAliases.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testDirContextAliases() throws Exception {
Tomcat tomcat = getTomcatInstance();
// Must have a real docBase - just use temp
StandardContext ctx = (StandardContext) tomcat.addContext("", System.getProperty("java.io.tmpdir"));
File lib = new File("webapps/examples/WEB-INF/lib");
ctx.setAliases("/WEB-INF/lib=" + lib.getCanonicalPath());
Tomcat.addServlet(ctx, "test", new TestServlet());
ctx.addServletMapping("/", "test");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
String result = res.toString();
replacedertTrue(result.indexOf("00-Preplaced") > -1);
replacedertTrue(result.indexOf("01-Preplaced") > -1);
replacedertTrue(result.indexOf("02-Preplaced") > -1);
}
16
Source : TestApplicationHttpRequest.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
private void doQueryStringTest(String originalQueryString, String forwardQueryString, Map<String, String[]> expected) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
if (forwardQueryString == null) {
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/display"));
} else {
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/display?" + forwardQueryString));
}
ctx.addServletMapping("/forward", "forward");
Tomcat.addServlet(ctx, "display", new DisplayParameterServlet(expected));
ctx.addServletMapping("/display", "display");
tomcat.start();
ByteChunk response = new ByteChunk();
StringBuilder target = new StringBuilder("http://localhost:");
target.append(getPort());
target.append("/forward");
if (originalQueryString != null) {
target.append('?');
target.append(originalQueryString);
}
int rc = getUrl(target.toString(), response, null);
replacedert.replacedertEquals(200, rc);
replacedert.replacedertEquals("OK", response.toString());
}
16
Source : HttpServer.java
with Apache License 2.0
from PaulWang92115
with Apache License 2.0
from PaulWang92115
/**
* servlet 容器,tomcat
* @param hostname
* @param port
*/
public void start(String hostname, Integer port) {
Tomcat tomcat = new Tomcat();
Server server = tomcat.getServer();
Service service = server.findService("Tomcat");
Connector connector = new Connector();
connector.setPort(port);
Engine engine = new StandardEngine();
engine.setDefaultHost(hostname);
Host host = new StandardHost();
host.setName(hostname);
String contextPath = "";
Context context = new StandardContext();
context.setPath(contextPath);
// 声明周期监听器
context.addLifecycleListener(new Tomcat.FixContextListener());
host.addChild(context);
engine.addChild(host);
service.setContainer(engine);
service.addConnector(connector);
tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet());
context.addServletMappingDecoded("/*", "dispatcher");
try {
tomcat.start();
tomcat.getServer().await();
} catch (LifecycleException e) {
e.printStackTrace();
}
}
16
Source : WebSocketBaseTest.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
protected Tomcat startServer(final Clreplaced<? extends WsContextListener> configClreplaced) throws LifecycleException {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(configClreplaced.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
return tomcat;
}
16
Source : TestBug49158.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
public static void addServlets(Tomcat tomcat) {
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, path, new TestBug49158Servlet());
ctx.addServletMappingDecoded("/" + path, path);
}
16
Source : TestAbortedUpload.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
@Override
protected void configureAndStartWebApplication() throws LifecycleException {
Tomcat tomcat = getTomcatInstance();
// Retain '/simple' url-pattern since it enables code re-use
Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "abort", new AbortServlet());
ctxt.addServletMappingDecoded("/simple", "abort");
tomcat.start();
}
16
Source : TestApplicationMapping.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doTestMappingForward(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
Tomcat.addServlet(ctx, "Forward", new ForwardServlet());
ctx.addServletMappingDecoded(mapping, "Forward");
Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
ctx.addServletMappingDecoded("/mapping", "Mapping");
tomcat.start();
ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
String body = bc.toString();
replacedert.replacedertTrue(body, body.contains("MatchValue=[mapping]"));
replacedert.replacedertTrue(body, body.contains("Pattern=[/mapping]"));
replacedert.replacedertTrue(body, body.contains("MatchType=[EXACT]"));
replacedert.replacedertTrue(body, body.contains("ServletName=[Mapping]"));
replacedert.replacedertTrue(body, body.contains("ForwardMatchValue=[" + matchValue + "]"));
replacedert.replacedertTrue(body, body.contains("ForwardPattern=[" + mapping + "]"));
replacedert.replacedertTrue(body, body.contains("ForwardMatchType=[" + matchType + "]"));
replacedert.replacedertTrue(body, body.contains("ForwardServletName=[Forward]"));
}
16
Source : TestApplicationMapping.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doTestMappingNamedInclude(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
Tomcat.addServlet(ctx, "Include", new NamedIncludeServlet());
ctx.addServletMappingDecoded(mapping, "Include");
Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
ctx.addServletMappingDecoded("/mapping", "Mapping");
tomcat.start();
ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
String body = bc.toString();
replacedert.replacedertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
replacedert.replacedertTrue(body, body.contains("Pattern=[" + mapping + "]"));
replacedert.replacedertTrue(body, body.contains("MatchType=[" + matchType + "]"));
replacedert.replacedertTrue(body, body.contains("ServletName=[Include]"));
}
16
Source : TestApplicationMapping.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doTestMappingInclude(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
Tomcat.addServlet(ctx, "Include", new IncludeServlet());
ctx.addServletMappingDecoded(mapping, "Include");
Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
ctx.addServletMappingDecoded("/mapping", "Mapping");
tomcat.start();
ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
String body = bc.toString();
replacedert.replacedertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
replacedert.replacedertTrue(body, body.contains("Pattern=[" + mapping + "]"));
replacedert.replacedertTrue(body, body.contains("MatchType=[" + matchType + "]"));
replacedert.replacedertTrue(body, body.contains("ServletName=[Include]"));
replacedert.replacedertTrue(body, body.contains("IncludeMatchValue=[mapping]"));
replacedert.replacedertTrue(body, body.contains("IncludePattern=[/mapping]"));
replacedert.replacedertTrue(body, body.contains("IncludeMatchType=[EXACT]"));
replacedert.replacedertTrue(body, body.contains("IncludeServletName=[Mapping]"));
}
16
Source : TestApplicationMapping.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doTestMappingNamedForward(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
Tomcat.addServlet(ctx, "Forward", new NamedForwardServlet());
ctx.addServletMappingDecoded(mapping, "Forward");
Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
ctx.addServletMappingDecoded("/mapping", "Mapping");
tomcat.start();
ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
String body = bc.toString();
replacedert.replacedertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
replacedert.replacedertTrue(body, body.contains("Pattern=[" + mapping + "]"));
replacedert.replacedertTrue(body, body.contains("MatchType=[" + matchType + "]"));
replacedert.replacedertTrue(body, body.contains("ServletName=[Forward]"));
}
16
Source : TestApplicationMapping.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doTestMappingDirect(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
ctx.addServletMappingDecoded(mapping, "Mapping");
tomcat.start();
ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
String body = bc.toString();
replacedert.replacedertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
replacedert.replacedertTrue(body, body.contains("Pattern=[" + mapping + "]"));
replacedert.replacedertTrue(body, body.contains("MatchType=[" + matchType + "]"));
replacedert.replacedertTrue(body, body.contains("ServletName=[Mapping]"));
}
16
Source : TestApplicationHttpRequest.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doQueryStringTest(String originalQueryString, String forwardQueryString, Map<String, String[]> expected) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
if (forwardQueryString == null) {
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/display"));
} else {
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/display?" + forwardQueryString));
}
ctx.addServletMappingDecoded("/forward", "forward");
Tomcat.addServlet(ctx, "display", new DisplayParameterServlet(expected));
ctx.addServletMappingDecoded("/display", "display");
tomcat.start();
ByteChunk response = new ByteChunk();
StringBuilder target = new StringBuilder("http://localhost:");
target.append(getPort());
target.append("/forward");
if (originalQueryString != null) {
target.append('?');
target.append(originalQueryString);
}
int rc = getUrl(target.toString(), response, null);
replacedert.replacedertEquals(200, rc);
replacedert.replacedertEquals("OK", response.toString());
}
16
Source : TestServletSecurity.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
public void doTestFooAndFooBar(boolean fooFirst) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "Foo", Foo.clreplaced.getName());
ctx.addServletMappingDecoded("/foo/*", "Foo");
Tomcat.addServlet(ctx, "FooBar", FooBar.clreplaced.getName());
ctx.addServletMappingDecoded("/foo/bar/*", "FooBar");
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc;
if (fooFirst) {
rc = getUrl("http://localhost:" + getPort() + "/foo", bc, null, null);
} else {
rc = getUrl("http://localhost:" + getPort() + "/foo/bar", bc, null, null);
}
bc.recycle();
replacedert.replacedertEquals(403, rc);
if (fooFirst) {
rc = getUrl("http://localhost:" + getPort() + "/foo/bar", bc, null, null);
} else {
rc = getUrl("http://localhost:" + getPort() + "/foo", bc, null, null);
}
replacedert.replacedertEquals(403, rc);
}
16
Source : App.java
with MIT License
from ERS-HCL
with MIT License
from ERS-HCL
public static void main(String[] args) throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());
Wrapper servletWrapper = Tomcat.addServlet(ctx, "rnd.web.service.rest.App", "org.glreplacedfish.jersey.servlet.ServletContainer");
servletWrapper.addInitParameter("javax.ws.rs.Application", "rnd.web.service.rest.AppConfig");
ctx.addServletMappingDecoded("/*", "rnd.web.service.rest.App");
tomcat.start();
tomcat.getServer().await();
}
15
Source : TestWebappClassLoaderMemoryLeak.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testTimerThreadLeak() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
if (ctx instanceof StandardContext) {
((StandardContext) ctx).setClearReferencesStopTimerThreads(true);
}
Tomcat.addServlet(ctx, "taskServlet", new TaskServlet());
ctx.addServletMapping("/", "taskServlet");
tomcat.start();
// This will trigger the timer & thread creation
getUrl("http://localhost:" + getPort() + "/");
// Stop the context
ctx.stop();
Thread[] threads = getThreads();
for (Thread thread : threads) {
if (thread != null && thread.isAlive() && TaskServlet.TIMER_THREAD_NAME.equals(thread.getName())) {
thread.join(5000);
if (thread.isAlive()) {
fail("Timer thread still running");
}
}
}
}
15
Source : TestStandardContextResources.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testResources2() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-3.0-fragments");
// app dir is relative to server home
StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
ctx.addServletMapping("/getresource", "getresource");
tomcat.start();
replacedertPageContains("/test/getresource?path=/resourceF.jsp", "<p>resourceF.jsp in resources2.jar</p>");
replacedertPageContains("/test/getresource?path=/resourceA.jsp", "<p>resourceA.jsp in the web application</p>");
replacedertPageContains("/test/getresource?path=/resourceB.jsp", "<p>resourceB.jsp in resources.jar</p>");
replacedertPageContains("/test/getresource?path=/folder/resourceC.jsp", "<p>resourceC.jsp in the web application</p>");
replacedertPageContains("/test/getresource?path=/folder/resourceD.jsp", "<p>resourceD.jsp in resources.jar</p>");
replacedertPageContains("/test/getresource?path=/folder/resourceE.jsp", "<p>resourceE.jsp in the web application</p>");
}
15
Source : TestApplicationHttpRequest.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testParameterImmutability() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/modify"));
ctx.addServletMapping("/forward", "forward");
Tomcat.addServlet(ctx, "modify", new ModifyParameterServlet());
ctx.addServletMapping("/modify", "modify");
tomcat.start();
ByteChunk response = new ByteChunk();
StringBuilder target = new StringBuilder("http://localhost:");
target.append(getPort());
target.append("/forward");
int rc = getUrl(target.toString(), response, null);
replacedert.replacedertEquals(200, rc);
replacedert.replacedertEquals("OK", response.toString());
}
15
Source : TestMimeHeadersIntegration.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void setupHeadersTest(Tomcat tomcat) {
Context ctx = tomcat.addContext("", getTemporaryDirectory().getAbsolutePath());
Tomcat.addServlet(ctx, "servlet", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain; charset=ISO-8859-1");
res.getWriter().write("OK");
}
});
ctx.addServletMappingDecoded("/", "servlet");
alv = new HeaderCountLogValve();
tomcat.getHost().getPipeline().addValve(alv);
}
15
Source : TestStream.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=61120
*/
@Test
public void testPathParam() throws Exception {
enableHttp2();
Tomcat tomcat = getTomcatInstance();
Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
ctxt.addServletMappingDecoded("/simple", "simple");
Tomcat.addServlet(ctxt, "pathparam", new PathParam());
ctxt.addServletMappingDecoded("/pathparam", "pathparam");
tomcat.start();
openClientConnection();
doHttpUpgrade();
sendClientPreface();
validateHttp2InitialResponse();
byte[] frameHeader = new byte[9];
ByteBuffer headersPayload = ByteBuffer.allocate(128);
buildGetRequest(frameHeader, headersPayload, null, 3, "/pathparam;jsessionid=" + PathParam.EXPECTED_SESSION_ID);
writeFrame(frameHeader, headersPayload);
readSimpleGetResponse();
replacedert.replacedertEquals("3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + "3-Header-[content-type]-[text/plain;charset=UTF-8]\n" + "3-Header-[content-length]-[2]\n" + "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n" + "3-Body-2\n" + "3-EndOfStream\n", output.getTrace());
}
15
Source : TestStream.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
@Test
public void testResponseTrailerFields() throws Exception {
enableHttp2();
Tomcat tomcat = getTomcatInstance();
Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
ctxt.addServletMappingDecoded("/simple", "simple");
Tomcat.addServlet(ctxt, "trailers", new ResponseTrailers());
ctxt.addServletMappingDecoded("/trailers", "trailers");
tomcat.start();
openClientConnection();
doHttpUpgrade();
sendClientPreface();
validateHttp2InitialResponse();
byte[] frameHeader = new byte[9];
ByteBuffer headersPayload = ByteBuffer.allocate(128);
buildGetRequest(frameHeader, headersPayload, null, 3, "/trailers");
writeFrame(frameHeader, headersPayload);
// Headers
parser.readFrame(true);
// Body
parser.readFrame(true);
// Trailers
parser.readFrame(true);
replacedert.replacedertEquals("3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + "3-Header-[content-type]-[text/plain;charset=UTF-8]\n" + "3-Header-[content-length]-[44]\n" + "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n" + "3-Body-44\n" + "3-HeadersStart\n" + "3-Header-[x-trailer-2]-[Trailer value two]\n" + "3-Header-[x-trailer-1]-[Trailer value one]\n" + "3-HeadersEnd\n" + "3-EndOfStream\n", output.getTrace());
}
15
Source : TestHttp2UpgradeHandler.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
// https://bz.apache.org/bugzilla/show_bug.cgi?id=60970
@Test
public void testLargeHeader() throws Exception {
enableHttp2();
Tomcat tomcat = getTomcatInstance();
Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
ctxt.addServletMappingDecoded("/simple", "simple");
Tomcat.addServlet(ctxt, "large", new LargeHeaderServlet());
ctxt.addServletMappingDecoded("/large", "large");
tomcat.start();
openClientConnection();
doHttpUpgrade();
sendClientPreface();
validateHttp2InitialResponse();
byte[] frameHeader = new byte[9];
ByteBuffer headersPayload = ByteBuffer.allocate(128);
buildGetRequest(frameHeader, headersPayload, null, 3, "/large");
writeFrame(frameHeader, headersPayload);
// Headers
parser.readFrame(true);
parser.readFrame(true);
// Body
parser.readFrame(true);
replacedert.replacedertEquals("3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + "3-Header-[x-ignore]-[...]\n" + "3-Header-[content-type]-[text/plain;charset=UTF-8]\n" + "3-Header-[content-length]-[2]\n" + "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n" + "3-Body-2\n" + "3-EndOfStream\n", output.getTrace());
}
15
Source : Http2TestBase.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
protected void configureAndStartWebApplication() throws LifecycleException {
Tomcat tomcat = getTomcatInstance();
Context ctxt = tomcat.addContext("", null);
Tomcat.addServlet(ctxt, "empty", new EmptyServlet());
ctxt.addServletMappingDecoded("/empty", "empty");
Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
ctxt.addServletMappingDecoded("/simple", "simple");
Tomcat.addServlet(ctxt, "large", new LargeServlet());
ctxt.addServletMappingDecoded("/large", "large");
Tomcat.addServlet(ctxt, "cookie", new CookieServlet());
ctxt.addServletMappingDecoded("/cookie", "cookie");
Tomcat.addServlet(ctxt, "parameter", new ParameterServlet());
ctxt.addServletMappingDecoded("/parameter", "parameter");
tomcat.start();
}
15
Source : TestHttp11OutputBuffer.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
@Test
public void testSendAck() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "echo", new EchoBodyServlet());
ctx.addServletMappingDecoded("/echo", "echo");
tomcat.start();
ExpectationClient client = new ExpectationClient();
client.setPort(tomcat.getConnector().getLocalPort());
// Expected content doesn't end with a CR-LF so if it isn't chunked make
// sure the content length is used as reading it line-by-line will fail
// since there is no "line".
client.setUseContentLength(true);
client.connect();
client.doRequestHeaders();
replacedert.replacedertTrue(client.isResponse100());
client.doRequestBody();
replacedert.replacedertTrue(client.isResponse200());
replacedert.replacedertTrue(client.isResponseBodyOK());
}
15
Source : TestStandardContextResources.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
@Test
public void testResources2() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp-fragments");
// app dir is relative to server home
StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
skipTldsForResourceJars(ctx);
Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
ctx.addServletMappingDecoded("/getresource", "getresource");
tomcat.start();
replacedertPageContains("/test/getresource?path=/resourceF.jsp", "<p>resourceF.jsp in resources2.jar</p>");
replacedertPageContains("/test/getresource?path=/resourceA.jsp", "<p>resourceA.jsp in the web application</p>");
replacedertPageContains("/test/getresource?path=/resourceB.jsp", "<p>resourceB.jsp in resources.jar</p>");
replacedertPageContains("/test/getresource?path=/folder/resourceC.jsp", "<p>resourceC.jsp in the web application</p>");
replacedertPageContains("/test/getresource?path=/folder/resourceD.jsp", "<p>resourceD.jsp in resources.jar</p>");
replacedertPageContains("/test/getresource?path=/folder/resourceE.jsp", "<p>resourceE.jsp in the web application</p>");
}
15
Source : TestApplicationMapping.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
private void doTestMappingAsync(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
Wrapper w = Tomcat.addServlet(ctx, "Async", new AsyncServlet());
w.setAsyncSupported(true);
ctx.addServletMappingDecoded(mapping, "Async");
Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
ctx.addServletMappingDecoded("/mapping", "Mapping");
tomcat.start();
ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
String body = bc.toString();
replacedert.replacedertTrue(body, body.contains("MatchValue=[mapping]"));
replacedert.replacedertTrue(body, body.contains("Pattern=[/mapping]"));
replacedert.replacedertTrue(body, body.contains("MatchType=[EXACT]"));
replacedert.replacedertTrue(body, body.contains("ServletName=[Mapping]"));
replacedert.replacedertTrue(body, body.contains("AsyncMatchValue=[" + matchValue + "]"));
replacedert.replacedertTrue(body, body.contains("AsyncPattern=[" + mapping + "]"));
replacedert.replacedertTrue(body, body.contains("AsyncMatchType=[" + matchType + "]"));
replacedert.replacedertTrue(body, body.contains("AsyncServletName=[Async]"));
}
15
Source : TestApplicationHttpRequest.java
with GNU General Public License v3.0
from guang19
with GNU General Public License v3.0
from guang19
@Test
public void testParameterImmutability() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "forward", new ForwardServlet("/modify"));
ctx.addServletMappingDecoded("/forward", "forward");
Tomcat.addServlet(ctx, "modify", new ModifyParameterServlet());
ctx.addServletMappingDecoded("/modify", "modify");
tomcat.start();
ByteChunk response = new ByteChunk();
StringBuilder target = new StringBuilder("http://localhost:");
target.append(getPort());
target.append("/forward");
int rc = getUrl(target.toString(), response, null);
replacedert.replacedertEquals(200, rc);
replacedert.replacedertEquals("OK", response.toString());
}
15
Source : TestMaxConnections.java
with MIT License
from chenmudu
with MIT License
from chenmudu
private synchronized void init() throws Exception {
Tomcat tomcat = getTomcatInstance();
StandardContext root = (StandardContext) tomcat.addContext("", SimpleHttpClient.TEMP_DIR);
root.setUnloadDelay(soTimeout);
Tomcat.addServlet(root, "Simple", new SimpleServlet());
root.addServletMappingDecoded("/test", "Simple");
tomcat.getConnector().setProperty("maxKeepAliveRequests", "1");
tomcat.getConnector().setProperty("maxThreads", "10");
tomcat.getConnector().setProperty("soTimeout", "20000");
tomcat.getConnector().setProperty("keepAliveTimeout", "50000");
tomcat.getConnector().setProperty("maxConnections", Integer.toString(MAX_CONNECTIONS));
tomcat.getConnector().setProperty("acceptCount", "1");
tomcat.start();
}
14
Source : TestAbstractAjpProcessor.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
/*
* Bug 55453
*/
@Test
public void test304WithBody() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "bug55453", new Tester304WithBodyServlet());
ctx.addServletMapping("/", "bug55453");
tomcat.start();
SimpleAjpClient ajpClient = new SimpleAjpClient();
ajpClient.setPort(getPort());
ajpClient.connect();
validateCpong(ajpClient.cping());
TesterAjpMessage forwardMessage = ajpClient.createForwardMessage();
forwardMessage.end();
TesterAjpMessage responseHeaders = ajpClient.sendMessage(forwardMessage, null);
// Expect 2 messages: headers, end
validateResponseHeaders(responseHeaders, 304, "Not Modified");
validateResponseEnd(ajpClient.readMessage(), true);
// Double check the connection is still open
validateCpong(ajpClient.cping());
ajpClient.disconnect();
}
14
Source : TestWebappClassLoaderExecutorMemoryLeak.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testTimerThreadLeak() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
if (ctx instanceof StandardContext) {
((StandardContext) ctx).setClearReferencesStopThreads(true);
}
ExecutorServlet executorServlet = new ExecutorServlet();
Tomcat.addServlet(ctx, "taskServlet", executorServlet);
ctx.addServletMapping("/", "taskServlet");
tomcat.start();
// This will trigger the timer & thread creation
getUrl("http://localhost:" + getPort() + "/");
// Stop the context
ctx.stop();
// If the thread still exists, we have a thread/memory leak
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
// ignore
}
replacedert.replacedertTrue(executorServlet.tpe.isShutdown());
replacedert.replacedertTrue(executorServlet.tpe.isTerminated());
}
14
Source : TestStandardContextValve.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testBug51653b() throws Exception {
// Set up a container
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Traces order of events across multiple components
StringBuilder trace = new StringBuilder();
// Add the page that generates the error
Tomcat.addServlet(ctx, "test", new Bug51653ErrorTrigger());
ctx.addServletMapping("/test", "test");
// Add the error page
Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
ctx.addServletMapping("/error", "errorPage");
// And the handling for 404 responses
ErrorPage errorPage = new ErrorPage();
errorPage.setErrorCode(Response.SC_NOT_FOUND);
errorPage.setLocation("/error");
ctx.addErrorPage(errorPage);
// Add the request listener
Bug51653RequestListener reqListener = new Bug51653RequestListener(trace);
((StandardContext) ctx).addApplicationEventListener(reqListener);
tomcat.start();
// Request a page that does not exist
int rc = getUrl("http://localhost:" + getPort() + "/test", new ByteChunk(), null);
// Need to allow time (but not too long in case the test fails) for
// ServletRequestListener to complete
int i = 20;
while (i > 0) {
if (trace.toString().endsWith("Destroy")) {
break;
}
Thread.sleep(250);
i--;
}
replacedertEquals(Response.SC_NOT_FOUND, rc);
replacedertEquals("InitErrorDestroy", trace.toString());
}
14
Source : TestStandardContextValve.java
with Apache License 2.0
from wangyingjie
with Apache License 2.0
from wangyingjie
@Test
public void testBug51653a() throws Exception {
// Set up a container
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Traces order of events across multiple components
StringBuilder trace = new StringBuilder();
// Add the error page
Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
ctx.addServletMapping("/error", "errorPage");
// And the handling for 404 responses
ErrorPage errorPage = new ErrorPage();
errorPage.setErrorCode(Response.SC_NOT_FOUND);
errorPage.setLocation("/error");
ctx.addErrorPage(errorPage);
// Add the request listener
Bug51653RequestListener reqListener = new Bug51653RequestListener(trace);
((StandardContext) ctx).addApplicationEventListener(reqListener);
tomcat.start();
// Request a page that does not exist
int rc = getUrl("http://localhost:" + getPort() + "/invalid", new ByteChunk(), null);
// Need to allow time (but not too long in case the test fails) for
// ServletRequestListener to complete
int i = 20;
while (i > 0) {
if (trace.toString().endsWith("Destroy")) {
break;
}
Thread.sleep(250);
i--;
}
replacedertEquals(Response.SC_NOT_FOUND, rc);
replacedertEquals("InitErrorDestroy", trace.toString());
}
14
Source : 510.java
with MIT License
from masud-technope
with MIT License
from masud-technope
@Test
public void testTimerThreadLeak() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
if (ctx instanceof StandardContext) {
((StandardContext) ctx).setClearReferencesStopThreads(true);
}
ExecutorServlet executorServlet = new ExecutorServlet();
Tomcat.addServlet(ctx, "taskServlet", executorServlet);
ctx.addServletMapping("/", "taskServlet");
tomcat.start();
// This will trigger the timer & thread creation
getUrl("http://localhost:" + getPort() + "/");
// Stop the context
ctx.stop();
// If the thread still exists, we have a thread/memory leak
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
replacedert.replacedertTrue(executorServlet.tpe.isShutdown());
replacedert.replacedertTrue(executorServlet.tpe.isTerminated());
}
See More Examples