Search in sources :

Example 1 with AppDTO

use of org.apache.activemq.artemis.dto.AppDTO in project activemq-artemis by apache.

the class ReplicatedFailoverTest method testReplicatedFailbackBackupFromLiveBackToBackup.

@Test
public void testReplicatedFailbackBackupFromLiveBackToBackup() throws Exception {
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8787);
    HttpServer httpServer = HttpServer.create(address, 100);
    httpServer.start();
    try {
        httpServer.createContext("/", new HttpHandler() {

            @Override
            public void handle(HttpExchange t) throws IOException {
                String response = "<html><body><b>This is a unit test</b></body></html>";
                t.sendResponseHeaders(200, response.length());
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
        });
        WebServerDTO wdto = new WebServerDTO();
        AppDTO appDTO = new AppDTO();
        appDTO.war = "console.war";
        appDTO.url = "console";
        wdto.apps = new ArrayList<AppDTO>();
        wdto.apps.add(appDTO);
        wdto.bind = "http://localhost:0";
        wdto.path = "console";
        WebServerComponent webServerComponent = new WebServerComponent();
        webServerComponent.configure(wdto, ".", ".");
        webServerComponent.start();
        backupServer.getServer().getNetworkHealthCheck().parseURIList("http://localhost:8787");
        Assert.assertTrue(backupServer.getServer().getNetworkHealthCheck().isStarted());
        backupServer.getServer().addExternalComponent(webServerComponent);
        // this is called when backup servers go from live back to backup
        backupServer.getServer().fail(true);
        Assert.assertTrue(backupServer.getServer().getNetworkHealthCheck().isStarted());
        Assert.assertTrue(backupServer.getServer().getExternalComponents().get(0).isStarted());
        ((ServiceComponent) (backupServer.getServer().getExternalComponents().get(0))).stop(true);
    } finally {
        httpServer.stop(0);
    }
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) ServiceComponent(org.apache.activemq.artemis.core.server.ServiceComponent) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) WebServerDTO(org.apache.activemq.artemis.dto.WebServerDTO) AppDTO(org.apache.activemq.artemis.dto.AppDTO) HttpServer(com.sun.net.httpserver.HttpServer) WebServerComponent(org.apache.activemq.artemis.component.WebServerComponent) Test(org.junit.Test)

Example 2 with AppDTO

use of org.apache.activemq.artemis.dto.AppDTO in project activemq-artemis by apache.

the class WebServerComponent method configure.

@Override
public void configure(ComponentDTO config, String artemisInstance, String artemisHome) throws Exception {
    webServerConfig = (WebServerDTO) config;
    uri = new URI(webServerConfig.bind);
    server = new Server();
    String scheme = uri.getScheme();
    if ("https".equals(scheme)) {
        SslContextFactory sslFactory = new SslContextFactory();
        sslFactory.setKeyStorePath(webServerConfig.keyStorePath == null ? artemisInstance + "/etc/keystore.jks" : webServerConfig.keyStorePath);
        sslFactory.setKeyStorePassword(webServerConfig.getKeyStorePassword() == null ? "password" : webServerConfig.getKeyStorePassword());
        if (webServerConfig.clientAuth != null) {
            sslFactory.setNeedClientAuth(webServerConfig.clientAuth);
            if (webServerConfig.clientAuth) {
                sslFactory.setTrustStorePath(webServerConfig.trustStorePath);
                sslFactory.setTrustStorePassword(webServerConfig.getTrustStorePassword());
            }
        }
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslFactory, "HTTP/1.1");
        HttpConfiguration https = new HttpConfiguration();
        https.addCustomizer(new SecureRequestCustomizer());
        HttpConnectionFactory httpFactory = new HttpConnectionFactory(https);
        connector = new ServerConnector(server, sslConnectionFactory, httpFactory);
    } else {
        connector = new ServerConnector(server);
    }
    connector.setPort(uri.getPort());
    connector.setHost(uri.getHost());
    server.setConnectors(new Connector[] { connector });
    handlers = new HandlerList();
    Path homeWarDir = Paths.get(artemisHome != null ? artemisHome : ".").resolve(webServerConfig.path).toAbsolutePath();
    Path instanceWarDir = Paths.get(artemisInstance != null ? artemisInstance : ".").resolve(webServerConfig.path).toAbsolutePath();
    if (webServerConfig.apps != null && webServerConfig.apps.size() > 0) {
        webContexts = new ArrayList<>();
        for (AppDTO app : webServerConfig.apps) {
            Path dirToUse = homeWarDir;
            if (new File(instanceWarDir.toFile().toString() + File.separator + app.war).exists()) {
                dirToUse = instanceWarDir;
            }
            WebAppContext webContext = deployWar(app.url, app.war, dirToUse);
            webContexts.add(webContext);
            if (app.war.startsWith("console")) {
                consoleUrl = webServerConfig.bind + "/" + app.url;
            }
        }
    }
    ResourceHandler homeResourceHandler = new ResourceHandler();
    homeResourceHandler.setResourceBase(homeWarDir.toString());
    homeResourceHandler.setDirectoriesListed(false);
    homeResourceHandler.setWelcomeFiles(new String[] { "index.html" });
    ContextHandler homeContext = new ContextHandler();
    homeContext.setContextPath("/");
    homeContext.setResourceBase(homeWarDir.toString());
    homeContext.setHandler(homeResourceHandler);
    homeContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    ResourceHandler instanceResourceHandler = new ResourceHandler();
    instanceResourceHandler.setResourceBase(instanceWarDir.toString());
    instanceResourceHandler.setDirectoriesListed(false);
    instanceResourceHandler.setWelcomeFiles(new String[] { "index.html" });
    ContextHandler instanceContext = new ContextHandler();
    instanceContext.setContextPath("/");
    instanceContext.setResourceBase(instanceWarDir.toString());
    instanceContext.setHandler(instanceResourceHandler);
    homeContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setServeIcon(false);
    handlers.addHandler(homeContext);
    handlers.addHandler(instanceContext);
    handlers.addHandler(defaultHandler);
    server.setHandler(handlers);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Path(java.nio.file.Path) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) AppDTO(org.apache.activemq.artemis.dto.AppDTO) File(java.io.File)

Aggregations

AppDTO (org.apache.activemq.artemis.dto.AppDTO)2 HttpExchange (com.sun.net.httpserver.HttpExchange)1 HttpHandler (com.sun.net.httpserver.HttpHandler)1 HttpServer (com.sun.net.httpserver.HttpServer)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 WebServerComponent (org.apache.activemq.artemis.component.WebServerComponent)1 ServiceComponent (org.apache.activemq.artemis.core.server.ServiceComponent)1 WebServerDTO (org.apache.activemq.artemis.dto.WebServerDTO)1 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)1 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)1 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)1 Server (org.eclipse.jetty.server.Server)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1