re base code
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / impl / Audit.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.fe.impl;
22
23 import org.openecomp.sdc.common.api.Constants;
24 import org.slf4j.Logger;
25
26 import javax.servlet.http.HttpServletRequest;
27 import java.util.Map;
28
29 public class Audit {
30
31         private Audit() {
32         }
33
34         public static void error(Logger log, HttpRequestInfo requestInfo, int status) {
35                 String errorMsg = "Internal Error";
36                 if (requestInfo != null && requestInfo.getHeaders() != null) {
37                         Map<String, String> requestHeaders = requestInfo.getHeaders();
38                         errorMsg = String.format(Constants.ERROR_LOG_FORMAT, requestHeaders.get(Constants.USER_ID_HEADER),
39                                         requestHeaders.get(Constants.FIRST_NAME_HEADER) + " "
40                                                         + requestHeaders.get(Constants.LAST_NAME_HEADER),
41                                         requestHeaders.get(Constants.ORIGIN_HEADER), requestHeaders.get(Constants.ACCESS_HEADER),
42                                         requestInfo.getRequestURL(), status);
43                 }
44                 log.error(errorMsg);
45         }
46
47         public static void error(Logger log, HttpServletRequest request, int status) {
48                 String errorMsg = "Internal Error";
49                 if (request != null) {
50
51                         errorMsg = String.format(Constants.ERROR_LOG_FORMAT, request.getHeader(Constants.USER_ID_HEADER),
52                                         request.getHeader(Constants.FIRST_NAME_HEADER) + " "
53                                                         + request.getHeader(Constants.LAST_NAME_HEADER),
54                                         request.getHeader(Constants.ORIGIN_HEADER), request.getHeader(Constants.ACCESS_HEADER),
55                                         request.getRequestURL(), status);
56                 }
57                 log.error(errorMsg);
58         }
59
60         public static void info(Logger log, HttpRequestInfo requestInfo, int status) {
61                 String errorMsg = "Internal Error";
62                 if (requestInfo != null && requestInfo.getHeaders() != null) {
63                         Map<String, String> requestHeaders = requestInfo.getHeaders();
64                         errorMsg = String.format(Constants.ERROR_LOG_FORMAT, requestHeaders.get(Constants.USER_ID_HEADER),
65                                         requestHeaders.get(Constants.FIRST_NAME_HEADER) + " "
66                                                         + requestHeaders.get(Constants.LAST_NAME_HEADER),
67                                         requestHeaders.get(Constants.ORIGIN_HEADER), requestHeaders.get(Constants.ACCESS_HEADER),
68                                         requestInfo.getRequestURL(), status);
69                 }
70                 log.info(errorMsg);
71         }
72 }