Search in sources :

Example 11 with HttpServer

use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.

the class HttpServerTest method testProcessing.

/**
 * Test processing.
 *
 * @throws Exception when an error occurs.
 */
@Test
void testProcessing() throws Exception {
    int port = findPort();
    HttpServer server = createServer(port);
    server.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create(HTTP_LOCALHOST_PREFIX + port)).build();
        HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        assertEquals(200, response.statusCode());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } finally {
        server.stop();
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) HttpClient(java.net.http.HttpClient) HttpServer(cloud.piranha.http.api.HttpServer) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 12 with HttpServer

use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.

the class HttpServerTest method testProcessing2.

/**
 * Test processing.
 *
 * @throws Exception when an error occurs.
 */
@Test
void testProcessing2() throws Exception {
    int port = findPort();
    HttpServer server = createServer(port);
    server.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create(HTTP_LOCALHOST_PREFIX + port)).build();
        HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        assertEquals(200, response.statusCode());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } finally {
        server.stop();
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) HttpClient(java.net.http.HttpClient) HttpServer(cloud.piranha.http.api.HttpServer) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 13 with HttpServer

use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.

the class HttpWebApplicationServerTest method testProcess.

/**
 * Test process method.
 *
 * @throws Exception when a serious error occurs.
 */
@Test
void testProcess() throws Exception {
    HttpWebApplicationServer server = new HttpWebApplicationServer();
    HttpServer httpServer = new DefaultHttpServer(8180, server, false);
    DefaultWebApplication application = new DefaultWebApplication();
    application.setContextPath("/context");
    application.addServlet("snoop", new TestSnoopServlet());
    application.addServletMapping("snoop", "/snoop/*");
    server.addWebApplication(application);
    server.initialize();
    server.start();
    httpServer.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(new URI("http://localhost:8180/context/snoop/index.html")).build();
        HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        assertEquals(200, response.statusCode());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    httpServer.stop();
    server.stop();
}
Also used : HttpRequest(java.net.http.HttpRequest) DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) DefaultHttpServer(cloud.piranha.http.impl.DefaultHttpServer) HttpClient(java.net.http.HttpClient) DefaultHttpServer(cloud.piranha.http.impl.DefaultHttpServer) HttpServer(cloud.piranha.http.api.HttpServer) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 14 with HttpServer

use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.

the class HttpWebApplicationServerTest method testSessionUrlRewriting2.

@Test
void testSessionUrlRewriting2() throws Exception {
    HttpWebApplicationServer server = new HttpWebApplicationServer();
    HttpServer httpServer = new DefaultHttpServer(8182, server, false);
    DefaultWebApplication application = new DefaultWebApplication();
    application.setContextPath("/context");
    application.addServlet("snoop", new TestSnoopServlet());
    application.addServletMapping("snoop", "/snoop/*");
    server.addWebApplication(application);
    server.initialize();
    server.start();
    httpServer.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(new URI("http://localhost:8182/context/snoop/index.html;jsessionid=sessionIdURL")).headers("Cookie", "JSESSIONID=sessionIdCookie").build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        assertEquals(200, response.statusCode());
        assertTrue(response.body().contains("sessionIdCookie"));
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    httpServer.stop();
    server.stop();
}
Also used : HttpRequest(java.net.http.HttpRequest) DefaultWebApplication(cloud.piranha.core.impl.DefaultWebApplication) DefaultHttpServer(cloud.piranha.http.impl.DefaultHttpServer) HttpClient(java.net.http.HttpClient) DefaultHttpServer(cloud.piranha.http.impl.DefaultHttpServer) HttpServer(cloud.piranha.http.api.HttpServer) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 15 with HttpServer

use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.

the class IsolatedServerPiranha method run.

/**
 * Start method.
 */
@Override
public void run() {
    long startTime = System.currentTimeMillis();
    LOGGER.log(INFO, () -> "Starting Piranha");
    webApplicationServer = new HttpWebApplicationServer();
    HttpServer httpServer = ServiceLoader.load(HttpServer.class).findFirst().orElseThrow();
    httpServer.setServerPort(8080);
    httpServer.setHttpServerProcessor(webApplicationServer);
    httpServer.setSSL(ssl);
    httpServer.start();
    webApplicationServer.start();
    File[] webapps = new File("webapps").listFiles();
    if (webapps != null) {
        if (webapps.length != 0) {
            // Limit threads used by Weld, since default is Runtime.getRuntime().availableProcessors(), which is per deployment.
            int threadsPerApp = Math.max(2, Runtime.getRuntime().availableProcessors() / webapps.length);
            System.setProperty("org.jboss.weld.executor.threadPoolSize", threadsPerApp + "");
        }
        File deployingFile = createDeployingFile();
        Arrays.stream(webapps).parallel().filter(warFile -> warFile.getName().toLowerCase().endsWith(".war")).forEach(warFile -> deploy(warFile, webApplicationServer));
        try {
            Files.delete(deployingFile.toPath());
        } catch (IOException ioe) {
            LOGGER.log(WARNING, "Unable to delete deploying file", ioe);
        }
    }
    long finishTime = System.currentTimeMillis();
    LOGGER.log(INFO, "Started Piranha");
    LOGGER.log(INFO, "It took {0} milliseconds", finishTime - startTime);
    File startedFile = createStartedFile();
    File pidFile = new File("tmp/piranha.pid");
    while (httpServer.isRunning()) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        if (!pidFile.exists()) {
            webApplicationServer.stop();
            httpServer.stop();
            try {
                Files.delete(startedFile.toPath());
            } catch (IOException ioe) {
                LOGGER.log(WARNING, "Unable to delete PID file", ioe);
            }
            System.exit(0);
        }
    }
    finishTime = System.currentTimeMillis();
    LOGGER.log(INFO, "Stopped Piranha");
    LOGGER.log(INFO, "We ran for {0} milliseconds", finishTime - startTime);
}
Also used : Piranha(cloud.piranha.core.api.Piranha) ZipImporter(org.jboss.shrinkwrap.api.importer.ZipImporter) Arrays(java.util.Arrays) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) WebApplicationRequest(cloud.piranha.core.api.WebApplicationRequest) WARNING(java.lang.System.Logger.Level.WARNING) Files(java.nio.file.Files) HttpServer(cloud.piranha.http.api.HttpServer) INFO(java.lang.System.Logger.Level.INFO) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) ServletException(jakarta.servlet.ServletException) File(java.io.File) HttpWebApplicationServer(cloud.piranha.http.webapp.HttpWebApplicationServer) WebApplicationResponse(cloud.piranha.core.api.WebApplicationResponse) Logger(java.lang.System.Logger) MicroWebApplication(cloud.piranha.micro.shrinkwrap.builder.MicroWebApplication) MicroOuterDeployer(cloud.piranha.micro.shrinkwrap.loader.MicroOuterDeployer) MicroConfiguration(cloud.piranha.micro.shrinkwrap.loader.MicroConfiguration) Level(java.lang.System.Logger.Level) HttpServer(cloud.piranha.http.api.HttpServer) IOException(java.io.IOException) File(java.io.File) HttpWebApplicationServer(cloud.piranha.http.webapp.HttpWebApplicationServer)

Aggregations

HttpServer (cloud.piranha.http.api.HttpServer)15 IOException (java.io.IOException)12 Test (org.junit.jupiter.api.Test)12 HttpClient (java.net.http.HttpClient)9 HttpRequest (java.net.http.HttpRequest)9 DefaultWebApplication (cloud.piranha.core.impl.DefaultWebApplication)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DefaultHttpServer (cloud.piranha.http.impl.DefaultHttpServer)3 HttpWebApplicationServer (cloud.piranha.http.webapp.HttpWebApplicationServer)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 WebApplicationExtension (cloud.piranha.core.api.WebApplicationExtension)2 DefaultWebApplicationExtensionContext (cloud.piranha.core.impl.DefaultWebApplicationExtensionContext)2 HttpServerRequest (cloud.piranha.http.api.HttpServerRequest)2 HttpServerResponse (cloud.piranha.http.api.HttpServerResponse)2 ServletException (jakarta.servlet.ServletException)2 File (java.io.File)2 URI (java.net.URI)2 AnnotationManager (cloud.piranha.core.api.AnnotationManager)1 Piranha (cloud.piranha.core.api.Piranha)1