Search in sources :

Example 36 with Server

use of org.mortbay.jetty.Server in project nutch by apache.

the class ProxyTestbed method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.err.println("TestbedProxy [-seg <segment_name> | -segdir <segments>] [-port <nnn>] [-forward] [-fake] [-delay nnn] [-debug]");
        System.err.println("-seg <segment_name>\tpath to a single segment (can be specified multiple times)");
        System.err.println("-segdir <segments>\tpath to a parent directory of multiple segments (as above)");
        System.err.println("-port <nnn>\trun the proxy on port <nnn> (special permissions may be needed for ports < 1024)");
        System.err.println("-forward\tif specified, requests to all unknown urls will be passed to");
        System.err.println("\t\toriginal servers. If false (default) unknown urls generate 404 Not Found.");
        System.err.println("-delay\tdelay every response by nnn seconds. If delay is negative use a random value up to nnn");
        System.err.println("-fake\tif specified, requests to all unknown urls will succeed with fake content");
        System.exit(-1);
    }
    Configuration conf = NutchConfiguration.create();
    int port = conf.getInt("segment.proxy.port", 8181);
    boolean forward = false;
    boolean fake = false;
    boolean delay = false;
    boolean debug = false;
    int delayVal = 0;
    HashSet<Path> segs = new HashSet<Path>();
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-segdir")) {
            FileSystem fs = FileSystem.get(conf);
            FileStatus[] fstats = fs.listStatus(new Path(args[++i]));
            Path[] paths = HadoopFSUtil.getPaths(fstats);
            segs.addAll(Arrays.asList(paths));
        } else if (args[i].equals("-port")) {
            port = Integer.parseInt(args[++i]);
        } else if (args[i].equals("-forward")) {
            forward = true;
        } else if (args[i].equals("-delay")) {
            delay = true;
            delayVal = Integer.parseInt(args[++i]);
        } else if (args[i].equals("-fake")) {
            fake = true;
        } else if (args[i].equals("-debug")) {
            debug = true;
        } else if (args[i].equals("-seg")) {
            segs.add(new Path(args[++i]));
        } else {
            LOG.error("Unknown argument: " + args[i]);
            System.exit(-1);
        }
    }
    // Create the server
    Server server = new Server();
    SocketConnector connector = new SocketConnector();
    connector.setPort(port);
    connector.setResolveNames(false);
    server.addConnector(connector);
    // create a list of handlers
    HandlerList list = new HandlerList();
    server.addHandler(list);
    if (debug) {
        LOG.info("* Added debug handler.");
        list.addHandler(new LogDebugHandler());
    }
    if (delay) {
        LOG.info("* Added delay handler: " + (delayVal < 0 ? "random delay up to " + (-delayVal) : "constant delay of " + delayVal));
        list.addHandler(new DelayHandler(delayVal));
    }
    // XXX alternatively, we can add the DispatchHandler as the first one,
    // XXX to activate handler plugins and redirect requests to appropriate
    // XXX handlers ... Here we always load these handlers
    Iterator<Path> it = segs.iterator();
    while (it.hasNext()) {
        Path p = it.next();
        try {
            SegmentHandler segment = new SegmentHandler(conf, p);
            list.addHandler(segment);
            LOG.info("* Added segment handler for: " + p);
        } catch (Exception e) {
            LOG.warn("Skipping segment '" + p + "': " + StringUtils.stringifyException(e));
        }
    }
    if (forward) {
        LOG.info("* Adding forwarding proxy for all unknown urls ...");
        ServletHandler servlets = new ServletHandler();
        servlets.addServletWithMapping(AsyncProxyServlet.class, "/*");
        servlets.addFilterWithMapping(LogDebugHandler.class, "/*", Handler.ALL);
        list.addHandler(servlets);
    }
    if (fake) {
        LOG.info("* Added fake handler for remaining URLs.");
        list.addHandler(new FakeHandler());
    }
    list.addHandler(new NotFoundHandler());
    // Start the http server
    server.start();
    server.join();
}
Also used : Path(org.apache.hadoop.fs.Path) HandlerList(org.mortbay.jetty.handler.HandlerList) ServletHandler(org.mortbay.jetty.servlet.ServletHandler) FileStatus(org.apache.hadoop.fs.FileStatus) NutchConfiguration(org.apache.nutch.util.NutchConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Server(org.mortbay.jetty.Server) FileSystem(org.apache.hadoop.fs.FileSystem) SocketConnector(org.mortbay.jetty.bio.SocketConnector) HashSet(java.util.HashSet)

Example 37 with Server

use of org.mortbay.jetty.Server in project wso2-axis2-transports by wso2.

the class JettyServer method setUp.

@Setup
@SuppressWarnings("unused")
private void setUp(HttpTestEnvironment env) throws Exception {
    server = new Server();
    SocketListener listener = new SocketListener();
    listener.setPort(env.getServerPort());
    server.addListener(listener);
    context = new HttpContext(server, Channel.CONTEXT_PATH + "/*");
    server.start();
}
Also used : SocketListener(org.mortbay.http.SocketListener) Server(org.mortbay.jetty.Server) HttpContext(org.mortbay.http.HttpContext) Setup(org.apache.axis2.transport.testkit.tests.Setup)

Aggregations

Server (org.mortbay.jetty.Server)37 SocketConnector (org.mortbay.jetty.bio.SocketConnector)16 WebAppContext (org.mortbay.jetty.webapp.WebAppContext)14 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)11 Context (org.mortbay.jetty.servlet.Context)10 ServletHolder (org.mortbay.jetty.servlet.ServletHolder)9 Connector (org.mortbay.jetty.Connector)8 IOException (java.io.IOException)5 DefaultHandler (org.mortbay.jetty.handler.DefaultHandler)5 Configuration (org.apache.hadoop.conf.Configuration)4 Handler (org.mortbay.jetty.Handler)4 ContextHandler (org.mortbay.jetty.handler.ContextHandler)4 HandlerList (org.mortbay.jetty.handler.HandlerList)4 ResourceHandler (org.mortbay.jetty.handler.ResourceHandler)3 SslSocketConnector (org.mortbay.jetty.security.SslSocketConnector)3 ServletHandler (org.mortbay.jetty.servlet.ServletHandler)3 ArrayList (java.util.ArrayList)2 Repository (javax.jcr.Repository)2 ServletException (javax.servlet.ServletException)2 Before (org.junit.Before)2