Extend probe mechanism
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / probes / HttpRequestMetadata.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.model.probes;
23
24 import com.google.common.base.MoreObjects;
25 import io.joshworks.restclient.http.HttpResponse;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.vid.aai.ExceptionWithRequestInfo;
28 import org.onap.vid.aai.ResponseWithRequestInfo;
29 import org.onap.vid.mso.RestObjectWithRequestInfo;
30 import org.onap.vid.utils.Logging;
31 import org.springframework.http.HttpMethod;
32
33 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
34
35 public class HttpRequestMetadata extends StatusMetadata {
36     private final HttpMethod httpMethod;
37     private final int httpCode;
38     private final String url;
39     private String rawData = "";
40
41     public HttpRequestMetadata(HttpMethod httpMethod, int httpCode, String url, String rawData, String description, long duration) {
42         super(description, duration);
43         this.httpMethod = httpMethod;
44         this.url = url;
45         this.httpCode = httpCode;
46         this.rawData = rawData;
47     }
48
49     public HttpRequestMetadata(ResponseWithRequestInfo response, String description, long duration) {
50         this(response, description, duration, true);
51     }
52
53     public HttpRequestMetadata(RestObjectWithRequestInfo response, String description, long duration) {
54         super(description, duration);
55         this.httpMethod = response.getHttpMethod();
56         this.url = response.getRequestedUrl();
57         this.httpCode = response.getHttpCode();
58         this.rawData = response.getRawData();
59     }
60
61     public HttpRequestMetadata(ResponseWithRequestInfo response, String description, long duration, boolean readRawData) {
62         super(description, duration);
63         this.httpMethod = response.getRequestHttpMethod();
64         this.url = response.getRequestUrl();
65         this.httpCode = response.getResponse().getStatus();
66         if (readRawData) {
67             try {
68                 this.rawData = response.getResponse().readEntity(String.class);
69             } catch (Exception e) {
70                 //Nothing to do here
71             }
72         }
73     }
74
75     public HttpRequestMetadata(ExceptionWithRequestInfo exception, long duration) {
76         this(exception.getHttpMethod(),
77                 defaultIfNull(exception.getHttpCode(), 0),
78                 exception.getRequestedUrl(),
79                 exception.getRawData(),
80                 Logging.exceptionToDescription(exception.getCause()),
81                 duration);
82     }
83
84     public HttpRequestMetadata(HttpResponse<String> response, HttpMethod method, String description, long duration, String url) {
85         this(method, response.getStatus(), url, response.getBody(), description, duration);
86     }
87
88
89
90     public HttpMethod getHttpMethod() {
91         return httpMethod;
92     }
93
94     public int getHttpCode() {
95         return httpCode;
96     }
97
98     public String getUrl() {
99         return url;
100     }
101
102     public String getRawData() {
103         return StringUtils.substring(rawData, 0, 500);
104     }
105
106     @Override
107     public String toString() {
108         return MoreObjects.toStringHelper(this)
109                 .add("httpMethod", httpMethod)
110                 .add("httpCode", httpCode)
111                 .add("url", url)
112                 .add("duration", duration)
113                 .add("description", description)
114                 .add("rawData", rawData)
115                 .toString();
116     }
117 }