715d0961e64987b96711dc0650647c8b9a69ed85
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.activityspec.api.server.filters;
18
19 import java.io.IOException;
20 import javax.servlet.Filter;
21 import javax.servlet.FilterChain;
22 import javax.servlet.FilterConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.ServletRequest;
25 import javax.servlet.ServletResponse;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.core.HttpHeaders;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response.Status;
31 import org.apache.commons.lang.StringUtils;
32 import org.openecomp.sdc.common.session.SessionContextProvider;
33 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
34
35 import static org.openecomp.activityspec.utils.ActivitySpecConstant.TENANT;
36 import static org.openecomp.activityspec.utils.ActivitySpecConstant.USER;
37 import static org.openecomp.activityspec.utils.ActivitySpecConstant.USER_ID_HEADER_PARAM;
38
39 public class ActivitySpecSessionContextFilter implements Filter {
40
41   private static final String MESSAGE_USER_MAY_NOT_BE_NULL = "{\"message\": \"User ID can not be null\"}";
42
43   @Override
44   public void init(FilterConfig filterConfig) {
45     //No ActivitySpec specific initialization required
46   }
47
48   @Override
49   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
50       FilterChain filterChain) throws IOException, ServletException {
51
52     final String userHeader = ((HttpServletRequest) servletRequest).getHeader(USER_ID_HEADER_PARAM);
53
54     // Not a real security, just make sure the request
55     // has passed some authentication gateway
56     if (StringUtils.isEmpty(userHeader)) {
57       sendErrorResponse(servletResponse);
58       return;
59     }
60
61     SessionContextProvider contextProvider = SessionContextProviderFactory.getInstance().createInterface();
62
63     try {
64       // use the system-wide user and tenant
65       contextProvider.create(USER, TENANT);
66       filterChain.doFilter(servletRequest, servletResponse);
67     } finally {
68       contextProvider.close();
69     }
70   }
71
72   private void sendErrorResponse(ServletResponse servletResponse) throws IOException {
73     HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
74     httpServletResponse.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
75     httpServletResponse.setStatus(Status.UNAUTHORIZED.getStatusCode());
76     servletResponse.getOutputStream().write(MESSAGE_USER_MAY_NOT_BE_NULL.getBytes());
77   }
78
79   @Override
80   public void destroy() {
81     //No ActivitySpec specific destroy required
82   }
83 }