Java Code Examples for org.glassfish.grizzly.servlet.WebappContext#addServlet()

The following examples show how to use org.glassfish.grizzly.servlet.WebappContext#addServlet() . 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: WebServer.java    From AthenaX with Apache License 2.0 6 votes vote down vote up
public WebServer(URI endpoint) throws IOException {
  this.server = GrizzlyServerFactory.createHttpServer(endpoint, new HttpHandler() {

    @Override
    public void service(Request rqst, Response rspns) throws Exception {
      rspns.setStatus(HttpStatus.NOT_FOUND_404.getStatusCode(), "Not found");
      rspns.getWriter().write("404: not found");
    }
  });

  WebappContext context = new WebappContext("WebappContext", BASE_PATH);
  ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
  registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS,
      PackagesResourceConfig.class.getName());

  StringJoiner sj = new StringJoiner(",");
  for (String s : PACKAGES) {
    sj.add(s);
  }

  registration.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, sj.toString());
  registration.addMapping(BASE_PATH);
  context.deploy(server);
}
 
Example 2
Source File: MCRJerseyTest.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void start() {
    System.out.println("Starting GrizzlyTestContainer...");
    try {
        this.server = GrizzlyHttpServerFactory.createHttpServer(uri, rc);

        // Initialize and register Jersey Servlet
        WebappContext context = new WebappContext("WebappContext", "");
        ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
        registration.setInitParameter("javax.ws.rs.Application", rc.getClass().getName());
        // Add an init parameter - this could be loaded from a parameter in the constructor
        registration.setInitParameter("myparam", "myvalue");

        registration.addMapping("/*");
        context.deploy(server);
    } catch (ProcessingException e) {
        throw new TestContainerException(e);
    }
}
 
Example 3
Source File: RpcServerManager.java    From nuls-v2 with MIT License 5 votes vote down vote up
public void startServer(String ip, int port) {
    URI serverURI = UriBuilder.fromUri("http://" + ip).port(port).build();
    // Create test web application context.
    WebappContext webappContext = new WebappContext("NULS-V2-SDK-PROVIDER-SERVER", "/");

    ServletRegistration servletRegistration = webappContext.addServlet("jersey-servlet", ServletContainer.class);
    servletRegistration.setInitParameter("javax.ws.rs.Application", "io.nuls.provider.api.config.NulsResourceConfig");
    servletRegistration.addMapping("/*");

    httpServer = new HttpServer();
    NetworkListener listener = new NetworkListener("grizzly2", ip, port);
    TCPNIOTransport transport = listener.getTransport();
    ThreadPoolConfig workerPool = ThreadPoolConfig.defaultConfig()
            .setCorePoolSize(4)
            .setMaxPoolSize(4)
            .setQueueLimit(1000)
            .setThreadFactory((new ThreadFactoryBuilder()).setNameFormat("grizzly-http-server-%d").build());
    transport.configureBlocking(false);
    transport.setSelectorRunnersCount(2);
    transport.setWorkerThreadPoolConfig(workerPool);
    transport.setIOStrategy(WorkerThreadIOStrategy.getInstance());
    transport.setTcpNoDelay(true);
    listener.setSecure(false);
    httpServer.addListener(listener);

    ServerConfiguration config = httpServer.getServerConfiguration();
    config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);

    webappContext.deploy(httpServer);

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        httpServer.start();
        Log.info("http restFul server is started!url is " + serverURI.toString());
    } catch (IOException e) {
        Log.error(e);
        httpServer.shutdownNow();
    }
}
 
Example 4
Source File: SrvTakeTest.java    From takes with MIT License 5 votes vote down vote up
@Test
public void executeATakesAsAServlet() throws Exception {
    final String name = "webapp";
    final HttpServer server = HttpServer.createSimpleServer("./", 18080);
    final WebappContext context = new WebappContext(name);
    final ServletRegistration servlet = context.addServlet(
        "takes",
        SrvTake.class
    );
    servlet.setInitParameter("take", TkApp.class.getName());
    servlet.addMapping("/test");
    context.deploy(server);
    server.start();
    new JdkRequest("http://localhost:18080/test")
        .fetch()
        .as(RestResponse.class)
        .assertStatus(HttpURLConnection.HTTP_OK)
        .assertBody(
            new StringContains(
                new FormattedText(
                    SrvTakeTest.MSG,
                    name
                ).asString()
            )
        );
    server.shutdownNow();
}
 
Example 5
Source File: GrizzlyTestServer.java    From dagger-servlet with Apache License 2.0 5 votes vote down vote up
public <T extends DaggerServletContextListener> void startServer(Class<T> listenerClass) {
    LOGGER.info("Starting test server");

    WebappContext context = new WebappContext("Test", getUri().getRawPath());
    context.addListener(listenerClass);

    daggerFilter = new DaggerFilter();
    FilterRegistration filterRegistration = context.addFilter("daggerFilter", daggerFilter);
    filterRegistration.addMappingForUrlPatterns(null, "/*");

    ServletRegistration servletRegistration = context.addServlet("TestServlet", new HttpServlet() {
    });
    servletRegistration.addMapping("/dagger-jersey/*");

    try {
        httpServer = GrizzlyServerFactory.createHttpServer(getUri(), (HttpHandler) null);
        context.deploy(httpServer);
        httpServer.start();
        LOGGER.info("Test server started");
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 6
Source File: RpcServerManager.java    From nuls-v2 with MIT License 4 votes vote down vote up
public void startServer(String ip, int port) {
    URI serverURI = UriBuilder.fromUri("http://" + ip).port(port).build();
    // Create test web application context.
    WebappContext webappContext = new WebappContext("NULS-RPC-SERVER", "/api");

    ServletRegistration servletRegistration = webappContext.addServlet("jersey-servlet", ServletContainer.class);
    servletRegistration.setInitParameter("javax.ws.rs.Application", "io.nuls.test.controller.NulsResourceConfig");
    servletRegistration.addMapping("/api/*");

    httpServer = new HttpServer();
    NetworkListener listener = new NetworkListener("grizzly2", ip, port);
    TCPNIOTransport transport = listener.getTransport();
    ThreadPoolConfig workerPool = ThreadPoolConfig.defaultConfig()
            .setCorePoolSize(4)
            .setMaxPoolSize(4)
            .setQueueLimit(1000)
            .setThreadFactory((new ThreadFactoryBuilder()).setNameFormat("grizzly-http-server-%d").build());
    transport.configureBlocking(false);
    transport.setSelectorRunnersCount(2);
    transport.setWorkerThreadPoolConfig(workerPool);
    transport.setIOStrategy(WorkerThreadIOStrategy.getInstance());
    transport.setTcpNoDelay(true);
    listener.setSecure(false);
    httpServer.addListener(listener);

    ServerConfiguration config = httpServer.getServerConfiguration();
    config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);

    webappContext.deploy(httpServer);

    try {
        ClassLoader loader = this.getClass().getClassLoader();

        addSwagerUi(loader);
        addClientUi(loader);

        httpServer.start();
        log.info("http restFul server is started!url is " + serverURI.toString());
    } catch (IOException e) {
        log.error("",e);
        httpServer.shutdownNow();
    }
}
 
Example 7
Source File: RpcServerManager.java    From nuls with MIT License 4 votes vote down vote up
public void startServer(String ip, int port) {
    URI serverURI = UriBuilder.fromUri("http://" + ip).port(port).build();
    // Create test web application context.
    WebappContext webappContext = new WebappContext("NULS-RPC-SERVER", "/api");

    ServletRegistration servletRegistration = webappContext.addServlet("jersey-servlet", ServletContainer.class);
    servletRegistration.setInitParameter("javax.ws.rs.Application", "io.nuls.client.rpc.config.NulsResourceConfig");
    servletRegistration.addMapping("/api/*");

    httpServer = new HttpServer();
    NetworkListener listener = new NetworkListener("grizzly2", ip, port);
    TCPNIOTransport transport = listener.getTransport();
    ThreadPoolConfig workerPool = ThreadPoolConfig.defaultConfig()
            .setCorePoolSize(4)
            .setMaxPoolSize(4)
            .setQueueLimit(1000)
            .setThreadFactory((new ThreadFactoryBuilder()).setNameFormat("grizzly-http-server-%d").build());
    transport.configureBlocking(false);
    transport.setSelectorRunnersCount(2);
    transport.setWorkerThreadPoolConfig(workerPool);
    transport.setIOStrategy(WorkerThreadIOStrategy.getInstance());
    transport.setTcpNoDelay(true);
    listener.setSecure(false);
    httpServer.addListener(listener);

    ServerConfiguration config = httpServer.getServerConfiguration();
    config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);

    webappContext.deploy(httpServer);

    try {
        ClassLoader loader = this.getClass().getClassLoader();

        addSwagerUi(loader);
        addClientUi(loader);

        httpServer.start();
        Log.info("http restFul server is started!url is " + serverURI.toString());
    } catch (IOException e) {
        Log.error(e);
        httpServer.shutdownNow();
    }
}