AT&T 2.0.19 Code drop, stage 2
[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) throw new ServletException("Invalid Servlet Delegate");
59                 delegate.init(sc);
60         }
61         
62         public ServletConfig getServletConfig() {
63                 return delegate.getServletConfig();
64         }
65
66         public String getServletInfo() {
67                 return delegate.getServletInfo();
68         }
69
70         public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
71                 if(roles==null) {
72                         delegate.service(req,resp);
73                 } else { // Validate
74                         try {
75                                 HttpServletRequest hreq = (HttpServletRequest)req;
76                                 boolean proceed = false;
77                                 for(String role : roles) {
78                                         if(hreq.isUserInRole(role)) {
79                                                 proceed = true;
80                                                 break;
81                                         }
82                                 }
83                                 if(proceed) {
84                                         delegate.service(req,resp);
85                                 } else {
86                                         //baseRequest.getServletContext().log(hreq.getUserPrincipal().getName()+" Refused " + roles);
87                                         ((HttpServletResponse)resp).sendError(403); // forbidden
88                                 }
89                         } catch(ClassCastException e) {
90                                 throw new ServletException("JASPIServlet only supports HTTPServletRequest/HttpServletResponse");
91                         }
92                 }
93         }
94
95         public void destroy() {
96                 delegate.destroy();
97         }
98
99
100 }