Commit bfb6ca6d by Dave Syer

Ditch SpringFilter in favour of proper dependency injection

parent b7db6586
package org.springframework.cloud.netflix.zuul;
import com.netflix.zuul.ZuulFilter;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.http.HttpServletRequest;
/**
* User: spencergibb
* Date: 5/1/14
* Time: 10:59 PM
*/
public abstract class SpringFilter extends ZuulFilter {
protected <T> T getBean(Class<T> beanClass) {
//FIXME: hack because zuul uses servlet 2.5?
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof ServletRequestAttributes)) {
throw new IllegalStateException("Current request is not a servlet request");
}
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttr;
HttpServletRequest request = attributes.getRequest();
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
return context.getBean(beanClass);
}
}
......@@ -9,6 +9,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessor;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;
import org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter;
import org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter;
......@@ -35,6 +37,12 @@ public class ZuulConfiguration {
private TraceRepository traces;
@Autowired
private RibbonClientPreprocessor preprocessor;
@Autowired
private SpringClientFactory clientFactory;
@Autowired
private DiscoveryClient discovery;
@Autowired
......@@ -93,7 +101,7 @@ public class ZuulConfiguration {
// route filters
@Bean
public RibbonRoutingFilter ribbonRoutingFilter() {
RibbonRoutingFilter filter = new RibbonRoutingFilter();
RibbonRoutingFilter filter = new RibbonRoutingFilter(preprocessor, clientFactory);
if (traces != null) {
filter.setTraces(traces);
}
......
......@@ -17,9 +17,8 @@ import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessor;
import org.springframework.cloud.netflix.zuul.SpringFilter;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.util.StringUtils;
import com.netflix.client.ClientException;
......@@ -27,12 +26,13 @@ import com.netflix.client.http.HttpRequest.Verb;
import com.netflix.client.http.HttpResponse;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.netflix.niws.client.http.RestClient;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.netflix.zuul.util.HTTPRequestUtils;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class RibbonRoutingFilter extends SpringFilter {
public class RibbonRoutingFilter extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(RibbonRoutingFilter.class);
......@@ -40,6 +40,16 @@ public class RibbonRoutingFilter extends SpringFilter {
private TraceRepository traces;
private RibbonClientPreprocessor preprocessor;
private SpringClientFactory clientFactory;
public RibbonRoutingFilter(RibbonClientPreprocessor preprocessor,
SpringClientFactory clientFactory) {
this.preprocessor = preprocessor;
this.clientFactory = clientFactory;
}
public void setTraces(TraceRepository traces) {
this.traces = traces;
}
......@@ -71,10 +81,9 @@ public class RibbonRoutingFilter extends SpringFilter {
String serviceId = (String) context.get("serviceId");
getBean(RibbonClientPreprocessor.class).preprocess(serviceId);
preprocessor.preprocess(serviceId);
//TODO: update to ribbon-rxnetty when available
RestClient restClient = getBean(SpringClientFactory.class).namedClient(serviceId, RestClient.class);
RestClient restClient = clientFactory.namedClient(serviceId, RestClient.class);
String uri = request.getRequestURI();
if (context.get("requestURI") != null) {
......
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