use of io.micronaut.http.filter.ServerFilterChain in project akhq by tchiotludo.
the class StaticFilter method doFilter.
@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
return Publishers.map(chain.proceed(request), response -> {
boolean first = response.getBody(NettyStreamedFileCustomizableResponseType.class).filter(n -> n.getMediaType().getName().equals(MediaType.TEXT_HTML)).isPresent();
boolean second = response.getBody(NettySystemFileCustomizableResponseType.class).filter(n -> n.getFile().getAbsoluteFile().toString().endsWith("ui/index.html")).isPresent();
if (first || second) {
try {
InputStream inputStream = Objects.requireNonNull(StaticFilter.class.getClassLoader().getResourceAsStream("ui/index.html"));
String content;
try (Reader reader = new InputStreamReader(inputStream)) {
content = CharStreams.toString(reader);
}
String finalBody = replace(content);
return HttpResponse.<String>ok().body(finalBody).contentType(MediaType.TEXT_HTML).contentLength(finalBody.length());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return response;
});
}
use of io.micronaut.http.filter.ServerFilterChain in project micronaut-core by micronaut-projects.
the class RouteExecutor method filterPublisher.
/**
* Applies server filters to a request/response.
*
* @param requestReference The request reference
* @param upstreamResponsePublisher The original response publisher
* @return A new response publisher that executes server filters
*/
public Publisher<MutableHttpResponse<?>> filterPublisher(AtomicReference<HttpRequest<?>> requestReference, Publisher<MutableHttpResponse<?>> upstreamResponsePublisher) {
List<HttpFilter> httpFilters = router.findFilters(requestReference.get());
if (httpFilters.isEmpty()) {
return upstreamResponsePublisher;
}
List<HttpFilter> filters = new ArrayList<>(httpFilters);
AtomicInteger integer = new AtomicInteger();
int len = filters.size();
final Function<MutableHttpResponse<?>, Publisher<MutableHttpResponse<?>>> handleStatusException = (response) -> handleStatusException(requestReference.get(), response);
final Function<Throwable, Publisher<MutableHttpResponse<?>>> onError = (t) -> onError(t, requestReference.get());
ServerFilterChain filterChain = new ServerFilterChain() {
@SuppressWarnings("unchecked")
@Override
public Publisher<MutableHttpResponse<?>> proceed(io.micronaut.http.HttpRequest<?> request) {
int pos = integer.incrementAndGet();
if (pos > len) {
throw new IllegalStateException("The FilterChain.proceed(..) method should be invoked exactly once per filter execution. The method has instead been invoked multiple times by an erroneous filter definition.");
}
if (pos == len) {
return upstreamResponsePublisher;
}
HttpFilter httpFilter = filters.get(pos);
HttpRequest<?> requestForFilter = requestReference.getAndSet(request);
try {
return Flux.from((Publisher<MutableHttpResponse<?>>) httpFilter.doFilter(requestForFilter, this)).flatMap(handleStatusException).onErrorResume(onError);
} catch (Throwable t) {
return onError.apply(t);
}
}
};
HttpFilter httpFilter = filters.get(0);
HttpRequest<?> request = requestReference.get();
try {
return Flux.from((Publisher<MutableHttpResponse<?>>) httpFilter.doFilter(request, filterChain)).flatMap(handleStatusException).onErrorResume(onError);
} catch (Throwable t) {
return onError.apply(t);
}
}
Aggregations