Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / filters / ReqValidationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.be.filters;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
25 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
28 import org.openecomp.sdc.common.api.UserRoleEnum;
29 import org.openecomp.sdc.common.datastructure.UserContext;
30 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 import javax.servlet.Filter;
37 import javax.servlet.FilterChain;
38 import javax.servlet.FilterConfig;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletRequest;
41 import javax.servlet.ServletResponse;
42 import javax.servlet.http.HttpServletResponse;
43 import java.io.IOException;
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.Set;
47
48 @Component("reqValidationFilter")
49 public class ReqValidationFilter implements Filter {
50
51     private static final Logger log = Logger.getLogger(ReqValidationFilter.class);
52     @Autowired
53     public ComponentExceptionMapper componentExceptionMapper;
54
55     @Override
56     public void init(FilterConfig filterConfig){
57
58     }
59
60     @Override
61     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
62         HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
63         try {
64             log.debug("Validating User roles - filter");
65             List<String> validRoles = Arrays.asList(UserRoleEnum.ADMIN.getName(), UserRoleEnum.DESIGNER.getName());
66             UserContext userContext = ThreadLocalsHolder.getUserContext();
67
68             if (userContext != null && CollectionUtils.isNotEmpty(userContext.getUserRoles())) {
69                 Set<String> userRoles = userContext.getUserRoles();
70                 if (!userRoles.stream().anyMatch(role -> validRoles.contains(role))) {
71                     log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, "SDC", "User role is invalid: {}", userRoles);
72                     throw new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
73                 }
74             }
75             filterChain.doFilter(servletRequest, servletResponse);
76         } catch (ComponentException exp) {
77             componentExceptionMapper.writeToResponse(exp, httpResponse);
78         }
79     }
80
81     @Override
82     public void destroy() {
83
84     }
85 }