org.mortbay.jetty.handler.ResourceHandler Java Examples

The following examples show how to use org.mortbay.jetty.handler.ResourceHandler. 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: Main.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
	Server server = new Server(PORT);
	
	// Static file handler
	Context fileContext = new Context(server, "/mxgraph", Context.SESSIONS);
	ResourceHandler fileHandler = new ResourceHandler();
	fileHandler.setResourceBase(".");
	fileContext.setHandler(fileHandler);

	// Servlets
	Context context = new Context(server, "/", Context.SESSIONS);
	context.addServlet(new ServletHolder(new Roundtrip()), "/Roundtrip");
	context.addServlet(new ServletHolder(new ServerView()), "/ServerView");
	context.addServlet(new ServletHolder(new ExportServlet()), "/Export");
	context.addServlet(new ServletHolder(new EchoServlet()), "/Echo");
	context.addServlet(new ServletHolder(new Deploy()), "/Deploy");
	context.addServlet(new ServletHolder(new Link()), "/Link");
	context.addServlet(new ServletHolder(new EmbedImage()), "/EmbedImage");
	context.addServlet(new ServletHolder(new Backend()), "/Backend");

	HandlerList handlers = new HandlerList();
	handlers.setHandlers(new Handler[] { new RedirectHandler(),
			fileContext, context, new DefaultHandler() });
	server.setHandler(handlers);

	System.out.println("Go to http://localhost:" + PORT + "/");
	
	server.start();
	server.join();
}
 
Example #3
Source File: LocalServerWebDriverContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Before
public void setupLocalServer() throws Exception {

    // Set up a local Jetty HTTP server
    Server server = new Server();
    server.addConnector(new SocketConnector());
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase("src/test/resources/server");
    server.addHandler(resourceHandler);
    server.start();

    // The server will have a random port assigned, so capture that
    localPort = server.getConnectors()[0].getLocalPort();
}
 
Example #4
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 #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: 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 #7
Source File: SLSWebApp.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public void start() throws Exception {
  // static files
  final ResourceHandler staticHandler = new ResourceHandler();
  staticHandler.setResourceBase("html");

  Handler handler = new AbstractHandler() {
    @Override
    public void handle(String target, HttpServletRequest request,
                       HttpServletResponse response, int dispatch) {
      try{
        // timeunit
        int timeunit = 1000;   // second, divide millionsecond / 1000
        String timeunitLabel = "second";
        if (request.getParameter("u")!= null &&
                request.getParameter("u").equalsIgnoreCase("m")) {
          timeunit = 1000 * 60;
          timeunitLabel = "minute";
        }

        // http request
        if (target.equals("/")) {
          printPageIndex(request, response);
        } else if (target.equals("/simulate")) {
          printPageSimulate(request, response, timeunit, timeunitLabel);
        } else if (target.equals("/track")) {
          printPageTrack(request, response, timeunit, timeunitLabel);
        } else
          // js/css request
          if (target.startsWith("/js") || target.startsWith("/css")) {
            response.setCharacterEncoding("utf-8");
            staticHandler.handle(target, request, response, dispatch);
          } else
            // json request
            if (target.equals("/simulateMetrics")) {
              printJsonMetrics(request, response);
            } else if (target.equals("/trackMetrics")) {
              printJsonTrack(request, response);
            }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  };

  server = new Server(port);
  server.setHandler(handler);

  server.start();
}
 
Example #8
Source File: SLSWebApp.java    From big-c with Apache License 2.0 4 votes vote down vote up
public void start() throws Exception {
  // static files
  final ResourceHandler staticHandler = new ResourceHandler();
  staticHandler.setResourceBase("html");

  Handler handler = new AbstractHandler() {
    @Override
    public void handle(String target, HttpServletRequest request,
                       HttpServletResponse response, int dispatch) {
      try{
        // timeunit
        int timeunit = 1000;   // second, divide millionsecond / 1000
        String timeunitLabel = "second";
        if (request.getParameter("u")!= null &&
                request.getParameter("u").equalsIgnoreCase("m")) {
          timeunit = 1000 * 60;
          timeunitLabel = "minute";
        }

        // http request
        if (target.equals("/")) {
          printPageIndex(request, response);
        } else if (target.equals("/simulate")) {
          printPageSimulate(request, response, timeunit, timeunitLabel);
        } else if (target.equals("/track")) {
          printPageTrack(request, response, timeunit, timeunitLabel);
        } else
          // js/css request
          if (target.startsWith("/js") || target.startsWith("/css")) {
            response.setCharacterEncoding("utf-8");
            staticHandler.handle(target, request, response, dispatch);
          } else
            // json request
            if (target.equals("/simulateMetrics")) {
              printJsonMetrics(request, response);
            } else if (target.equals("/trackMetrics")) {
              printJsonTrack(request, response);
            }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  };

  server = new Server(port);
  server.setHandler(handler);

  server.start();
}