Search in sources :

Example 1 with EmbeddedServer

use of io.micronaut.runtime.server.EmbeddedServer in project micronaut-gcp by micronaut-projects.

the class InvokerHttpServer method start.

@Override
public EmbeddedServer start() {
    if (running.compareAndSet(false, true)) {
        int retryCount = 0;
        while (retryCount <= 3) {
            try {
                this.server = new Server(port);
                ServletContextHandler servletContextHandler = new ServletContextHandler();
                servletContextHandler.setContextPath("/");
                server.setHandler(NotFoundHandler.forServlet(servletContextHandler));
                HttpFunction httpFunction = new HttpFunction() {

                    @Override
                    protected ApplicationContext buildApplicationContext(@Nullable Object context) {
                        ApplicationContext ctx = InvokerHttpServer.this.getApplicationContext();
                        this.applicationContext = ctx;
                        return ctx;
                    }
                };
                HttpServlet servlet = new HttpServlet() {

                    @Override
                    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
                        try {
                            httpFunction.service(new HttpRequestImpl(req), new HttpResponseImpl(resp));
                        } catch (Exception e) {
                            throw new ServletException(e);
                        }
                    }
                };
                ServletHolder servletHolder = new ServletHolder(servlet);
                servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(""));
                servletContextHandler.addServlet(servletHolder, "/*");
                server.start();
                logServerInfo();
                break;
            } catch (BindException e) {
                if (randomPort) {
                    this.port = SocketUtils.findAvailableTcpPort();
                    retryCount++;
                } else {
                    throw new ServerStartupException(e.getMessage(), e);
                }
            } catch (Exception e) {
                throw new ServerStartupException("Error starting Google Cloud Function server: " + e.getMessage(), e);
            }
        }
    }
    return this;
}
Also used : Server(org.eclipse.jetty.server.Server) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) HttpServlet(javax.servlet.http.HttpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) HttpServerException(io.micronaut.http.server.exceptions.HttpServerException) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ApplicationContext(io.micronaut.context.ApplicationContext) MultipartConfigElement(javax.servlet.MultipartConfigElement) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException) HttpFunction(io.micronaut.gcp.function.http.HttpFunction) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Nullable(io.micronaut.core.annotation.Nullable)

Example 2 with EmbeddedServer

use of io.micronaut.runtime.server.EmbeddedServer in project micronaut-views by micronaut-projects.

the class SecurityViewModelProcessorTest method aCustomSecurityPropertyNameCanBeInjectedToTheModel.

@Test
void aCustomSecurityPropertyNameCanBeInjectedToTheModel() {
    // given:
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "SecurityViewModelProcessorSpec", "micronaut.views.soy.enabled", StringUtils.FALSE, "micronaut.security.views-model-decorator.security-key", "securitycustom"));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(BooksController.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(MockAuthenticationProvider.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(SecurityViewModelProcessor.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/").basicAuth("john", "secret");
    HttpResponse<String> response = httpClient.toBlocking().exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    // and:
    assertFalse(html.contains("User: john"));
    // and:
    assertTrue(html.contains("Custom: john"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : SecurityViewModelProcessor(io.micronaut.views.model.security.SecurityViewModelProcessor) MockAuthenticationProvider(io.micronaut.views.model.security.MockAuthenticationProvider) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) BooksController(io.micronaut.views.model.security.BooksController) Test(org.junit.jupiter.api.Test)

Example 3 with EmbeddedServer

use of io.micronaut.runtime.server.EmbeddedServer in project micronaut-views by micronaut-projects.

the class UnmodifiableModelAndViewTest method aModifiableViewModelStillAddsSecurity.

@Test
void aModifiableViewModelStillAddsSecurity() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "UnmodifiableModelAndViewSpec", "micronaut.views.soy.enabled", StringUtils.FALSE));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // given:
    BlockingHttpClient client = httpClient.toBlocking();
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(UnmodifiableFruitsController.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/modifiable").basicAuth("john", "secret");
    HttpResponse<String> response = client.exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    // and:
    assertTrue(html.contains("<blink>Security was added</blink>"));
    // and:
    assertTrue(html.contains("<h1>fruit: plum</h1>"));
    // and:
    assertTrue(html.contains("<h1>color: plum</h1>"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : UnmodifiableFruitsController(io.micronaut.views.model.UnmodifiableFruitsController) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 4 with EmbeddedServer

use of io.micronaut.runtime.server.EmbeddedServer in project micronaut-views by micronaut-projects.

the class UnmodifiableModelAndViewTest method anUnmodifiableViewModelReportsErrorButDoesNotCrash.

@Test
void anUnmodifiableViewModelReportsErrorButDoesNotCrash() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "UnmodifiableModelAndViewSpec", "micronaut.views.soy.enabled", StringUtils.FALSE));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // given:
    BlockingHttpClient client = httpClient.toBlocking();
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(UnmodifiableFruitsController.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/unmodifiable").basicAuth("john", "secret");
    HttpResponse<String> response = client.exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    // and:
    assertTrue(html.contains("<blink>Security was added</blink>"));
    // and:
    assertTrue(html.contains("<h1>fruit: plum</h1>"));
    // and:
    assertTrue(html.contains("<h1>color: plum</h1>"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : UnmodifiableFruitsController(io.micronaut.views.model.UnmodifiableFruitsController) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 5 with EmbeddedServer

use of io.micronaut.runtime.server.EmbeddedServer in project micronaut-views by micronaut-projects.

the class SecurityViewModelProcessorTest method securityPropertyIsInjectedToTheModel.

@Test
void securityPropertyIsInjectedToTheModel() {
    // given:
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "SecurityViewModelProcessorSpec", "micronaut.views.soy.enabled", StringUtils.FALSE));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(BooksController.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(MockAuthenticationProvider.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(SecurityViewModelProcessor.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/").basicAuth("john", "secret");
    HttpResponse<String> response = httpClient.toBlocking().exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    assertTrue(html.contains("User: john email: john@email.com"));
    // and:
    assertTrue(html.contains("Developing Microservices"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : SecurityViewModelProcessor(io.micronaut.views.model.security.SecurityViewModelProcessor) MockAuthenticationProvider(io.micronaut.views.model.security.MockAuthenticationProvider) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) BooksController(io.micronaut.views.model.security.BooksController) Test(org.junit.jupiter.api.Test)

Aggregations

EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)5 HttpClient (io.micronaut.http.client.HttpClient)4 Test (org.junit.jupiter.api.Test)4 BlockingHttpClient (io.micronaut.http.client.BlockingHttpClient)2 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)2 UnmodifiableFruitsController (io.micronaut.views.model.UnmodifiableFruitsController)2 BooksController (io.micronaut.views.model.security.BooksController)2 MockAuthenticationProvider (io.micronaut.views.model.security.MockAuthenticationProvider)2 SecurityViewModelProcessor (io.micronaut.views.model.security.SecurityViewModelProcessor)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 Nullable (io.micronaut.core.annotation.Nullable)1 HttpFunction (io.micronaut.gcp.function.http.HttpFunction)1 HttpServerException (io.micronaut.http.server.exceptions.HttpServerException)1 ServerStartupException (io.micronaut.http.server.exceptions.ServerStartupException)1 IOException (java.io.IOException)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1