Commit 4c53fd2b by Dominik Mostek

Fallback cause is propagated to fallback provider

Cause can now be propagated to fallback provider in order to respond accordingly to a exception type. New interface is introduced in order not to break any existing falback providers.
parent 6dbdb1d8
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.zuul.filters.route;
import org.springframework.http.client.ClientHttpResponse;
/**
* Extension of {@link ZuulFallbackProvider} which adds possibility to choose proper response
* based on the exception which caused the main method to fail.
*
* @author Dominik Mostek
*/
public interface FallbackProvider extends ZuulFallbackProvider {
/**
* Provides a fallback response based on the cause of the failed execution.
*
* @param cause cause of the main method failure
* @return the fallback response
*/
ClientHttpResponse fallbackResponse(Throwable cause);
}
...@@ -23,7 +23,9 @@ import org.springframework.http.client.ClientHttpResponse; ...@@ -23,7 +23,9 @@ import org.springframework.http.client.ClientHttpResponse;
/** /**
* Provides fallback when a failure occurs on a route. * Provides fallback when a failure occurs on a route.
* @author Ryan Baxter * @author Ryan Baxter
* @deprecated Use {@link FallbackProvider}
*/ */
@Deprecated
public interface ZuulFallbackProvider { public interface ZuulFallbackProvider {
/** /**
......
...@@ -22,6 +22,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; ...@@ -22,6 +22,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommand; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommand;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider; import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import com.netflix.client.AbstractLoadBalancerAwareClient; import com.netflix.client.AbstractLoadBalancerAwareClient;
import com.netflix.client.ClientRequest; import com.netflix.client.ClientRequest;
...@@ -68,7 +69,13 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar ...@@ -68,7 +69,13 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
public AbstractRibbonCommand(String commandKey, LBC client, public AbstractRibbonCommand(String commandKey, LBC client,
RibbonCommandContext context, ZuulProperties zuulProperties, RibbonCommandContext context, ZuulProperties zuulProperties,
ZuulFallbackProvider fallbackProvider, IClientConfig config) { ZuulFallbackProvider fallbackProvider, IClientConfig config) {
super(getSetter(commandKey, zuulProperties)); this(getSetter(commandKey, zuulProperties), client, context, fallbackProvider, config);
}
protected AbstractRibbonCommand(Setter setter, LBC client,
RibbonCommandContext context,
ZuulFallbackProvider fallbackProvider, IClientConfig config) {
super(setter);
this.client = client; this.client = client;
this.context = context; this.context = context;
this.zuulFallbackProvider = fallbackProvider; this.zuulFallbackProvider = fallbackProvider;
...@@ -124,11 +131,24 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar ...@@ -124,11 +131,24 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
@Override @Override
protected ClientHttpResponse getFallback() { protected ClientHttpResponse getFallback() {
if(zuulFallbackProvider != null) { if(zuulFallbackProvider != null) {
return zuulFallbackProvider.fallbackResponse(); return getFallbackResponse();
} }
return super.getFallback(); return super.getFallback();
} }
protected ClientHttpResponse getFallbackResponse() {
if (zuulFallbackProvider instanceof FallbackProvider) {
Throwable cause = getFailedExecutionException();
cause = cause == null ? getExecutionException() : cause;
if (cause == null) {
zuulFallbackProvider.fallbackResponse();
} else {
return ((FallbackProvider) zuulFallbackProvider).fallbackResponse(cause);
}
}
return zuulFallbackProvider.fallbackResponse();
}
public LBC getClient() { public LBC getClient() {
return client; return client;
} }
......
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