Search in sources :

Example 1 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class TldConfig method execute.

/**
 * Scan for and configure all tag library descriptors found in this
 * web application.
 *
 * This supports a Tomcat-specific extension to the TLD search
 * order defined in the JSP spec. It allows tag libraries packaged as JAR
 * files to be shared by web applications by simply dropping them in a
 * location that all web applications have access to (e.g.,
 * <CATALINA_HOME>/lib). It also supports some of the weird and
 * wonderful arrangements present when Tomcat gets embedded.
 *
 * The set of shared JARs to be scanned for TLDs is narrowed down by
 * the <tt>noTldJars</tt> class variable, which contains the names of JARs
 * that are known not to contain any TLDs.
 */
// Context.addApplicationListener(ApplicationListener) is deprecated.
@SuppressWarnings("deprecation")
public void execute() {
    long t1 = System.currentTimeMillis();
    /*
         * Priority order of URIs required by spec is:
         * 1. J2EE platform taglibs - Tomcat doesn't provide these
         * 2. web.xml entries
         * 3. JARS in WEB-INF/lib & TLDs under WEB-INF (equal priority)
         * 4. Additional entries from the container
         * 
         * Keep processing order in sync with o.a.j.compiler.TldLocationsCache
         */
    // Stage 2 - web.xml entries
    tldScanWebXml();
    // Stage 3a - TLDs under WEB-INF (not lib or classes)
    tldScanResourcePaths(WEB_INF);
    // Stages 3b & 4
    JarScanner jarScanner = context.getJarScanner();
    TldJarScannerCallback tldCallBack = new TldJarScannerCallback();
    jarScanner.scan(context.getServletContext(), context.getLoader().getClassLoader(), tldCallBack, noTldJars);
    if (tldCallBack.scanFoundNoTLDs()) {
        log.info(sm.getString("tldConfig.noTldSummary"));
    }
    // Now add all the listeners we found to the listeners for this context
    String[] list = getTldListeners();
    if (log.isDebugEnabled())
        log.debug(sm.getString("tldConfig.addListeners", Integer.valueOf(list.length)));
    for (int i = 0; i < list.length; i++) {
        context.addApplicationListener(new ApplicationListener(list[i], true));
    }
    long t2 = System.currentTimeMillis();
    if (context instanceof StandardContext) {
        ((StandardContext) context).setTldScanTime(t2 - t1);
    }
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) JarScanner(org.apache.tomcat.JarScanner)

Example 2 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class TestWebSocket method testNoUpgrade.

@Test
public void testNoUpgrade() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(TesterEchoServer.Config.class.getName(), false));
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketClient client = new WebSocketClient(getPort());
    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();
    // Make sure we got an error response
    // No upgrade means it is not treated an as upgrade request so a 404 is
    // generated when the request reaches the Default Servlet.s
    String responseLine = client.reader.readLine();
    Assert.assertTrue(responseLine.startsWith("HTTP/1.1 404"));
    // Finished with the socket
    client.close();
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterEchoServer(org.apache.tomcat.websocket.TesterEchoServer) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 3 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class TestWebSocket method testSimple.

@Test
public void testSimple() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(TesterEchoServer.Config.class.getName(), false));
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketClient client = new WebSocketClient(getPort());
    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: keep-alive, upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();
    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    Assert.assertTrue(responseLine.startsWith("HTTP/1.1 101"));
    // Swallow the headers
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        responseHeaderLine = client.reader.readLine();
    }
    // Now we can do WebSocket
    client.sendMessage("foo", false);
    client.sendMessage("foo", true);
    Assert.assertEquals("foofoo", client.readMessage());
    // Finished with the socket
    client.close();
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterEchoServer(org.apache.tomcat.websocket.TesterEchoServer) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 4 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class TestWebSocket method testDetectWrongVersion.

@Test
public void testDetectWrongVersion() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(TesterEchoServer.Config.class.getName(), false));
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketClient client = new WebSocketClient(getPort());
    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 8" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();
    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    Assert.assertTrue(responseLine.startsWith("HTTP/1.1 426"));
    List<String> headerlines = new ArrayList<String>();
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        headerlines.add(responseHeaderLine);
        responseHeaderLine = client.reader.readLine();
    }
    Assert.assertTrue(headerlines.contains("Sec-WebSocket-Version: 13"));
    // Finished with the socket
    client.close();
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterEchoServer(org.apache.tomcat.websocket.TesterEchoServer) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) ArrayList(java.util.ArrayList) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class TestWebSocket method testKey.

@Test
public void testKey() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(TesterEchoServer.Config.class.getName(), false));
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketClient client = new WebSocketClient(getPort());
    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();
    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    Assert.assertTrue(responseLine.startsWith("HTTP/1.1 101"));
    String accept = null;
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        if (responseHeaderLine.startsWith("Sec-WebSocket-Accept: ")) {
            accept = responseHeaderLine.substring(responseHeaderLine.indexOf(':') + 2);
            break;
        }
        responseHeaderLine = client.reader.readLine();
    }
    Assert.assertTrue(accept != null);
    MessageDigest sha1Helper = MessageDigest.getInstance("SHA1");
    sha1Helper.reset();
    sha1Helper.update("TODO".getBytes(B2CConverter.ISO_8859_1));
    String source = Base64.encode(sha1Helper.digest(WS_ACCEPT));
    Assert.assertEquals(source, accept);
    sha1Helper.reset();
    sha1Helper.update("TOD".getBytes(B2CConverter.ISO_8859_1));
    source = Base64.encode(sha1Helper.digest(WS_ACCEPT));
    Assert.assertFalse(source.equals(accept));
    // Finished with the socket
    client.close();
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterEchoServer(org.apache.tomcat.websocket.TesterEchoServer) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) MessageDigest(java.security.MessageDigest) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ApplicationListener (org.apache.catalina.deploy.ApplicationListener)9 InitialContext (javax.naming.InitialContext)5 Context (org.apache.catalina.Context)5 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)5 Tomcat (org.apache.catalina.startup.Tomcat)5 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 TesterEchoServer (org.apache.tomcat.websocket.TesterEchoServer)5 Test (org.junit.Test)5 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)3 ArrayList (java.util.ArrayList)2 MessageDigest (java.security.MessageDigest)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)1 ServletRequestListener (javax.servlet.ServletRequestListener)1 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)1 HttpSessionListener (javax.servlet.http.HttpSessionListener)1 StandardContext (org.apache.catalina.core.StandardContext)1 JarScanner (org.apache.tomcat.JarScanner)1