diff --git a/src/main/java/gov/usgs/earthquake/nshmp/www/ContextPathCookie.java b/src/main/java/gov/usgs/earthquake/nshmp/www/ContextPathCookie.java new file mode 100644 index 0000000000000000000000000000000000000000..dcdf890cbee25489ebd82b2a897498ad7ca8498d --- /dev/null +++ b/src/main/java/gov/usgs/earthquake/nshmp/www/ContextPathCookie.java @@ -0,0 +1,39 @@ +package gov.usgs.earthquake.nshmp.www; + +import java.time.Duration; + +import org.reactivestreams.Publisher; + +import io.micronaut.context.annotation.Requires; +import io.micronaut.context.annotation.Value; +import io.micronaut.core.async.publisher.Publishers; +import io.micronaut.http.HttpMethod; +import io.micronaut.http.HttpRequest; +import io.micronaut.http.MutableHttpResponse; +import io.micronaut.http.annotation.Filter; +import io.micronaut.http.cookie.Cookie; +import io.micronaut.http.filter.HttpServerFilter; +import io.micronaut.http.filter.ServerFilterChain; + +/** + * Add context path as a cookie for Swagger to use. + * + * @author U.S. Geological Survey + */ +@Requires(property = "micronaut.server.context-path") +@Filter(methods = { HttpMethod.GET, HttpMethod.HEAD }, + patterns = { "/**" }) +public class ContextPathCookie implements HttpServerFilter { + private final Cookie contextPathCookie; + + ContextPathCookie(@Value("${micronaut.server.context-path}") String contextPath) { + this.contextPathCookie = Cookie.of("contextPath", contextPath).maxAge(Duration.ofMinutes(2L)); + } + + @Override + public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, + ServerFilterChain chain) { + return Publishers.map(chain.proceed(request), response -> response.cookie(contextPathCookie)); + } + +} diff --git a/src/main/java/gov/usgs/earthquake/nshmp/www/services/ServicesUtil.java b/src/main/java/gov/usgs/earthquake/nshmp/www/services/ServicesUtil.java index 8554752867c637218efbaa47e6fccff0dad3c8ec..064864558a000d76a6e8749f568d31e116f04aee 100644 --- a/src/main/java/gov/usgs/earthquake/nshmp/www/services/ServicesUtil.java +++ b/src/main/java/gov/usgs/earthquake/nshmp/www/services/ServicesUtil.java @@ -19,9 +19,9 @@ import gov.usgs.earthquake.nshmp.www.meta.Status; import io.micronaut.http.HttpResponse; -class ServicesUtil { +public class ServicesUtil { - static HttpResponse<String> handleError( + public static HttpResponse<String> handleError( Throwable e, String name, String url) {