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.io.OutputStream;
25 import java.io.PrintWriter;
26 import java.net.ConnectException;
27 import java.security.Principal;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.LocatorException;
31 import org.onap.aaf.cadi.client.EClient;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.cadi.client.Rcli;
34 import org.onap.aaf.cadi.client.Result;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.cadi.config.Config;
37 import org.onap.aaf.cadi.oauth.TimedToken;
38 import org.onap.aaf.cadi.oauth.TokenClient;
39 import org.onap.aaf.cadi.oauth.TokenClientFactory;
40 import org.onap.aaf.cadi.oauth.TzClient;
41 import org.onap.aaf.cadi.principal.TaggedPrincipal;
42 import org.onap.aaf.misc.env.APIException;
43 import org.onap.aaf.misc.env.util.StringBuilderWriter;
45 public class SimpleRESTClient {
46 private static final String APPLICATION_JSON = "application/json";
47 private static final String[] EMPTY = new String[0];
48 private final TokenClient tokenClient;
49 private final TzClient restClient;
50 private int callTimeout;
51 private String client_id;
54 private Headers headers = new Headers() {
56 public String[] headers() {
60 public SimpleRESTClient(final TokenClientFactory tcf, final String tokenURL, final String endpoint, final String[] scope) throws CadiException, LocatorException, APIException {
61 callTimeout = Integer.parseInt(tcf.access.getProperty(Config.AAF_CALL_TIMEOUT,Config.AAF_CALL_TIMEOUT_DEF));
62 tokenClient = tcf.newClient(tokenURL);
63 Result<TimedToken> rtt = tokenClient.getToken(scope);
65 restClient = tcf.newTzClient(endpoint);
67 if ((client_id = tcf.access.getProperty(Config.AAF_APPID, null))==null) {
68 if ((client_id = tcf.access.getProperty(Config.CADI_ALIAS, null))==null) {
69 throw new CadiException(Config.AAF_APPID + " or " + Config.CADI_ALIAS + " needs to be defined");
73 restClient.setToken(client_id,rtt.value);
74 } catch (IOException e) {
75 throw new CadiException(e);
78 throw new CadiException(rtt.error);
82 public SimpleRESTClient timeout(int newTimeout) {
83 callTimeout = newTimeout;
87 //Format:<ID>:<APP>:<protocol>[:AS][,<ID>:<APP>:<protocol>]*
88 public SimpleRESTClient endUser(Principal principal) {
89 if (principal==null) {
92 if (principal instanceof TaggedPrincipal) {
93 TaggedPrincipal tp = (TaggedPrincipal)principal;
94 chain = tp.getName() + ':' + (app==null?"":app) + ':' + tp.tag() + ":AS";
96 chain = principal.getName() + (app==null?"":':'+app);
103 * Single Threaded Class for building up content
104 * @author Instrumental
107 public static class Input {
108 private static final byte[] EMPTY_STREAM_BYTES = "".getBytes();
110 private String content;
111 private StringBuilder sb;
118 public Input(final String content) {
119 this.content = content;
122 public void set(final String content) {
123 this.content = content;
126 public PrintWriter writer() {
127 return new PrintWriter(new StringBuilderWriter(builder()));
130 public StringBuilder builder() {
132 sb = new StringBuilder();
139 * Reuse StringBuilder object
141 public void clear() {
149 public String toString() {
152 } else if (sb!=null) {
153 return sb.toString();
159 public byte[] getBytes() {
163 rv = EMPTY_STREAM_BYTES;
165 rv = sb.toString().getBytes();
168 rv = content.getBytes();
175 /////////////////////////////////////////////////////////////
179 /////////////////////////////////////////////////////////////
180 public void create(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException {
181 post(path,APPLICATION_JSON, input);
184 public void post(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException {
185 post(path,APPLICATION_JSON, input);
188 public void post(final String path, final String contentType, final Input input) throws RESTException, CadiException, LocatorException, APIException {
189 Future<Void> future = restClient.best(new Retryable<Future<Void>>() {
191 public Future<Void> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
192 return client.create(path, contentType, new ETransfer(input));
195 if (!future.get(callTimeout)) {
196 throw new RESTException(future);
200 /////////////////////////////////////////////////////////////
204 /////////////////////////////////////////////////////////////
205 public String read(final String path) throws RESTException, CadiException, LocatorException, APIException {
206 return get(path,APPLICATION_JSON);
209 public String get(final String path) throws RESTException, CadiException, LocatorException, APIException {
210 return get(path,APPLICATION_JSON);
213 public String get(final String path, final String accepts) throws RESTException, CadiException, LocatorException, APIException {
214 Future<String> future = restClient.best(new Retryable<Future<String>>() {
216 public Future<String> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
217 return client.read(path,accepts, headers());
220 if (future.get(callTimeout)) {
223 throw new RESTException(future);
227 /////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////
233 public String update(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException {
234 return put(path,APPLICATION_JSON, input);
237 public String put(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException {
238 return put(path,APPLICATION_JSON, input);
241 public String put(final String path, final String contentType, final Input input) throws RESTException, CadiException, LocatorException, APIException {
242 Future<String> future = restClient.best(new Retryable<Future<String>>() {
244 public Future<String> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
245 return client.update(path, contentType, new ETransfer(input));
248 if (future.get(callTimeout)) {
251 throw new RESTException(future);
255 /////////////////////////////////////////////////////////////
259 /////////////////////////////////////////////////////////////
260 public void delete(final String path) throws RESTException, CadiException, LocatorException, APIException {
261 delete(path,APPLICATION_JSON);
264 public void delete(final String path, final String contentType) throws RESTException, CadiException, LocatorException, APIException {
265 Future<Void> future = restClient.best(new Retryable<Future<Void>>() {
267 public Future<Void> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
268 return client.delete(path, contentType);
271 if (!future.get(callTimeout)) {
272 throw new RESTException(future);
276 /////////////////////////////////////////////////////////////
278 private static class ETransfer implements EClient.Transfer {
280 public ETransfer(final Input input) {
285 public void transfer(OutputStream os) throws IOException, APIException {
286 os.write(input.getBytes());
290 public interface Headers {
294 public String[] headers() {
296 return headers.headers();
298 String[] strs = headers.headers();
299 String[] rv = new String[strs.length+2];
300 rv[0]=Config.CADI_USER_CHAIN;
302 for (int i = 0;i<strs.length;++i) {