org.mockserver.responsewriter.NettyResponseWriter Maven / Gradle / Ivy
package org.mockserver.responsewriter;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.mockserver.cors.CORSHeaders;
import org.mockserver.log.model.MessageLogEntry;
import org.mockserver.logging.MockServerLogger;
import org.mockserver.model.ConnectionOptions;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static org.mockserver.configuration.ConfigurationProperties.enableCORSForAPI;
import static org.mockserver.configuration.ConfigurationProperties.enableCORSForAllResponses;
import static org.mockserver.mock.HttpStateHandler.PATH_PREFIX;
import static org.mockserver.model.Header.header;
import static org.mockserver.model.HttpResponse.notFoundResponse;
import static org.mockserver.model.HttpResponse.response;
/**
* @author jamesdbloom
*/
public class NettyResponseWriter extends ResponseWriter {
private final ChannelHandlerContext ctx;
private final MockServerLogger mockServerLogger;
private CORSHeaders addCORSHeaders = new CORSHeaders();
public NettyResponseWriter(ChannelHandlerContext ctx, MockServerLogger mockServerLogger) {
this.mockServerLogger = mockServerLogger;
this.ctx = ctx;
}
@Override
public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus) {
writeResponse(request, responseStatus, "", "application/json");
}
@Override
public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus, String body, String contentType) {
HttpResponse response = response()
.withStatusCode(responseStatus.code())
.withBody(body);
if (body != null && !body.isEmpty()) {
response.replaceHeader(header(CONTENT_TYPE.toString(), contentType + "; charset=utf-8"));
}
writeResponse(request, response, true);
}
@Override
public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) {
if (response == null) {
response = notFoundResponse();
}
if (enableCORSForAllResponses()) {
addCORSHeaders.addCORSHeaders(request, response);
} else if (apiResponse && enableCORSForAPI()) {
addCORSHeaders.addCORSHeaders(request, response);
}
if (apiResponse) {
response.withHeader("version", org.mockserver.Version.getVersion());
final String path = request.getPath().getValue();
if (!path.startsWith(PATH_PREFIX)) {
response.withHeader("deprecated",
"\"" + path + "\" is deprecated use \"" + PATH_PREFIX + path + "\" instead");
}
}
addConnectionHeader(request, response);
writeAndCloseSocket(ctx, request, response);
}
private void writeAndCloseSocket(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
boolean closeChannel;
ConnectionOptions connectionOptions = response.getConnectionOptions();
if (connectionOptions != null && connectionOptions.getCloseSocket() != null) {
closeChannel = connectionOptions.getCloseSocket();
} else {
closeChannel = !(request.isKeepAlive() != null && request.isKeepAlive());
}
if (closeChannel) {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} else {
ctx.writeAndFlush(response);
}
}
}