io.micronaut.azure.function.http.test.AzureFunctionEmbeddedServer Maven / Gradle / Ivy
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.azure.function.http.test;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatusType;
import io.micronaut.azure.function.http.AzureFunctionHttpRequest;
import io.micronaut.azure.function.http.AzureFunctionHttpResponse;
import io.micronaut.azure.function.http.HttpRequestMessageBuilder;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.io.IOUtils;
import io.micronaut.function.BinaryTypeConfiguration;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.server.HttpServerConfiguration;
import io.micronaut.http.server.exceptions.HttpServerException;
import io.micronaut.http.server.exceptions.ServerStartupException;
import io.micronaut.runtime.ApplicationConfiguration;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.servlet.http.BodyBuilder;
import io.micronaut.servlet.http.ServletExchange;
import io.micronaut.servlet.http.ServletHttpHandler;
import io.micronaut.servlet.http.ServletHttpResponse;
import jakarta.inject.Singleton;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.LogManager;
import java.util.logging.Logger;
/**
* Server used for testing Azure HTTP functions.
*
* @author gkrocher
* @since 2.0.0
*/
@Singleton
@Internal
final class AzureFunctionEmbeddedServer implements EmbeddedServer {
private final ApplicationContext applicationContext;
private final ServerPort serverPort;
private final AtomicBoolean running = new AtomicBoolean(false);
private Server server;
/**
* Default constructor.
* @param applicationContext the app context
* @param httpServerConfiguration the http server configuration
*/
AzureFunctionEmbeddedServer(
ApplicationContext applicationContext,
HttpServerConfiguration httpServerConfiguration
) {
this.applicationContext = applicationContext;
this.serverPort = createServerPort(httpServerConfiguration);
}
private ServerPort createServerPort(HttpServerConfiguration httpServerConfiguration) {
Optional portOpt = httpServerConfiguration.getPort();
if (portOpt.isPresent()) {
Integer port = portOpt.get();
if (port == -1) {
return new ServerPort(true, 0);
} else {
return new ServerPort(false, port);
}
} else {
if (applicationContext.getEnvironment().getActiveNames().contains(Environment.TEST)) {
return new ServerPort(true, 0);
} else {
return new ServerPort(false, 8080);
}
}
}
@Override
public EmbeddedServer start() {
if (running.compareAndSet(false, true)) {
int port = serverPort.port();
try {
this.server = new Server(port);
this.server.setHandler(new AzureHandler(applicationContext, applicationContext.getConversionService()));
this.server.start();
} catch (Exception e) {
throw new ServerStartupException(e.getMessage(), e);
}
}
return this;
}
@Override
public EmbeddedServer stop() {
if (running.compareAndSet(true, false)) {
try {
applicationContext.stop();
server.stop();
} catch (Exception e) {
// ignore / unrecoverable
}
}
return this;
}
@Override
public int getPort() {
return server.getURI().getPort();
}
@Override
public String getHost() {
return "localhost";
}
@Override
public String getScheme() {
return "http";
}
@Override
public URL getURL() {
String spec = getScheme() + "://" + getHost() + ":" + getPort();
try {
return new URL(spec);
} catch (MalformedURLException e) {
throw new HttpServerException("Invalid server URL " + spec);
}
}
@Override
public URI getURI() {
try {
return getURL().toURI();
} catch (URISyntaxException e) {
throw new HttpServerException("Invalid server URL " + getURL());
}
}
@Override
public ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public ApplicationConfiguration getApplicationConfiguration() {
return applicationContext.getBean(ApplicationConfiguration.class);
}
@Override
public boolean isRunning() {
return running.get();
}
/**
* Internal handler.
*/
private static final class AzureHandler extends AbstractHandler {
private final ServletHttpHandler>, HttpResponseMessage> httpHandler;
/**
* Default constructor.
* @param applicationContext The app context
*/
AzureHandler(ApplicationContext applicationContext, ConversionService conversionService) {
httpHandler = new ServletHttpHandler<>(applicationContext, conversionService) {
@Override
public boolean isRunning() {
return applicationContext.isRunning();
}
@Override
protected ServletExchange>, HttpResponseMessage> createExchange(HttpRequestMessage> request, HttpResponseMessage response) {
throw new UnsupportedOperationException("Creating the exchange directly is not supported");
}
};
}
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpRequestMessageBuilder
© 2015 - 2025 Weber Informatics LLC | Privacy Policy