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