2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.activityspec.api.server.filters;
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;
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;
39 public class ActivitySpecSessionContextFilter implements Filter {
41 private static final String MESSAGE_USER_MAY_NOT_BE_NULL = "{\"message\": \"User ID can not be null\"}";
44 public void init(FilterConfig filterConfig) {
45 //No ActivitySpec specific initialization required
49 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
50 FilterChain filterChain) throws IOException, ServletException {
52 final String userHeader = ((HttpServletRequest) servletRequest).getHeader(USER_ID_HEADER_PARAM);
54 // Not a real security, just make sure the request
55 // has passed some authentication gateway
56 if (StringUtils.isEmpty(userHeader)) {
57 sendErrorResponse(servletResponse);
61 SessionContextProvider contextProvider = SessionContextProviderFactory.getInstance().createInterface();
64 // use the system-wide user and tenant
65 contextProvider.create(USER, TENANT);
66 filterChain.doFilter(servletRequest, servletResponse);
68 contextProvider.close();
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());
80 public void destroy() {
81 //No ActivitySpec specific destroy required