0fb4d60dbd26be787eb1f94fcc83f62fde5df12d
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / client / ErrMessage.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
22 package org.onap.aaf.cadi.aaf.client;
23
24 import java.io.PrintStream;
25
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;
32
33 import aaf.v2_0.Error;
34
35 public class ErrMessage {
36         private RosettaDF<Error> errDF;
37         
38         public ErrMessage(RosettaEnv env) throws APIException {
39                 errDF = env.newDataFactory(Error.class);
40         }
41
42         /**
43          * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.
44          * 
45          * This code will create a meaningful string from this format. 
46          * 
47          * @param ps
48          * @param df
49          * @param r
50          * @throws APIException
51          */
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));
56         }
57         
58         /**
59          * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.
60          * 
61          * This code will create a meaningful string from this format. 
62          * 
63          * @param sb
64          * @param df
65          * @param r
66          * @throws APIException
67          */
68         public StringBuilder toMsg(StringBuilder sb,  String attErrJson) throws APIException {
69                 return toMsg(sb,errDF.newData().in(TYPE.JSON).load(attErrJson).asObject());
70         }
71         
72         public StringBuilder toMsg(Future<?> future) {
73                 return toMsg(new StringBuilder(),future);
74         }
75         
76         public StringBuilder toMsg(StringBuilder sb, Future<?> future) {
77                 try {
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());
82                         sb.append(": ");
83                         sb.append(future.body());
84                 }
85                 return sb;
86         }
87
88         public StringBuilder toMsg(StringBuilder sb, Error err) {
89                 sb.append(err.getMessageId());
90                 sb.append(' ');
91                 String[] vars = new String[err.getVariables().size()];
92                 err.getVariables().toArray(vars);
93                 Vars.convert(sb, err.getText(),vars);
94                 return sb;
95         }
96 }