diff --git a/src/main/java/com/inteligr8/rs/HeaderAuthorizationFilter.java b/src/main/java/com/inteligr8/rs/HeaderAuthorizationFilter.java new file mode 100644 index 0000000..8347950 --- /dev/null +++ b/src/main/java/com/inteligr8/rs/HeaderAuthorizationFilter.java @@ -0,0 +1,58 @@ +/* + * 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 . + */ +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 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> header : this.headers.entrySet()) + requestContext.getHeaders().addAll(header.getKey(), header.getValue()); + } + +}