diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java index 7267f11313189..957fac534e2d7 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java @@ -2,12 +2,14 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import javax.ws.rs.ServiceUnavailableException; import javax.ws.rs.container.AsyncResponse; @@ -37,32 +39,26 @@ * @author Kristoffer Sjogren * @version $Revision: 1 $ */ -public class VertxHttpRequest extends BaseHttpRequest { - protected ResteasyHttpHeaders httpHeaders; - protected SynchronousDispatcher dispatcher; - protected String httpMethod; - protected String remoteHost; - protected InputStream inputStream; - protected Map attributes = new HashMap(); - protected VertxHttpResponse response; - private final boolean is100ContinueExpected; +public final class VertxHttpRequest extends BaseHttpRequest { + private ResteasyHttpHeaders httpHeaders; + private String httpMethod; + private Supplier remoteHost; + private InputStream inputStream; + private Map attributes; + private VertxHttpResponse response; private VertxExecutionContext executionContext; private final Context context; - private volatile boolean flushed; public VertxHttpRequest(Context context, ResteasyHttpHeaders httpHeaders, ResteasyUriInfo uri, String httpMethod, - String remoteHost, + Supplier remoteHost, SynchronousDispatcher dispatcher, - VertxHttpResponse response, - boolean is100ContinueExpected) { + VertxHttpResponse response) { super(uri); this.context = context; - this.is100ContinueExpected = is100ContinueExpected; this.response = response; - this.dispatcher = dispatcher; this.httpHeaders = httpHeaders; this.httpMethod = httpMethod; this.remoteHost = remoteHost; @@ -81,20 +77,25 @@ public void setHttpMethod(String method) { @Override public Enumeration getAttributeNames() { - Enumeration en = new Enumeration() { - private Iterator it = attributes.keySet().iterator(); - - @Override - public boolean hasMoreElements() { - return it.hasNext(); - } + final Map attributes = this.attributes; + if (attributes == null) { + return Collections.emptyEnumeration(); + } else { + Enumeration en = new Enumeration() { + private Iterator it = attributes.keySet().iterator(); + + @Override + public boolean hasMoreElements() { + return it.hasNext(); + } - @Override - public String nextElement() { - return it.next(); - } - }; - return en; + @Override + public String nextElement() { + return it.next(); + } + }; + return en; + } } @Override @@ -102,23 +103,24 @@ public ResteasyAsynchronousContext getAsyncContext() { return executionContext; } - public boolean isFlushed() { - return flushed; - } - @Override public Object getAttribute(String attribute) { - return attributes.get(attribute); + return attributes != null ? attributes.get(attribute) : null; } @Override public void setAttribute(String name, Object value) { + if (attributes == null) { + attributes = new HashMap(); + } attributes.put(name, value); } @Override public void removeAttribute(String name) { - attributes.remove(name); + if (attributes != null) { + attributes.remove(name); + } } @Override @@ -128,12 +130,12 @@ public HttpHeaders getHttpHeaders() { @Override public String getRemoteHost() { - return remoteHost; + return remoteHost.get(); } @Override public String getRemoteAddress() { - return remoteHost; + return remoteHost.get(); } @Override @@ -155,10 +157,6 @@ public VertxHttpResponse getResponse() { return response; } - public boolean is100ContinueExpected() { - return is100ContinueExpected; - } - @Override public void forward(String path) { throw new NotImplementedYetException(); @@ -309,7 +307,6 @@ public boolean cancel(int retryAfter) { } protected synchronized void vertxFlush() { - flushed = true; try { vertxResponse.finish(); } catch (IOException e) { diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java index a73732dd94a6c..38a6df6c13c8a 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.function.Supplier; import javax.enterprise.inject.Instance; import javax.enterprise.inject.spi.CDI; @@ -96,12 +97,16 @@ private void dispatch(RoutingContext routingContext, InputStream is, VertxOutput HttpServerResponse response = request.response(); VertxHttpResponse vertxResponse = new VertxHttpResponse(request, dispatcher.getProviderFactory(), request.method(), allocator, output); - // client address may not be available with VirtualHttp - SocketAddress socketAddress = request.remoteAddress(); - String host = socketAddress != null ? socketAddress.host() : null; + // client address may not be available with VirtualHttp; + // using a supplier to make the remote Address resolution lazy: often it's not needed. + Supplier hostNameProvider = () -> { + SocketAddress socketAddress = request.remoteAddress(); + String host = socketAddress != null ? socketAddress.host() : null; + return host; + }; - VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, headers, uriInfo, request.rawMethod(), - host, dispatcher.getDispatcher(), vertxResponse, false); + VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, headers, uriInfo, request.rawMethod(), hostNameProvider, + dispatcher.getDispatcher(), vertxResponse); vertxRequest.setInputStream(is); try { ResteasyContext.pushContext(SecurityContext.class, new QuarkusResteasySecurityContext(request));