9269eacbfd654f778be21b991a56b27b49365524
[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.onap.sdc.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
32 import org.apache.commons.lang.StringUtils;
33 import org.onap.sdc.activityspec.utils.ActivitySpecConstant;
34 import org.openecomp.sdc.common.session.SessionContextProvider;
35 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
36
37 public class ActivitySpecSessionContextFilter implements Filter {
38
39     private static final String MESSAGE_USER_MAY_NOT_BE_NULL = "{\"message\": \"User ID can not be null\"}";
40
41     @Override
42     public void init(FilterConfig filterConfig) {
43         //No ActivitySpec specific initialization required
44     }
45
46     @Override
47     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
48             throws IOException, ServletException {
49
50         final String userHeader =
51                 ((HttpServletRequest) servletRequest).getHeader(ActivitySpecConstant.USER_ID_HEADER_PARAM);
52
53         // Not a real security, just make sure the request
54         // has passed some authentication gateway
55         if (StringUtils.isEmpty(userHeader)) {
56             sendErrorResponse(servletResponse);
57             return;
58         }
59
60         SessionContextProvider contextProvider = SessionContextProviderFactory.getInstance().createInterface();
61
62         try {
63             // use the system-wide user and tenant
64             contextProvider.create(ActivitySpecConstant.USER, ActivitySpecConstant.TENANT);
65             filterChain.doFilter(servletRequest, servletResponse);
66         } finally {
67             contextProvider.close();
68         }
69     }
70
71     private void sendErrorResponse(ServletResponse servletResponse) throws IOException {
72         HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
73         httpServletResponse.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
74         httpServletResponse.setStatus(Status.UNAUTHORIZED.getStatusCode());
75         servletResponse.getOutputStream().write(MESSAGE_USER_MAY_NOT_BE_NULL.getBytes());
76     }
77
78     @Override
79     public void destroy() {
80         //No ActivitySpec specific destroy required
81     }
82 }