net.optionfactory.spring.upstream.params.UpstreamAnnotatedCookiesInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of upstream Show documentation
Show all versions of upstream Show documentation
optionfactory-spring REST upstream core project
package net.optionfactory.spring.upstream.params;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import net.optionfactory.spring.upstream.Upstream;
import net.optionfactory.spring.upstream.UpstreamHttpInterceptor;
import net.optionfactory.spring.upstream.UpstreamHttpRequestExecution;
import net.optionfactory.spring.upstream.contexts.EndpointDescriptor;
import net.optionfactory.spring.upstream.contexts.InvocationContext;
import net.optionfactory.spring.upstream.contexts.RequestContext;
import net.optionfactory.spring.upstream.contexts.ResponseContext;
import net.optionfactory.spring.upstream.expressions.Expressions;
import net.optionfactory.spring.upstream.expressions.BooleanExpression;
import net.optionfactory.spring.upstream.expressions.StringExpression;
public class UpstreamAnnotatedCookiesInterceptor implements UpstreamHttpInterceptor {
private final Map> conf = new ConcurrentHashMap<>();
private record AnnotatedCookie(BooleanExpression condition, StringExpression value) {
}
@Override
public void preprocess(Class> k, Expressions expressions, Map endpoints) {
for (final var endpoint : endpoints.values()) {
final var anns = Stream.of(endpoint.method().getAnnotationsByType(Upstream.Cookie.class))
.map(annotation -> {
final var condition = expressions.bool(annotation.condition());
final var value = expressions.string(annotation.value(), annotation.valueType());
return new AnnotatedCookie(condition, value);
})
.toList();
conf.put(endpoint.method(), anns);
}
}
@Override
public ResponseContext intercept(InvocationContext invocation, RequestContext request, UpstreamHttpRequestExecution execution) throws IOException {
final var annotatedCookies = conf.get(invocation.endpoint().method());
final var ectx = invocation.expressions().context(invocation, request);
for (final var annotatedCookie : annotatedCookies) {
if (!annotatedCookie.condition().evaluate(ectx)) {
continue;
}
request.headers().add("Cookie", annotatedCookie.value().evaluate(ectx));
}
return execution.execute(invocation, request);
}
}