001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.api; 019 020import static org.slf4j.LoggerFactory.getLogger; 021 022import java.util.Map; 023import javax.servlet.http.HttpServletResponse; 024import javax.ws.rs.core.UriInfo; 025 026import org.fcrepo.kernel.api.models.FedoraResource; 027 028import org.slf4j.Logger; 029import org.springframework.beans.BeansException; 030import org.springframework.context.ApplicationContext; 031import org.springframework.context.support.ApplicationObjectSupport; 032import org.springframework.stereotype.Component; 033 034import com.google.common.collect.Multimap; 035 036/** 037 * Inject optional headers from external processes 038 * 039 * @author whikloj 040 * @since 2015-10-30 041 */ 042@Component 043public class HttpHeaderInjector extends ApplicationObjectSupport { 044 045 private static final Logger LOGGER = getLogger(HttpHeaderInjector.class); 046 047 private ApplicationContext applicationContext; 048 049 @Override 050 protected void initApplicationContext() throws BeansException { 051 applicationContext = getApplicationContext(); 052 } 053 054 @Override 055 protected boolean isContextRequired() { 056 return true; 057 } 058 059 /** 060 * Add additional Http Headers 061 * 062 * @param servletResponse the response 063 * @param uriInfo the URI context 064 * @param resource the resource 065 */ 066 public void addHttpHeaderToResponseStream(final HttpServletResponse servletResponse, 067 final UriInfo uriInfo, 068 final FedoraResource resource) { 069 070 getUriAwareHttpHeaderFactories().forEach((bean, factory) -> { 071 LOGGER.debug("Adding HTTP headers using: {}", bean); 072 final Multimap<String, String> h = factory.createHttpHeadersForResource(uriInfo, resource); 073 074 h.entries().forEach(entry -> { 075 servletResponse.addHeader(entry.getKey(), entry.getValue()); 076 }); 077 }); 078 } 079 080 private Map<String, UriAwareHttpHeaderFactory> getUriAwareHttpHeaderFactories() { 081 return applicationContext.getBeansOfType(UriAwareHttpHeaderFactory.class); 082 } 083 084}