e64d9149d8df916c856366c7601dc1172e69e34b
[vfc/nfvo/wfengine.git] / CommonLibrary / rest-client / src / main / java / org / openo / baseservice / remoteservice / exception / ServiceException.java
1 /*
2  * Copyright (c) 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 package org.openo.baseservice.remoteservice.exception;
17
18 import java.text.MessageFormat;
19
20 /**
21  * The base class for all common exception.<br/>
22  * <p>
23  * </p>
24  * 
25  * @author
26  * @version SDNO 0.5 28-May-2016
27  */
28 public class ServiceException extends Exception {
29
30     /**
31      * default exception id.
32      */
33     public static final String DEFAULT_ID = "framwork.remote.SystemError";
34
35     /**
36      * Serial number.
37      */
38     private static final long serialVersionUID = 5703294364555144738L;
39
40     /**
41      * Exception id.
42      */
43     private String id = DEFAULT_ID;
44
45     private Object[] args = null;
46
47     private int httpCode = 500;
48
49     private ExceptionArgs exceptionArgs = null;
50
51     /**
52      * The default constructor<br/>
53      * <p>
54      * This method is only used as deserialized, in other cases, use parameterized constructor.
55      * </p>
56      * 
57      * @since SDNO 0.5
58      */
59     public ServiceException() {
60         super("");
61     }
62
63     /**
64      * Constructor<br/>
65      * <p>
66      * </p>
67      * 
68      * @since SDNO 0.5
69      * @param id: details.
70      * @param cause: reason.
71      */
72     public ServiceException(final String id, final Throwable cause) {
73         super(cause);
74         this.setId(id);
75     }
76
77     /**
78      * Constructor<br/>
79      * <p>
80      * </p>
81      * 
82      * @since SDNO 0.5
83      * @param message: details.
84      */
85     public ServiceException(final String message) {
86         super(message);
87     }
88
89     /**
90      * Constructor<br/>
91      * <p>
92      * </p>
93      * 
94      * @since SDNO 0.5
95      * @param id: exception id.
96      * @param message: details.
97      */
98     public ServiceException(final String id, final String message) {
99         super(message);
100         this.setId(id);
101     }
102
103     /**
104      * Constructor<br/>
105      * <p>
106      * </p>
107      * 
108      * @since SDNO 0.5
109      * @param id: exception id.
110      * @param httpCode: http status code.
111      */
112     public ServiceException(final String id, final int httpCode) {
113         super();
114         this.setId(id);
115         this.setHttpCode(httpCode);
116     }
117
118     /**
119      * Constructor<br/>
120      * <p>
121      * </p>
122      * 
123      * @since SDNO 0.5
124      * @param id: exception id.
125      * @param httpCode: http code.
126      * @param exceptionArgs: Exception handling frame parameters.
127      */
128     public ServiceException(final String id, final int httpCode, final ExceptionArgs exceptionArgs) {
129         super();
130         this.setId(id);
131         this.setHttpCode(httpCode);
132         this.setExceptionArgs(exceptionArgs);
133     }
134
135     /**
136      * Constructor<br/>
137      * <p>
138      * Have a placeholder exception, use args formatted message.
139      * </p>
140      * 
141      * @since SDNO 0.5
142      * @param id: exception id.
143      * @param message: details.
144      * @param args: Placeholders for parameters
145      */
146     public ServiceException(final String id, final String message, final Object... args) {
147         super(MessageFormat.format(message, args));
148         this.setId(id);
149         this.args = args;
150     }
151
152     /**
153      * Constructor<br/>
154      * <p>
155      * Have a placeholder exception, use args formatted message
156      * </p>
157      * 
158      * @since SDNO 0.5
159      * @param id: exception id.
160      * @param message: details.
161      * @param cause: reason.
162      * @param args: placeholder for parameters
163      */
164     public ServiceException(final String id, final String message, final Throwable cause, final Object... args) {
165         super(MessageFormat.format(message, args), cause);
166         this.setId(id);
167         this.args = args;
168     }
169
170     /**
171      * Constructor<br/>
172      * <p>
173      * </p>
174      * 
175      * @since SDNO 0.5
176      * @param id: exception id.
177      * @param message: details.
178      * @param cause: reason.
179      */
180     public ServiceException(final String id, final String message, final Throwable cause) {
181         super(message, cause);
182         this.setId(id);
183     }
184
185     /**
186      * Constructor<br/>
187      * <p>
188      * </p>
189      * 
190      * @since SDNO 0.5
191      * @param cause: reason.
192      */
193     public ServiceException(final Throwable cause) {
194         super(cause);
195     }
196
197     /**
198      * Get exceptoin id.<br/>
199      * 
200      * @return
201      * @since SDNO 0.5
202      */
203     public String getId() {
204         if(id == null || id.isEmpty()) {
205             return DEFAULT_ID;
206         }
207         return id;
208     }
209
210     public void setId(final String id) {
211         this.id = id;
212     }
213
214     public int getHttpCode() {
215         return this.httpCode;
216     }
217
218     public void setHttpCode(final int httpCode) {
219         this.httpCode = httpCode;
220     }
221
222     /**
223      * Obtain the ROA exception handling framework parameters<br/>
224      * 
225      * @return exception args.
226      * @since SDNO 0.5
227      */
228     public ExceptionArgs getExceptionArgs() {
229         return exceptionArgs;
230     }
231
232     public void setExceptionArgs(final ExceptionArgs exceptionArgs) {
233         this.exceptionArgs = exceptionArgs;
234     }
235
236     /**
237      * Gets the parameter information<br/>
238      * 
239      * @return parameter list.
240      * @since SDNO 0.5
241      */
242     protected Object[] getArgs() {
243         if(args == null || args.length == 0 || DEFAULT_ID.equals(getId())) {
244             return new Object[] {};
245         }
246         return args;
247     }
248
249     @Override
250     public String toString() {
251         return "exception.id: " + getId() + "; " + super.toString();
252     }
253
254 }