Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / AuditLogController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import javax.servlet.http.HttpServletRequest;
41
42 import org.slf4j.MDC;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
48
49 import org.openecomp.portalsdk.core.domain.AuditLog;
50 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.openecomp.portalsdk.core.service.AuditService;
52 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
53 import org.openecomp.portalapp.portal.domain.EPUser;
54 import org.openecomp.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
55 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
56 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
57 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
58 import org.openecomp.portalapp.portal.utils.PortalConstants;
59 import org.openecomp.portalapp.util.EPUserUtils;
60
61 @RestController
62 @RequestMapping("/portalApi/auditLog")
63 public class AuditLogController extends EPRestrictedBaseController {
64         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardController.class);
65
66         @Autowired
67         private AuditService auditService;
68
69         /**
70          * Store audit log of the specified access type.
71          * 
72          * @param request
73          *            HttpServletRequest
74          * @param affectedAppId
75          *            App ID
76          * @param type
77          *            Access type
78          * @param comment
79          *            Comment
80          */
81         @RequestMapping(value = "/store", method = RequestMethod.GET, produces = "application/json")
82         public void auditLog(HttpServletRequest request, @RequestParam String affectedAppId, @RequestParam String type,
83                         @RequestParam String comment) {
84                 logger.debug(EELFLoggerDelegate.debugLogger, "auditLog: appId {}, type {], comment {}", affectedAppId, type,
85                                 comment);
86                 String cd_type = null;
87                 try {
88                         EPUser user = EPUserUtils.getUserSession(request);
89                         /* Check type of Activity CD */
90                         if (type.equals("app")) {
91                                 cd_type = AuditLog.CD_ACTIVITY_APP_ACCESS;
92                         } else if (type.equals("tab")) {
93                                 cd_type = AuditLog.CD_ACTIVITY_TAB_ACCESS;
94                         } else if (type.equals("functional")) {
95                                 cd_type = AuditLog.CD_ACTIVITY_FUNCTIONAL_ACCESS;
96                         } else if (type.equals("leftMenu")) {
97                                 cd_type = AuditLog.CD_ACTIVITY_LEFT_MENU_ACCESS;
98                         } else {
99                                 logger.error(EELFLoggerDelegate.errorLogger,
100                                                 "Storing auditLog failed! Activity CD type is not correct.");
101                         }
102                         /* Store the audit log only if it contains valid Activity CD */
103                         if (cd_type != null) {
104                                 AuditLog auditLog = new AuditLog();
105                                 auditLog.setActivityCode(cd_type);
106                                 /*
107                                  * Check affectedAppId and comment and see if these two values are valid
108                                  */
109                                 if (comment != null && !comment.equals("") && !comment.equals("undefined"))
110                                         auditLog.setComments(EcompPortalUtils.truncateString(comment, PortalConstants.AUDIT_LOG_COMMENT_SIZE));
111                                 if (affectedAppId != null && !affectedAppId.equals("") && !affectedAppId.equals("undefined"))
112                                         auditLog.setAffectedRecordId(affectedAppId);
113                                 long userId = EPUserUtils.getUserId(request);
114                                 auditLog.setUserId(userId);
115                                 auditService.logActivity(auditLog, null);
116
117                                 // Log file
118                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
119                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
120                                 logger.info(EELFLoggerDelegate.auditLogger, EPLogUtil.formatAuditLogMessage(
121                                                 "AuditLogController.auditLog", cd_type, user.getOrgUserId(), affectedAppId, comment));
122                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
123                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
124                         }
125                 } catch (Exception e) {
126                         logger.error(EELFLoggerDelegate.errorLogger, "auditLog failed", e);
127                 }
128         }
129
130 }