Merge branch 'master' into 2.0.x

This commit is contained in:
Spencer Gibb
2017-08-04 14:59:08 -06:00
5 changed files with 95 additions and 43 deletions

View File

@@ -140,7 +140,7 @@ public class FormBodyWrapperFilter extends ZuulFilter {
private HttpServletRequest request;
private byte[] contentData;
private volatile byte[] contentData;
private MediaType contentType;
@@ -183,6 +183,9 @@ public class FormBodyWrapperFilter extends ZuulFilter {
}
private synchronized void buildContentData() {
if (this.contentData != null) {
return;
}
try {
MultiValueMap<String, Object> builder = RequestContentDataExtractor.extract(this.request);
FormHttpOutputMessage data = new FormHttpOutputMessage();
@@ -192,8 +195,9 @@ public class FormBodyWrapperFilter extends ZuulFilter {
FormBodyWrapperFilter.this.formHttpMessageConverter.write(builder, this.contentType, data);
// copy new content type including multipart boundary
this.contentType = data.getHeaders().getContentType();
this.contentData = data.getInput();
this.contentLength = this.contentData.length;
byte[] input = data.getInput();
this.contentLength = input.length;
this.contentData = input;
}
catch (Exception e) {
throw new IllegalStateException("Cannot convert form data", e);

View File

@@ -25,7 +25,8 @@ import org.springframework.boot.autoconfigure.web.servlet.error.ErrorController;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
@@ -37,6 +38,8 @@ import com.netflix.zuul.context.RequestContext;
*
* @author Spencer Gibb
* @author Dave Syer
* @author João Salavessa
* @author Biju Kunjummen
*/
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping {
@@ -46,6 +49,8 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping {
private ErrorController errorController;
private PathMatcher pathMatcher = new AntPathMatcher();
private volatile boolean dirty = true;
public ZuulHandlerMapping(RouteLocator routeLocator, ZuulController zuul) {
@@ -80,10 +85,7 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping {
if (this.errorController != null && urlPath.equals(this.errorController.getErrorPath())) {
return null;
}
String[] ignored = this.routeLocator.getIgnoredPaths().toArray(new String[0]);
if (PatternMatchUtils.simpleMatch(ignored, urlPath)) {
return null;
}
if (isIgnoredPath(urlPath, this.routeLocator.getIgnoredPaths())) return null;
RequestContext ctx = RequestContext.getCurrentContext();
if (ctx.containsKey("forward.to")) {
return null;
@@ -99,6 +101,17 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping {
return super.lookupHandler(urlPath, request);
}
private boolean isIgnoredPath(String urlPath, Collection<String> ignored) {
if (ignored != null) {
for (String ignoredPath : ignored) {
if (this.pathMatcher.match(ignoredPath, urlPath)) {
return true;
}
}
}
return false;
}
private void registerHandlers() {
Collection<Route> routes = this.routeLocator.getRoutes();
if (routes.isEmpty()) {

View File

@@ -1,22 +1,21 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* * Copyright 2013-2016 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.
* 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;
package org.springframework.cloud.netflix.test;
import feign.Client;
import feign.httpclient.ApacheHttpClient;

View File

@@ -1,21 +1,22 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* * Copyright 2013-2016 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.
* 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.test;
import feign.Client;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
@@ -23,8 +24,7 @@ import okhttp3.OkHttpClient;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@@ -17,7 +17,11 @@
package org.springframework.cloud.netflix.zuul.web;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
@@ -29,11 +33,9 @@ import org.springframework.mock.web.MockHttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Dave Syer
* @author Biju Kunjummen
*/
public class ZuulHandlerMappingTests {
@@ -59,7 +61,7 @@ public class ZuulHandlerMappingTests {
.singletonList(new Route("foo", "/foo/**", "foo", "", null, null)));
this.request.setServletPath("/foo/");
this.mapping.setDirty(true);
assertNotNull(this.mapping.getHandler(this.request));
assertThat(this.mapping.getHandler(this.request)).isNotNull();
}
@Test
@@ -69,7 +71,7 @@ public class ZuulHandlerMappingTests {
;
this.request.setServletPath("/");
this.mapping.setDirty(true);
assertNotNull(this.mapping.getHandler(this.request));
assertThat(this.mapping.getHandler(this.request)).isNotNull();
}
@Test
@@ -78,7 +80,41 @@ public class ZuulHandlerMappingTests {
.singletonList(new Route("default", "/**", "foo", "", null, null)));
this.request.setServletPath("/error");
this.mapping.setDirty(true);
assertNull(this.mapping.getHandler(this.request));
assertThat(this.mapping.getHandler(this.request)).isNull();
}
@Test
public void ignoredPathsShouldNotReturnAHandler() throws Exception {
assertThat(mappingWithIgnoredPathsAndRoutes(Arrays.asList("/p1/**"),
new Route("p1", "/p1/**", "p1", "", null, null))
.getHandler(requestForAPath("/p1"))).isNull();
assertThat(mappingWithIgnoredPathsAndRoutes(Arrays.asList("/p1/**/p3/"),
new Route("p1", "/p1/**/p3", "p1", "", null, null))
.getHandler(requestForAPath("/p1/p2/p3"))).isNull();
assertThat(mappingWithIgnoredPathsAndRoutes(Arrays.asList("/p1/**/p3/**"),
new Route("p1", "/p1/**/p3", "p1", "", null, null))
.getHandler(requestForAPath("/p1/p2/p3"))).isNull();
assertThat(mappingWithIgnoredPathsAndRoutes(Arrays.asList("/p1/**/p4/"),
new Route("p1", "/p1/**/p4/", "p1", "", null, null))
.getHandler(requestForAPath("/p1/p2/p3/p4"))).isNull();
}
private ZuulHandlerMapping mappingWithIgnoredPathsAndRoutes(List<String> ignoredPaths, Route route) {
RouteLocator routeLocator = Mockito.mock(RouteLocator.class);
Mockito.when(routeLocator.getIgnoredPaths())
.thenReturn(ignoredPaths);
Mockito.when(routeLocator.getRoutes()).thenReturn(Collections.singletonList(route));
ZuulHandlerMapping zuulHandlerMapping = new ZuulHandlerMapping(routeLocator, new ZuulController());
return zuulHandlerMapping;
}
private MockHttpServletRequest requestForAPath(String path) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServletPath(path);
return request;
}
}