Add Servlet Sample
[aaf/authz.git] / cadi / servlet-sample / src / main / java / org / onap / aaf / sample / cadi / MyServlet.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.sample.cadi;
23
24 import java.io.IOException;
25 import java.security.Principal;
26
27 import javax.servlet.Servlet;
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33
34 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.config.Config;
37 import org.onap.aaf.cadi.principal.TaggedPrincipal;
38
39 // Uncomment if you utilized the "MiniJASPIWrap" in the Servlet setup in "main()", and want to protect your service via Permission or mapped role
40 //    @RolesAllowed({"com.att.aaf.myPerm|myInstance|myAction"})
41     public class MyServlet implements Servlet {
42         private ServletConfig servletConfig;
43     
44         public void init(ServletConfig config) throws ServletException {
45             servletConfig = config;
46         }
47     
48         public ServletConfig getServletConfig() {
49             return servletConfig;
50         }
51     
52         public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
53             HttpServletRequest request;
54             try {
55                 request = (HttpServletRequest)req;
56             } catch (ClassCastException e) {
57                 throw new ServletException("Only serving HTTP today",e);
58             }
59             
60             res.getOutputStream().println("<html><header><title>CSP Servlet Test</title></header><body><h1>You're good to go!</h1><pre>" +
61                     request.getUserPrincipal());
62             
63             String perm = request.getParameter("PERM");
64             if (perm!=null) {
65                 if (request.isUserInRole(perm)) {
66                     if (perm.indexOf('|')<0) { 
67                         res.getOutputStream().println("\nCongrats!, You are in Role " + perm);
68                     } else { 
69                         res.getOutputStream().println("\nCongrats!, You have Permission " + perm);
70                     }
71                 } else {
72                     if (perm.indexOf('|')<0) { 
73                         res.getOutputStream().println("\nSorry, you are NOT in Role " + perm);
74                     } else {
75                         res.getOutputStream().println("\nSorry, you do NOT have Permission " + perm);
76                     }
77                 }
78             }
79             
80             // You can get the working AAFCon from Trans
81             AAFCon<?> aafcon = AAFCon.obtain(req);
82             if (aafcon!=null) {
83                 try {
84                     res.getOutputStream().println("----- Perms JSON from direct call -----");
85                     final Principal up = request.getUserPrincipal();
86                     TaggedPrincipal tp;
87                     if (up instanceof TaggedPrincipal) {
88                         tp = (TaggedPrincipal)up;
89                     } else {
90                         tp = new TaggedPrincipal() {
91                             @Override
92                             public String getName() {
93                                 return up.getName();
94                             }
95
96                             @Override
97                             public String tag() {
98                                 return "Unknown";
99                             }
100                         };
101                     }
102                     // This call will be "as the user calling", but only if permission is set to trust.
103 //                    Future<String> future = aafcon.clientAs(Config.AAF_DEFAULT_VERSION,tp).read("/authz/perms/user/"+request.getUserPrincipal().getName(),"application/Perms+json");
104                     Future<String> future = aafcon.client(Config.AAF_DEFAULT_VERSION).read("/authz/perms/user/"+request.getUserPrincipal().getName(),"application/Perms+json");
105                     if (future.get(4000 /* timeout */)) {
106                         res.getOutputStream().print(future.value);
107                     } else {
108                         System.err.println(future.code() + ", " + future.body());
109                         res.getOutputStream().print(future.code() + ", " + future.body());
110                     }
111                 } catch (Exception e) {
112                     e.printStackTrace();
113                 }
114             } else {
115                 res.getOutputStream().println("No AAFCon instantiated");
116             }
117             res.getOutputStream().print("</pre></body></html>");
118             
119         }
120     
121         public String getServletInfo() {
122             return "MyServlet";
123         }
124     
125         public void destroy() {
126         }
127     }