add the ServiceException Constructor(httpcode and message)
[vfc/nfvo/wfengine.git] / rest-client / src / main / java / org / openo / baseservice / remoteservice / exception / ServiceException.java
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 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   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  
58      */
59     public ServiceException() {
60         super("");
61     }
62
63     /**
64      * Constructor<br/>
65      * <p>
66      * </p>
67      * 
68      * @since  
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  
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  
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  
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      * the exception include the httpcode and message.
122      * </p>
123      * 
124      * @since
125      * @param httpCode http code.
126      * @param message details.
127      */
128     public ServiceException(final int httpCode, final String message) {
129         super(message);
130         this.setHttpCode(httpCode);
131     }
132
133     /**
134      * Constructor<br/>
135      * <p>
136      * </p>
137      * 
138      * @since  
139      * @param id: exception id.
140      * @param httpCode: http code.
141      * @param exceptionArgs: Exception handling frame parameters.
142      */
143     public ServiceException(final String id, final int httpCode, final ExceptionArgs exceptionArgs) {
144         super();
145         this.setId(id);
146         this.setHttpCode(httpCode);
147         this.setExceptionArgs(exceptionArgs);
148     }
149
150     /**
151      * Constructor<br/>
152      * <p>
153      * Have a placeholder exception, use args formatted message.
154      * </p>
155      * 
156      * @since  
157      * @param id: exception id.
158      * @param message: details.
159      * @param args: Placeholders for parameters
160      */
161     public ServiceException(final String id, final String message, final Object... args) {
162         super(MessageFormat.format(message, args));
163         this.setId(id);
164         this.args = args;
165     }
166
167     /**
168      * Constructor<br/>
169      * <p>
170      * Have a placeholder exception, use args formatted message
171      * </p>
172      * 
173      * @since  
174      * @param id: exception id.
175      * @param message: details.
176      * @param cause: reason.
177      * @param args: placeholder for parameters
178      */
179     public ServiceException(final String id, final String message, final Throwable cause, final Object... args) {
180         super(MessageFormat.format(message, args), cause);
181         this.setId(id);
182         this.args = args;
183     }
184
185     /**
186      * Constructor<br/>
187      * <p>
188      * </p>
189      * 
190      * @since  
191      * @param id: exception id.
192      * @param message: details.
193      * @param cause: reason.
194      */
195     public ServiceException(final String id, final String message, final Throwable cause) {
196         super(message, cause);
197         this.setId(id);
198     }
199
200     /**
201      * Constructor<br/>
202      * <p>
203      * </p>
204      * 
205      * @since  
206      * @param cause: reason.
207      */
208     public ServiceException(final Throwable cause) {
209         super(cause);
210     }
211
212     /**
213      * Get exceptoin id.<br/>
214      * 
215      * @return
216      * @since  
217      */
218     public String getId() {
219         if(id == null || id.isEmpty()) {
220             return DEFAULT_ID;
221         }
222         return id;
223     }
224
225     public void setId(final String id) {
226         this.id = id;
227     }
228
229     public int getHttpCode() {
230         return this.httpCode;
231     }
232
233     public void setHttpCode(final int httpCode) {
234         this.httpCode = httpCode;
235     }
236
237     /**
238      * Obtain the ROA exception handling framework parameters<br/>
239      * 
240      * @return exception args.
241      * @since  
242      */
243     public ExceptionArgs getExceptionArgs() {
244         return exceptionArgs;
245     }
246
247     public void setExceptionArgs(final ExceptionArgs exceptionArgs) {
248         this.exceptionArgs = exceptionArgs;
249     }
250
251     /**
252      * Gets the parameter information<br/>
253      * 
254      * @return parameter list.
255      * @since  
256      */
257     protected Object[] getArgs() {
258         if(args == null || args.length == 0 || DEFAULT_ID.equals(getId())) {
259             return new Object[] {};
260         }
261         return args;
262     }
263
264     @Override
265     public String toString() {
266         return "exception.id: " + getId() + "; " + super.toString();
267     }
268
269 }