1 package org.openecomp.sdc.common.ecomplogwrapper;
3 import static java.net.HttpURLConnection.HTTP_BAD_METHOD;
4 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
5 import static java.net.HttpURLConnection.HTTP_CLIENT_TIMEOUT;
6 import static java.net.HttpURLConnection.HTTP_CONFLICT;
7 import static java.net.HttpURLConnection.HTTP_ENTITY_TOO_LARGE;
8 import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
9 import static java.net.HttpURLConnection.HTTP_GONE;
10 import static java.net.HttpURLConnection.HTTP_LENGTH_REQUIRED;
11 import static java.net.HttpURLConnection.HTTP_NOT_ACCEPTABLE;
12 import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
13 import static java.net.HttpURLConnection.HTTP_PAYMENT_REQUIRED;
14 import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
15 import static java.net.HttpURLConnection.HTTP_PROXY_AUTH;
16 import static java.net.HttpURLConnection.HTTP_REQ_TOO_LONG;
17 import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
18 import static java.net.HttpURLConnection.HTTP_UNSUPPORTED_TYPE;
20 import java.util.List;
21 import java.util.StringTokenizer;
23 import javax.ws.rs.container.ContainerRequestContext;
25 import org.openecomp.sdc.common.ecomplog.Enums.EcompLoggerErrorCode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Created by dd4296 on 12/20/2017.
32 * base class for metric and audit ecomplog logging
33 * holding the specific logic for data extraction
35 public class EcompLoggerSdcUtilBase {
37 protected static Logger log = LoggerFactory.getLogger(EcompLoggerSdcUtilBase.class.getName());
39 String getRequestIDfromHeaders(List<Object> requestHeader) {
40 // this method gets list of type object.
41 // toString method returns the RequestId with brackets.
42 String requestHeaderString = requestHeader.toString();
43 return requestHeaderString.replace("[","").replace("]","");
47 // this method translates http error code to ECOMP Logger Error code
48 // this is a naive translation and is not a result of any documented format ECOMP specification
49 protected EcompLoggerErrorCode convertHttpCodeToErrorCode(int httpResponseCode) {
50 if (isSuccessError(httpResponseCode))
51 return EcompLoggerErrorCode.SUCCESS;
53 if (isSchemaError(httpResponseCode))
54 return EcompLoggerErrorCode.SCHEMA_ERROR;
56 if (isDataError(httpResponseCode))
57 return EcompLoggerErrorCode.DATA_ERROR;
59 if (isPermissionsError(httpResponseCode))
60 return EcompLoggerErrorCode.PERMISSION_ERROR;
62 if (isTimeoutOrAvailabilityError(httpResponseCode))
63 return EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR;
65 if (isBusinessProcessError(httpResponseCode))
66 return EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
69 return EcompLoggerErrorCode.UNKNOWN_ERROR;
72 private boolean isTimeoutOrAvailabilityError(int httpResponseCode) {
74 switch (httpResponseCode) {
75 case HTTP_BAD_REQUEST:
76 case HTTP_UNAUTHORIZED:
78 case HTTP_CLIENT_TIMEOUT:
86 private boolean isPermissionsError(int httpResponseCode) {
88 switch (httpResponseCode) {
89 case HTTP_PAYMENT_REQUIRED:
99 private boolean isDataError(int httpResponseCode) {
101 switch (httpResponseCode) {
102 case HTTP_NOT_ACCEPTABLE:
103 case HTTP_LENGTH_REQUIRED:
104 case HTTP_PRECON_FAILED:
105 case HTTP_REQ_TOO_LONG:
106 case HTTP_ENTITY_TOO_LARGE:
107 case HTTP_UNSUPPORTED_TYPE:
114 private boolean isSchemaError(int httpResponseCode) {
115 return HTTP_CONFLICT == httpResponseCode;
118 private boolean isSuccessError(int httpResponseCode) {
119 return httpResponseCode < 399;
122 private boolean isBusinessProcessError(int httpResponseCode) {
123 return httpResponseCode > 499;
126 protected String getPartnerName(String userAgent, String userId, String url) {
128 if (!isNullOrEmpty(userId)) {
132 String urlUser = getUserIdFromUrl(url);
134 if (!isNullOrEmpty(urlUser))
137 String userAgentName = getUserIdFromUserAgent(userAgent);
139 if (!isNullOrEmpty(userAgentName))
140 return userAgentName;
145 private String getUserIdFromUserAgent(String userAgent) {
146 if (userAgent != null && userAgent.length() > 0) {
147 if (userAgent.toLowerCase().contains("firefox")) {
151 if (userAgent.toLowerCase().contains("msie")) {
152 return "explorer_FE";
155 if (userAgent.toLowerCase().contains("chrome")) {
164 private String getUserIdFromUrl(String url) {
165 if (url != null && url.toLowerCase().contains("user")) {
166 StringTokenizer st = new StringTokenizer(url, "/");
167 while (st.hasMoreElements()) {
168 if ("user".equalsIgnoreCase(st.nextToken())) {
169 return st.nextToken();
176 protected String getUrl(ContainerRequestContext requestContext) {
180 if (requestContext.getUriInfo() != null && requestContext.getUriInfo().getRequestUri() != null) {
181 url = requestContext.getUriInfo().getRequestUri().toURL().toString();
183 } catch (Exception ex) {
184 log.error("failed to get url from request context ", ex);
190 private boolean isNullOrEmpty(String str) {
191 return (str == null || str.isEmpty());
194 protected String getServiceName(ContainerRequestContext requestContext) {
195 return (requestContext.getUriInfo().getRequestUri().toString())
196 .replace(requestContext.getUriInfo().getBaseUri().toString(), "/");