Commit e015ec5a authored by Brian Long's avatar Brian Long
Browse files

Merge branch 'develop' into stable

parents eb7ac34f 795c9fef
Loading
Loading
Loading
Loading
+58 −0
Original line number Diff line number Diff line
/*
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.inteligr8.rs;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map.Entry;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import jakarta.ws.rs.client.ClientRequestContext;

/**
 * This class implements a header-based authorization filter.
 *  
 * @author brian@inteligr8.com
 */
public class HeaderAuthorizationFilter implements AuthorizationFilter {
	
	private final MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
	
	/**
	 * @param headers An array of header name/value pairs.
	 */
    public HeaderAuthorizationFilter(String headerName, String headerValue) {
	    this.headers.add(headerName, headerValue);
	}
    
    public HeaderAuthorizationFilter add(String headerName, String headerValue) {
        this.headers.add(headerName, headerValue);
        return this;
    }
	
	/**
	 * This method applies the 'Authorization' header to the {@link ClientRequestContext}.
	 * 
	 * @param requestContext A request context.
	 */
	@Override
	public void filter(ClientRequestContext requestContext) throws UnsupportedEncodingException {
	    for (Entry<String, List<String>> header : this.headers.entrySet())
	        requestContext.getHeaders().addAll(header.getKey(), header.getValue());
	}

}