re base code
[sdc.git] / services / activity-spec / activity-spec-web / activity-spec-service / src / main / java / org / onap / sdc / activityspec / api / server / filters / ActivitySpecSessionContextFilter.java
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 org.apache.commons.lang.StringUtils;
20 import org.onap.sdc.activityspec.utils.ActivitySpecConstant;
21 import org.openecomp.sdc.common.session.SessionContextProvider;
22 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
23
24 import javax.servlet.*;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.ws.rs.core.HttpHeaders;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response.Status;
30 import java.io.IOException;
31
32 public class ActivitySpecSessionContextFilter implements Filter {
33
34     private static final String MESSAGE_USER_MAY_NOT_BE_NULL = "{\"message\": \"User ID can not be null\"}";
35
36     @Override
37     public void init(FilterConfig filterConfig) {
38         //No ActivitySpec specific initialization required
39     }
40
41     @Override
42     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
43             throws IOException, ServletException {
44
45         final String userHeader =
46                 ((HttpServletRequest) servletRequest).getHeader(ActivitySpecConstant.USER_ID_HEADER_PARAM);
47
48         // Not a real security, just make sure the request
49         // has passed some authentication gateway
50         if (StringUtils.isEmpty(userHeader)) {
51             sendErrorResponse(servletResponse);
52             return;
53         }
54
55         SessionContextProvider contextProvider = SessionContextProviderFactory.getInstance().createInterface();
56
57         try {
58             // use the system-wide user and tenant
59             contextProvider.create(ActivitySpecConstant.USER, ActivitySpecConstant.TENANT);
60             filterChain.doFilter(servletRequest, servletResponse);
61         } finally {
62             contextProvider.close();
63         }
64     }
65
66     private void sendErrorResponse(ServletResponse servletResponse) throws IOException {
67         HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
68         httpServletResponse.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
69         httpServletResponse.setStatus(Status.UNAUTHORIZED.getStatusCode());
70         servletResponse.getOutputStream().write(MESSAGE_USER_MAY_NOT_BE_NULL.getBytes());
71     }
72
73     @Override
74     public void destroy() {
75         //No ActivitySpec specific destroy required
76     }
77 }