2be162cc55c5af8be9ade7a8ecda0196e53f2d91
[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                 // Simple "GET" API
56                 ///////
57                 
58                 oauthHello.route(HttpMethods.GET,"/hello/:perm*",API.TOKEN,new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"Hello OAuth"){
59                         @Override
60                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
61                                 resp.setStatus(200 /* OK */);
62                                 ServletOutputStream os = resp.getOutputStream();
63                                 os.print("Hello AAF ");
64                                 String perm = pathParam(req, "perm");
65                                 if(perm!=null && perm.length()>0) {
66                                         os.print('(');
67                                         os.print(req.getUserPrincipal().getName());
68                                         TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
69                                         try {
70                                                 if(req.isUserInRole(perm)) {
71                                                         os.print(" has ");
72                                                 } else {
73                                                         os.print(" does not have ");
74                                                 }
75                                         } finally {
76                                                 tt.done();
77                                         }
78                                         os.print("Permission: ");
79                                         os.print(perm);
80                                         os.print(')');
81                                 }
82                                 os.println();
83                                 
84                                 trans.info().printf("Said 'Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
85                         }
86                 }); 
87
88                 ////////
89                 // REST APIs
90                 ///////
91                 oauthHello.route(oauthHello.env,HttpMethods.GET,"/resthello/:perm*",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello OAuth") {
92                         @Override
93                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
94                                 resp.setStatus(200 /* OK */);
95                                 StringBuilder sb = new StringBuilder("{\"resp\": \"Hello REST AAF\",\"principal\": \"");
96                                 sb.append(req.getUserPrincipal().getName());
97                                 sb.append('"');
98                                 String perm = pathParam(req, "perm");
99                                 if(perm!=null && perm.length()>0) {
100                                         TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
101                                         try {
102                                                 sb.append(",\"validation\": { \"permission\" : \"");
103                                                 sb.append(perm);
104                                                 sb.append("\",\"has\" : \"");
105                                                 sb.append(req.isUserInRole(perm));
106                                                 sb.append("\"}");
107                                         } finally {
108                                                 tt.done();
109                                         }
110                                 }
111                                 sb.append("}");
112                                 ServletOutputStream os = resp.getOutputStream();
113                                 os.println(sb.toString());
114                                 trans.info().printf("Said 'RESTful Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
115                         }
116                 },"application/json"); 
117                 
118                 
119                 
120         }
121 }