From 097714241c927bdc4ce9613bd6bec17dbb4d5b67 Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Mon, 2 Nov 2015 23:59:42 -0600 Subject: [PATCH] HttpHeaderInjection to include headers optional modules Related to: https://jira.duraspace.org/browse/FCREPO-1795 --- .../http/api/ContentExposingResource.java | 8 ++ .../http/commons/api/HttpHeaderInjector.java | 82 +++++++++++++++++++ .../api/UriAwareHttpHeaderFactory.java | 41 ++++++++++ 3 files changed, 131 insertions(+) create mode 100644 fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/HttpHeaderInjector.java create mode 100644 fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/UriAwareHttpHeaderFactory.java diff --git a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/ContentExposingResource.java b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/ContentExposingResource.java index 778f356884..c7def4525f 100644 --- a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/ContentExposingResource.java +++ b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/ContentExposingResource.java @@ -69,6 +69,7 @@ import com.fasterxml.jackson.core.JsonParseException; import org.apache.jena.atlas.RuntimeIOException; import org.apache.jena.riot.RiotException; +import org.fcrepo.http.commons.api.HttpHeaderInjector; import org.fcrepo.http.commons.api.rdf.HttpTripleUtil; import org.fcrepo.http.commons.domain.MultiPrefer; import org.fcrepo.http.commons.domain.PreferTag; @@ -128,6 +129,10 @@ public abstract class ContentExposingResource extends FedoraBaseResource { @Optional private HttpTripleUtil httpTripleUtil; + @Inject + @Optional + private HttpHeaderInjector httpHeaderInject; + @BeanParam protected MultiPrefer prefer; @@ -427,6 +432,9 @@ protected void addResourceHttpHeaders(final FedoraResource resource) { } else { servletResponse.addHeader("Link", "<" + LDP_NAMESPACE + "RDFSource>;rel=\"type\""); } + if (httpHeaderInject != null) { + httpHeaderInject.addHttpHeaderToResponseStream(servletResponse, uriInfo, resource()); + } } diff --git a/fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/HttpHeaderInjector.java b/fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/HttpHeaderInjector.java new file mode 100644 index 0000000000..8c22a2f01d --- /dev/null +++ b/fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/HttpHeaderInjector.java @@ -0,0 +1,82 @@ +/** + * Copyright 2015 DuraSpace, Inc. + * + * 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.fcrepo.http.commons.api; + +import static org.slf4j.LoggerFactory.getLogger; + +import java.util.Map; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.UriInfo; + +import org.fcrepo.kernel.api.models.FedoraResource; + +import org.slf4j.Logger; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ApplicationObjectSupport; +import org.springframework.stereotype.Component; + +import com.google.common.collect.Multimap; + +/** + * Inject optional headers from external processes + * + * @author whikloj + * @since 2015-10-30 + */ +@Component +public class HttpHeaderInjector extends ApplicationObjectSupport { + + private static final Logger LOGGER = getLogger(HttpHeaderInjector.class); + + private ApplicationContext applicationContext; + + @Override + protected void initApplicationContext() throws BeansException { + applicationContext = getApplicationContext(); + } + + @Override + protected boolean isContextRequired() { + return true; + } + + /** + * Add additional Http Headers + * + * @param servletResponse the response + * @param uriInfo the URI context + * @param resource the resource + */ + public void addHttpHeaderToResponseStream(final HttpServletResponse servletResponse, + final UriInfo uriInfo, + final FedoraResource resource) { + + getUriAwareHttpHeaderFactories().forEach((bean, factory) -> { + LOGGER.debug("Adding HTTP headers using: {}", bean); + final Multimap h = factory.createHttpHeadersForResource(uriInfo, resource); + + h.entries().forEach(entry -> { + servletResponse.addHeader(entry.getKey(), entry.getValue()); + }); + }); + } + + private Map getUriAwareHttpHeaderFactories() { + return applicationContext.getBeansOfType(UriAwareHttpHeaderFactory.class); + } + +} diff --git a/fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/UriAwareHttpHeaderFactory.java b/fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/UriAwareHttpHeaderFactory.java new file mode 100644 index 0000000000..8d4b64656e --- /dev/null +++ b/fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/UriAwareHttpHeaderFactory.java @@ -0,0 +1,41 @@ +/** + * Copyright 2015 DuraSpace, Inc. + * + * 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.fcrepo.http.commons.api; + +import javax.ws.rs.core.UriInfo; + +import org.fcrepo.kernel.api.models.FedoraResource; + +import com.google.common.collect.Multimap; + +/** + * Helper interface to inject Http Headers from external modules. + * + * @author whikloj + * @since 2015-11-01 + */ +public interface UriAwareHttpHeaderFactory { + + /** + * Given a resource and session, update the JAX-RS response with any additional headers. + * + * @param uriInfo contextual information for building URLs + * @param resource the resource from the request + * @return Multimap of HTTP Header field names and field values + */ + Multimap createHttpHeadersForResource(UriInfo uriInfo, FedoraResource resource); + +}