Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / probes / HttpRequestMetadata.java
1 package org.onap.vid.model.probes;
2
3 import com.google.common.base.MoreObjects;
4 import org.apache.commons.lang3.StringUtils;
5 import org.onap.vid.aai.ExceptionWithRequestInfo;
6 import org.onap.vid.aai.ResponseWithRequestInfo;
7 import org.onap.vid.mso.RestObjectWithRequestInfo;
8 import org.onap.vid.utils.Logging;
9 import org.springframework.http.HttpMethod;
10
11 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
12
13 public class HttpRequestMetadata extends StatusMetadata {
14     private final HttpMethod httpMethod;
15     private final int httpCode;
16     private final String url;
17     private String rawData = "";
18
19     public HttpRequestMetadata(HttpMethod httpMethod, int httpCode, String url, String rawData, String description, long duration) {
20         super(description, duration);
21         this.httpMethod = httpMethod;
22         this.url = url;
23         this.httpCode = httpCode;
24         this.rawData = rawData;
25     }
26
27     public HttpRequestMetadata(ResponseWithRequestInfo response, String description, long duration) {
28         this(response, description, duration, true);
29     }
30
31     public HttpRequestMetadata(RestObjectWithRequestInfo response, String description, long duration) {
32         super(description, duration);
33         this.httpMethod = response.getHttpMethod();
34         this.url = response.getRequestedUrl();
35         this.httpCode = response.getHttpCode();
36         this.rawData = response.getRawData();
37     }
38
39     public HttpRequestMetadata(ResponseWithRequestInfo response, String description, long duration, boolean readRawData) {
40         super(description, duration);
41         this.httpMethod = response.getRequestHttpMethod();
42         this.url = response.getRequestUrl();
43         this.httpCode = response.getResponse().getStatus();
44         if (readRawData) {
45             try {
46                 this.rawData = response.getResponse().readEntity(String.class);
47             } catch (Exception e) {
48                 //Nothing to do here
49             }
50         }
51     }
52
53     public HttpRequestMetadata(ExceptionWithRequestInfo exception, long duration) {
54         this(exception.getHttpMethod(),
55                 defaultIfNull(exception.getHttpCode(), 0),
56                 exception.getRequestedUrl(),
57                 exception.getRawData(),
58                 Logging.exceptionToDescription(exception.getCause()),
59                 duration);
60     }
61
62
63
64     public HttpMethod getHttpMethod() {
65         return httpMethod;
66     }
67
68     public int getHttpCode() {
69         return httpCode;
70     }
71
72     public String getUrl() {
73         return url;
74     }
75
76     public String getRawData() {
77         return StringUtils.substring(rawData, 0, 500);
78     }
79
80     @Override
81     public String toString() {
82         return MoreObjects.toStringHelper(this)
83                 .add("httpMethod", httpMethod)
84                 .add("httpCode", httpCode)
85                 .add("url", url)
86                 .add("duration", duration)
87                 .add("description", description)
88                 .add("rawData", rawData)
89                 .toString();
90     }
91 }