Search in sources :

Example 1 with DefaultWebApplication

use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.

the class MicroInnerDeployer method getWebApplication.

WebApplication getWebApplication(Archive<?> archive, ClassLoader newClassLoader) {
    WebApplication webApplication = new DefaultWebApplication();
    webApplication.setClassLoader(newClassLoader);
    // The main resource representing the (war) archive itself.
    webApplication.addResource(new ShrinkWrapResource(archive));
    // Get the list of embedded archives containing a "/META-INF/resources" folder.
    Node resourceNodes = archive.get("/META-INF/piranha/resource-libs");
    if (resourceNodes != null) {
        for (Node resourceNode : resourceNodes.getChildren()) {
            ArchiveAsset resourceArchiveAsset = (ArchiveAsset) resourceNode.getAsset();
            // Add the archive as a resource with the "/META-INF/resources" folder shifted to its root
            webApplication.addResource(new ShrinkWrapResource("/META-INF/resources", resourceArchiveAsset.getArchive()));
        }
    }
    return webApplication;
}
Also used : DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) ShrinkWrapResource(cloud.piranha.resource.shrinkwrap.ShrinkWrapResource) Node(org.jboss.shrinkwrap.api.Node) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) WebApplication(cloud.piranha.core.api.WebApplication)

Example 2 with DefaultWebApplication

use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.

the class MicroPiranha method run.

/**
 * Run method.
 */
@Override
public void run() {
    long startTime = System.currentTimeMillis();
    LOGGER.log(INFO, () -> "Starting Piranha");
    webApplicationServer = new HttpWebApplicationServer();
    if (httpPort > 0) {
        HttpServer httpServer = ServiceLoader.load(HttpServer.class).findFirst().orElseThrow();
        httpServer.setServerPort(httpPort);
        httpServer.setHttpServerProcessor(webApplicationServer);
        httpServer.start();
    }
    if (httpsPort > 0) {
        HttpServer httpsServer = ServiceLoader.load(HttpServer.class).findFirst().orElseThrow();
        httpsServer.setHttpServerProcessor(webApplicationServer);
        httpsServer.setServerPort(httpsPort);
        httpsServer.setSSL(true);
        httpsServer.start();
    }
    String contextPath = null;
    if (warFile != null && warFile.getName().toLowerCase().endsWith(".war")) {
        contextPath = warFile.getName().substring(0, warFile.getName().length() - 4);
        if (webAppDir == null) {
            webAppDir = new File(contextPath);
        }
        extractWarFile(warFile, webAppDir);
    }
    if (webAppDir != null && webAppDir.exists()) {
        if (contextPath == null) {
            contextPath = webAppDir.getName();
        }
        DefaultWebApplication webApplication = new DefaultWebApplication();
        webApplication.addResource(new DirectoryResource(webAppDir));
        DefaultWebApplicationClassLoader classLoader = new DefaultWebApplicationClassLoader(webAppDir);
        webApplication.setClassLoader(classLoader);
        if (Boolean.getBoolean("cloud.piranha.modular.enable") || jpmsEnabled) {
            setupLayers(classLoader);
        }
        if (classLoader.getResource("/META-INF/services/" + WebApplicationExtension.class.getName()) == null) {
            DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
            extensionContext.add(extensionClass);
            extensionContext.configure(webApplication);
        } else {
            DefaultWebApplicationExtensionContext extensionContext = new DefaultWebApplicationExtensionContext();
            ServiceLoader<WebApplicationExtension> serviceLoader = ServiceLoader.load(WebApplicationExtension.class, classLoader);
            extensionContext.add(serviceLoader.iterator().next());
            extensionContext.configure(webApplication);
        }
        if (contextPath.equalsIgnoreCase("ROOT")) {
            contextPath = "";
        } else if (!contextPath.startsWith("/")) {
            contextPath = "/" + contextPath;
        }
        webApplication.setContextPath(contextPath);
        webApplicationServer.addWebApplication(webApplication);
        try {
            webApplication.initialize();
        } catch (Exception e) {
            LOGGER.log(ERROR, "Failed to initialize web application");
            System.exit(0);
        }
    }
    if (webAppDir == null && warFile == null) {
        LOGGER.log(WARNING, "No web application deployed");
    }
    webApplicationServer.start();
    long finishTime = System.currentTimeMillis();
    LOGGER.log(INFO, "Started Piranha");
    LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);
    if (pid != null) {
        File pidFile = new File("tmp", "piranha-micro.pid");
        if (!pidFile.getParentFile().exists()) {
            if (!pidFile.getParentFile().mkdirs()) {
                LOGGER.log(WARNING, "Unable to create tmp directory for PID file");
            }
        }
        try (PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) {
            writer.println(pid);
            writer.flush();
        } catch (IOException ioe) {
            LOGGER.log(WARNING, "Unable to write PID file", ioe);
        }
    }
    while (!stop) {
        if (pid != null) {
            File pidFile = new File("tmp", "piranha-micro.pid");
            if (!pidFile.exists()) {
                stop();
            }
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) DefaultWebApplicationExtensionContext(cloud.piranha.core.impl.DefaultWebApplicationExtensionContext) DefaultWebApplicationClassLoader(cloud.piranha.core.impl.DefaultWebApplicationClassLoader) WebApplicationExtension(cloud.piranha.core.api.WebApplicationExtension) FileWriter(java.io.FileWriter) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HttpWebApplicationServer(cloud.piranha.http.webapp.HttpWebApplicationServer) DirectoryResource(cloud.piranha.resource.impl.DirectoryResource) HttpServer(cloud.piranha.http.api.HttpServer) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 3 with DefaultWebApplication

use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.

the class NanoPiranhaBuilder method build.

/**
 * Build Piranha Nano.
 *
 * @return our instance of Piranha Nano.
 */
public NanoPiranha build() {
    NanoPiranha piranha = new NanoPiranha();
    if (webApplication == null) {
        webApplication = new DefaultWebApplication();
    }
    piranha.setWebApplication(webApplication);
    resources.forEach(resource -> webApplication.addResource(resource));
    filters.entrySet().forEach(entry -> {
        String filterName = entry.getKey();
        Filter filter = entry.getValue();
        NanoFilterConfig filterConfig = new NanoFilterConfig(piranha.getWebApplication());
        filterConfig.setFilterName(filterName);
        Map<String, String> initParameters = filterInitParameters.get(filterName);
        if (initParameters != null) {
            initParameters.entrySet().forEach(initParameter -> {
                String name = initParameter.getKey();
                String value = initParameter.getValue();
                filterConfig.setInitParameter(name, value);
            });
        }
        try {
            filter.init(filterConfig);
        } catch (ServletException se) {
            throw new RuntimeException("Unable to initialize filter", se);
        }
        piranha.addFilter(filter);
    });
    if (servlet != null) {
        NanoServletConfig servletConfig = new NanoServletConfig(piranha.getWebApplication());
        servletConfig.setServletName(servletName);
        servletInitParameters.entrySet().forEach(entry -> {
            String name = entry.getKey();
            String value = entry.getValue();
            servletConfig.setInitParameter(name, value);
        });
        try {
            servlet.init(servletConfig);
        } catch (ServletException se) {
            throw new RuntimeException("Unable to initialize servlet", se);
        }
        piranha.setServlet(servlet);
    }
    return piranha;
}
Also used : ServletException(jakarta.servlet.ServletException) DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) Filter(jakarta.servlet.Filter)

Example 4 with DefaultWebApplication

use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.

the class NanoRequestTest method testLogout.

/**
 * Test logout method.
 */
@Test
void testLogout() {
    try {
        DefaultWebApplication webApplication = new DefaultWebApplication();
        NanoResponse response = new NanoResponse();
        response.setWebApplication(webApplication);
        NanoRequest request = new NanoRequest();
        request.setWebApplication(webApplication);
        webApplication.linkRequestAndResponse(request, response);
        request.logout();
    } catch (ServletException ex) {
        fail();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultWebApplication

use of cloud.piranha.core.impl.DefaultWebApplication in project piranha by piranhacloud.

the class NanoRequestTest method testChangeSessionId.

/**
 * Test changeSessionId method.
 */
@Test
void testChangeSessionId() {
    DefaultWebApplication webApplication = new DefaultWebApplication();
    NanoResponse response = new NanoResponse();
    response.setWebApplication(webApplication);
    NanoRequest request = new NanoRequest();
    request.setWebApplication(webApplication);
    webApplication.linkRequestAndResponse(request, response);
    String sessionId1 = request.getSession(true).getId();
    String sessionId2 = request.changeSessionId();
    webApplication.unlinkRequestAndResponse(request, response);
    assertNotEquals(sessionId1, sessionId2);
}
Also used : DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultWebApplication (cloud.piranha.core.impl.DefaultWebApplication)101 Test (org.junit.jupiter.api.Test)97 File (java.io.File)46 DirectoryResource (cloud.piranha.resource.impl.DirectoryResource)31 DefaultWebApplicationClassLoader (cloud.piranha.core.impl.DefaultWebApplicationClassLoader)20 AnnotationScanInitializer (cloud.piranha.extension.annotationscan.AnnotationScanInitializer)16 InternalAnnotationScanAnnotationManager (cloud.piranha.extension.annotationscan.internal.InternalAnnotationScanAnnotationManager)16 ClassResource (cloud.piranha.resource.impl.ClassResource)16 DefaultWebApplicationExtensionContext (cloud.piranha.core.impl.DefaultWebApplicationExtensionContext)15 DefaultWebApplicationResponse (cloud.piranha.core.impl.DefaultWebApplicationResponse)15 DefaultWebApplicationRequest (cloud.piranha.core.impl.DefaultWebApplicationRequest)14 ServletException (jakarta.servlet.ServletException)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 EmbeddedRequest (cloud.piranha.embedded.EmbeddedRequest)7 EmbeddedResponse (cloud.piranha.embedded.EmbeddedResponse)7 WebXmlInitializer (cloud.piranha.extension.webxml.WebXmlInitializer)7 ServletRegistration (jakarta.servlet.ServletRegistration)7 EmbeddedRequestBuilder (cloud.piranha.embedded.EmbeddedRequestBuilder)6 EmbeddedResponseBuilder (cloud.piranha.embedded.EmbeddedResponseBuilder)6 IOException (java.io.IOException)6