AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-hello / src / main / java / org / onap / aaf / auth / hello / API_Hello.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.auth.hello;
23
24 import javax.servlet.ServletOutputStream;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.onap.aaf.auth.env.AuthzTrans;
29 import org.onap.aaf.auth.hello.AAF_Hello.API;
30 import org.onap.aaf.auth.rserv.HttpCode;
31 import org.onap.aaf.auth.rserv.HttpMethods;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.TimeTaken;
34
35 /**
36  * API Apis
37  * @author Jonathan
38  *
39  */
40 public class API_Hello {
41
42
43         // Hide Public Constructor
44         private API_Hello() {}
45         
46         /**
47          * Normal Init level APIs
48          * 
49          * @param oauthHello
50          * @param facade
51          * @throws Exception
52          */
53         public static void init(final AAF_Hello oauthHello) throws Exception {
54                 ////////
55                 // Overall APIs
56                 ///////
57                 oauthHello.route(HttpMethods.GET,"/hello/:perm*",API.TOKEN,new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"Hello OAuth"){
58                         @Override
59                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
60                                 resp.setStatus(200 /* OK */);
61                                 ServletOutputStream os = resp.getOutputStream();
62                                 os.print("Hello AAF ");
63                                 String perm = pathParam(req, "perm");
64                                 if(perm!=null && perm.length()>0) {
65                                         os.print('(');
66                                         os.print(req.getUserPrincipal().getName());
67                                         TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
68                                         try {
69                                                 if(req.isUserInRole(perm)) {
70                                                         os.print(" has ");
71                                                 } else {
72                                                         os.print(" does not have ");
73                                                 }
74                                         } finally {
75                                                 tt.done();
76                                         }
77                                         os.print("Permission: ");
78                                         os.print(perm);
79                                         os.print(')');
80                                 }
81                                 os.println();
82                                 
83                                 trans.info().printf("Said 'Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
84                         }
85                 }); 
86
87         }
88 }