@Override public void addApplicationListener(ApplicationListener listener) { synchronized (applicationListenersLock) { ApplicationListener results[] = new ApplicationListener[applicationListeners.length + 1]; for (int i = 0; i < applicationListeners.length; i++) { if (listener.equals(applicationListeners[i])) { log.info(sm.getString("standardContext.duplicateListener", listener.getClassName())); return; } results[i] = applicationListeners[i]; } results[applicationListeners.length] = listener; applicationListeners = results; } fireContainerEvent("addApplicationListener", listener); // FIXME - add instance if already started? }
/** * Add a new Listener class name to the set of Listeners * configured for this application. * * @param listener Java class name of a listener class */ @Override public void addApplicationListener(ApplicationListener listener) { synchronized (applicationListenersLock) { ApplicationListener results[] = new ApplicationListener[applicationListeners.length + 1]; for (int i = 0; i < applicationListeners.length; i++) { if (listener.equals(applicationListeners[i])) { log.info(sm.getString( "standardContext.duplicateListener",listener)); return; } results[i] = applicationListeners[i]; } results[applicationListeners.length] = listener; applicationListeners = results; } fireContainerEvent("addApplicationListener", listener); // FIXME - add instance if already started? }
/** * Check that a {@link ServletContextListener} cannot install a * {@link javax.servlet.ServletContainerInitializer}. * @throws Exception */ @Test public void testServletContextListener() throws Exception { Tomcat tomcat = getTomcatInstance(); Context context = tomcat.addContext("/", System.getProperty("java.io.tmpdir")); // SCL2 pretends to be in web.xml, and tries to install a // ServletContainerInitializer. context.addApplicationListener(new ApplicationListener( SCL2.class.getName(), false)); tomcat.start(); //check that the ServletContainerInitializer wasn't initialized. assertFalse(SCL3.initialized); }
/** * Test JNDI is available to ServletContextListeners. */ @Test public void testBug49132() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp org.apache.catalina.Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); // Enable JNDI - it is disabled by default tomcat.enableNaming(); ContextEnvironment environment = new ContextEnvironment(); environment.setType(BUG49132_VALUE.getClass().getName()); environment.setName(BUG49132_NAME); environment.setValue(BUG49132_VALUE); ctx.getNamingResources().addEnvironment(environment); ctx.addApplicationListener(new ApplicationListener( Bug49132Listener.class.getName(), false)); tomcat.start(); assertEquals(LifecycleState.STARTED, ctx.getState()); }
@Test(expected=javax.websocket.DeploymentException.class) public void testConnectToServerEndpointInvalidScheme() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( TesterEchoServer.Config.class.getName(), false)); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ftp://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC)); }
@Test(expected=javax.websocket.DeploymentException.class) public void testConnectToServerEndpointNoHost() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( TesterEchoServer.Config.class.getName(), false)); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + TesterEchoServer.Config.PATH_ASYNC)); }
/** * Return the set of application listener class names configured * for this application. */ @Override public String[] findApplicationListeners() { ArrayList<String> list = new ArrayList<String>(applicationListeners.length); for (ApplicationListener applicationListener: applicationListeners) { list.add(applicationListener.getClassName()); } return list.toArray(new String[list.size()]); }
/** * Remove the specified application listener class from the set of * listeners for this application. * * @param listener Java class name of the listener to be removed */ @Override public void removeApplicationListener(String listener) { synchronized (applicationListenersLock) { // Make sure this listener is currently present int n = -1; for (int i = 0; i < applicationListeners.length; i++) { if (applicationListeners[i].getClassName().equals(listener)) { n = i; break; } } if (n < 0) return; // Remove the specified listener int j = 0; ApplicationListener results[] = new ApplicationListener[applicationListeners.length - 1]; for (int i = 0; i < applicationListeners.length; i++) { if (i != n) results[j++] = applicationListeners[i]; } applicationListeners = results; } // Inform interested listeners fireContainerEvent("removeApplicationListener", listener); // FIXME - behavior if already started? }
private void resetContext() throws Exception { // Restore the original state ( pre reading web.xml in start ) // If you extend this - override this method and make sure to clean up // Don't reset anything that is read from a <Context.../> element since // <Context .../> elements are read at initialisation will not be read // again for this object for (Container child : findChildren()) { removeChild(child); } startupTime = 0; startTime = 0; tldScanTime = 0; // Bugzilla 32867 distributable = false; applicationListeners = new ApplicationListener[0]; applicationEventListenersObjects = new Object[0]; applicationLifecycleListenersObjects = new Object[0]; jspConfigDescriptor = new ApplicationJspConfigDescriptor(); initializers.clear(); createdServlets.clear(); postConstructMethods.clear(); preDestroyMethods.clear(); if(log.isDebugEnabled()) log.debug("resetContext " + getObjectName()); }
@Test public void testNoConnection() 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("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 String responseLine = client.reader.readLine(); assertTrue(responseLine.startsWith("HTTP/1.1 400")); // Finished with the socket client.close(); }
@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(); assertTrue(responseLine.startsWith("HTTP/1.1 404")); // Finished with the socket client.close(); }
/** * Return the set of application listener class names configured for this * application. */ @Override public String[] findApplicationListeners() { ArrayList<String> list = new ArrayList<String>(applicationListeners.length); for (ApplicationListener applicationListener : applicationListeners) { list.add(applicationListener.getClassName()); } return list.toArray(new String[list.size()]); }
/** * Remove the specified application listener class from the set of listeners * for this application. * * @param listener * Java class name of the listener to be removed */ @Override public void removeApplicationListener(String listener) { synchronized (applicationListenersLock) { // Make sure this listener is currently present int n = -1; for (int i = 0; i < applicationListeners.length; i++) { if (applicationListeners[i].getClassName().equals(listener)) { n = i; break; } } if (n < 0) return; // Remove the specified listener int j = 0; ApplicationListener results[] = new ApplicationListener[applicationListeners.length - 1]; for (int i = 0; i < applicationListeners.length; i++) { if (i != n) results[j++] = applicationListeners[i]; } applicationListeners = results; } // Inform interested listeners fireContainerEvent("removeApplicationListener", listener); // FIXME - behavior if already started? }
private void resetContext() throws Exception { // Restore the original state ( pre reading web.xml in start ) // If you extend this - override this method and make sure to clean up // Don't reset anything that is read from a <Context.../> element since // <Context .../> elements are read at initialisation will not be read // again for this object for (Container child : findChildren()) { removeChild(child); } startupTime = 0; startTime = 0; tldScanTime = 0; // Bugzilla 32867 distributable = false; applicationListeners = new ApplicationListener[0]; applicationEventListenersObjects = new Object[0]; applicationLifecycleListenersObjects = new Object[0]; jspConfigDescriptor = new ApplicationJspConfigDescriptor(); initializers.clear(); createdServlets.clear(); postConstructMethods.clear(); preDestroyMethods.clear(); if (log.isDebugEnabled()) log.debug("resetContext " + getObjectName()); }
/** * Remove the specified application listener class from the set of * listeners for this application. * * @param listener Java class name of the listener to be removed */ @Override public void removeApplicationListener(String listener) { synchronized (applicationListenersLock) { // Make sure this welcome file is currently present int n = -1; for (int i = 0; i < applicationListeners.length; i++) { if (applicationListeners[i].getClassName().equals(listener)) { n = i; break; } } if (n < 0) return; // Remove the specified constraint int j = 0; ApplicationListener results[] = new ApplicationListener[applicationListeners.length - 1]; for (int i = 0; i < applicationListeners.length; i++) { if (i != n) results[j++] = applicationListeners[i]; } applicationListeners = results; } // Inform interested listeners fireContainerEvent("removeApplicationListener", listener); // FIXME - behavior if already started? }
@Test public void testNoConnection() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); 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("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 String responseLine = client.reader.readLine(); assertTrue(responseLine.startsWith("HTTP/1.1 400")); // Finished with the socket client.close(); }
@Test public void testNoUpgrade() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); 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(); assertTrue(responseLine.startsWith("HTTP/1.1 404")); // Finished with the socket client.close(); }
@Test public void testBug54096() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp org.apache.catalina.Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); // Enable JNDI - it is disabled by default tomcat.enableNaming(); ContextEnvironment environmentA = new ContextEnvironment(); environmentA.setType(Bug54096EnvA.class.getName()); environmentA.setName(BUG54096_NameA); environmentA.setValue(BUG54096_ValueA); ctx.getNamingResources().addEnvironment(environmentA); ContextEnvironment environmentB = new ContextEnvironment(); environmentB.setType(Bug54096EnvB.class.getName()); environmentB.setName(BUG54096_NameB); environmentB.setValue(BUG54096_ValueB); ctx.getNamingResources().addEnvironment(environmentB); ctx.addApplicationListener(new ApplicationListener( Bug54096Listener.class.getName(), false)); tomcat.start(); assertEquals(LifecycleState.STARTED, ctx.getState()); }
@Test public void testPingPongMessages() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( TesterEchoServer.Config.class.getName(), false)); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider .getWebSocketContainer(); tomcat.start(); Session wsSession = wsContainer.connectToServer( TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder .create().build(), new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC)); CountDownLatch latch = new CountDownLatch(1); TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties() .get("endpoint"); tep.setLatch(latch); PongMessageHandler handler = new PongMessageHandler(latch); wsSession.addMessageHandler(handler); wsSession.getBasicRemote().sendPing(applicationData); boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS); Assert.assertTrue(latchResult); Assert.assertArrayEquals(applicationData.array(), (handler.getMessages().peek()).getApplicationData().array()); }
@Test public void testBug54716() throws Exception { TestUtil.generateMask(); // Set up utility classes Bug54716 server = new Bug54716(); SingletonConfigurator.setInstance(server); ServerConfigListener.setPojoClazz(Bug54716.class); Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( ServerConfigListener.class.getName(), false)); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); tomcat.start(); Client client = new Client(); URI uri = new URI("ws://localhost:" + getPort() + "/"); wsContainer.connectToServer(client, uri); // Server should close the connection after the exception on open. boolean closed = client.waitForClose(5); Assert.assertTrue("Server failed to close connection", closed); }
@Test public void testOnOpenPojoMethod() throws Exception { // Set up utility classes OnOpenServerEndpoint server = new OnOpenServerEndpoint(); SingletonConfigurator.setInstance(server); ServerConfigListener.setPojoClazz(OnOpenServerEndpoint.class); Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( ServerConfigListener.class.getName(), false)); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); tomcat.start(); Client client = new Client(); URI uri = new URI("ws://localhost:" + getPort() + "/"); Session session = wsContainer.connectToServer(client, uri); client.waitForClose(5); Assert.assertTrue(session.isOpen()); }
@Test public void testBug54807() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( Bug54807Config.class.getName(), false)); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); Assert.assertEquals(LifecycleState.STARTED, ctx.getState()); }
@Test public void testConnectToServerEndpoint() throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( TesterEchoServer.Config.class.getName(), false)); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); Session wsSession = wsContainer.connectToServer( TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC)); CountDownLatch latch = new CountDownLatch(1); BasicText handler = new BasicText(latch); wsSession.addMessageHandler(handler); wsSession.getBasicRemote().sendText(MESSAGE_STRING_1); boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS); Assert.assertTrue(latchResult); Queue<String> messages = handler.getMessages(); Assert.assertEquals(1, messages.size()); Assert.assertEquals(MESSAGE_STRING_1, messages.peek()); }
private void doMaxMessageSize(String path, long size, boolean expectOpen) throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(new ApplicationListener( TesterEchoServer.Config.class.getName(), false)); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); Session s = connectToEchoServer(wsContainer, EndpointA.class, path); StringBuilder msg = new StringBuilder(); for (long i = 0; i < size; i++) { msg.append('x'); } s.getBasicRemote().sendText(msg.toString()); // Wait for up to 5 seconds for session to close boolean open = s.isOpen(); int count = 0; while (open != expectOpen && count < 50) { Thread.sleep(100); count++; open = s.isOpen(); } Assert.assertEquals(Boolean.valueOf(expectOpen), Boolean.valueOf(s.isOpen())); }