2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.cadi.enduser;
23 import java.io.IOException;
24 import java.net.ConnectException;
25 import java.security.Principal;
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;
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;
49 private Headers headers = new Headers() {
51 public String[] headers() {
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);
60 restClient = tcf.newTzClient(endpoint);
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");
68 restClient.setToken(client_id,rtt.value);
69 } catch (IOException e) {
70 throw new CadiException(e);
73 throw new CadiException(rtt.error);
77 public SimpleRESTClient timeout(int newTimeout) {
78 callTimeout = newTimeout;
82 //Format:<ID>:<APP>:<protocol>[:AS][,<ID>:<APP>:<protocol>]*
83 public SimpleRESTClient as(Principal principal) {
87 if(principal instanceof TaggedPrincipal) {
88 TaggedPrincipal tp = (TaggedPrincipal)principal;
89 chain = tp.getName() + ':' + (app==null?"":app) + ':' + tp.tag() + ":AS";
91 chain = principal.getName() + (app==null?"":':'+app);
97 public String get(final String path) throws CadiException, LocatorException, APIException {
98 return get(path,"application/json");
101 public String get(final String path, final String accepts) throws CadiException, LocatorException, APIException {
102 return restClient.best(new Retryable<String>() {
104 public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
105 Future<String> future = client.read(path,accepts, headers());
106 if(future.get(callTimeout)) {
109 throw new APIException(future.code() + future.body());
115 public interface Headers {
119 public String[] headers() {
121 return headers.headers();
123 String[] strs = headers.headers();
124 String[] rv = new String[strs.length+2];
125 rv[0]=Config.CADI_USER_CHAIN;
127 for(int i = 0;i<strs.length;++i) {