Add ReactiveHttpFilter

This commit is contained in:
Rossen Stoyanchev
2015-11-20 12:48:24 -05:00
parent 0f8a4bf706
commit 57dc8199fb
5 changed files with 272 additions and 1 deletions
@@ -0,0 +1,68 @@
/*
* 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);
}
}
}
}
@@ -0,0 +1,30 @@
/*
* 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);
}
@@ -0,0 +1,31 @@
/*
* 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);
}