io.quarkus.resteasy.runtime.standalone.VertxHttpResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-resteasy Show documentation
Show all versions of quarkus-resteasy Show documentation
REST endpoint framework implementing JAX-RS and more
package io.quarkus.resteasy.runtime.standalone;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.NewCookie;
import jakarta.ws.rs.ext.RuntimeDelegate;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import io.netty.buffer.ByteBuf;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
public class VertxHttpResponse implements HttpResponse {
private int status = 200;
private OutputStream os;
private MultivaluedMap outputHeaders;
final HttpServerRequest request;
final HttpServerResponse response;
private boolean committed;
private boolean finished;
private ResteasyProviderFactory providerFactory;
private final HttpMethod method;
private final VertxOutput output;
private final RoutingContext routingContext;
public VertxHttpResponse(HttpServerRequest request, ResteasyProviderFactory providerFactory,
final HttpMethod method, BufferAllocator allocator, VertxOutput output, RoutingContext routingContext) {
this.routingContext = routingContext;
outputHeaders = new MultivaluedHashMap();
this.method = method;
os = (method == null || !method.equals(HttpMethod.HEAD)) ? new VertxOutputStream(this, allocator)
: null;
this.request = request;
this.response = request.response();
this.providerFactory = providerFactory;
this.output = output;
}
@Override
public void setOutputStream(OutputStream os) {
this.os = os;
}
@Override
public int getStatus() {
return status;
}
@Override
public void setStatus(int status) {
this.status = status;
}
@Override
public MultivaluedMap getOutputHeaders() {
return outputHeaders;
}
@Override
public OutputStream getOutputStream() throws IOException {
return os;
}
@Override
public void addNewCookie(NewCookie cookie) {
outputHeaders.add(jakarta.ws.rs.core.HttpHeaders.SET_COOKIE, cookie);
}
void checkException() throws IOException {
// todo from old code, do we still need it?
}
@Override
public void sendError(int status) throws IOException {
checkException();
sendError(status, null);
}
@Override
public void sendError(int status, String message) throws IOException {
checkException();
if (committed) {
throw new IllegalStateException();
}
response.setStatusCode(status);
if (message != null) {
response.end(message);
} else {
response.end();
}
committed = true;
}
@Override
public boolean isCommitted() {
return committed;
}
@Override
public void reset() {
if (committed) {
throw new IllegalStateException("Response already committed");
}
outputHeaders.clear();
}
private void transformHeaders() {
getOutputHeaders().forEach(this::transformHeadersList);
}
private void transformHeadersList(final String key, final List