Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / filter / AUTHZServlet.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.filter;
23
24 import java.io.IOException;
25
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;
33
34 /**
35  *
36  * @author Jonathan
37  *
38  */
39 public class AUTHZServlet<S extends Servlet> implements Servlet {
40     private String[] roles;
41     private Servlet delegate;
42
43     protected AUTHZServlet(Class<S> cls) {
44         try {
45             delegate = cls.newInstance();
46         } catch (Exception e) {
47             delegate = null;
48         }
49         RolesAllowed rolesAllowed = cls.getAnnotation(RolesAllowed.class);
50         if (rolesAllowed == null) {
51             roles = null;
52         } else {
53             roles = rolesAllowed.value();
54         }
55     }
56
57     public void init(ServletConfig sc) throws ServletException {
58         if (delegate == null) {
59             throw new ServletException("Invalid Servlet Delegate");
60         }
61         delegate.init(sc);
62     }
63
64     public ServletConfig getServletConfig() {
65         return delegate.getServletConfig();
66     }
67
68     public String getServletInfo() {
69         return delegate.getServletInfo();
70     }
71
72     public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
73         if (roles == null) {
74             delegate.service(req, resp);
75             return;
76         }
77
78         // Validate
79         try {
80             HttpServletRequest hreq = (HttpServletRequest)req;
81             for (String role : roles) {
82                 if (hreq.isUserInRole(role)) {
83                     delegate.service(req, resp);
84                     return;
85                 }
86             }
87
88             ((HttpServletResponse)resp).sendError(403); // forbidden
89         } catch (ClassCastException e) {
90             throw new ServletException("JASPIServlet only supports HTTPServletRequest/HttpServletResponse");
91         }
92     }
93
94     public void destroy() {
95         delegate.destroy();
96     }
97
98 }