97727abddb95a3467b726d8ca1ced48f8c0a6030
[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 java.io.BufferedReader;
25 import java.io.InputStreamReader;
26
27 import javax.servlet.ServletOutputStream;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.hello.AAF_Hello.API;
33 import org.onap.aaf.auth.rserv.HttpCode;
34 import org.onap.aaf.auth.rserv.HttpMethods;
35 import org.onap.aaf.misc.env.Env;
36 import org.onap.aaf.misc.env.TimeTaken;
37
38 /**
39  * API Apis
40  * @author Jonathan
41  *
42  */
43 public class API_Hello {
44
45
46         private static final String APPLICATION_JSON = "application/json";
47         protected static final byte[] NOT_JSON = "Data does not look like JSON".getBytes();
48
49         // Hide Public Constructor
50         private API_Hello() {}
51         
52         /**
53          * Normal Init level APIs
54          * 
55          * @param oauthHello
56          * @param facade
57          * @throws Exception
58          */
59         public static void init(final AAF_Hello oauthHello) throws Exception {
60                 ////////
61                 // Simple "GET" API
62                 ///////
63                 
64                 oauthHello.route(HttpMethods.GET,"/hello/:perm*",API.TOKEN,new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"Hello OAuth"){
65                         @Override
66                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
67                                 resp.setStatus(200 /* OK */);
68                                 ServletOutputStream os = resp.getOutputStream();
69                                 os.print("Hello AAF ");
70                                 String perm = pathParam(req, "perm");
71                                 if(perm!=null && perm.length()>0) {
72                                         os.print('(');
73                                         os.print(req.getUserPrincipal().getName());
74                                         TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
75                                         try {
76                                                 if(req.isUserInRole(perm)) {
77                                                         os.print(" has ");
78                                                 } else {
79                                                         os.print(" does not have ");
80                                                 }
81                                         } finally {
82                                                 tt.done();
83                                         }
84                                         os.print("Permission: ");
85                                         os.print(perm);
86                                         os.print(')');
87                                 }
88                                 os.println();
89                                 
90                                 trans.info().printf("Said 'Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
91                         }
92                 }); 
93
94 ////////////////
95 // REST APIs
96 ////////////////
97
98                 ////////////////
99                 // CREATE/POST
100                 ////////////////
101                 oauthHello.route(oauthHello.env,HttpMethods.POST,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Create") {
102                         @Override
103                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
104                                 BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
105                                 StringBuilder sb = new StringBuilder();
106                                 while(br.ready()) {
107                                         sb.append(br.readLine());
108                                 }
109                                 String content = sb.toString();
110                                 trans.info().printf("Content from %s: %s\n", pathParam(req, ":id"),content);
111                                 if(content.startsWith("{") && content.endsWith("}")) {
112                                         resp.setStatus(201 /* OK */);
113                                 } else {
114                                         resp.getOutputStream().write(NOT_JSON);
115                                         resp.setStatus(406);
116                                 }
117                         }
118                 },APPLICATION_JSON); 
119
120
121                 ////////////////
122                 // READ/GET
123                 ////////////////
124                 oauthHello.route(oauthHello.env,HttpMethods.GET,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Read") {
125                         @Override
126                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
127                                 resp.setStatus(200 /* OK */);
128                                 StringBuilder sb = new StringBuilder("{\"resp\": \"Hello REST AAF\",\"principal\": \"");
129                                 sb.append(req.getUserPrincipal().getName());
130                                 sb.append('"');
131                                 String perm = pathParam(req, "perm");
132                                 trans.info().printf("Read request from %s: %s\n", pathParam(req, ":id"),perm);
133                                 if(perm!=null && perm.length()>0) {
134                                         TimeTaken tt = trans.start("Authorize perm", Env.REMOTE);
135                                         try {
136                                                 sb.append(",\"validation\": { \"permission\" : \"");
137                                                 sb.append(perm);
138                                                 sb.append("\",\"has\" : \"");
139                                                 sb.append(req.isUserInRole(perm));
140                                                 sb.append("\"}");
141                                         } finally {
142                                                 tt.done();
143                                         }
144                                 }
145                                 sb.append("}");
146                                 ServletOutputStream os = resp.getOutputStream();
147                                 os.println(sb.toString());
148                                 trans.info().printf("Said 'RESTful Hello' to %s, Authentication type: %s",trans.getUserPrincipal().getName(),trans.getUserPrincipal().getClass().getSimpleName());
149                         }
150                 },APPLICATION_JSON); 
151                 
152                 ////////////////
153                 // UPDATE/PUT
154                 ////////////////
155                 oauthHello.route(oauthHello.env,HttpMethods.PUT,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Update") {
156                         @Override
157                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
158                                 BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
159                                 StringBuilder sb = new StringBuilder();
160                                 while(br.ready()) {
161                                         sb.append(br.readLine());
162                                 }
163                                 String content = sb.toString();
164                                 trans.info().printf("Content from %s: %s\n", pathParam(req, ":id"),content);
165                                 if(content.startsWith("{") && content.endsWith("}")) {
166                                         resp.setStatus(200 /* OK */);
167                                         resp.getOutputStream().print(content);
168                                 } else {
169                                         resp.getOutputStream().write(NOT_JSON);
170                                         resp.setStatus(406);
171                                 }
172                         }
173                 },APPLICATION_JSON); 
174
175
176                 ////////////////
177                 // DELETE
178                 ////////////////
179                 oauthHello.route(oauthHello.env,HttpMethods.DELETE,"/resthello/:id",new HttpCode<AuthzTrans, AAF_Hello>(oauthHello,"REST Hello Delete") {
180                         @Override
181                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
182                                 trans.info().printf("Delete requested on %s\n", pathParam(req, ":id"));
183                                 resp.setStatus(200 /* OK */);
184                         }
185                 },APPLICATION_JSON); 
186
187         }
188 }