org.mortbay.jetty.servlet.ServletHandler Java Examples

The following examples show how to use org.mortbay.jetty.servlet.ServletHandler. 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: VmBoundServiceTest.java    From jrpip with Apache License 2.0 6 votes vote down vote up
private void setupServer() throws Exception
{
    this.server = new HttpServer();
    SocketListener listener = new SocketListener();
    listener.setPort(PORT);
    this.server.addListener(listener);
    HttpContext context = new HttpContext();
    context.setContextPath("/");

    ServletHandler servletHandler = new ServletHandler();
    context.addHandler(servletHandler);

    // Map a servlet onto the container
    ServletHolder holder =
            servletHandler.addServlet("JrpipServlet", "/JrpipServlet", "com.gs.jrpip.server.JrpipServlet");
    holder.put("serviceInterface.Echo", "com.gs.jrpip.Echo");
    holder.put("vmBoundServiceClass.Echo", "com.gs.jrpip.EchoImpl");
    holder.setInitOrder(10);

    this.server.addContext(context);

    this.server.start();
    this.servlet = (JrpipServlet) holder.getServlet();
}
 
Example #2
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 #3
Source File: MyriadWebServer.java    From incubator-myriad with Apache License 2.0 6 votes vote down vote up
public void start() throws Exception {
  this.jetty.addConnector(connector);

  ServletHandler servletHandler = new ServletHandler();

  String filterName = "MyriadGuiceFilter";
  FilterHolder holder = new FilterHolder(filter);
  holder.setName(filterName);

  FilterMapping filterMapping = new FilterMapping();
  filterMapping.setPathSpec("/*");
  filterMapping.setDispatches(Handler.ALL);
  filterMapping.setFilterName(filterName);

  servletHandler.addFilter(holder, filterMapping);

  Context context = new Context();
  context.setServletHandler(servletHandler);
  context.addServlet(DefaultServlet.class, "/");

  String staticDir = this.getClass().getClassLoader().getResource("webapp/public").toExternalForm();
  context.setResourceBase(staticDir);

  this.jetty.addHandler(context);
  this.jetty.start();
}
 
Example #4
Source File: HttpServer2.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec,
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
 
Example #5
Source File: GitkitExample.java    From identity-toolkit-java with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  Server server = new Server(4567);
  ServletHandler servletHandler = new ServletHandler();
  SessionHandler sessionHandler = new SessionHandler();
  sessionHandler.setHandler(servletHandler);
  server.setHandler(sessionHandler);
  servletHandler.addServletWithMapping(LoginServlet.class, "/login");
  servletHandler.addServletWithMapping(WidgetServlet.class, "/gitkit");
  servletHandler.addServletWithMapping(LoginServlet.class, "/");
  server.start();
  server.join();
}
 
Example #6
Source File: HttpServer.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication. 
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled. 
 * 
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec, 
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
 
Example #7
Source File: HttpServer2.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec,
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
 
Example #8
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 #9
Source File: HttpServer.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Define a filter for a context and set up default url mappings.
 */
public void defineFilter(Context ctx, String name,
    String classname, Map<String,String> parameters, String[] urls) {

  FilterHolder holder = new FilterHolder();
  holder.setName(name);
  holder.setClassName(classname);
  holder.setInitParameters(parameters);
  FilterMapping fmap = new FilterMapping();
  fmap.setPathSpecs(urls);
  fmap.setDispatches(Handler.ALL);
  fmap.setFilterName(name);
  ServletHandler handler = ctx.getServletHandler();
  handler.addFilter(holder, fmap);
}
 
Example #10
Source File: HttpServer.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Add an internal servlet in the server, specifying whether or not to
 * protect with Kerberos authentication. 
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 +   * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled. 
 * 
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param requireAuth Require Kerberos authenticate to access servlet
 */
public void addInternalServlet(String name, String pathSpec, 
    Class<? extends HttpServlet> clazz, boolean requireAuth) {
  ServletHolder holder = new ServletHolder(clazz);
  if (name != null) {
    holder.setName(name);
  }
  webAppContext.addServlet(holder, pathSpec);

  if(requireAuth && UserGroupInformation.isSecurityEnabled()) {
     LOG.info("Adding Kerberos (SPNEGO) filter to " + name);
     ServletHandler handler = webAppContext.getServletHandler();
     FilterMapping fmap = new FilterMapping();
     fmap.setPathSpec(pathSpec);
     fmap.setFilterName(SPNEGO_FILTER);
     fmap.setDispatches(Handler.ALL);
     handler.addFilterMapping(fmap);
  }
}
 
Example #11
Source File: AtomClientServerTest.java    From rome with Apache License 2.0 6 votes vote down vote up
@Before
public void setUpClass() throws Exception {

    LOG.info("---------------------------------------------");
    LOG.info("Starting Jetty");
    LOG.info("---------------------------------------------");

    setupServer();
    final HttpContext context = createContext();
    final ServletHandler servlets = createServletHandler();
    context.addHandler(servlets);
    server.addContext(context);
    server.start();

    service = AtomClientFactory.getAtomService(getEndpoint(), new BasicAuthStrategy(getUsername(), getPassword()));
}
 
Example #12
Source File: HttpServer.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Add the path spec to the filter path mapping.
 * @param pathSpec The path spec
 * @param webAppCtx The WebApplicationContext to add to
 */
protected void addFilterPathMapping(String pathSpec,
    Context webAppCtx) {
  ServletHandler handler = webAppCtx.getServletHandler();
  for(String name : filterNames) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpec(pathSpec);
    fmap.setFilterName(name);
    fmap.setDispatches(Handler.ALL);
    handler.addFilterMapping(fmap);
  }
}
 
Example #13
Source File: HttpServer.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Define a filter for a context and set up default url mappings.
 */
protected void defineFilter(Context ctx, String name,
    String classname, Map<String,String> parameters, String[] urls) {

  FilterHolder holder = new FilterHolder();
  holder.setName(name);
  holder.setClassName(classname);
  holder.setInitParameters(parameters);
  FilterMapping fmap = new FilterMapping();
  fmap.setPathSpecs(urls);
  fmap.setDispatches(Handler.ALL);
  fmap.setFilterName(name);
  ServletHandler handler = ctx.getServletHandler();
  handler.addFilter(holder, fmap);
}
 
Example #14
Source File: AbstractJettyTest.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * @see junit.framework.TestCase#setUp()
 */
@Override
protected void setUp() throws Exception {
    setupServer();

    final HttpContext context = createContext();

    final ServletHandler servlets = createServletHandler();
    context.addHandler(servlets);

    server.addContext(context);

    server.start();
}
 
Example #15
Source File: AbstractJettyTest.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * @return
 */
private ServletHandler createServletHandler() {
    final ServletHandler servlets = new ServletHandler();
    servlets.addServlet("FetcherTestServlet", FetcherTestServlet.SERVLET_MAPPING, "com.rometools.fetcher.FetcherTestServlet");
    servlets.addServlet("FetcherTestServlet", FetcherTestServlet.SERVLET_MAPPING2, "com.rometools.fetcher.FetcherTestServlet");
    return servlets;
}
 
Example #16
Source File: TestObjectFactory.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
public static Server getJettyServer() {
  Server server = new Server();
  ServletHandler context = new ServletHandler();
  ServletHolder holder = new ServletHolder(DefaultServlet.class);
  holder.setInitParameter("resourceBase", System.getProperty("user.dir"));
  holder.setInitParameter("dirAllowed", "true");
  context.setServer(server);
  context.addServlet(holder);
  server.setHandler(context);    

  return server;
}
 
Example #17
Source File: AbstractJettyTest.java    From rome with Apache License 2.0 5 votes vote down vote up
public void testBasicAuthentication() {
    try {
        setupServer();


        final URL url = this.getClass().getClassLoader().getResource("testuser.properties");
        final UserRealm userRealm = new HashUserRealm("test", url.toString());

        final BasicAuthenticator basicauthenticator = new BasicAuthenticator();

        final SecurityHandler securityHandler = new SecurityHandler();

        final SecurityConstraint securityConstraint = new SecurityConstraint();
        securityConstraint.setName("test");
        securityConstraint.addRole("*");
        securityConstraint.setAuthenticate(true);

        final ServletHandler servletHandler = createServletHandler();

        final HttpContext context = createContext();
        context.setRealm(userRealm);
        context.setAuthenticator(basicauthenticator);
        context.addHandler(securityHandler);
        context.addSecurityConstraint("/", securityConstraint);
        context.addHandler(servletHandler);

        server.addContext(context);
        server.start();

        final FeedFetcher feedFetcher = getAuthenticatedFeedFetcher();
        final SyndFeed feed = feedFetcher.retrieveFeed(new URL("http://localhost:" + testPort + "/rome/FetcherTestServlet/"));
        assertNotNull(feed);
        assertEquals("atom_1.0.feed.title", feed.getTitle());

    } catch (final Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

}
 
Example #18
Source File: HttpServer.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Define a filter for a context and set up default url mappings.
 */
protected void defineFilter(Context ctx, String name,
    String classname, Map<String,String> parameters, String[] urls) {

  FilterHolder holder = new FilterHolder();
  holder.setName(name);
  holder.setClassName(classname);
  holder.setInitParameters(parameters);
  FilterMapping fmap = new FilterMapping();
  fmap.setPathSpecs(urls);
  fmap.setDispatches(Handler.ALL);
  fmap.setFilterName(name);
  ServletHandler handler = ctx.getServletHandler();
  handler.addFilter(holder, fmap);
}
 
Example #19
Source File: JettyContainer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void start() {
    String serverPort = ConfigUtils.getProperty(JETTY_PORT);
    int port;
    if (serverPort == null || serverPort.length() == 0) {
        port = DEFAULT_JETTY_PORT;
    } else {
        port = Integer.parseInt(serverPort);
    }
    connector = new SelectChannelConnector();
    connector.setPort(port);
    ServletHandler handler = new ServletHandler();
    
    String resources = ConfigUtils.getProperty(JETTY_DIRECTORY);
    if (resources != null && resources.length() > 0) {
        FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT);
        resourceHolder.setInitParameter("resources", resources);
    }
    
    ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");
    pageHolder.setInitParameter("pages", ConfigUtils.getProperty(JETTY_PAGES));
    pageHolder.setInitOrder(2);
    
    Server server = new Server();
    server.addConnector(connector);
    server.addHandler(handler);
    try {
        server.start();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + e.getMessage(), e);
    }
}
 
Example #20
Source File: HttpServer.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Add the path spec to the filter path mapping.
 * @param pathSpec The path spec
 * @param webAppCtx The WebApplicationContext to add to
 */
protected void addFilterPathMapping(String pathSpec,
    Context webAppCtx) {
  ServletHandler handler = webAppCtx.getServletHandler();
  for(String name : filterNames) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpec(pathSpec);
    fmap.setFilterName(name);
    fmap.setDispatches(Handler.ALL);
    handler.addFilterMapping(fmap);
  }
}
 
Example #21
Source File: JettyContainer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void start() {
    String serverPort = ConfigUtils.getProperty(JETTY_PORT);
    int port;
    if (serverPort == null || serverPort.length() == 0) {
        port = DEFAULT_JETTY_PORT;
    } else {
        port = Integer.parseInt(serverPort);
    }
    connector = new SelectChannelConnector();
    connector.setPort(port);
    ServletHandler handler = new ServletHandler();
    
    String resources = ConfigUtils.getProperty(JETTY_DIRECTORY);
    if (resources != null && resources.length() > 0) {
        FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT);
        resourceHolder.setInitParameter("resources", resources);
    }
    
    ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");
    pageHolder.setInitParameter("pages", ConfigUtils.getProperty(JETTY_PAGES));
    pageHolder.setInitOrder(2);
    
    Server server = new Server();
    server.addConnector(connector);
    server.addHandler(handler);
    try {
        server.start();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + e.getMessage(), e);
    }
}
 
Example #22
Source File: RequestTracingFilterOldServletComponentTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
private static Handler generateServletContextHandler() throws IOException {
    ServletHandler servletHandler = new ServletHandler();

    servletHandler.addServletWithMapping(BlockingServlet.class, BLOCKING_PATH);
    servletHandler.addServletWithMapping(BlockingForwardServlet.class, BLOCKING_FORWARD_PATH);
    servletHandler.addFilterWithMapping(RequestTracingFilter.class.getName(), "/*", Handler.ALL);

    Context context = new Context(null, null, null, servletHandler, null);
    context.setContextPath("/");
    return context;
}
 
Example #23
Source File: HttpServer2.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Add the path spec to the filter path mapping.
 * @param pathSpec The path spec
 * @param webAppCtx The WebApplicationContext to add to
 */
protected void addFilterPathMapping(String pathSpec,
    Context webAppCtx) {
  ServletHandler handler = webAppCtx.getServletHandler();
  for(String name : filterNames) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpec(pathSpec);
    fmap.setFilterName(name);
    fmap.setDispatches(Handler.ALL);
    handler.addFilterMapping(fmap);
  }
}
 
Example #24
Source File: HttpServer.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Add the path spec to the filter path mapping.
 * @param pathSpec The path spec
 * @param webAppCtx The WebApplicationContext to add to
 */
protected void addFilterPathMapping(String pathSpec,
    Context webAppCtx) {
  ServletHandler handler = webAppCtx.getServletHandler();
  for(String name : filterNames) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpec(pathSpec);
    fmap.setFilterName(name);
    fmap.setDispatches(Handler.ALL);
    handler.addFilterMapping(fmap);
  }
}
 
Example #25
Source File: HttpServer.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Define a filter for a context and set up default url mappings.
 */
public void defineFilter(Context ctx, String name,
    String classname, Map<String,String> parameters, String[] urls) {

  FilterHolder holder = new FilterHolder();
  holder.setName(name);
  holder.setClassName(classname);
  holder.setInitParameters(parameters);
  FilterMapping fmap = new FilterMapping();
  fmap.setPathSpecs(urls);
  fmap.setDispatches(Handler.ALL);
  fmap.setFilterName(name);
  ServletHandler handler = ctx.getServletHandler();
  handler.addFilter(holder, fmap);
}
 
Example #26
Source File: HttpServer2.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Add the path spec to the filter path mapping.
 * @param pathSpec The path spec
 * @param webAppCtx The WebApplicationContext to add to
 */
protected void addFilterPathMapping(String pathSpec,
    Context webAppCtx) {
  ServletHandler handler = webAppCtx.getServletHandler();
  for(String name : filterNames) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpec(pathSpec);
    fmap.setFilterName(name);
    fmap.setDispatches(Handler.ALL);
    handler.addFilterMapping(fmap);
  }
}
 
Example #27
Source File: HttpServer.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Add the path spec to the filter path mapping.
 * @param pathSpec The path spec
 * @param webAppCtx The WebApplicationContext to add to
 */
protected void addFilterPathMapping(String pathSpec,
    Context webAppCtx) {
  ServletHandler handler = webAppCtx.getServletHandler();
  for(String name : filterNames) {
    FilterMapping fmap = new FilterMapping();
    fmap.setPathSpec(pathSpec);
    fmap.setFilterName(name);
    fmap.setDispatches(Handler.ALL);
    handler.addFilterMapping(fmap);
  }
}
 
Example #28
Source File: JettyContainer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void start() {
    String serverPort = ConfigUtils.getProperty(JETTY_PORT);
    int port;
    if (serverPort == null || serverPort.length() == 0) {
        port = DEFAULT_JETTY_PORT;
    } else {
        port = Integer.parseInt(serverPort);
    }
    connector = new SelectChannelConnector();
    connector.setPort(port);
    ServletHandler handler = new ServletHandler();
    
    String resources = ConfigUtils.getProperty(JETTY_DIRECTORY);
    if (resources != null && resources.length() > 0) {
        FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT);
        resourceHolder.setInitParameter("resources", resources);
    }
    
    ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");
    pageHolder.setInitParameter("pages", ConfigUtils.getProperty(JETTY_PAGES));
    pageHolder.setInitOrder(2);
    
    Server server = new Server();
    server.addConnector(connector);
    server.addHandler(handler);
    try {
        server.start();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + e.getMessage(), e);
    }
}
 
Example #29
Source File: JettyContainer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void start() {
    String serverPort = ConfigUtils.getProperty(JETTY_PORT);
    int port;
    if (serverPort == null || serverPort.length() == 0) {
        port = DEFAULT_JETTY_PORT;
    } else {
        port = Integer.parseInt(serverPort);
    }
    connector = new SelectChannelConnector();
    connector.setPort(port);
    ServletHandler handler = new ServletHandler();
    
    String resources = ConfigUtils.getProperty(JETTY_DIRECTORY);
    if (resources != null && resources.length() > 0) {
        FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT);
        resourceHolder.setInitParameter("resources", resources);
    }
    
    ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");
    pageHolder.setInitParameter("pages", ConfigUtils.getProperty(JETTY_PAGES));
    pageHolder.setInitOrder(2);
    
    Server server = new Server();
    server.addConnector(connector);
    server.addHandler(handler);
    try {
        server.start();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + e.getMessage(), e);
    }
}
 
Example #30
Source File: JettyHttpServer.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
public JettyHttpServer(URL url, final HttpHandler handler) {
        super(url, handler);
        this.url = url;
        // TODO we should leave this setting to slf4j
        // we must disable the debug logging for production use
        Log.setLog(new StdErrLog());
        Log.getLog().setDebugEnabled(false);

        DispatcherServlet.addHttpHandler(url.getParameter(Constants.BIND_PORT_KEY, url.getPort()), handler);

        int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setDaemon(true);
        threadPool.setMaxThreads(threads);
        threadPool.setMinThreads(threads);

        SelectChannelConnector connector = new SelectChannelConnector();

        String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost());
        if (!url.isAnyHost() && NetUtils.isValidLocalHost(bindIp)) {
            connector.setHost(bindIp);
        }
        connector.setPort(url.getParameter(Constants.BIND_PORT_KEY, url.getPort()));

        server = new Server();
        server.setThreadPool(threadPool);
        server.addConnector(connector);

        ServletHandler servletHandler = new ServletHandler();
        ServletHolder servletHolder = servletHandler.addServletWithMapping(DispatcherServlet.class, "/*");
        servletHolder.setInitOrder(2);

        // dubbo's original impl can't support the use of ServletContext
//        server.addHandler(servletHandler);
        // TODO Context.SESSIONS is the best option here?
        Context context = new Context(server, "/", Context.SESSIONS);
        context.setServletHandler(servletHandler);
        ServletManager.getInstance().addServletContext(url.getParameter(Constants.BIND_PORT_KEY, url.getPort()), context.getServletContext());

        try {
            server.start();
        } catch (Exception e) {
            throw new IllegalStateException("Failed to start jetty server on " + url.getParameter(Constants.BIND_IP_KEY) + ":" + url.getParameter(Constants.BIND_PORT_KEY) + ", cause: "
                    + e.getMessage(), e);
        }
    }