Search in sources :

Example 81 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestServletAnnotations method testWebServletAnnotationIgnore.

@Test
public void testWebServletAnnotationIgnore() throws Exception {
    //an existing servlet OF THE SAME NAME has even 1 non-default mapping we can't use
    //any of the url mappings in the annotation
    WebAppContext wac = new WebAppContext();
    ServletHolder servlet = new ServletHolder();
    servlet.setClassName("org.eclipse.jetty.servlet.OtherDServlet");
    servlet.setName("DServlet");
    wac.getServletHandler().addServlet(servlet);
    ServletMapping m = new ServletMapping();
    m.setPathSpec("/default");
    m.setDefault(true);
    m.setServletName("DServlet");
    wac.getServletHandler().addServletMapping(m);
    ServletMapping m2 = new ServletMapping();
    m2.setPathSpec("/other");
    m2.setServletName("DServlet");
    wac.getServletHandler().addServletMapping(m2);
    WebServletAnnotation annotation = new WebServletAnnotation(wac, "org.eclipse.jetty.annotations.ServletD", null);
    annotation.apply();
    ServletMapping[] resultMappings = wac.getServletHandler().getServletMappings();
    assertEquals(2, resultMappings.length);
    for (ServletMapping r : resultMappings) {
        assertEquals(1, r.getPathSpecs().length);
        if (!r.getPathSpecs()[0].equals("/default") && !r.getPathSpecs()[0].equals("/other"))
            fail("Unexpected path in mapping");
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ServletMapping(org.eclipse.jetty.servlet.ServletMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Test(org.junit.Test)

Example 82 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class TestResourceAnnotations method init.

@Before
public void init() throws Exception {
    server = new Server();
    wac = new WebAppContext();
    wac.setServer(server);
    injections = new InjectionCollection();
    wac.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
    InitialContext ic = new InitialContext();
    comp = (Context) ic.lookup("java:comp");
    env = comp.createSubcontext("env");
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) InjectionCollection(org.eclipse.jetty.plus.annotation.InjectionCollection) InitialContext(javax.naming.InitialContext) Before(org.junit.Before)

Example 83 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class OneWebApp method main.

public static void main(String[] args) throws Exception {
    // Create a basic jetty server object that will listen on port 8080.
    // Note that if you set this to port 0 then a randomly available port
    // will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    // The WebAppContext is the entity that controls the environment in
    // which a web application lives and breathes. In this example the
    // context path is being set to "/" so it is suitable for serving root
    // context requests and then we see it setting the location of the war.
    // A whole host of other configurations are available, ranging from
    // configuring to support annotation scanning in the webapp (through
    // PlusConfiguration) to choosing where the webapp will unpack itself.
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    File warFile = new File("../../tests/test-jmx/jmx-webapp/target/jmx-webapp");
    webapp.setWar(warFile.getAbsolutePath());
    // A WebAppContext is a ContextHandler as well so it needs to be set to
    // the server so it is aware of where to send the appropriate requests.
    server.setHandler(webapp);
    // Start things up! 
    server.start();
    // The use of server.join() the will make the current thread join and
    // wait until the server is done executing.
    // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
    server.join();
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) File(java.io.File)

Example 84 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class AnnotatedServerEndpointTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    File testdir = MavenTestingUtils.getTargetTestingDir(AnnotatedServerEndpointTest.class.getName());
    server = new WSServer(testdir, "app");
    server.createWebInf();
    server.copyEndpoint(ConfiguredEchoSocket.class);
    server.copyClass(EchoSocketConfigurator.class);
    server.copyClass(DateDecoder.class);
    server.copyClass(TimeEncoder.class);
    server.start();
    WebAppContext webapp = server.createWebAppContext();
    server.deployWebapp(webapp);
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 85 with WebAppContext

use of org.eclipse.jetty.webapp.WebAppContext in project jetty.project by eclipse.

the class LargeAnnotatedTest method testEcho.

@Test
public void testEcho() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.createWebInf();
    wsb.copyEndpoint(LargeEchoConfiguredSocket.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        // wsb.dump();
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.getPolicy().setMaxTextMessageSize(128 * 1024);
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> foo = client.connect(clientEcho, uri.resolve("echo/large"));
            // wait for connect
            foo.get(1, TimeUnit.SECONDS);
            // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
            byte[] txt = new byte[100 * 1024];
            Arrays.fill(txt, (byte) 'o');
            String msg = new String(txt, StandardCharsets.UTF_8);
            clientEcho.sendMessage(msg);
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", msg, msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

WebAppContext (org.eclipse.jetty.webapp.WebAppContext)142 Server (org.eclipse.jetty.server.Server)58 File (java.io.File)37 Test (org.junit.Test)29 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)20 ServerConnector (org.eclipse.jetty.server.ServerConnector)18 URL (java.net.URL)16 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 URI (java.net.URI)10 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)9 FileWriter (java.io.FileWriter)7 Configuration (org.apache.hadoop.conf.Configuration)7 HashLoginService (org.eclipse.jetty.security.HashLoginService)7 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)7 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)7 BeforeClass (org.junit.BeforeClass)7 OutputStream (java.io.OutputStream)6 InitialContext (javax.naming.InitialContext)6