Merge "Remove useless trailing whitespace"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / ExceptionWithRequestInfo.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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
21 package org.onap.vid.aai;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.springframework.http.HttpMethod;
25
26 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
27
28 public class ExceptionWithRequestInfo extends RuntimeException {
29
30     private final HttpMethod httpMethod;
31     private final String requestedUrl;
32     private final Integer httpCode;
33     private final String rawData;
34
35     public ExceptionWithRequestInfo(HttpMethod httpMethod, String requestedUrl, String rawData, Integer httpCode, Throwable cause) {
36         super(toMessage(httpMethod, requestedUrl, cause), cause);
37         this.httpMethod = httpMethod;
38         this.requestedUrl = requestedUrl;
39         this.rawData = rawData;
40         this.httpCode = httpCode;
41     }
42
43     public ExceptionWithRequestInfo(HttpMethod httpMethod, String requestedUrl, Throwable cause) {
44         this(httpMethod, requestedUrl, null, null, cause);
45     }
46
47     public String getRequestedUrl() {
48         return requestedUrl;
49     }
50
51     public String getRawData() {
52         return rawData;
53     }
54
55     public HttpMethod getHttpMethod() {
56         return httpMethod;
57     }
58
59     public Integer getHttpCode() {
60         return httpCode;
61     }
62
63     private static String toMessage(HttpMethod httpMethod, String requestedUrl, Throwable cause) {
64         if (StringUtils.isEmpty(requestedUrl)) {
65             return String.valueOf(cause);
66         } else {
67             return "" +
68                     "Exception while handling " +
69                     defaultIfNull(httpMethod, "request").toString() +
70                     " " + requestedUrl +
71                     ": " + (cause == null ? "null" : cause.toString());
72         }
73     }
74 }