2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.vid.model.probes;
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;
33 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
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 = "";
41 public HttpRequestMetadata(HttpMethod httpMethod, int httpCode, String url, String rawData, String description, long duration) {
42 super(description, duration);
43 this.httpMethod = httpMethod;
45 this.httpCode = httpCode;
46 this.rawData = rawData;
49 public HttpRequestMetadata(ResponseWithRequestInfo response, String description, long duration) {
50 this(response, description, duration, true);
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();
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();
68 this.rawData = response.getResponse().readEntity(String.class);
69 } catch (Exception e) {
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()),
84 public HttpRequestMetadata(HttpResponse<String> response, HttpMethod method, String description, long duration, String url) {
85 this(method, response.getStatus(), url, response.getBody(), description, duration);
90 public HttpMethod getHttpMethod() {
94 public int getHttpCode() {
98 public String getUrl() {
102 public String getRawData() {
103 return StringUtils.substring(rawData, 0, 500);
107 public String toString() {
108 return MoreObjects.toStringHelper(this)
109 .add("httpMethod", httpMethod)
110 .add("httpCode", httpCode)
112 .add("duration", duration)
113 .add("description", description)
114 .add("rawData", rawData)