提交 57dc8199 编写于 作者: R Rossen Stoyanchev

Add ReactiveHttpFilter

上级 0f8a4bf7
/*
* Copyright 2002-2015 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.http.server;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.reactivestreams.Publisher;
import org.springframework.util.Assert;
/**
* An {@link ReactiveHttpHandler} decorator that delegates to a list of
* {@link ReactiveHttpFilter}s and the target {@link ReactiveHttpHandler}.
*
* @author Rossen Stoyanchev
*/
public class FilterChainHttpHandler implements ReactiveHttpHandler {
private final List<ReactiveHttpFilter> filters;
private final ReactiveHttpHandler targetHandler;
public FilterChainHttpHandler(ReactiveHttpHandler targetHandler, ReactiveHttpFilter... filters) {
Assert.notNull(targetHandler, "'targetHandler' is required.");
this.filters = (filters != null ? Arrays.asList(filters) : Collections.emptyList());
this.targetHandler = targetHandler;
}
@Override
public Publisher<Void> handle(ReactiveServerHttpRequest request, ReactiveServerHttpResponse response) {
return new DefaultHttpFilterChain().filter(request, response);
}
private class DefaultHttpFilterChain implements ReactiveHttpFilterChain {
private int index;
@Override
public Publisher<Void> filter(ReactiveServerHttpRequest request, ReactiveServerHttpResponse response) {
if (this.index < filters.size()) {
ReactiveHttpFilter filter = filters.get(this.index++);
return filter.filter(request, response, this);
}
else {
return targetHandler.handle(request, response);
}
}
}
}
/*
* Copyright 2002-2015 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.http.server;
import org.reactivestreams.Publisher;
/**
* @author Rossen Stoyanchev
*/
public interface ReactiveHttpFilter {
Publisher<Void> filter(ReactiveServerHttpRequest request, ReactiveServerHttpResponse response,
ReactiveHttpFilterChain chain);
}
/*
* Copyright 2002-2015 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.http.server;
import org.reactivestreams.Publisher;
import org.springframework.http.server.ReactiveServerHttpRequest;
import org.springframework.http.server.ReactiveServerHttpResponse;
/**
* @author Rossen Stoyanchev
*/
public interface ReactiveHttpFilterChain {
Publisher<Void> filter(ReactiveServerHttpRequest request, ReactiveServerHttpResponse response);
}
/*
* Copyright 2002-2015 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.http.server;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.Publishers;
import reactor.rx.Streams;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* @author Rossen Stoyanchev
*/
public class FilterChainHttpHandlerTests {
private ReactiveServerHttpRequest request;
private ReactiveServerHttpResponse response;
@Before
public void setUp() throws Exception {
this.request = mock(ReactiveServerHttpRequest.class);
this.response = mock(ReactiveServerHttpResponse.class);
}
@Test
public void multipleFilters() throws Exception {
StubHandler handler = new StubHandler();
TestFilter filter1 = new TestFilter();
TestFilter filter2 = new TestFilter();
TestFilter filter3 = new TestFilter();
FilterChainHttpHandler filterHandler = new FilterChainHttpHandler(handler, filter1, filter2, filter3);
Publisher<Void> voidPublisher = filterHandler.handle(this.request, this.response);
Streams.wrap(voidPublisher).toList().await(10, TimeUnit.SECONDS);
assertTrue(filter1.invoked());
assertTrue(filter2.invoked());
assertTrue(filter3.invoked());
assertTrue(handler.invoked());
}
@Test
public void zeroFilters() throws Exception {
StubHandler handler = new StubHandler();
FilterChainHttpHandler filterHandler = new FilterChainHttpHandler(handler);
Publisher<Void> voidPublisher = filterHandler.handle(this.request, this.response);
Streams.wrap(voidPublisher).toList().await(10, TimeUnit.SECONDS);
assertTrue(handler.invoked());
}
@Test
public void shortcircuitFilter() throws Exception {
StubHandler handler = new StubHandler();
TestFilter filter1 = new TestFilter();
ShortcircuitingFilter filter2 = new ShortcircuitingFilter();
TestFilter filter3 = new TestFilter();
FilterChainHttpHandler filterHandler = new FilterChainHttpHandler(handler, filter1, filter2, filter3);
Publisher<Void> voidPublisher = filterHandler.handle(this.request, this.response);
Streams.wrap(voidPublisher).toList().await(10, TimeUnit.SECONDS);
assertTrue(filter1.invoked());
assertTrue(filter2.invoked());
assertFalse(filter3.invoked());
assertFalse(handler.invoked());
}
private static class TestFilter implements ReactiveHttpFilter {
private boolean invoked;
public boolean invoked() {
return this.invoked;
}
@Override
public Publisher<Void> filter(ReactiveServerHttpRequest req, ReactiveServerHttpResponse res,
ReactiveHttpFilterChain chain) {
this.invoked = true;
return doFilter(req, res, chain);
}
public Publisher<Void> doFilter(ReactiveServerHttpRequest req, ReactiveServerHttpResponse res,
ReactiveHttpFilterChain chain) {
return chain.filter(req, res);
}
}
private static class ShortcircuitingFilter extends TestFilter {
@Override
public Publisher<Void> doFilter(ReactiveServerHttpRequest req, ReactiveServerHttpResponse res,
ReactiveHttpFilterChain chain) {
return Publishers.empty();
}
}
private static class StubHandler implements ReactiveHttpHandler {
private boolean invoked;
public boolean invoked() {
return this.invoked;
}
@Override
public Publisher<Void> handle(ReactiveServerHttpRequest req, ReactiveServerHttpResponse res) {
this.invoked = true;
return Publishers.empty();
}
}
}
......@@ -55,7 +55,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("hm", TestHandlerMapping.class);
wac.registerSingleton("ha", HttpHandlerAdapter.class);
wac.registerSingleton("hhrh", SimpleHandlerResultHandler.class);
wac.registerSingleton("rh", SimpleHandlerResultHandler.class);
wac.refresh();
DispatcherHandler dispatcherHandler = new DispatcherHandler();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册