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.sample.cadi;
24 import java.io.IOException;
25 import java.security.Principal;
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;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.principal.TaggedPrincipal;
38 // Uncomment if you utilized the "MiniJASPIWrap" in the Servlet setup in "main()", and want to protect your service via Permission or mapped role
39 // @RolesAllowed({"com.att.aaf.myPerm|myInstance|myAction"})
40 public class MyServlet implements Servlet {
41 private ServletConfig servletConfig;
43 public void init(ServletConfig config) throws ServletException {
44 servletConfig = config;
47 public ServletConfig getServletConfig() {
51 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
52 HttpServletRequest request;
54 request = (HttpServletRequest)req;
55 } catch (ClassCastException e) {
56 throw new ServletException("Only serving HTTP today",e);
59 res.getOutputStream().println("<html><header><title>CSP Servlet Test</title></header><body><h1>You're good to go!</h1><pre>" +
60 request.getUserPrincipal());
62 String perm = request.getParameter("PERM");
64 if (request.isUserInRole(perm)) {
65 if (perm.indexOf('|')<0) {
66 res.getOutputStream().println("\nCongrats!, You are in Role " + perm);
68 res.getOutputStream().println("\nCongrats!, You have Permission " + perm);
71 if (perm.indexOf('|')<0) {
72 res.getOutputStream().println("\nSorry, you are NOT in Role " + perm);
74 res.getOutputStream().println("\nSorry, you do NOT have Permission " + perm);
79 // You can get the working AAFCon from Trans
80 AAFCon<?> aafcon = AAFCon.obtain(req);
83 res.getOutputStream().println("----- Perms JSON from direct call -----");
84 final Principal up = request.getUserPrincipal();
86 if (up instanceof TaggedPrincipal) {
87 tp = (TaggedPrincipal)up;
89 tp = new TaggedPrincipal() {
91 public String getName() {
101 // This call will be "as the user calling", but only if permission is set to trust.
102 // Future<String> future = aafcon.clientAs("2.0",tp).read("/authz/perms/user/"+request.getUserPrincipal().getName(),"application/Perms+json");
103 Future<String> future = aafcon.client("2.0").read("/authz/perms/user/"+request.getUserPrincipal().getName(),"application/Perms+json");
104 if (future.get(4000 /* timeout */)) {
105 res.getOutputStream().print(future.value);
107 System.err.println(future.code() + ", " + future.body());
108 res.getOutputStream().print(future.code() + ", " + future.body());
110 } catch (Exception e) {
114 res.getOutputStream().println("No AAFCon instantiated");
116 res.getOutputStream().print("</pre></body></html>");
120 public String getServletInfo() {
124 public void destroy() {