org.mortbay.jetty.handler.ContextHandler Java Examples

The following examples show how to use org.mortbay.jetty.handler.ContextHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JettyLauncher.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server();

    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(PORT);
    server.addConnector(connector);
    server.setStopAtShutdown(true);

    // the orders of handlers is very important!
    ContextHandler contextHandler = new ContextHandler();
    contextHandler.setContextPath("/reports");
    contextHandler.setResourceBase("./reports/");
    contextHandler.addHandler(new ResourceHandler());
    server.addHandler(contextHandler);

    server.addHandler(new WebAppContext("webapp", "/nextreports-server"));

    long t = System.currentTimeMillis();
    server.start();
    t = System.currentTimeMillis() - t;
    String version = server.getClass().getPackage().getImplementationVersion();
    System.out.println("Started Jetty Server " + version + " on port " + PORT + " in " + t / 1000 + "s");
    
    server.join();
}
 
Example #2
Source File: TestProtocolHttpClient.java    From nutch-htmlunit with Apache License 2.0 6 votes vote down vote up
protected void setUp() throws Exception {

    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setResourceBase(RES_DIR);
    ServletHandler sh = new ServletHandler();
    sh.addServlet("org.apache.jasper.servlet.JspServlet", "*.jsp");
    context.addHandler(sh);
    context.addHandler(new SessionHandler());

    server = new Server();
    server.addHandler(context);

    conf = new Configuration();
    conf.addResource("nutch-default.xml");
    conf.addResource("nutch-site-test.xml");
    
    http = new Http();
    http.setConf(conf);
  }
 
Example #3
Source File: TestProtocolHttpClient.java    From anthelion with Apache License 2.0 6 votes vote down vote up
protected void setUp() throws Exception {

    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setResourceBase(RES_DIR);
    ServletHandler sh = new ServletHandler();
    sh.addServlet("org.apache.jasper.servlet.JspServlet", "*.jsp");
    context.addHandler(sh);
    context.addHandler(new SessionHandler());

    server = new Server();
    server.addHandler(context);

    conf = new Configuration();
    conf.addResource("nutch-default.xml");
    conf.addResource("nutch-site-test.xml");
    
    http = new Http();
    http.setConf(conf);
  }
 
Example #4
Source File: AbstractServerMojo.java    From opoopress with Apache License 2.0 5 votes vote down vote up
protected Server createJettyServer(Site site){
    Server server = new Server();
    //FIXME
    server.setStopAtShutdown(true);

    Connector defaultConnector = createConnector(null, port);
    server.setConnectors( new Connector[] { defaultConnector } );

    String root = site.getRoot();
    String resourceBase = site.getDestination().getPath();
    getLog().info("Server resource base: " + resourceBase);

    if("".equals(root)){
        ResourceHandler resourceHandler = new ResourceHandler();
        //resourceHandler.setDirectoriesListed(true);
        resourceHandler.setWelcomeFiles(new String[]{"index.html"});

        resourceHandler.setResourceBase(resourceBase/*site.getDestination().getPath()*/);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resourceHandler, new DefaultHandler()});
        server.setHandler(handlers);
        //server.setHandlers(new Handler[]{handlers, logHandler});
        //getLog().info( "Startisng preview server on http://localhost:" + port + "/" );
    } else {
        getLog().info("Using " + ContextHandler.class.getName());
        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath(root);
        contextHandler.setHandler(new ResourceHandler());
        contextHandler.setResourceBase(resourceBase/*site.getDestination().getPath()*/);
        //server.setHandler(contextHandler);
        server.setHandlers(new Handler[]{contextHandler, new DefaultHandler()});
        //server.setHandlers(new Handler[]{contextHandler, logHandler});
        //log.info( "Starting preview server on http://localhost:" + port + root );
    }
    return server;
}
 
Example #5
Source File: CrawlDBTestUtil.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new JettyServer with one static root context
 * 
 * @param port port to listen to
 * @param staticContent folder where static content lives
 * @throws UnknownHostException 
 */
public static Server getServer(int port, String staticContent) throws UnknownHostException{
  Server webServer = new org.mortbay.jetty.Server();
  SocketConnector listener = new SocketConnector();
  listener.setPort(port);
  listener.setHost("127.0.0.1");
  webServer.addConnector(listener);
  ContextHandler staticContext = new ContextHandler();
  staticContext.setContextPath("/");
  staticContext.setResourceBase(staticContent);
  staticContext.addHandler(new ResourceHandler());
  webServer.addHandler(staticContext);
  return webServer;
}
 
Example #6
Source File: HttpServer.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Infer the mime type for the response based on the extension of the request
 * URI. Returns null if unknown.
 */
private String inferMimeType(ServletRequest request) {
  String path = ((HttpServletRequest)request).getRequestURI();
  ContextHandler.SContext sContext = (ContextHandler.SContext)config.getServletContext();
  MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
  Buffer mimeBuffer = mimes.getMimeByExtension(path);
  return (mimeBuffer == null) ? null : mimeBuffer.toString();
}
 
Example #7
Source File: HttpServer2.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Infer the mime type for the response based on the extension of the request
 * URI. Returns null if unknown.
 */
private String inferMimeType(ServletRequest request) {
  String path = ((HttpServletRequest)request).getRequestURI();
  ContextHandler.SContext sContext = (ContextHandler.SContext)config.getServletContext();
  MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
  Buffer mimeBuffer = mimes.getMimeByExtension(path);
  return (mimeBuffer == null) ? null : mimeBuffer.toString();
}
 
Example #8
Source File: HttpServer.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Infer the mime type for the response based on the extension of the request
 * URI. Returns null if unknown.
 */
private String inferMimeType(ServletRequest request) {
  String path = ((HttpServletRequest)request).getRequestURI();
  ContextHandler.SContext sContext = (ContextHandler.SContext)config.getServletContext();
  MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
  Buffer mimeBuffer = mimes.getMimeByExtension(path);
  return (mimeBuffer == null) ? null : mimeBuffer.toString();
}
 
Example #9
Source File: HttpServer2.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Infer the mime type for the response based on the extension of the request
 * URI. Returns null if unknown.
 */
private String inferMimeType(ServletRequest request) {
  String path = ((HttpServletRequest)request).getRequestURI();
  ContextHandler.SContext sContext = (ContextHandler.SContext)config.getServletContext();
  MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
  Buffer mimeBuffer = mimes.getMimeByExtension(path);
  return (mimeBuffer == null) ? null : mimeBuffer.toString();
}
 
Example #10
Source File: HttpServer.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Infer the mime type for the response based on the extension of the 
 * request URI. Returns null if unknown.
 */
private String inferMimeType(ServletRequest request) {
  String path = ((HttpServletRequest)request).getRequestURI();
  ContextHandler.SContext sContext = 
    (ContextHandler.SContext)config.getServletContext();
  MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
  Buffer mimeBuffer = mimes.getMimeByExtension(path);
  return (mimeBuffer == null) ? null : mimeBuffer.toString();
}
 
Example #11
Source File: CrawlDBTestUtil.java    From anthelion with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new JettyServer with one static root context
 * 
 * @param port port to listen to
 * @param staticContent folder where static content lives
 * @throws UnknownHostException 
 */
public static Server getServer(int port, String staticContent) throws UnknownHostException{
  Server webServer = new org.mortbay.jetty.Server();
  SocketConnector listener = new SocketConnector();
  listener.setPort(port);
  listener.setHost("127.0.0.1");
  webServer.addConnector(listener);
  ContextHandler staticContext = new ContextHandler();
  staticContext.setContextPath("/");
  staticContext.setResourceBase(staticContent);
  staticContext.addHandler(new ResourceHandler());
  webServer.addHandler(staticContext);
  return webServer;
}
 
Example #12
Source File: JettyRun.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setContextHandlers(ContextHandler[] contextHandlers) {
    this.contextHandlers = contextHandlers;
}
 
Example #13
Source File: JettyRun.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setContextHandlers(ContextHandler[] contextHandlers) {
    this.contextHandlers = contextHandlers;
}
 
Example #14
Source File: JettyRun.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ContextHandler[] getConfiguredContextHandlers() {
    return this.contextHandlers;
}
 
Example #15
Source File: JettyRun.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ContextHandler[] getConfiguredContextHandlers() {
    return this.contextHandlers;
}
 
Example #16
Source File: JettyRun.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ContextHandler[] getConfiguredContextHandlers() {
    return this.contextHandlers;
}
 
Example #17
Source File: ExhibitorMain.java    From exhibitor with Apache License 2.0 4 votes vote down vote up
private void addSecurityFile(HashUserRealm realm, String securityFile, Context root) throws Exception
{
    // create a temp Jetty context to parse the security portion of the web.xml file

    /*
        TODO

        This code assumes far too much internal knowledge of Jetty. I don't know
        of simple way to parse the web.xml though and don't want to write it myself.
     */

    final URL url = new URL("file", null, securityFile);
    final WebXmlConfiguration webXmlConfiguration = new WebXmlConfiguration();
    WebAppContext context = new WebAppContext();
    context.setServer(server);
    webXmlConfiguration.setWebAppContext(context);
    ContextHandler contextHandler = new ContextHandler("/")
    {
        @Override
        protected void startContext() throws Exception
        {
            super.startContext();
            setServer(server);
            webXmlConfiguration.configure(url.toString());
        }
    };
    contextHandler.start();
    try
    {
        SecurityHandler securityHandler = webXmlConfiguration.getWebAppContext().getSecurityHandler();

        if ( realm != null )
        {
            securityHandler.setUserRealm(realm);
        }

        root.setSecurityHandler(securityHandler);
    }
    finally
    {
        contextHandler.stop();
    }
}
 
Example #18
Source File: JettyRun.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setContextHandlers(ContextHandler[] contextHandlers) {
    this.contextHandlers = contextHandlers;
}
 
Example #19
Source File: JettyRun.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ContextHandler[] getConfiguredContextHandlers() {
    return this.contextHandlers;
}
 
Example #20
Source File: JettyRun.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setContextHandlers(ContextHandler[] contextHandlers) {
    this.contextHandlers = contextHandlers;
}