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.onap.sdc.activityspec.api.server.filters;
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;
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;
32 public class ActivitySpecSessionContextFilter implements Filter {
34 private static final String MESSAGE_USER_MAY_NOT_BE_NULL = "{\"message\": \"User ID can not be null\"}";
37 public void init(FilterConfig filterConfig) {
38 //No ActivitySpec specific initialization required
42 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
43 throws IOException, ServletException {
45 final String userHeader =
46 ((HttpServletRequest) servletRequest).getHeader(ActivitySpecConstant.USER_ID_HEADER_PARAM);
48 // Not a real security, just make sure the request
49 // has passed some authentication gateway
50 if (StringUtils.isEmpty(userHeader)) {
51 sendErrorResponse(servletResponse);
55 SessionContextProvider contextProvider = SessionContextProviderFactory.getInstance().createInterface();
58 // use the system-wide user and tenant
59 contextProvider.create(ActivitySpecConstant.USER, ActivitySpecConstant.TENANT);
60 filterChain.doFilter(servletRequest, servletResponse);
62 contextProvider.close();
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());
74 public void destroy() {
75 //No ActivitySpec specific destroy required