]> git.basschouten.com Git - openhab-addons.git/blob
fbeae9da021e07dbaa27165e67eb8c1bb5845bec
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.remoteopenhab.internal.rest;
14
15 import java.io.IOException;
16
17 import javax.ws.rs.client.ClientRequestContext;
18 import javax.ws.rs.client.ClientRequestFilter;
19 import javax.ws.rs.core.HttpHeaders;
20 import javax.ws.rs.core.MultivaluedMap;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24
25 /**
26  * Inserts Authorization and Cache-Control headers for requests on the streaming REST API.
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 @NonNullByDefault
31 public class RemoteopenhabStreamingRequestFilter implements ClientRequestFilter {
32
33     private final String accessToken;
34
35     public RemoteopenhabStreamingRequestFilter(String accessToken) {
36         this.accessToken = accessToken;
37     }
38
39     @Override
40     public void filter(@Nullable ClientRequestContext requestContext) throws IOException {
41         if (requestContext != null) {
42             MultivaluedMap<String, Object> headers = requestContext.getHeaders();
43             if (!accessToken.isEmpty()) {
44                 headers.putSingle(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
45             }
46             headers.putSingle(HttpHeaders.CACHE_CONTROL, "no-cache");
47         }
48     }
49 }