Update project structure for aaf/cadi
[aaf/cadi.git] / aaf / src / main / java / org / onap / aaf / 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 org.onap.aaf.cadi.aaf.client;\r
24 \r
25 import java.io.PrintStream;\r
26 \r
27 import org.onap.aaf.cadi.client.Future;\r
28 import org.onap.aaf.cadi.util.Vars;\r
29 \r
30 import aaf.v2_0.Error;\r
31 \r
32 import org.onap.aaf.inno.env.APIException;\r
33 import org.onap.aaf.inno.env.Data.TYPE;\r
34 import org.onap.aaf.rosetta.env.RosettaDF;\r
35 import org.onap.aaf.rosetta.env.RosettaEnv;\r
36 \r
37 public class ErrMessage {\r
38         private RosettaDF<Error> errDF;\r
39         \r
40         public ErrMessage(RosettaEnv env) throws APIException {\r
41                 errDF = env.newDataFactory(Error.class);\r
42         }\r
43 \r
44         /**\r
45          * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.\r
46          * \r
47          * This code will create a meaningful string from this format. \r
48          * \r
49          * @param ps\r
50          * @param df\r
51          * @param r\r
52          * @throws APIException\r
53          */\r
54         public void printErr(PrintStream ps,  String attErrJson) throws APIException {\r
55                 StringBuilder sb = new StringBuilder();\r
56                 Error err = errDF.newData().in(TYPE.JSON).load(attErrJson).asObject();\r
57                 ps.println(toMsg(sb,err));\r
58         }\r
59         \r
60         /**\r
61          * AT&T Requires a specific Error Format for RESTful Services, which AAF complies with.\r
62          * \r
63          * This code will create a meaningful string from this format. \r
64          * \r
65          * @param sb\r
66          * @param df\r
67          * @param r\r
68          * @throws APIException\r
69          */\r
70         public StringBuilder toMsg(StringBuilder sb,  String attErrJson) throws APIException {\r
71                 return toMsg(sb,errDF.newData().in(TYPE.JSON).load(attErrJson).asObject());\r
72         }\r
73         \r
74         public StringBuilder toMsg(Future<?> future) {\r
75                 return toMsg(new StringBuilder(),future);\r
76         }\r
77         \r
78         public StringBuilder toMsg(StringBuilder sb, Future<?> future) {\r
79                 try {\r
80                         toMsg(sb,errDF.newData().in(TYPE.JSON).load(future.body()).asObject());\r
81                 } catch(Exception e) {\r
82                         //just print what we can\r
83                         sb.append(future.code());\r
84                         sb.append(": ");\r
85                         sb.append(future.body());\r
86                 }\r
87                 return sb;\r
88         }\r
89 \r
90         public StringBuilder toMsg(StringBuilder sb, Error err) {\r
91                 sb.append(err.getMessageId());\r
92                 sb.append(' ');\r
93                 String[] vars = new String[err.getVariables().size()];\r
94                 err.getVariables().toArray(vars);\r
95                 Vars.convert(sb, err.getText(),vars);\r
96                 return sb;\r
97         }\r
98 }\r