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====================================================
22 package org.onap.aaf.cadi.aaf.client;
24 import java.io.PrintStream;
26 import org.onap.aaf.cadi.client.Future;
27 import org.onap.aaf.cadi.util.Vars;
28 import org.onap.aaf.misc.env.APIException;
29 import org.onap.aaf.misc.env.Data.TYPE;
30 import org.onap.aaf.misc.rosetta.env.RosettaDF;
31 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
33 import aaf.v2_0.Error;
35 public class ErrMessage {
36 private RosettaDF<Error> errDF;
38 public ErrMessage(RosettaEnv env) throws APIException {
39 errDF = env.newDataFactory(Error.class);
43 * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.
45 * This code will create a meaningful string from this format.
50 * @throws APIException
52 public void printErr(PrintStream ps, String attErrJson) throws APIException {
53 StringBuilder sb = new StringBuilder();
54 Error err = errDF.newData().in(TYPE.JSON).load(attErrJson).asObject();
55 ps.println(toMsg(sb,err));
59 * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.
61 * This code will create a meaningful string from this format.
66 * @throws APIException
68 public StringBuilder toMsg(StringBuilder sb, String attErrJson) throws APIException {
69 return toMsg(sb,errDF.newData().in(TYPE.JSON).load(attErrJson).asObject());
72 public StringBuilder toMsg(Future<?> future) {
73 return toMsg(new StringBuilder(),future);
76 public StringBuilder toMsg(StringBuilder sb, Future<?> future) {
78 toMsg(sb,errDF.newData().in(TYPE.JSON).load(future.body()).asObject());
79 } catch (Exception e) {
80 //just print what we can
81 sb.append(future.code());
83 sb.append(future.body());
88 public StringBuilder toMsg(StringBuilder sb, Error err) {
89 sb.append(err.getMessageId());
91 Object[] vars = new String[err.getVariables().size()];
92 err.getVariables().toArray(vars);
93 Vars.convert(sb, err.getText(),vars);
97 public Error getError(Future<?> future) throws APIException {
98 return errDF.newData().in(TYPE.JSON).load(future.body()).asObject();