2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.logging.filter.base;
23 import java.net.HttpURLConnection;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class HttpURLConnectionMetricUtil
28 extends AbstractMetricLogFilter<HttpURLConnection, HttpURLConnection, HttpURLConnection> {
29 protected static final Logger logger = LoggerFactory.getLogger(HttpURLConnectionMetricUtil.class);
31 public void logBefore(HttpURLConnection request, ONAPComponentsList targetEntity) {
32 setTargetEntity(targetEntity);
33 pre(request, request);
36 public void logAfter(HttpURLConnection request) {
37 post(request, request);
41 protected String getTargetServiceName(HttpURLConnection request) {
42 return request.getURL().getPath();
46 protected int getHttpStatusCode(HttpURLConnection response) {
48 return response.getResponseCode();
49 } catch (Exception e) {
50 logger.error("getHttpStatusCode failed, defaulting to 500", e);
56 protected String getResponseCode(HttpURLConnection response) {
58 return String.valueOf(response.getResponseCode());
59 } catch (Exception e) {
60 logger.error("getResponseCode failed, defaulting to 500", e);
66 protected String getTargetEntity(HttpURLConnection request) {
67 return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
70 public void pre(HttpURLConnection request) {
74 public void filter(HttpURLConnection request, HttpURLConnection response) {
75 post(request, response);
79 protected void addHeader(HttpURLConnection request, String headerName, String headerValue) {
80 request.setRequestProperty(headerName, headerValue);