edd4e6dfa4c78e80097bdf6eae3e90d2746238ca
[so.git] / common / src / main / java / org / onap / so / client / cds / BasicAuthClientInterceptor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 Bell Canada.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.cds;
22
23 import io.grpc.CallOptions;
24 import io.grpc.Channel;
25 import io.grpc.ClientCall;
26 import io.grpc.ClientCall.Listener;
27 import io.grpc.ClientInterceptor;
28 import io.grpc.ForwardingClientCall;
29 import io.grpc.Metadata;
30 import io.grpc.Metadata.Key;
31 import io.grpc.MethodDescriptor;
32
33 public class BasicAuthClientInterceptor implements ClientInterceptor {
34
35     private CDSProperties props;
36
37     public BasicAuthClientInterceptor(CDSProperties props) {
38         this.props = props;
39     }
40
41     @Override
42     public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
43         MethodDescriptor<ReqT, RespT> method,
44         CallOptions callOptions,
45         Channel channel) {
46
47         Key<String> authHeader = Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);
48
49         return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
50             channel.newCall(method, callOptions)) {
51             @Override
52             public void start(Listener<RespT> responseListener, Metadata headers) {
53                 headers.put(authHeader, props.getBasicAuth());
54                 super.start(responseListener, headers);
55             }
56         };
57     }
58 }