Added oparent to sdc main
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / log / wrappers / LoggerSdcUtilBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.common.log.wrappers;
22
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;
27
28 import javax.ws.rs.container.ContainerRequestContext;
29 import java.util.List;
30 import java.util.StringTokenizer;
31
32 import static java.net.HttpURLConnection.*;
33
34 /**
35  * Created by dd4296 on 12/20/2017.
36  *
37  * base class for metric and audit log logging
38  * holding the specific logic for data extraction
39  */
40 public class LoggerSdcUtilBase {
41
42     protected static Logger log = LoggerFactory.getLogger(LoggerSdcUtilBase.class.getName());
43
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("]","");
49     }
50
51
52
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;
58         }
59
60         if (isSchemaError(httpResponseCode)) {
61             return EcompLoggerErrorCode.SCHEMA_ERROR;
62         }
63         if (isDataError(httpResponseCode)) {
64             return EcompLoggerErrorCode.DATA_ERROR;
65         }
66         if (isPermissionsError(httpResponseCode)) {
67             return EcompLoggerErrorCode.PERMISSION_ERROR;
68         }
69         if (isTimeoutOrAvailabilityError(httpResponseCode)) {
70             return EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR;
71         }
72         if (isBusinessProcessError(httpResponseCode)) {
73             return EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
74         }
75         return EcompLoggerErrorCode.UNKNOWN_ERROR;
76     }
77
78     private boolean isTimeoutOrAvailabilityError(int httpResponseCode) {
79
80         switch (httpResponseCode) {
81             case HTTP_BAD_REQUEST:
82             case HTTP_UNAUTHORIZED:
83             case HTTP_NOT_FOUND:
84             case HTTP_CLIENT_TIMEOUT:
85             case HTTP_GONE:
86                 return true;
87             default:
88                 return false;
89         }
90
91     }
92
93     private boolean isPermissionsError(int httpResponseCode) {
94
95         switch (httpResponseCode) {
96             case HTTP_PAYMENT_REQUIRED:
97             case HTTP_FORBIDDEN:
98             case HTTP_BAD_METHOD:
99             case HTTP_PROXY_AUTH:
100                 return true;
101         }
102
103         return false;
104     }
105
106     private boolean isDataError(int httpResponseCode) {
107
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:
115                 return true;
116         }
117
118         return false;
119     }
120
121     private boolean isSchemaError(int httpResponseCode) {
122         return HTTP_CONFLICT == httpResponseCode;
123     }
124
125     private boolean isSuccessError(int httpResponseCode) {
126         return httpResponseCode < 399;
127     }
128
129     private boolean isBusinessProcessError(int httpResponseCode) {
130         return httpResponseCode > 499;
131     }
132
133     protected String getPartnerName(String userAgent, String userId, String url) {
134
135         if (!StringUtils.isEmpty(userId)) {
136             return userId;
137         }
138
139         String urlUser = getUserIdFromUrl(url);
140
141         if (!StringUtils.isEmpty(urlUser))
142             return urlUser;
143
144         String userAgentName = getUserIdFromUserAgent(userAgent);
145
146         if (!StringUtils.isEmpty(userAgentName))
147             return userAgentName;
148
149         return "";
150     }
151
152     private String getUserIdFromUserAgent(String userAgent) {
153         if (userAgent != null && userAgent.length() > 0) {
154             if (userAgent.toLowerCase().contains("firefox")) {
155                 return "fireFox_FE";
156             }
157
158             if (userAgent.toLowerCase().contains("msie")) {
159                 return "explorer_FE";
160             }
161
162             if (userAgent.toLowerCase().contains("chrome")) {
163                 return "chrome_FE";
164             }
165
166             return userAgent;
167         }
168         return null;
169     }
170
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();
177                 }
178             }
179         }
180         return null;
181     }
182
183     protected String getUrl(ContainerRequestContext requestContext) {
184         String url = "";
185
186         try {
187             if (requestContext.getUriInfo() != null && requestContext.getUriInfo().getRequestUri() != null) {
188                 url = requestContext.getUriInfo().getRequestUri().toURL().toString();
189             }
190         } catch (Exception ex) {
191             log.error("failed to get url from request context ", ex);
192         }
193
194         return url;
195     }
196
197     protected String getServiceName(ContainerRequestContext requestContext) {
198         return (requestContext.getUriInfo().getRequestUri().toString())
199                 .replace(requestContext.getUriInfo().getBaseUri().toString(), "/");
200     }
201 }