Search in sources :

Example 1 with AsyncSocketServlet

use of org.traccar.api.AsyncSocketServlet in project traccar by tananaev.

the class WebServer method initApi.

private void initApi(Config config, ServletContextHandler servletHandler) {
    servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/api/socket");
    JettyWebSocketServletContainerInitializer.configure(servletHandler, null);
    String mediaPath = config.getString(Keys.MEDIA_PATH);
    if (mediaPath != null) {
        ServletHolder servletHolder = new ServletHolder(DefaultServlet.class);
        servletHolder.setInitParameter("resourceBase", new File(mediaPath).getAbsolutePath());
        servletHolder.setInitParameter("dirAllowed", "false");
        servletHolder.setInitParameter("pathInfoOnly", "true");
        servletHandler.addServlet(servletHolder, "/api/media/*");
        servletHandler.addFilter(MediaFilter.class, "/api/media/*", EnumSet.allOf(DispatcherType.class));
    }
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.registerClasses(JacksonFeature.class, ResourceErrorHandler.class, SecurityRequestFilter.class, CorsResponseFilter.class, DateParameterConverterProvider.class);
    resourceConfig.packages(ServerResource.class.getPackage().getName());
    resourceConfig.register(new ContainerLifecycleListener() {

        @Override
        public void onStartup(Container container) {
            var injectionManager = container.getApplicationHandler().getInjectionManager();
            var serviceLocator = ((ImmediateHk2InjectionManager) injectionManager).getServiceLocator();
            GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
            var guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
            guiceBridge.bridgeGuiceInjector(Main.getInjector());
        }

        @Override
        public void onReload(Container container) {
        }

        @Override
        public void onShutdown(Container container) {
        }
    });
    servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/api/*");
}
Also used : Container(org.glassfish.jersey.server.spi.Container) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ContainerLifecycleListener(org.glassfish.jersey.server.spi.ContainerLifecycleListener) GuiceIntoHK2Bridge(org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AsyncSocketServlet(org.traccar.api.AsyncSocketServlet) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) DispatcherType(javax.servlet.DispatcherType) File(java.io.File)

Example 2 with AsyncSocketServlet

use of org.traccar.api.AsyncSocketServlet in project traccar by tananaev.

the class WebServer method initApi.

private void initApi() {
    ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletHandler.setContextPath("/api");
    servletHandler.getSessionHandler().setSessionManager(sessionManager);
    servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/socket");
    if (config.hasKey("media.path")) {
        ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class);
        servletHolder.setInitParameter("resourceBase", config.getString("media.path"));
        servletHolder.setInitParameter("dirAllowed", config.getString("media.dirAllowed", "false"));
        servletHolder.setInitParameter("pathInfoOnly", "true");
        servletHandler.addServlet(servletHolder, "/media/*");
        servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class));
    }
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.registerClasses(JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class);
    resourceConfig.registerClasses(SecurityRequestFilter.class, CorsResponseFilter.class);
    resourceConfig.packages(ServerResource.class.getPackage().getName());
    servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
    handlers.addHandler(servletHandler);
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AsyncSocketServlet(org.traccar.api.AsyncSocketServlet) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType)

Example 3 with AsyncSocketServlet

use of org.traccar.api.AsyncSocketServlet in project traccar by traccar.

the class WebServer method initApi.

private void initApi() {
    ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletHandler.setContextPath("/api");
    servletHandler.getSessionHandler().setSessionManager(sessionManager);
    servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/socket");
    if (config.hasKey("media.path")) {
        ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class);
        servletHolder.setInitParameter("resourceBase", config.getString("media.path"));
        servletHolder.setInitParameter("dirAllowed", config.getString("media.dirAllowed", "false"));
        servletHolder.setInitParameter("pathInfoOnly", "true");
        servletHandler.addServlet(servletHolder, "/media/*");
        servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class));
    }
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.registerClasses(JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class);
    resourceConfig.registerClasses(SecurityRequestFilter.class, CorsResponseFilter.class);
    resourceConfig.packages(ServerResource.class.getPackage().getName());
    servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
    handlers.addHandler(servletHandler);
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AsyncSocketServlet(org.traccar.api.AsyncSocketServlet) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType)

Aggregations

DispatcherType (javax.servlet.DispatcherType)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)3 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)3 AsyncSocketServlet (org.traccar.api.AsyncSocketServlet)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 File (java.io.File)1 Container (org.glassfish.jersey.server.spi.Container)1 ContainerLifecycleListener (org.glassfish.jersey.server.spi.ContainerLifecycleListener)1 GuiceIntoHK2Bridge (org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge)1