Search in sources :

Example 1 with AdditionalServletWithClassLoader

use of org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader in project pulsar by yahoo.

the class PulsarService method addBrokerAdditionalServlets.

private void addBrokerAdditionalServlets(WebService webService, Map<String, Object> attributeMap, ServiceConfiguration config) {
    if (this.getBrokerAdditionalServlets() != null) {
        Collection<AdditionalServletWithClassLoader> additionalServletCollection = this.getBrokerAdditionalServlets().getServlets().values();
        for (AdditionalServletWithClassLoader servletWithClassLoader : additionalServletCollection) {
            servletWithClassLoader.loadConfig(config);
            AdditionalServlet additionalServlet = servletWithClassLoader.getServlet();
            if (additionalServlet instanceof AdditionalServletWithPulsarService) {
                ((AdditionalServletWithPulsarService) additionalServlet).setPulsarService(this);
            }
            webService.addServlet(servletWithClassLoader.getBasePath(), servletWithClassLoader.getServletHolder(), config.isAuthenticationEnabled(), attributeMap);
            LOG.info("Broker add additional servlet basePath {} ", servletWithClassLoader.getBasePath());
        }
    }
}
Also used : AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader)

Example 2 with AdditionalServletWithClassLoader

use of org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader in project pulsar by yahoo.

the class ProxyAdditionalServletTest method mockAdditionalServlet.

private void mockAdditionalServlet() {
    Servlet servlet = new Servlet() {

        @Override
        public void init(ServletConfig servletConfig) throws ServletException {
            log.info("[init]");
        }

        @Override
        public ServletConfig getServletConfig() {
            log.info("[getServletConfig]");
            return null;
        }

        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
            log.info("[service] path: {}", ((Request) servletRequest).getOriginalURI());
            String value = servletRequest.getParameterMap().get(QUERY_PARAM)[0];
            ServletOutputStream servletOutputStream = servletResponse.getOutputStream();
            servletResponse.setContentLength(value.getBytes().length);
            servletOutputStream.write(value.getBytes());
            servletOutputStream.flush();
        }

        @Override
        public String getServletInfo() {
            log.info("[getServletInfo]");
            return null;
        }

        @Override
        public void destroy() {
            log.info("[destroy]");
        }
    };
    AdditionalServlet proxyAdditionalServlet = Mockito.mock(AdditionalServlet.class);
    Mockito.when(proxyAdditionalServlet.getBasePath()).thenReturn(BASE_PATH);
    Mockito.when(proxyAdditionalServlet.getServletHolder()).thenReturn(new ServletHolder(servlet));
    AdditionalServlets proxyAdditionalServlets = Mockito.mock(AdditionalServlets.class);
    Map<String, AdditionalServletWithClassLoader> map = new HashMap<>();
    map.put("prometheus-proxy-servlet", new AdditionalServletWithClassLoader(proxyAdditionalServlet, null));
    Mockito.when(proxyAdditionalServlets.getServlets()).thenReturn(map);
    Mockito.when(proxyService.getProxyAdditionalServlets()).thenReturn(proxyAdditionalServlets);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) ServletOutputStream(javax.servlet.ServletOutputStream) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletConfig(javax.servlet.ServletConfig) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) Servlet(javax.servlet.Servlet) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader) AdditionalServlets(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)

Example 3 with AdditionalServletWithClassLoader

use of org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader in project pulsar by yahoo.

the class BrokerAdditionalServletTest method mockAdditionalServlet.

private void mockAdditionalServlet(PulsarService pulsar) {
    Servlet servlet = new OrdinaryServlet();
    AdditionalServlet brokerAdditionalServlet = Mockito.mock(AdditionalServlet.class);
    Mockito.when(brokerAdditionalServlet.getBasePath()).thenReturn(BASE_PATH);
    Mockito.when(brokerAdditionalServlet.getServletHolder()).thenReturn(new ServletHolder(servlet));
    AdditionalServletWithPulsarService brokerAdditionalServletWithPulsarService = new AdditionalServletWithPulsarService() {

        private PulsarService pulsarService;

        @Override
        public void setPulsarService(PulsarService pulsarService) {
            this.pulsarService = pulsarService;
        }

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
        // No-op
        }

        @Override
        public String getBasePath() {
            return WITH_PULSAR_SERVICE_BASE_PATH;
        }

        @Override
        public ServletHolder getServletHolder() {
            return new ServletHolder(new WithPulsarServiceServlet(pulsarService));
        }

        @Override
        public void close() {
        // No-op
        }
    };
    AdditionalServlets brokerAdditionalServlets = Mockito.mock(AdditionalServlets.class);
    Map<String, AdditionalServletWithClassLoader> map = new HashMap<>();
    map.put("broker-additional-servlet", new AdditionalServletWithClassLoader(brokerAdditionalServlet, null));
    map.put("broker-additional-servlet-with-pulsar-service", new AdditionalServletWithClassLoader(brokerAdditionalServletWithPulsarService, null));
    Mockito.when(brokerAdditionalServlets.getServlets()).thenReturn(map);
    Mockito.when(pulsar.getBrokerAdditionalServlets()).thenReturn(brokerAdditionalServlets);
}
Also used : AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) AdditionalServletWithPulsarService(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) Servlet(javax.servlet.Servlet) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader) AdditionalServlets(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)

Example 4 with AdditionalServletWithClassLoader

use of org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader in project pulsar by yahoo.

the class ProxyServiceStarter method addWebServerHandlers.

public static void addWebServerHandlers(WebServer server, ProxyConfiguration config, ProxyService service, BrokerDiscoveryProvider discoveryProvider) throws Exception {
    if (service != null) {
        PrometheusMetricsServlet metricsServlet = service.getMetricsServlet();
        if (metricsServlet != null) {
            server.addServlet("/metrics", new ServletHolder(metricsServlet), Collections.emptyList(), config.isAuthenticateMetricsEndpoint());
        }
    }
    server.addRestResources("/", VipStatus.class.getPackage().getName(), VipStatus.ATTRIBUTE_STATUS_FILE_PATH, config.getStatusFilePath());
    server.addRestResources("/proxy-stats", ProxyStats.class.getPackage().getName(), ProxyStats.ATTRIBUTE_PULSAR_PROXY_NAME, service);
    AdminProxyHandler adminProxyHandler = new AdminProxyHandler(config, discoveryProvider);
    ServletHolder servletHolder = new ServletHolder(adminProxyHandler);
    servletHolder.setInitParameter("preserveHost", "true");
    server.addServlet("/admin", servletHolder);
    server.addServlet("/lookup", servletHolder);
    for (ProxyConfiguration.HttpReverseProxyConfig revProxy : config.getHttpReverseProxyConfigs()) {
        log.debug("Adding reverse proxy with config {}", revProxy);
        ServletHolder proxyHolder = new ServletHolder(ProxyServlet.Transparent.class);
        proxyHolder.setInitParameter("proxyTo", revProxy.getProxyTo());
        proxyHolder.setInitParameter("prefix", "/");
        server.addServlet(revProxy.getPath(), proxyHolder);
    }
    // add proxy additional servlets
    if (service != null && service.getProxyAdditionalServlets() != null) {
        Collection<AdditionalServletWithClassLoader> additionalServletCollection = service.getProxyAdditionalServlets().getServlets().values();
        for (AdditionalServletWithClassLoader servletWithClassLoader : additionalServletCollection) {
            servletWithClassLoader.loadConfig(config);
            server.addServlet(servletWithClassLoader.getBasePath(), servletWithClassLoader.getServletHolder(), Collections.emptyList(), config.isAuthenticationEnabled());
            log.info("proxy add additional servlet basePath {} ", servletWithClassLoader.getBasePath());
        }
    }
    if (config.isWebSocketServiceEnabled()) {
        // add WebSocket servlet
        // Use local broker address to avoid different IP address when using a VIP for service discovery
        ServiceConfiguration serviceConfiguration = PulsarConfigurationLoader.convertFrom(config);
        serviceConfiguration.setBrokerClientTlsEnabled(config.isTlsEnabledWithBroker());
        WebSocketService webSocketService = new WebSocketService(createClusterData(config), serviceConfiguration);
        webSocketService.start();
        final WebSocketServlet producerWebSocketServlet = new WebSocketProducerServlet(webSocketService);
        server.addServlet(WebSocketProducerServlet.SERVLET_PATH, new ServletHolder(producerWebSocketServlet));
        server.addServlet(WebSocketProducerServlet.SERVLET_PATH_V2, new ServletHolder(producerWebSocketServlet));
        final WebSocketServlet consumerWebSocketServlet = new WebSocketConsumerServlet(webSocketService);
        server.addServlet(WebSocketConsumerServlet.SERVLET_PATH, new ServletHolder(consumerWebSocketServlet));
        server.addServlet(WebSocketConsumerServlet.SERVLET_PATH_V2, new ServletHolder(consumerWebSocketServlet));
        final WebSocketServlet readerWebSocketServlet = new WebSocketReaderServlet(webSocketService);
        server.addServlet(WebSocketReaderServlet.SERVLET_PATH, new ServletHolder(readerWebSocketServlet));
        server.addServlet(WebSocketReaderServlet.SERVLET_PATH_V2, new ServletHolder(readerWebSocketServlet));
        final WebSocketServlet pingPongWebSocketServlet = new WebSocketPingPongServlet(webSocketService);
        server.addServlet(WebSocketPingPongServlet.SERVLET_PATH, new ServletHolder(pingPongWebSocketServlet));
        server.addServlet(WebSocketPingPongServlet.SERVLET_PATH_V2, new ServletHolder(pingPongWebSocketServlet));
    }
}
Also used : WebSocketReaderServlet(org.apache.pulsar.websocket.WebSocketReaderServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ProxyServlet(org.eclipse.jetty.proxy.ProxyServlet) WebSocketServlet(org.eclipse.jetty.websocket.servlet.WebSocketServlet) WebSocketProducerServlet(org.apache.pulsar.websocket.WebSocketProducerServlet) WebSocketConsumerServlet(org.apache.pulsar.websocket.WebSocketConsumerServlet) WebSocketPingPongServlet(org.apache.pulsar.websocket.WebSocketPingPongServlet) PrometheusMetricsServlet(org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) WebSocketService(org.apache.pulsar.websocket.WebSocketService) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader)

Example 5 with AdditionalServletWithClassLoader

use of org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader in project incubator-pulsar by apache.

the class ProxyAdditionalServletTest method mockAdditionalServlet.

private void mockAdditionalServlet() {
    Servlet servlet = new Servlet() {

        @Override
        public void init(ServletConfig servletConfig) throws ServletException {
            log.info("[init]");
        }

        @Override
        public ServletConfig getServletConfig() {
            log.info("[getServletConfig]");
            return null;
        }

        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
            log.info("[service] path: {}", ((Request) servletRequest).getOriginalURI());
            String value = servletRequest.getParameterMap().get(QUERY_PARAM)[0];
            ServletOutputStream servletOutputStream = servletResponse.getOutputStream();
            servletResponse.setContentLength(value.getBytes().length);
            servletOutputStream.write(value.getBytes());
            servletOutputStream.flush();
        }

        @Override
        public String getServletInfo() {
            log.info("[getServletInfo]");
            return null;
        }

        @Override
        public void destroy() {
            log.info("[destroy]");
        }
    };
    AdditionalServlet proxyAdditionalServlet = Mockito.mock(AdditionalServlet.class);
    Mockito.when(proxyAdditionalServlet.getBasePath()).thenReturn(BASE_PATH);
    Mockito.when(proxyAdditionalServlet.getServletHolder()).thenReturn(new ServletHolder(servlet));
    AdditionalServlets proxyAdditionalServlets = Mockito.mock(AdditionalServlets.class);
    Map<String, AdditionalServletWithClassLoader> map = new HashMap<>();
    map.put("prometheus-proxy-servlet", new AdditionalServletWithClassLoader(proxyAdditionalServlet, null));
    Mockito.when(proxyAdditionalServlets.getServlets()).thenReturn(map);
    Mockito.when(proxyService.getProxyAdditionalServlets()).thenReturn(proxyAdditionalServlets);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) ServletOutputStream(javax.servlet.ServletOutputStream) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletConfig(javax.servlet.ServletConfig) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) Servlet(javax.servlet.Servlet) AdditionalServlet(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet) AdditionalServletWithClassLoader(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader) AdditionalServlets(org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)

Aggregations

AdditionalServletWithClassLoader (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithClassLoader)12 AdditionalServlet (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlet)9 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)9 HashMap (java.util.HashMap)6 Servlet (javax.servlet.Servlet)6 AdditionalServletWithPulsarService (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServletWithPulsarService)6 AdditionalServlets (org.apache.pulsar.broker.web.plugin.servlet.AdditionalServlets)6 ServletConfig (javax.servlet.ServletConfig)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 ServletRequest (javax.servlet.ServletRequest)3 ServletResponse (javax.servlet.ServletResponse)3 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)3 PulsarConfiguration (org.apache.pulsar.common.configuration.PulsarConfiguration)3 WebSocketConsumerServlet (org.apache.pulsar.websocket.WebSocketConsumerServlet)3 WebSocketPingPongServlet (org.apache.pulsar.websocket.WebSocketPingPongServlet)3 WebSocketProducerServlet (org.apache.pulsar.websocket.WebSocketProducerServlet)3 WebSocketReaderServlet (org.apache.pulsar.websocket.WebSocketReaderServlet)3 WebSocketService (org.apache.pulsar.websocket.WebSocketService)3 ProxyServlet (org.eclipse.jetty.proxy.ProxyServlet)3 WebSocketServlet (org.eclipse.jetty.websocket.servlet.WebSocketServlet)3