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.config.Config;
37 import org.onap.aaf.cadi.principal.TaggedPrincipal;
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;
44 public void init(ServletConfig config) throws ServletException {
45 servletConfig = config;
48 public ServletConfig getServletConfig() {
52 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
53 HttpServletRequest request;
55 request = (HttpServletRequest)req;
56 } catch (ClassCastException e) {
57 throw new ServletException("Only serving HTTP today",e);
60 res.getOutputStream().println("<html><header><title>CSP Servlet Test</title></header><body><h1>You're good to go!</h1><pre>" +
61 request.getUserPrincipal());
63 String perm = request.getParameter("PERM");
65 if (request.isUserInRole(perm)) {
66 if (perm.indexOf('|')<0) {
67 res.getOutputStream().println("\nCongrats!, You are in Role " + perm);
69 res.getOutputStream().println("\nCongrats!, You have Permission " + perm);
72 if (perm.indexOf('|')<0) {
73 res.getOutputStream().println("\nSorry, you are NOT in Role " + perm);
75 res.getOutputStream().println("\nSorry, you do NOT have Permission " + perm);
80 // You can get the working AAFCon from Trans
81 AAFCon<?> aafcon = AAFCon.obtain(req);
84 res.getOutputStream().println("----- Perms JSON from direct call -----");
85 final Principal up = request.getUserPrincipal();
87 if (up instanceof TaggedPrincipal) {
88 tp = (TaggedPrincipal)up;
90 tp = new TaggedPrincipal() {
92 public String getName() {
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);
108 System.err.println(future.code() + ", " + future.body());
109 res.getOutputStream().print(future.code() + ", " + future.body());
111 } catch (Exception e) {
115 res.getOutputStream().println("No AAFCon instantiated");
117 res.getOutputStream().print("</pre></body></html>");
121 public String getServletInfo() {
125 public void destroy() {