2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.remoteopenhab.internal.rest;
15 import java.io.IOException;
16 import java.util.concurrent.ConcurrentHashMap;
18 import javax.ws.rs.client.ClientRequestContext;
19 import javax.ws.rs.client.ClientRequestFilter;
20 import javax.ws.rs.core.HttpHeaders;
21 import javax.ws.rs.core.MultivaluedMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Inserts Authorization and Cache-Control headers for requests on the streaming REST API.
31 * @author Laurent Garnier - Initial contribution
34 public class RemoteopenhabStreamingRequestFilter implements ClientRequestFilter {
36 private final Logger logger = LoggerFactory.getLogger(RemoteopenhabStreamingRequestFilter.class);
38 private final ConcurrentHashMap<String, String> credentialTokens = new ConcurrentHashMap<>();
41 public void filter(@Nullable ClientRequestContext requestContext) throws IOException {
42 if (requestContext != null) {
43 MultivaluedMap<String, Object> headers = requestContext.getHeaders();
44 String credentialToken = credentialTokens.get(requestContext.getUri().toString());
45 if (credentialToken != null) {
46 if (!credentialToken.isEmpty()) {
47 headers.putSingle(HttpHeaders.AUTHORIZATION, "Basic " + credentialToken);
50 logger.warn("No credential token set! uri={}", requestContext.getUri());
52 headers.putSingle(HttpHeaders.CACHE_CONTROL, "no-cache");
56 public void setCredentialToken(String target, String token) {
57 logger.debug("Set credential token. target={}, token={}", target, token);
58 credentialTokens.put(target, token);