Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HttpHeaderInjection to include headers optional modules
Related to: https://jira.duraspace.org/browse/FCREPO-1795
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/HttpHeaderInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/api/UriAwareHttpHeaderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |