Skip to content

Commit

Permalink
HttpHeaderInjection to include headers optional modules
Browse files Browse the repository at this point in the history
  • Loading branch information
whikloj authored and Andrew Woods committed Nov 5, 2015
1 parent a143180 commit 0977142
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -128,6 +129,10 @@ public abstract class ContentExposingResource extends FedoraBaseResource {
@Optional
private HttpTripleUtil httpTripleUtil;

@Inject
@Optional
private HttpHeaderInjector httpHeaderInject;

@BeanParam
protected MultiPrefer prefer;

Expand Down Expand Up @@ -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());
}

}

Expand Down
@@ -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<String, String> h = factory.createHttpHeadersForResource(uriInfo, resource);

h.entries().forEach(entry -> {
servletResponse.addHeader(entry.getKey(), entry.getValue());
});
});
}

private Map<String, UriAwareHttpHeaderFactory> getUriAwareHttpHeaderFactories() {
return applicationContext.getBeansOfType(UriAwareHttpHeaderFactory.class);
}

}
@@ -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<String, String> createHttpHeadersForResource(UriInfo uriInfo, FedoraResource resource);

}

0 comments on commit 0977142

Please sign in to comment.