Search in sources :

Example 71 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.

the class SplitFileServer method main.

public static void main(String[] args) throws Exception {
    // Create the Server object and a corresponding ServerConnector and then
    // set the port for the connector. In this example the server will
    // listen on port 8090. If you set this to port 0 then when the server
    // has been started you can called connector.getLocalPort() to
    // programmatically get the port the server started on.
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(8090);
    server.setConnectors(new Connector[] { connector });
    // Create a Context Handler and ResourceHandler. The ContextHandler is
    // getting set to "/" path but this could be anything you like for
    // builing out your url. Note how we are setting the ResourceBase using
    // our jetty maven testing utilities to get the proper resource
    // directory, you needn't use these, you simply need to supply the paths
    // you are looking to serve content from.
    ResourceHandler rh0 = new ResourceHandler();
    ContextHandler context0 = new ContextHandler();
    context0.setContextPath("/");
    File dir0 = MavenTestingUtils.getTestResourceDir("dir0");
    context0.setBaseResource(Resource.newResource(dir0));
    context0.setHandler(rh0);
    // Rinse and repeat the previous item, only specifying a different
    // resource base.
    ResourceHandler rh1 = new ResourceHandler();
    ContextHandler context1 = new ContextHandler();
    context1.setContextPath("/");
    File dir1 = MavenTestingUtils.getTestResourceDir("dir1");
    context1.setBaseResource(Resource.newResource(dir1));
    context1.setHandler(rh1);
    // Create a ContextHandlerCollection and set the context handlers to it.
    // This will let jetty process urls against the declared contexts in
    // order to match up content.
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { context0, context1 });
    server.setHandler(contexts);
    // Start things up! 
    server.start();
    // Dump the server state
    System.out.println(server.dump());
    // 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 : ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) File(java.io.File)

Example 72 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.

the class JaspiTest method before.

@Before
public void before() throws Exception {
    System.setProperty("org.apache.geronimo.jaspic.configurationFile", "src/test/resources/jaspi.xml");
    _server = new Server();
    _connector = new LocalConnector(_server);
    _server.addConnector(_connector);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    _server.setHandler(contexts);
    TestLoginService loginService = new TestLoginService("TestRealm");
    loginService.putUser("user", new Password("password"), new String[] { "users" });
    loginService.putUser("admin", new Password("secret"), new String[] { "users", "admins" });
    _server.addBean(loginService);
    ContextHandler context = new ContextHandler();
    contexts.addHandler(context);
    context.setContextPath("/ctx");
    JaspiAuthenticatorFactory jaspiAuthFactory = new JaspiAuthenticatorFactory();
    ConstraintSecurityHandler security = new ConstraintSecurityHandler();
    context.setHandler(security);
    security.setAuthenticatorFactory(jaspiAuthFactory);
    // security.setAuthenticator(new BasicAuthenticator());
    Constraint constraint = new Constraint("All", "users");
    constraint.setAuthenticate(true);
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec("/jaspi/*");
    mapping.setConstraint(constraint);
    security.addConstraintMapping(mapping);
    TestHandler handler = new TestHandler();
    security.setHandler(handler);
    ContextHandler other = new ContextHandler();
    contexts.addHandler(other);
    other.setContextPath("/other");
    ConstraintSecurityHandler securityOther = new ConstraintSecurityHandler();
    other.setHandler(securityOther);
    securityOther.setAuthenticatorFactory(jaspiAuthFactory);
    securityOther.addConstraintMapping(mapping);
    securityOther.setHandler(new TestHandler());
    _server.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Server(org.eclipse.jetty.server.Server) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) LocalConnector(org.eclipse.jetty.server.LocalConnector) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) Password(org.eclipse.jetty.util.security.Password) Before(org.junit.Before)

Example 73 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.

the class PartialRFC2616Test method init.

@Before
public void init() throws Exception {
    server = new Server();
    connector = new LocalConnector(server);
    connector.setIdleTimeout(10000);
    server.addConnector(connector);
    ContextHandler vcontext = new ContextHandler();
    vcontext.setContextPath("/");
    vcontext.setVirtualHosts(new String[] { "VirtualHost" });
    vcontext.setHandler(new DumpHandler("Virtual Dump"));
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setHandler(new DumpHandler());
    HandlerCollection collection = new HandlerCollection();
    collection.setHandlers(new Handler[] { vcontext, context });
    server.setHandler(collection);
    server.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Before(org.junit.Before)

Example 74 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.

the class RequestTest method testBadMultiPart.

@Test
public void testBadMultiPart() throws Exception {
    //a bad multipart where one of the fields has no name
    final File testTmpDir = File.createTempFile("badmptest", null);
    if (testTmpDir.exists())
        testTmpDir.delete();
    testTmpDir.mkdir();
    testTmpDir.deleteOnExit();
    assertTrue(testTmpDir.list().length == 0);
    ContextHandler contextHandler = new ContextHandler();
    contextHandler.setContextPath("/foo");
    contextHandler.setResourceBase(".");
    contextHandler.setHandler(new BadMultiPartRequestHandler(testTmpDir));
    contextHandler.addEventListener(new MultiPartCleanerListener() {

        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            MultiPartInputStreamParser m = (MultiPartInputStreamParser) sre.getServletRequest().getAttribute(Request.__MULTIPART_INPUT_STREAM);
            ContextHandler.Context c = (ContextHandler.Context) sre.getServletRequest().getAttribute(Request.__MULTIPART_CONTEXT);
            assertNotNull(m);
            assertNotNull(c);
            assertTrue(c == sre.getServletContext());
            super.requestDestroyed(sre);
            String[] files = testTmpDir.list();
            assertTrue(files.length == 0);
        }
    });
    _server.stop();
    _server.setHandler(contextHandler);
    _server.start();
    String multipart = "--AaB03x\r\n" + "content-disposition: form-data; name=\"xxx\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data;  filename=\"foo.upload\"\r\n" + "Content-Type: text/plain;charset=ISO-8859-1\r\n" + "\r\n" + "000000000000000000000000000000000000000000000000000\r\n" + "--AaB03x--\r\n";
    String request = "GET /foo/x.html HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: multipart/form-data; boundary=\"AaB03x\"\r\n" + "Content-Length: " + multipart.getBytes().length + "\r\n" + "Connection: close\r\n" + "\r\n" + multipart;
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        String responses = _connector.getResponse(request);
        //System.err.println(responses);
        assertTrue(responses.startsWith("HTTP/1.1 500"));
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletRequestEvent(javax.servlet.ServletRequestEvent) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) File(java.io.File) Test(org.junit.Test)

Example 75 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.

the class ResponseTest method testLocale.

@Test
public void testLocale() throws Exception {
    Response response = getResponse();
    ContextHandler context = new ContextHandler();
    context.addLocaleEncoding(Locale.ENGLISH.toString(), "ISO-8859-1");
    context.addLocaleEncoding(Locale.ITALIAN.toString(), "ISO-8859-2");
    response.getHttpChannel().getRequest().setContext(context.getServletContext());
    response.setLocale(java.util.Locale.ITALIAN);
    assertEquals(null, response.getContentType());
    response.setContentType("text/plain");
    assertEquals("text/plain;charset=ISO-8859-2", response.getContentType());
    response.recycle();
    response.setContentType("text/plain");
    response.setCharacterEncoding("utf-8");
    response.setLocale(java.util.Locale.ITALIAN);
    assertEquals("text/plain;charset=utf-8", response.getContentType());
    assertTrue(response.toString().indexOf("charset=utf-8") > 0);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Test(org.junit.Test)

Aggregations

ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)127 Server (org.eclipse.jetty.server.Server)47 ServerConnector (org.eclipse.jetty.server.ServerConnector)27 URI (java.net.URI)21 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)20 Test (org.junit.Test)20 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)19 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)19 IOException (java.io.IOException)18 Handler (org.eclipse.jetty.server.Handler)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)14 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)14 File (java.io.File)13 ServletException (javax.servlet.ServletException)13 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)13 BeforeClass (org.junit.BeforeClass)11 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)10 Config (com.ctrip.framework.apollo.Config)10 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)10 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)10