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.openecomp.sdc.common.log.wrappers;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import java.util.List;
30 import java.util.StringTokenizer;
32 import static java.net.HttpURLConnection.*;
35 * Created by dd4296 on 12/20/2017.
37 * base class for metric and audit log logging
38 * holding the specific logic for data extraction
40 public class LoggerSdcUtilBase {
42 protected static Logger log = LoggerFactory.getLogger(LoggerSdcUtilBase.class.getName());
44 String getRequestIDfromHeaders(List<Object> requestHeader) {
45 // this method gets list of type object.
46 // toString method returns the RequestId with brackets.
47 String requestHeaderString = requestHeader.toString();
48 return requestHeaderString.replace("[","").replace("]","");
53 // this method translates http error code to ECOMP Logger Error code
54 // this is a naive translation and is not a result of any documented format ECOMP specification
55 protected EcompLoggerErrorCode convertHttpCodeToErrorCode(int httpResponseCode) {
56 if (isSuccessError(httpResponseCode)) {
57 return EcompLoggerErrorCode.SUCCESS;
60 if (isSchemaError(httpResponseCode)) {
61 return EcompLoggerErrorCode.SCHEMA_ERROR;
63 if (isDataError(httpResponseCode)) {
64 return EcompLoggerErrorCode.DATA_ERROR;
66 if (isPermissionsError(httpResponseCode)) {
67 return EcompLoggerErrorCode.PERMISSION_ERROR;
69 if (isTimeoutOrAvailabilityError(httpResponseCode)) {
70 return EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR;
72 if (isBusinessProcessError(httpResponseCode)) {
73 return EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
75 return EcompLoggerErrorCode.UNKNOWN_ERROR;
78 private boolean isTimeoutOrAvailabilityError(int httpResponseCode) {
80 switch (httpResponseCode) {
81 case HTTP_BAD_REQUEST:
82 case HTTP_UNAUTHORIZED:
84 case HTTP_CLIENT_TIMEOUT:
93 private boolean isPermissionsError(int httpResponseCode) {
95 switch (httpResponseCode) {
96 case HTTP_PAYMENT_REQUIRED:
106 private boolean isDataError(int httpResponseCode) {
108 switch (httpResponseCode) {
109 case HTTP_NOT_ACCEPTABLE:
110 case HTTP_LENGTH_REQUIRED:
111 case HTTP_PRECON_FAILED:
112 case HTTP_REQ_TOO_LONG:
113 case HTTP_ENTITY_TOO_LARGE:
114 case HTTP_UNSUPPORTED_TYPE:
121 private boolean isSchemaError(int httpResponseCode) {
122 return HTTP_CONFLICT == httpResponseCode;
125 private boolean isSuccessError(int httpResponseCode) {
126 return httpResponseCode < 399;
129 private boolean isBusinessProcessError(int httpResponseCode) {
130 return httpResponseCode > 499;
133 protected String getPartnerName(String userAgent, String userId, String url) {
135 if (!StringUtils.isEmpty(userId)) {
139 String urlUser = getUserIdFromUrl(url);
141 if (!StringUtils.isEmpty(urlUser))
144 String userAgentName = getUserIdFromUserAgent(userAgent);
146 if (!StringUtils.isEmpty(userAgentName))
147 return userAgentName;
152 private String getUserIdFromUserAgent(String userAgent) {
153 if (userAgent != null && userAgent.length() > 0) {
154 if (userAgent.toLowerCase().contains("firefox")) {
158 if (userAgent.toLowerCase().contains("msie")) {
159 return "explorer_FE";
162 if (userAgent.toLowerCase().contains("chrome")) {
171 private String getUserIdFromUrl(String url) {
172 if (url != null && url.toLowerCase().contains("user")) {
173 StringTokenizer st = new StringTokenizer(url, "/");
174 while (st.hasMoreElements()) {
175 if ("user".equalsIgnoreCase(st.nextToken())) {
176 return st.nextToken();
183 protected String getUrl(ContainerRequestContext requestContext) {
187 if (requestContext.getUriInfo() != null && requestContext.getUriInfo().getRequestUri() != null) {
188 url = requestContext.getUriInfo().getRequestUri().toURL().toString();
190 } catch (Exception ex) {
191 log.error("failed to get url from request context ", ex);
197 protected String getServiceName(ContainerRequestContext requestContext) {
198 return (requestContext.getUriInfo().getRequestUri().toString())
199 .replace(requestContext.getUriInfo().getBaseUri().toString(), "/");