Commit 47747f32 by Spencer Gibb

Polish and extensibility

parent 834dfe22
...@@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
...@@ -38,8 +39,8 @@ import lombok.extern.apachecommons.CommonsLog; ...@@ -38,8 +39,8 @@ import lombok.extern.apachecommons.CommonsLog;
@CommonsLog @CommonsLog
public class RibbonRoutingFilter extends ZuulFilter { public class RibbonRoutingFilter extends ZuulFilter {
private ProxyRequestHelper helper; protected ProxyRequestHelper helper;
private RibbonCommandFactory<?> ribbonCommandFactory; protected RibbonCommandFactory<?> ribbonCommandFactory;
public RibbonRoutingFilter(ProxyRequestHelper helper, public RibbonRoutingFilter(ProxyRequestHelper helper,
RibbonCommandFactory<?> ribbonCommandFactory) { RibbonCommandFactory<?> ribbonCommandFactory) {
...@@ -90,7 +91,7 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -90,7 +91,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
return null; return null;
} }
private RibbonCommandContext buildCommandContext(RequestContext context) { protected RibbonCommandContext buildCommandContext(RequestContext context) {
HttpServletRequest request = context.getRequest(); HttpServletRequest request = context.getRequest();
MultiValueMap<String, String> headers = this.helper MultiValueMap<String, String> headers = this.helper
...@@ -115,7 +116,7 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -115,7 +116,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
requestEntity); requestEntity);
} }
private ClientHttpResponse forward(RibbonCommandContext context) throws Exception { protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
Map<String, Object> info = this.helper.debug(context.getVerb(), context.getUri(), Map<String, Object> info = this.helper.debug(context.getVerb(), context.getUri(),
context.getHeaders(), context.getParams(), context.getRequestEntity()); context.getHeaders(), context.getParams(), context.getRequestEntity());
...@@ -127,36 +128,43 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -127,36 +128,43 @@ public class RibbonRoutingFilter extends ZuulFilter {
return response; return response;
} }
catch (HystrixRuntimeException ex) { catch (HystrixRuntimeException ex) {
info.put("status", "500"); return handleException(info, ex);
ClientException clientException = findClientException(ex);
if (clientException != null) {
int statusCode = 500;
if (clientException.getErrorType() == ClientException.ErrorType.SERVER_THROTTLED) {
statusCode = 503;
}
throw new ZuulException(clientException, "Forwarding error", statusCode,
clientException.getErrorType().toString());
}
throw new ZuulException(ex, "Forwarding error", 500,
ex.getFailureType().toString());
} }
} }
protected ClientException findClientException(HystrixRuntimeException ex) { protected ClientHttpResponse handleException(Map<String, Object> info, HystrixRuntimeException ex) throws ZuulException {
if (ex.getCause() != null int statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
&& ex.getCause() instanceof ClientException) { Throwable cause = ex;
return (ClientException) ex.getCause(); String message = ex.getFailureType().toString();
ClientException clientException = findClientException(ex);
if (clientException == null) {
clientException = findClientException(ex.getFallbackException());
} }
if (ex.getFallbackException() != null
&& ex.getFallbackException().getCause() != null if (clientException != null) {
&& ex.getFallbackException().getCause() instanceof ClientException) { if (clientException.getErrorType() == ClientException.ErrorType.SERVER_THROTTLED) {
return (ClientException) ex.getFallbackException().getCause(); statusCode = HttpStatus.SERVICE_UNAVAILABLE.value();
}
cause = clientException;
message = clientException.getErrorType().toString();
} }
return null; info.put("status", String.valueOf(statusCode));
throw new ZuulException(cause, "Forwarding error", statusCode, message);
}
protected ClientException findClientException(Throwable t) {
if (t == null) {
return null;
}
if (t instanceof ClientException) {
return (ClientException)t;
}
return findClientException(t.getCause());
} }
private InputStream getRequestBody(HttpServletRequest request) { protected InputStream getRequestBody(HttpServletRequest request) {
InputStream requestEntity = null; InputStream requestEntity = null;
// ApacheHttpClient4Handler does not support body in delete requests // ApacheHttpClient4Handler does not support body in delete requests
if (request.getMethod().equals("DELETE")) { if (request.getMethod().equals("DELETE")) {
...@@ -175,7 +183,7 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -175,7 +183,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
return requestEntity; return requestEntity;
} }
private String getVerb(HttpServletRequest request) { protected String getVerb(HttpServletRequest request) {
String method = request.getMethod(); String method = request.getMethod();
if (method == null) { if (method == null) {
return "GET"; return "GET";
...@@ -183,7 +191,7 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -183,7 +191,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
return method; return method;
} }
private void setResponse(ClientHttpResponse resp) protected void setResponse(ClientHttpResponse resp)
throws ClientException, IOException { throws ClientException, IOException {
this.helper.setResponse(resp.getStatusCode().value(), this.helper.setResponse(resp.getStatusCode().value(),
resp.getBody() == null ? null : resp.getBody(), resp.getHeaders()); resp.getBody() == null ? null : resp.getBody(), resp.getHeaders());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment