Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / adaptors / rest-adaptor-provider / src / main / java / org / onap / ccsdk / features / rest / adaptor / utils / BasicAuthorizationInterceptor.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  *
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.features.rest.adaptor.utils;\r
19 \r
20 import java.io.IOException;\r
21 import java.nio.charset.Charset;\r
22 import java.util.Base64;
23 import org.springframework.http.HttpRequest;\r
24 import org.springframework.http.client.ClientHttpRequestExecution;\r
25 import org.springframework.http.client.ClientHttpRequestInterceptor;\r
26 import org.springframework.http.client.ClientHttpResponse;\r
27 import org.springframework.util.Assert;\r
28 \r
29 public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {\r
30     private static final Charset UTF_8 = Charset.forName("UTF-8");\r
31 \r
32     private final String username;\r
33 \r
34     private final String pass;\r
35 \r
36     /**\r
37      * Create a new interceptor which adds a BASIC authorization header for the given username and pass.\r
38      *\r
39      * @param username the username to use\r
40      * @param pass the password to use\r
41      */\r
42     public BasicAuthorizationInterceptor(String username, String pass) {\r
43         Assert.hasLength(username, "Username must not be empty");\r
44         this.username = username;\r
45         this.pass = (pass != null ? pass : "");\r
46     }\r
47 \r
48     @Override\r
49     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)\r
50             throws IOException {\r
51 \r
52         String token = Base64.getEncoder().encodeToString((this.username + ":" + this.pass).getBytes(UTF_8));
53         request.getHeaders().add("Authorization", "Basic " + token);\r
54         return execution.execute(request, body);\r
55     }\r
56 }\r