metric log updates and util fix
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / HttpURLConnectionMetricUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 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.logging.filter.base;
22
23 import java.net.HttpURLConnection;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class HttpURLConnectionMetricUtil
28         extends AbstractMetricLogFilter<HttpURLConnection, HttpURLConnection, HttpURLConnection> {
29     protected static final Logger logger = LoggerFactory.getLogger(HttpURLConnectionMetricUtil.class);
30
31     public void logBefore(HttpURLConnection request, ONAPComponentsList targetEntity) {
32         setTargetEntity(targetEntity);
33         pre(request, request);
34     }
35
36     public void logAfter(HttpURLConnection request) {
37         post(request, request);
38     }
39
40     @Override
41     protected String getTargetServiceName(HttpURLConnection request) {
42         return request.getURL().getPath();
43     }
44
45     @Override
46     protected int getHttpStatusCode(HttpURLConnection response) {
47         try {
48             return response.getResponseCode();
49         } catch (Exception e) {
50             logger.error("getHttpStatusCode failed, defaulting to 500", e);
51         }
52         return 500;
53     }
54
55     @Override
56     protected String getResponseCode(HttpURLConnection response) {
57         try {
58             return String.valueOf(response.getResponseCode());
59         } catch (Exception e) {
60             logger.error("getResponseCode failed, defaulting to 500", e);
61         }
62         return "500";
63     }
64
65     @Override
66     protected String getTargetEntity(HttpURLConnection request) {
67         return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
68     }
69
70     @Override
71     protected void addHeader(HttpURLConnection request, String headerName, String headerValue) {
72         request.setRequestProperty(headerName, headerValue);
73     }
74 }