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