fix(vertx): Check response before writing
Ensure response is in valid state before writing
Fixes:
MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/HttpConnector.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/HttpConnector.java
@@ -49,11 +49,8 @@ import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import java.io.UnsupportedEncodingException;
-import java.net.ConnectException;
-import java.net.NoRouteToHostException;
import java.net.URI;
import java.net.URLEncoder;
-import java.net.UnknownHostException;
import java.util.Map.Entry;
/**
@@ -318,7 +315,7 @@ class HttpConnector implements IApiConnectionResponse, IApiConnection {
@Override
public void handle(Throwable error) {
ConnectorException ce = ErrorHandler.handleConnectionError(error);
- logger.error(error.getMessage(), error);
+ logger.error("Connection Error: " + error.getMessage(), error);
resultHandler.handle(AsyncResultImpl
.<IApiConnectionResponse> create(ce));

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/http/HttpPolicyAdapter.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/http/HttpPolicyAdapter.java
@@ -114,7 +114,7 @@ public class HttpPolicyAdapter {
private void handleEngineResult(ApiRequest request, IEngineResult engineResult) {
// Everything worked
- if (engineResult.isResponse()) {
+ if (engineResult.isResponse() && responseIsNotClosed()) {
ApiResponse response = engineResult.getApiResponse();
setCorsHeadersForSwaggerUi(request, response);
@@ -210,27 +210,30 @@ public class HttpPolicyAdapter {
}
private void handleError(ApiRequest apimanRequest, Throwable error, HttpServerResponse vertxResponse) {
+ if (responseIsNotClosed()) {
vertxResponse.setChunked(true);
- policyErrorWriter.write(apimanRequest, error, new IApiClientResponse() {
-
- @Override
- public void write(StringBuffer buffer) {
- vertxRequest.resume();
- vertxResponse.end(buffer.toString());
+ policyErrorWriter.write(apimanRequest, error, new DefaultApiClientResponse());
+ } else {
+ // probably log this
+ }
}
- @Override
- public void write(StringBuilder builder) {
- vertxRequest.resume();
- vertxResponse.end(builder.toString());
+ private void handlePolicyFailure(ApiRequest apimanRequest, PolicyFailure policyFailure, HttpServerResponse vertxResponse) {
+ if (responseIsNotClosed()) {
+ vertxResponse.setChunked(true);
+ policyFailureWriter.write(apimanRequest, policyFailure, new DefaultApiClientResponse());
+ } else {
+ // probably log this
+ }
}
- @Override
- public void write(String body) {
- vertxRequest.resume();
- vertxResponse.end(body);
+ private boolean responseIsNotClosed() {
+ return !vertxResponse.closed() && !vertxResponse.ended();
}
+
+ private class DefaultApiClientResponse implements IApiClientResponse {
+
@Override
public void setStatusCode(int code) {
vertxResponse.setStatusCode(code);
@@ -240,17 +243,11 @@ public class HttpPolicyAdapter {
public void setHeader(String headerName, String headerValue) {
vertxResponse.putHeader(headerName, headerValue);
}
- });
- }
-
- private void handlePolicyFailure(ApiRequest apimanRequest, PolicyFailure policyFailure, HttpServerResponse vertxResponse) {
- vertxResponse.setChunked(true);
- policyFailureWriter.write(apimanRequest, policyFailure, new IApiClientResponse() {
@Override
- public void write(StringBuffer buffer) {
+ public void write(String body) {
vertxRequest.resume();
- vertxResponse.end(buffer.toString());
+ vertxResponse.end(body);
}
@Override
@@ -260,20 +257,9 @@ public class HttpPolicyAdapter {
}
@Override
- public void write(String body) {
+ public void write(StringBuffer buffer) {
vertxRequest.resume();
- vertxResponse.end(body);
- }
-
- @Override
- public void setStatusCode(int code) {
- vertxResponse.setStatusCode(code);
- }
-
- @Override
- public void setHeader(String headerName, String headerValue) {
- vertxResponse.putHeader(headerName, headerValue);
+ vertxResponse.end(buffer.toString());
}
- });
}
}

fix(vertx): Check response before writing

Ensure response is in valid state before writing 

Fixes: #948