2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.filter;
24 import java.io.IOException;
26 import javax.servlet.Servlet;
27 import javax.servlet.ServletConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
39 public class AUTHZServlet<S extends Servlet> implements Servlet {
40 private String[] roles;
41 private Servlet delegate;
43 protected AUTHZServlet(Class<S> cls) {
45 delegate = cls.newInstance();
46 } catch (Exception e) {
49 RolesAllowed rolesAllowed = cls.getAnnotation(RolesAllowed.class);
50 if (rolesAllowed == null) {
53 roles = rolesAllowed.value();
57 public void init(ServletConfig sc) throws ServletException {
58 if (delegate == null) {
59 throw new ServletException("Invalid Servlet Delegate");
64 public ServletConfig getServletConfig() {
65 return delegate.getServletConfig();
68 public String getServletInfo() {
69 return delegate.getServletInfo();
72 public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
74 delegate.service(req, resp);
80 HttpServletRequest hreq = (HttpServletRequest)req;
81 for (String role : roles) {
82 if (hreq.isUserInRole(role)) {
83 delegate.service(req, resp);
88 ((HttpServletResponse)resp).sendError(403); // forbidden
89 } catch (ClassCastException e) {
90 throw new ServletException("JASPIServlet only supports HTTPServletRequest/HttpServletResponse");
94 public void destroy() {