Support for selection of capabilities
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / exception / ResponseFormat.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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 package org.openecomp.sdc.exception;
21
22 import lombok.Setter;
23
24 /**
25  * Nested POJOs to express required JSON format of the error
26  * <p>
27  * { "requestError": { "serviceException": { "messageId": "", "text": "", "variables": [] } } }
28  *
29  * @author paharoni
30  */
31 public class ResponseFormat {
32
33     @Setter
34     private int status;
35     private RequestErrorWrapper requestErrorWrapper;
36
37     public ResponseFormat() {
38         super();
39     }
40
41     public ResponseFormat(int status) {
42         super();
43         this.status = status;
44     }
45
46     public Integer getStatus() {
47         return status;
48     }
49
50     public RequestErrorWrapper getRequestError() {
51         return requestErrorWrapper;
52     }
53
54     public void setRequestError(RequestErrorWrapper requestErrorWrapper) {
55         this.requestErrorWrapper = requestErrorWrapper;
56     }
57
58     public void setPolicyException(PolicyException policyException) {
59         this.requestErrorWrapper = new RequestErrorWrapper(new RequestError());
60         requestErrorWrapper.setPolicyException(policyException);
61     }
62
63     public void setServiceException(ServiceException serviceException) {
64         this.requestErrorWrapper = new RequestErrorWrapper(new RequestError());
65         requestErrorWrapper.setServiceException(serviceException);
66     }
67
68     public void setOkResponseInfo(OkResponseInfo okResponseInfo) {
69         this.requestErrorWrapper = new RequestErrorWrapper(new RequestError());
70         requestErrorWrapper.setOkResponseInfo(okResponseInfo);
71     }
72
73     public String getFormattedMessage() {
74         if (this.requestErrorWrapper.requestError.okResponseInfo != null) {
75             return this.requestErrorWrapper.requestError.okResponseInfo.getFormattedErrorMessage();
76         }
77         if (this.requestErrorWrapper.requestError.serviceException != null) {
78             return this.requestErrorWrapper.requestError.serviceException.getFormattedErrorMessage();
79         }
80         return this.requestErrorWrapper.requestError.policyException.getFormattedErrorMessage();
81     }
82
83     public String getText() {
84         if (this.requestErrorWrapper.requestError.okResponseInfo != null) {
85             return this.requestErrorWrapper.requestError.okResponseInfo.getText();
86         }
87         if (this.requestErrorWrapper.requestError.serviceException != null) {
88             return this.requestErrorWrapper.requestError.serviceException.getText();
89         }
90         return this.requestErrorWrapper.requestError.policyException.getText();
91     }
92
93     public String[] getVariables() {
94         if (this.requestErrorWrapper.requestError.okResponseInfo != null) {
95             return this.requestErrorWrapper.requestError.okResponseInfo.getVariables();
96         }
97         if (this.requestErrorWrapper.requestError.serviceException != null) {
98             return this.requestErrorWrapper.requestError.serviceException.getVariables();
99         }
100         return this.requestErrorWrapper.requestError.policyException.getVariables();
101     }
102
103     public String getMessageId() {
104         if (this.requestErrorWrapper.requestError.okResponseInfo != null) {
105             return this.requestErrorWrapper.requestError.okResponseInfo.getMessageId();
106         }
107         if (this.requestErrorWrapper.requestError.serviceException != null) {
108             return this.requestErrorWrapper.requestError.serviceException.getMessageId();
109         }
110         return this.requestErrorWrapper.requestError.policyException.getMessageId();
111     }
112
113     public class RequestErrorWrapper {
114
115         private RequestError requestError;
116
117         public RequestErrorWrapper() {
118             this.requestError = new RequestError();
119         }
120
121         public RequestErrorWrapper(RequestError requestError) {
122             this.requestError = requestError;
123         }
124
125         public RequestError getRequestError() {
126             return requestError;
127         }
128
129         public void setRequestError(RequestError requestError) {
130             this.requestError = requestError;
131         }
132
133         public void setPolicyException(PolicyException policyException) {
134             requestError.setPolicyException(policyException);
135         }
136
137         public void setServiceException(ServiceException serviceException) {
138             requestError.setServiceException(serviceException);
139         }
140
141         public void setOkResponseInfo(OkResponseInfo okResponseInfo) {
142             requestError.setOkResponseInfo(okResponseInfo);
143         }
144     }
145
146     public class RequestError {
147
148         @SuppressWarnings("unused")
149         private PolicyException policyException;
150         @SuppressWarnings("unused")
151         private ServiceException serviceException;
152         @SuppressWarnings("unused")
153         private OkResponseInfo okResponseInfo;
154
155         public PolicyException getPolicyException() {
156             return policyException;
157         }
158
159         public ServiceException getServiceException() {
160             return serviceException;
161         }
162
163         public OkResponseInfo getOkResponseInfo() {
164             return okResponseInfo;
165         }
166
167         public void setPolicyException(PolicyException policyException) {
168             this.policyException = policyException;
169         }
170
171         public void setServiceException(ServiceException serviceException) {
172             this.serviceException = serviceException;
173         }
174
175         public void setOkResponseInfo(OkResponseInfo okResponseInfo) {
176             this.okResponseInfo = okResponseInfo;
177         }
178     }
179
180     @Override
181     public String toString() {
182         return "ResponseFormat[" + "status=" + status + ", requestErrorWrapper=" + requestErrorWrapper + ']';
183     }
184 }