fa296451f6957ce062db5590c07e10a1b3a28a2f
[sdnc/apps.git] /
1 /*
2  * ============LICENSE_START===================================================
3  * Copyright (c) 2018 Amdocs
4  * ============================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=====================================================
17  */
18 package org.onap.sdnc.apps.pomba.networkdiscovery;
19
20 import javax.ws.rs.core.Response.Status;
21
22 public class ApplicationException extends Exception {
23     public enum Error {
24         GENERAL_FAILURE("NET.0001", "An error occurred: %s"),
25         MISSING_PARAM("NET.0002", "Missing required parameter %s"),
26         UNAUTHORIZED("NET.0003", "Unauthorized");
27
28         private final String responseCode;
29         private final String message;
30
31         private Error(String responseCode, String message) {
32             this.responseCode = responseCode;
33             this.message = message;
34         }
35
36         public String getMessage(Object... args) {
37             return String.format(this.message, args);
38         }
39
40         public String getResponseCode() {
41             return this.responseCode;
42         }
43     }
44
45     private static final long serialVersionUID = -4874149714911165454L;
46
47     private final Status httpStatus;
48     private String responseCode;
49
50     public ApplicationException(String message) {
51         this(message, Status.INTERNAL_SERVER_ERROR);
52     }
53
54     public ApplicationException(Error errorCode, Status httpStatus, Object... args) {
55         super(errorCode.getMessage(args));
56         if (httpStatus == null) {
57             throw new NullPointerException("httpStatus");
58         }
59
60         this.responseCode = errorCode.getResponseCode();
61         this.httpStatus = httpStatus;
62     }
63
64     public ApplicationException(String message, Status httpStatus) {
65         super(message);
66         if (httpStatus == null) {
67             throw new NullPointerException("httpStatus");
68         }
69         this.httpStatus = httpStatus;
70     }
71
72     public ApplicationException(String message, Exception cause) {
73         super(message, cause);
74         this.httpStatus = Status.INTERNAL_SERVER_ERROR;
75     }
76
77     public Status getHttpStatus() {
78         return this.httpStatus;
79     }
80
81     public String getResponseCode() {
82         return this.responseCode;
83     }
84
85 }