nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / interceptor / PortalResourceInterceptor.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.interceptor;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.openecomp.portalapp.controller.sessionmgt.SessionCommunicationController;
26 import org.openecomp.portalapp.portal.controller.ExternalAppsRestfulController;
27 import org.openecomp.portalapp.portal.controller.SharedContextRestController;
28 import org.openecomp.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
29 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
30 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
31 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
32 import org.openecomp.portalapp.service.sessionmgt.ManageService;
33 import org.openecomp.portalapp.service.sessionmgt.RemoteWebServiceCallService;
34 import org.openecomp.portalsdk.core.exception.UrlAccessRestrictedException;
35 import org.openecomp.portalsdk.core.interceptor.ResourceInterceptor;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalTimeoutHandler;
38 import org.openecomp.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.web.method.HandlerMethod;
41
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 public class PortalResourceInterceptor extends ResourceInterceptor {
45         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalResourceInterceptor.class);
46
47         @Autowired
48         public RemoteWebServiceCallService remoteWebServiceCallService;
49
50         @Autowired
51         public ManageService manageService;
52         
53         @Autowired
54         private EPEELFLoggerAdvice epAdvice;
55
56         static ObjectMapper mapper = new ObjectMapper();
57
58         @Override
59         public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
60                         throws Exception {
61
62                 if (handler instanceof HandlerMethod) {
63                         HandlerMethod method = (HandlerMethod) handler;
64                         Object controllerObj = method.getBean();
65                         /**
66                          * These classes provide REST endpoints used by other application
67                          * servers, NOT by an end user's browser.
68                          */
69                         if (controllerObj instanceof SessionCommunicationController
70                                         || controllerObj instanceof SharedContextRestController
71                                         || controllerObj instanceof ExternalAppsRestfulController) {
72                                 // check user authentication for RESTful calls
73                                 String secretKey = null;
74                                 try {
75                                         epAdvice.loadServletRequestBasedDefaults(request, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
76                                         if (!remoteWebServiceCallService.verifyRESTCredential(secretKey, request.getHeader("uebkey"),
77                                                         request.getHeader("username"), request.getHeader("password"))) {
78                                                 throw new UrlAccessRestrictedException();
79                                         }
80                                 } catch (Exception e) {
81                                         logger.error(EELFLoggerDelegate.errorLogger, "Error authenticating RESTful service. Details: " + EcompPortalUtils.getStackTrace(e));
82                                         EPLogUtil.logEcompError(EPAppMessagesEnum.BeRestApiAuthenticationError);
83                                         throw new UrlAccessRestrictedException();
84                                 }
85                         }
86                 }
87
88                 handleSessionUpdates(request);
89                 return true;
90         }
91
92         protected void handleSessionUpdates(HttpServletRequest request) {
93                 PortalTimeoutHandler.handleSessionUpdatesNative(request, null, null, null, null, manageService);
94         }
95 }