9535ad64de0abc1e7dac87ab0fd4a49cb3673a4c
[aaf/authz.git] / cadi / oauth-enduser / src / main / java / org / onap / aaf / cadi / enduser / SimpleRESTClient.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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.aaf.cadi.enduser;
22
23 import java.io.IOException;
24 import java.net.ConnectException;
25 import java.security.Principal;
26
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.LocatorException;
29 import org.onap.aaf.cadi.client.Future;
30 import org.onap.aaf.cadi.client.Rcli;
31 import org.onap.aaf.cadi.client.Result;
32 import org.onap.aaf.cadi.client.Retryable;
33 import org.onap.aaf.cadi.config.Config;
34 import org.onap.aaf.cadi.oauth.TimedToken;
35 import org.onap.aaf.cadi.oauth.TokenClient;
36 import org.onap.aaf.cadi.oauth.TokenClientFactory;
37 import org.onap.aaf.cadi.oauth.TzClient;
38 import org.onap.aaf.cadi.principal.TaggedPrincipal;
39 import org.onap.aaf.misc.env.APIException;
40
41 public class SimpleRESTClient {
42         private static final String[] EMPTY = new String[0];
43         private final TokenClient tokenClient;
44         private final TzClient restClient;
45         private int callTimeout;
46         private String client_id;
47         private String app;
48         private String chain;
49         private Headers headers = new Headers() {
50                 @Override
51                 public String[] headers() {
52                         return EMPTY;
53                 }};
54         
55         public SimpleRESTClient(final TokenClientFactory tcf, final String tokenURL, final String endpoint, final String[] scope) throws CadiException, LocatorException, APIException {
56                 callTimeout = Integer.parseInt(tcf.access.getProperty(Config.AAF_CALL_TIMEOUT,Config.AAF_CALL_TIMEOUT_DEF));
57                 tokenClient = tcf.newClient(tokenURL);
58                 Result<TimedToken> rtt = tokenClient.getToken(scope);
59                 if(rtt.isOK()) {
60                         restClient = tcf.newTzClient(endpoint);
61                         
62                         if((client_id = tcf.access.getProperty(Config.AAF_APPID, null))==null) {
63                                 if((client_id = tcf.access.getProperty(Config.CADI_ALIAS, null))==null) {
64                                         throw new CadiException(Config.AAF_APPID + " or " + Config.CADI_ALIAS + " needs to be defined");
65                                 }                               
66                         }
67                         try {
68                                 restClient.setToken(client_id,rtt.value);
69                         } catch (IOException e) {
70                                 throw new CadiException(e);
71                         }
72                 } else {
73                         throw new CadiException(rtt.error);
74                 }
75         }
76         
77         public SimpleRESTClient timeout(int newTimeout) {
78                 callTimeout = newTimeout;
79                 return this;
80         }
81
82         //Format:<ID>:<APP>:<protocol>[:AS][,<ID>:<APP>:<protocol>]*
83         public SimpleRESTClient as(Principal principal) {
84                 if(principal==null) {
85                         chain = null;
86                 } else {
87                         if(principal instanceof TaggedPrincipal) {
88                                 TaggedPrincipal tp = (TaggedPrincipal)principal;
89                                 chain = tp.getName() + ':' + (app==null?"":app) + ':' + tp.tag() + ":AS";
90                         } else {
91                                 chain = principal.getName() + (app==null?"":':'+app);
92                         }
93                 }
94                 return this;
95         }
96         
97         public String get(final String path) throws CadiException, LocatorException, APIException  {
98                 return get(path,"application/json");
99         }
100
101         public String get(final String path, final String accepts) throws CadiException, LocatorException, APIException  {
102                 return restClient.best(new Retryable<String>() {
103                         @Override
104                         public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
105                                 Future<String> future = client.read(path,accepts, headers());
106                                 if(future.get(callTimeout)) {
107                                         return future.value;
108                                 } else {
109                                         throw new APIException(future.code()  + future.body());
110                                 }                                       
111                         }
112                 });
113         }
114         
115         public interface Headers {
116                 String[] headers();
117         }
118         
119         public String[] headers() {
120                 if(chain==null) {
121                         return headers.headers();
122                 } else {
123                         String[] strs = headers.headers();
124                         String[] rv = new String[strs.length+2];
125                         rv[0]=Config.CADI_USER_CHAIN;
126                         rv[1]=chain;
127                         for(int i = 0;i<strs.length;++i) {
128                                 rv[i+2]=strs[i];
129                         }
130                         return rv;
131                 }
132         }
133 }