4b619c66dc795db46f66f9350fa17dcf46e9b6f7
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / aaf / client / ErrMessage.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.aaf.client;\r
24 \r
25 import java.io.PrintStream;\r
26 \r
27 import aaf.v2_0.Error;\r
28 \r
29 import com.att.cadi.client.Future;\r
30 import com.att.cadi.util.Vars;\r
31 import com.att.inno.env.APIException;\r
32 import com.att.inno.env.Data.TYPE;\r
33 import com.att.rosetta.env.RosettaDF;\r
34 import com.att.rosetta.env.RosettaEnv;\r
35 \r
36 public class ErrMessage {\r
37         private RosettaDF<Error> errDF;\r
38         \r
39         public ErrMessage(RosettaEnv env) throws APIException {\r
40                 errDF = env.newDataFactory(Error.class);\r
41         }\r
42 \r
43         /**\r
44          * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.\r
45          * \r
46          * This code will create a meaningful string from this format. \r
47          * \r
48          * @param ps\r
49          * @param df\r
50          * @param r\r
51          * @throws APIException\r
52          */\r
53         public void printErr(PrintStream ps,  String attErrJson) throws APIException {\r
54                 StringBuilder sb = new StringBuilder();\r
55                 Error err = errDF.newData().in(TYPE.JSON).load(attErrJson).asObject();\r
56                 ps.println(toMsg(sb,err));\r
57         }\r
58         \r
59         /**\r
60          * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.\r
61          * \r
62          * This code will create a meaningful string from this format. \r
63          * \r
64          * @param sb\r
65          * @param df\r
66          * @param r\r
67          * @throws APIException\r
68          */\r
69         public StringBuilder toMsg(StringBuilder sb,  String attErrJson) throws APIException {\r
70                 return toMsg(sb,errDF.newData().in(TYPE.JSON).load(attErrJson).asObject());\r
71         }\r
72         \r
73         public StringBuilder toMsg(Future<?> future) {\r
74                 return toMsg(new StringBuilder(),future);\r
75         }\r
76         \r
77         public StringBuilder toMsg(StringBuilder sb, Future<?> future) {\r
78                 try {\r
79                         toMsg(sb,errDF.newData().in(TYPE.JSON).load(future.body()).asObject());\r
80                 } catch(Exception e) {\r
81                         //just print what we can\r
82                         sb.append(future.code());\r
83                         sb.append(": ");\r
84                         sb.append(future.body());\r
85                 }\r
86                 return sb;\r
87         }\r
88 \r
89         public StringBuilder toMsg(StringBuilder sb, Error err) {\r
90                 sb.append(err.getMessageId());\r
91                 sb.append(' ');\r
92                 String[] vars = new String[err.getVariables().size()];\r
93                 err.getVariables().toArray(vars);\r
94                 Vars.convert(sb, err.getText(),vars);\r
95                 return sb;\r
96         }\r
97 }\r