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.HTTP_BAD_METHOD;
33 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
34 import static java.net.HttpURLConnection.HTTP_CLIENT_TIMEOUT;
35 import static java.net.HttpURLConnection.HTTP_CONFLICT;
36 import static java.net.HttpURLConnection.HTTP_ENTITY_TOO_LARGE;
37 import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
38 import static java.net.HttpURLConnection.HTTP_GONE;
39 import static java.net.HttpURLConnection.HTTP_LENGTH_REQUIRED;
40 import static java.net.HttpURLConnection.HTTP_NOT_ACCEPTABLE;
41 import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
42 import static java.net.HttpURLConnection.HTTP_PAYMENT_REQUIRED;
43 import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
44 import static java.net.HttpURLConnection.HTTP_PROXY_AUTH;
45 import static java.net.HttpURLConnection.HTTP_REQ_TOO_LONG;
46 import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
47 import static java.net.HttpURLConnection.HTTP_UNSUPPORTED_TYPE;
50 * Created by dd4296 on 12/20/2017.
52 * base class for metric and audit log logging
53 * holding the specific logic for data extraction
55 public class LoggerSdcUtilBase {
57 private static final int SUCCESS_ERROR_CODE_LIMIT = 399;
58 private static final int BUSINESS_PROCESS_ERROR_BOUNDRY = 499;
59 protected static Logger log = LoggerFactory.getLogger(LoggerSdcUtilBase.class.getName());
61 String getRequestIDfromHeaders(List<Object> requestHeader) {
62 // this method gets list of type object.
63 // toString method returns the RequestId with brackets.
64 String requestHeaderString = requestHeader.toString();
65 return requestHeaderString.replace("[", "").replace("]", "");
69 // this method translates http error code to ECOMP Logger Error code
70 // this is a naive translation and is not a result of any documented format ECOMP specification
71 protected EcompLoggerErrorCode convertHttpCodeToErrorCode(int httpResponseCode) {
72 if (isSuccessError(httpResponseCode)) {
73 return EcompLoggerErrorCode.SUCCESS;
76 if (isSchemaError(httpResponseCode)) {
77 return EcompLoggerErrorCode.SCHEMA_ERROR;
79 if (isDataError(httpResponseCode)) {
80 return EcompLoggerErrorCode.DATA_ERROR;
82 if (isPermissionsError(httpResponseCode)) {
83 return EcompLoggerErrorCode.PERMISSION_ERROR;
85 if (isTimeoutOrAvailabilityError(httpResponseCode)) {
86 return EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR;
88 if (isBusinessProcessError(httpResponseCode)) {
89 return EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
91 return EcompLoggerErrorCode.UNKNOWN_ERROR;
94 private boolean isTimeoutOrAvailabilityError(int httpResponseCode) {
96 switch (httpResponseCode) {
97 case HTTP_BAD_REQUEST:
98 case HTTP_UNAUTHORIZED:
100 case HTTP_CLIENT_TIMEOUT:
109 private boolean isPermissionsError(int httpResponseCode) {
111 switch (httpResponseCode) {
112 case HTTP_PAYMENT_REQUIRED:
114 case HTTP_BAD_METHOD:
115 case HTTP_PROXY_AUTH:
123 private boolean isDataError(int httpResponseCode) {
125 switch (httpResponseCode) {
126 case HTTP_NOT_ACCEPTABLE:
127 case HTTP_LENGTH_REQUIRED:
128 case HTTP_PRECON_FAILED:
129 case HTTP_REQ_TOO_LONG:
130 case HTTP_ENTITY_TOO_LARGE:
131 case HTTP_UNSUPPORTED_TYPE:
139 private boolean isSchemaError(int httpResponseCode) {
140 return HTTP_CONFLICT == httpResponseCode;
143 private boolean isSuccessError(int httpResponseCode) {
144 return httpResponseCode < SUCCESS_ERROR_CODE_LIMIT;
147 private boolean isBusinessProcessError(int httpResponseCode) {
148 return httpResponseCode > BUSINESS_PROCESS_ERROR_BOUNDRY;
151 protected String getPartnerName(String userAgent, String userId, String url) {
153 if (!StringUtils.isEmpty(userId)) {
157 String urlUser = getUserIdFromUrl(url);
159 if (!StringUtils.isEmpty(urlUser)) {
163 String userAgentName = getUserIdFromUserAgent(userAgent);
165 if (!StringUtils.isEmpty(userAgentName)) {
166 return userAgentName;
172 private String getUserIdFromUserAgent(String userAgent) {
173 if (userAgent != null && userAgent.length() > 0) {
174 if (userAgent.toLowerCase().contains("firefox")) {
178 if (userAgent.toLowerCase().contains("msie")) {
179 return "explorer_FE";
182 if (userAgent.toLowerCase().contains("chrome")) {
191 private String getUserIdFromUrl(String url) {
192 if (url != null && url.toLowerCase().contains("user")) {
193 StringTokenizer st = new StringTokenizer(url, "/");
194 while (st.hasMoreElements()) {
195 if ("user".equalsIgnoreCase(st.nextToken())) {
196 return st.nextToken();
203 protected String getUrl(ContainerRequestContext requestContext) {
207 if (requestContext.getUriInfo() != null && requestContext.getUriInfo().getRequestUri() != null) {
208 url = requestContext.getUriInfo().getRequestUri().toURL().toString();
210 } catch (Exception ex) {
211 log.error("failed to get url from request context ", ex);
217 protected String getServiceName(ContainerRequestContext requestContext) {
218 return (requestContext.getUriInfo().getRequestUri().toString())
219 .replace(requestContext.getUriInfo().getBaseUri().toString(), "/");