Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / logging / aop / EPEELFLoggerAspect.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.logging.aop;
39
40 import org.aspectj.lang.ProceedingJoinPoint;
41 import org.aspectj.lang.annotation.Around;
42 import org.aspectj.lang.annotation.Aspect;
43 import org.aspectj.lang.annotation.Pointcut;
44 import org.openecomp.portalapp.portal.transport.FieldsValidator;
45 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
46 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
47 import org.openecomp.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
48 import org.springframework.beans.factory.annotation.Autowired;
49
50 @Aspect
51 @org.springframework.context.annotation.Configuration
52 public class EPEELFLoggerAspect {
53         
54         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPEELFLoggerAspect.class);
55
56         @Autowired
57         EPEELFLoggerAdvice epAdvice;
58         
59         /*
60          * Point-cut expression to handle all INCOMING_REST_MESSAGES
61          */
62         @Pointcut("execution(public * org.openecomp.portalapp.portal.controller.*.*(..))")
63         public void incomingAuditMessages() {}
64         
65         /*
66          * Handles all INCOMING_REST_MESSAGES from kpiDashboard
67          */
68         @Pointcut("execution(public * org.openecomp.portalapp.kpidash.controller.*.*(..))")
69         public void kpiDashboardAuditMessages() {}
70
71         /*
72          * Point-cut expression to handle all session management INCOMING_REST_MESSAGES
73          */
74         @Pointcut("execution(public * org.openecomp.portalapp.controller.sessionmgt.*.*(..))")
75         public void sessionMgtIncomingAuditMessages() {}
76         
77         /*
78          * Point-cut expression to handle UserProfileController INCOMING_REST_MESSAGES
79          */
80         @Pointcut("execution(public * org.openecomp.portalapp.controller.core.UserProfileController.*(..))")
81         public void userProfileIncomingAuditMessages() {}
82         
83         /*
84          * Point-cut expression to handle UserProfileController INCOMING_REST_MESSAGES
85          */
86         @Pointcut("execution(public * org.openecomp.portalapp.controller.ONAPWelcomeController.*(..))")
87         public void welcomeIncomingAuditMessages() {}
88         
89         /*
90          * Point-cut expression to handle INCOMING Logout Rest Messages
91          */
92         @Pointcut("execution(public * org.openecomp.portalapp.controller.ECOMPLogoutController.*(..))")
93         public void logoutAuditMessages() {}
94
95         
96         /*
97          * Point-cut expression which handles all the OUTGOING_REST_MESSAGES.
98          */
99         @Pointcut("execution(public * org.openecomp.portalapp.portal.service.ApplicationsRestClientServiceImpl.*(..))")
100         public void outgoingAuditMessages() {}
101         
102         /*
103          * Point-cut expression to handle all the session management OUTGOING_REST_MESSAGES.
104          */
105         @Pointcut("execution(public * org.openecomp.portalapp.service.sessionmgt.SessionCommunication.*(..))")
106         public void sessionMgtOutgoingAuditMessages() {}
107         
108         /*
109          * Point-cut expression which handles all the LDAP_PHONEBOOK_USER_SEARCH calls.
110          */
111         @Pointcut("execution(public * org.openecomp.portalapp.portal.service.EPLdapServiceImpl.*(..))")
112         public void phoneBookSearchAuditMessages() {}
113         
114         /*
115          * Handles Audit, Metrics & Debug logging for the point-cut
116          * expression defined at class-level
117          */
118         @Around("(incomingAuditMessages() || kpiDashboardAuditMessages() || sessionMgtIncomingAuditMessages() || "
119                         + "userProfileIncomingAuditMessages() || welcomeIncomingAuditMessages()) && @within(epAuditLog)")
120         public Object incomingAuditMessagesAroundClass(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
121                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
122         }
123         
124         /*
125          * Handles Audit, Metrics & Debug logging for the point-cut
126          * expression defined at class-level
127          */
128         @Around("(outgoingAuditMessages() || sessionMgtOutgoingAuditMessages()) && @within(epAuditLog)")
129         public Object outgoingAuditMessagesAroundClass(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
130                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE);
131         }
132         
133         
134         /*
135          * Handles Audit, Metrics & Debug logging for the point-cut
136          * expression defined at method-level
137          */
138         @Around("(outgoingAuditMessages() || sessionMgtOutgoingAuditMessages()) && @annotation(epAuditLog)")
139         public Object outgoingAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
140                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE);
141         }
142         
143         /*
144          * Handles Audit, Metrics & Debug logging for the point-cut
145          * expression defined at method-level
146          */
147         @Around("(incomingAuditMessages() || kpiDashboardAuditMessages() || sessionMgtIncomingAuditMessages() ||"
148                         + "userProfileIncomingAuditMessages() || welcomeIncomingAuditMessages()) && @annotation(epAuditLog)")
149         public Object incomingAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
150                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
151         }
152         
153         @Around("@annotation(epAuditLog)")
154         public Object loginAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
155                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.FE_LOGIN_ATTEMPT);
156         }
157         
158         @Around("logoutAuditMessages() && @annotation(epAuditLog)")
159         public Object logoutAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
160                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.FE_LOGOUT);
161         }
162         
163         @Around("phoneBookSearchAuditMessages() && @annotation(epAuditLog)")
164         public Object phonebookSearchAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
165                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.LDAP_PHONEBOOK_USER_SEARCH);
166         }
167         
168         private Object logAroundMethod(ProceedingJoinPoint joinPoint, SecurityEventTypeEnum securityEventType) throws Throwable {
169                 //Before
170                 Object[] passOnArgs = new Object[] {joinPoint.getSignature().getDeclaringType().getName(), joinPoint.getSignature().getName()};
171                 Object[] returnArgs = epAdvice.before(securityEventType, joinPoint.getArgs(), passOnArgs);
172                 
173                 //Call the actual method
174                 Object result = null;
175                 String statusCode = "COMPLETE";
176                 String responseCode = "200";
177                 try {
178                         result = joinPoint.proceed();
179                 } catch(Exception e) {
180                         logger.error(EELFLoggerDelegate.errorLogger, "logAroundMethod failed", e);
181                         statusCode = "ERROR";
182                         responseCode = "500"; //Internal server error
183                 }
184                 
185                 //Check the result
186                 if (securityEventType!=null) {
187                         if (result==null) {
188                                 statusCode = "ERROR";
189                                 //Check if there is an internal response code
190                                 //and use it if the caller function has configured it.
191                                 responseCode = epAdvice.getInternalResponseCode();
192                                 if (responseCode==null||responseCode=="") {
193                                         responseCode = "500"; //Internal server error
194                                 }
195                         } else if (result instanceof FieldsValidator) {
196                                 FieldsValidator fieldsValidator = (FieldsValidator) result;
197                                 if (fieldsValidator!=null && fieldsValidator.httpStatusCode!=null) {
198                                         responseCode = fieldsValidator.httpStatusCode.toString();
199                                 }
200                         }
201                 }
202                 
203                 //After
204                 epAdvice.after(securityEventType, statusCode, responseCode, joinPoint.getArgs(), returnArgs, passOnArgs);
205                 
206                 return result;
207         }
208         
209         //Metrics Logging
210         @Pointcut("execution(* *(..))")
211     public void performMetricsLogging() {}
212         
213         @Around("performMetricsLogging() && @within(epMetricsLog)")
214         public Object metricsLoggingAroundClass(ProceedingJoinPoint joinPoint, EPMetricsLog epMetricsLog) throws Throwable {
215                 return this.logAroundMethod(joinPoint, null);
216         }
217         
218         @Around("performMetricsLogging() && @annotation(epMetricsLog)")
219         public Object metricsLoggingAroundMethod(ProceedingJoinPoint joinPoint, EPMetricsLog epMetricsLog) throws Throwable {
220                 return this.logAroundMethod(joinPoint, null);
221         }
222 }