0f6d8435161f3aa762be452f01d4786f1feed5f9
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.common.util.restclient;
18
19 import java.text.MessageFormat;
20
21 /**
22  * The base class for all common exception.<br/>
23  * <p>
24  * </p>
25  * 
26  * @author
27  * @version 28-May-2016
28  */
29 public class ServiceException extends Exception {
30
31     /**
32      * default exception id.
33      */
34     public static final String DEFAULT_ID = "framwork.remote.SystemError";
35
36     /**
37      * Serial number.
38      */
39     private static final long serialVersionUID = 5703294364555144738L;
40
41     /**
42      * Exception id.
43      */
44     private String id = DEFAULT_ID;
45
46     private Object[] args = null;
47
48     private int httpCode = 500;
49
50     private ExceptionArgs exceptionArgs = null;
51
52     /**
53      * The default constructor<br/>
54      * <p>
55      * This method is only used as deserialized, in other cases, use parameterized constructor.
56      * </p>
57      * 
58      * @since
59      */
60     public ServiceException() {
61         super("");
62     }
63
64     /**
65      * Constructor<br/>
66      * <p>
67      * </p>
68      * 
69      * @since
70      * @param id: details.
71      * @param cause: reason.
72      */
73     public ServiceException(final String id, final Throwable cause) {
74         super(cause);
75         this.setId(id);
76     }
77
78     /**
79      * Constructor<br/>
80      * <p>
81      * </p>
82      * 
83      * @since
84      * @param message: details.
85      */
86     public ServiceException(final String message) {
87         super(message);
88     }
89
90     /**
91      * Constructor<br/>
92      * <p>
93      * </p>
94      * 
95      * @since
96      * @param id: exception id.
97      * @param message: details.
98      */
99     public ServiceException(final String id, final String message) {
100         super(message);
101         this.setId(id);
102     }
103
104     /**
105      * Constructor<br/>
106      * <p>
107      * </p>
108      * 
109      * @since
110      * @param id: exception id.
111      * @param httpCode: http status code.
112      */
113     public ServiceException(final String id, final int httpCode) {
114         super();
115         this.setId(id);
116         this.setHttpCode(httpCode);
117     }
118
119     /**
120      * Constructor<br/>
121      * <p>
122      * the exception include the httpcode and message.
123      * </p>
124      * 
125      * @since
126      * @param httpCode http code.
127      * @param message details.
128      */
129     public ServiceException(final int httpCode, final String message) {
130         super(message);
131         this.setHttpCode(httpCode);
132     }
133
134     /**
135      * Constructor<br/>
136      * <p>
137      * </p>
138      * 
139      * @since
140      * @param id: exception id.
141      * @param httpCode: http code.
142      * @param exceptionArgs: Exception handling frame parameters.
143      */
144     public ServiceException(final String id, final int httpCode, final ExceptionArgs exceptionArgs) {
145         super();
146         this.setId(id);
147         this.setHttpCode(httpCode);
148         this.setExceptionArgs(exceptionArgs);
149     }
150
151     /**
152      * Constructor<br/>
153      * <p>
154      * Have a placeholder exception, use args formatted message.
155      * </p>
156      * 
157      * @since
158      * @param id: exception id.
159      * @param message: details.
160      * @param args: Placeholders for parameters
161      */
162     public ServiceException(final String id, final String message, final Object... args) {
163         super(MessageFormat.format(message, args));
164         this.setId(id);
165         this.args = args;
166     }
167
168     /**
169      * Constructor<br/>
170      * <p>
171      * Have a placeholder exception, use args formatted message
172      * </p>
173      * 
174      * @since
175      * @param id: exception id.
176      * @param message: details.
177      * @param cause: reason.
178      * @param args: placeholder for parameters
179      */
180     public ServiceException(final String id, final String message, final Throwable cause, final Object... args) {
181         super(MessageFormat.format(message, args), cause);
182         this.setId(id);
183         this.args = args;
184     }
185
186     /**
187      * Constructor<br/>
188      * <p>
189      * </p>
190      * 
191      * @since
192      * @param id: exception id.
193      * @param message: details.
194      * @param cause: reason.
195      */
196     public ServiceException(final String id, final String message, final Throwable cause) {
197         super(message, cause);
198         this.setId(id);
199     }
200
201     /**
202      * Constructor<br/>
203      * <p>
204      * </p>
205      * 
206      * @since
207      * @param cause: reason.
208      */
209     public ServiceException(final Throwable cause) {
210         super(cause);
211     }
212
213     /**
214      * Get exceptoin id.<br/>
215      * 
216      * @return
217      * @since
218      */
219     public String getId() {
220         if(id == null || id.isEmpty()) {
221             return DEFAULT_ID;
222         }
223         return id;
224     }
225
226     public void setId(final String id) {
227         this.id = id;
228     }
229
230     public int getHttpCode() {
231         return this.httpCode;
232     }
233
234     public void setHttpCode(final int httpCode) {
235         this.httpCode = httpCode;
236     }
237
238     /**
239      * Obtain the ROA exception handling framework parameters<br/>
240      * 
241      * @return exception args.
242      * @since
243      */
244     public ExceptionArgs getExceptionArgs() {
245         return exceptionArgs;
246     }
247
248     public void setExceptionArgs(final ExceptionArgs exceptionArgs) {
249         this.exceptionArgs = exceptionArgs;
250     }
251
252     /**
253      * Gets the parameter information<br/>
254      * 
255      * @return parameter list.
256      * @since
257      */
258     protected Object[] getArgs() {
259         if(args == null || args.length == 0 || DEFAULT_ID.equals(getId())) {
260             return new Object[] {};
261         }
262         return args;
263     }
264
265     @Override
266     public String toString() {
267         return "exception.id: " + getId() + "; " + super.toString();
268     }
269
270 }