Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / logging / ErrorObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.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
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 error code string.  This is also the string
280          * configured in Nagios to alert on
281          *
282          * @return the error code string
283          */
284         // Get the X.Y.Z representation of the error code
285         public String getErrorCodeString() {
286                 String prefix = null;
287                 switch (disposition) {
288                 default:
289                         prefix = "";
290                         break;
291                 case "5":
292                         prefix = "ERR.";
293                         break;
294                 }
295                 return prefix + disposition + "." + category + "." + errorCode;
296         }
297
298         /**
299          * {@inheritDoc}
300          */
301         @Override
302         public String toString() {
303                 return "ErrorObject [errorCode="+ errorCode + ", errorText=" + errorText
304                                 + ", restErrorCode=" + restErrorCode + ", httpResponseCode="+ httpResponseCode
305                                 + ", severity=" + severity + ", disposition=" + disposition + ", category=" + category +"]";
306         }
307         
308 }