a1bca90e3c546210b26fdbfbeafd8c84e3922d76
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / logging / ErrorObject.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.logging;
23
24 import javax.ws.rs.core.Response.Status;
25
26 /**
27  * 
28  * Contains the definition of all error message fields to be mapped from the Error
29  * properties file
30  *  
31  */
32 public class ErrorObject {
33         
34         private String disposition;
35         private String category;
36         private String severity;
37         private Status httpResponseCode = Status.INTERNAL_SERVER_ERROR; // default
38         private String restErrorCode = "3002";
39         private String errorCode;
40         private String errorText;
41
42         /**
43          * Instantiates a new error object.
44          */
45         public ErrorObject() {
46                 super();
47         }
48
49         /**
50          * Creates an error object with the default HTTP Error Code (Status.INTERNAL_SERVER_ERROR)
51          *
52          * @param disposition the disposition
53          * @param category the category
54          * @param severity the severity
55          * @param httpResponseCode the http response code
56          * @param restErrorCode the rest error code
57          * @param errorCode the error code
58          * @param errorText the error text
59          */
60         public ErrorObject(String disposition, String category, String severity, Integer httpResponseCode, String restErrorCode, String errorCode, String errorText) {
61                 super();
62                 this.setDisposition(disposition);
63                 this.setCategory(category);
64                 this.severity = severity;
65                 this.setHTTPResponseCode(httpResponseCode);
66                 this.setRESTErrorCode(restErrorCode);
67                 this.setErrorCode(errorCode);
68                 this.setErrorText(errorText);
69         }
70
71         // OLD STARTS HERE
72
73         /**
74          * Instantiates a new error object.
75          *
76          * @param severity the severity
77          * @param errorCode the error code
78          * @param errorText the error text
79          * @param disposition the disposition
80          * @param category the category
81          */
82         public ErrorObject(String severity, String errorCode, String errorText, String disposition, String category) {
83                 this(severity, Status.INTERNAL_SERVER_ERROR, errorCode, errorText, disposition, category);
84         }
85         
86         /**
87          * Instantiates a new error object.
88          *
89          * @param severity the severity
90          * @param httpResponseCode the http response code
91          * @param errorCode the error code
92          * @param errorText the error text
93          * @param disposition the disposition
94          * @param category the category
95          */
96         public ErrorObject(String severity, Integer httpResponseCode, String errorCode, String errorText, String disposition, String category) {
97                 super();
98                 this.severity = severity;
99                 this.setHTTPResponseCode(httpResponseCode);
100                 this.setErrorCode(errorCode);
101                 this.setErrorText(errorText);
102                 this.setDisposition(disposition);
103                 this.setCategory(category);
104         }
105         
106         /**
107          * Instantiates a new error object.
108          *
109          * @param severity the severity
110          * @param httpResponseCode the http response code
111          * @param errorCode the error code
112          * @param errorText the error text
113          * @param disposition the disposition
114          * @param category the category
115          */
116         public ErrorObject(String severity, Status httpResponseCode, String errorCode, String errorText, String disposition, String category) {
117                 super();
118                 this.severity = severity;
119                 this.setHTTPResponseCode(httpResponseCode);
120                 this.setErrorCode(errorCode);
121                 this.setErrorText(errorText);
122                 this.setDisposition(disposition);
123                 this.setCategory(category);
124         }
125         
126         /**
127          * Gets the disposition.
128          *
129          * @return the disposition
130          */
131         public String getDisposition() {
132                 return disposition;
133         }
134
135         /**
136          * Sets the disposition.
137          *
138          * @param disposition the new disposition
139          */
140         public void setDisposition(String disposition) {
141                 this.disposition = disposition;
142         }
143
144         /**
145          * Gets the category.
146          *
147          * @return the category
148          */
149         public String getCategory() {
150                 return category;
151         }
152
153         /**
154          * Sets the category.
155          *
156          * @param category the new category
157          */
158         public void setCategory(String category) {
159                 this.category = category;
160         }
161         
162         /**
163          * Gets the severity.
164          *
165          * @return the severity
166          */
167         public String getSeverity() {
168                 return severity;
169         }
170
171         /**
172          * Sets the severity.
173          *
174          * @param severity the new severity
175          */
176         public void setSeverity(String severity) {
177                 this.severity = severity;
178         }
179
180         /**
181          * Gets the error code.
182          *
183          * @return the error code
184          */
185         public String getErrorCode() {
186                 return errorCode;
187         }
188
189         /**
190          * Sets the error code.
191          *
192          * @param errorCode the new error code
193          */
194         public void setErrorCode(String errorCode) {
195                 this.errorCode = errorCode;
196         }
197         
198         /**
199          * Gets the HTTP response code.
200          *
201          * @return the HTTP response code
202          */
203         public Status getHTTPResponseCode() {
204                 return httpResponseCode;
205         }
206
207         /**
208          * Sets the HTTP response code.
209          *
210          * @param httpResponseCode the new HTTP response code
211          */
212         public void setHTTPResponseCode(Integer httpResponseCode) {
213                 this.httpResponseCode = Status.fromStatusCode(httpResponseCode);
214                 if (this.httpResponseCode == null) {
215                         throw new IllegalArgumentException("setHTTPResponseCode was passed an invalid Integer value, fix error.properties or your code "+httpResponseCode);
216                 }
217         }
218         
219         /**
220          * Sets the HTTP response code.
221          *
222          * @param httpResponseCode the new HTTP response code
223          */
224         public void setHTTPResponseCode(String httpResponseCode) {
225                 this.httpResponseCode = Status.fromStatusCode(Integer.valueOf(httpResponseCode));
226                 if (this.httpResponseCode == null) {
227                         throw new IllegalArgumentException("setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "+httpResponseCode);
228                 }
229         }
230         
231         /**
232          * Sets the REST error code.
233          *
234          * @param restErrorCode the new REST error code
235          */
236         public void setRESTErrorCode(String restErrorCode) {
237                 this.restErrorCode = restErrorCode;
238         }
239         
240         /**
241          * Gets the REST error code.
242          *
243          * @return the REST error code
244          */
245         public String getRESTErrorCode() {
246                 return this.restErrorCode;
247         }
248         
249         /**
250          * Sets the HTTP response code.
251          *
252          * @param httpResponseCode the new HTTP response code
253          */
254         public void setHTTPResponseCode(Status httpResponseCode) {
255                 this.httpResponseCode = httpResponseCode;
256                 if (this.httpResponseCode == null) {
257                         throw new IllegalArgumentException("setHTTPResponseCode was passed an invalid String value, fix error.properties or your code "+httpResponseCode);
258                 }
259         }
260
261         /**
262          * Gets the error text.
263          *
264          * @return the error text
265          */
266         public String getErrorText() {
267                 return errorText;
268         }
269
270         /**
271          * Sets the error text.
272          *
273          * @param errorText the new error text
274          */
275         public void setErrorText(String errorText) {
276                 this.errorText = errorText;
277         }
278
279         /**
280          * Gets the error code string.  This is also the string
281          * configured in Nagios to alert on
282          *
283          * @return the error code string
284          */
285         // Get the X.Y.Z representation of the error code
286         public String getErrorCodeString() {
287                 String prefix = null;
288                 switch (disposition) {
289                 default:
290                         prefix = "";
291                         break;
292                 case "5":
293                         prefix = "ERR.";
294                         break;
295                 }
296                 return prefix + disposition + "." + category + "." + errorCode;
297         }
298         
299         /**
300          * Gets the severity Code.  This is also the string
301          * configured in Nagios to alert on
302          *
303          * @return the severity
304          */
305         // Get the numerical value of severity
306         public String getSeverityCode(String severity) {
307                 String severityCode = "";
308                 switch (severity) {
309                 case "WARN":
310                         severityCode = "1";
311                         break;
312                 case "ERROR":
313                         severityCode = "2";
314                         break;
315                 case "FATAL":
316                         severityCode = "3";
317                         break;
318                 }
319                 return severityCode;
320         }
321
322         /**
323          * {@inheritDoc}
324          */
325         @Override
326         public String toString() {
327                 return "ErrorObject [errorCode="+ errorCode + ", errorText=" + errorText
328                                 + ", restErrorCode=" + restErrorCode + ", httpResponseCode="+ httpResponseCode
329                                 + ", severity=" + severity + ", disposition=" + disposition + ", category=" + category +"]";
330         }
331         
332 }