AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / api / API_Delegate.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.service.api;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25 import static org.onap.aaf.auth.rserv.HttpMethods.DELETE;
26 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
27 import static org.onap.aaf.auth.rserv.HttpMethods.POST;
28 import static org.onap.aaf.auth.rserv.HttpMethods.PUT;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.eclipse.jetty.http.HttpStatus;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.layer.Result;
36 import org.onap.aaf.auth.service.AAF_Service;
37 import org.onap.aaf.auth.service.Code;
38 import org.onap.aaf.auth.service.facade.AuthzFacade;
39 import org.onap.aaf.auth.service.mapper.Mapper.API;
40
41 public class API_Delegate {
42         public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
43                 /**
44                  * Add a delegate
45                  */
46                 authzAPI.route(POST, "/authz/delegate",API.DELG_REQ,new Code(facade,"Add a Delegate", true) {
47
48                         @Override
49                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
50                                 Result<Void> r = context.createDelegate(trans, req, resp);
51                                 switch(r.status) {
52                                         case OK:
53                                                 resp.setStatus(HttpStatus.CREATED_201); 
54                                                 break;
55                                         default:
56                                                 context.error(trans,resp,r);
57                                 }                               
58                         }                       
59                 });
60                 
61                 /**
62                  * Update a delegate
63                  */
64                 authzAPI.route(PUT, "/authz/delegate",API.DELG_REQ,new Code(facade,"Update a Delegate", true) {
65
66                         @Override
67                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
68                                 Result<Void> r = context.updateDelegate(trans, req, resp);
69                                 switch(r.status) {
70                                         case OK:
71                                                 resp.setStatus(HttpStatus.OK_200); 
72                                                 break;
73                                         default:
74                                                 context.error(trans,resp,r);
75                                 }                               
76                         }                       
77                 });
78                 
79                 /**
80                  * DELETE delegates for a user
81                  */
82                 authzAPI.route(DELETE, "/authz/delegate",API.DELG_REQ,new Code(facade,"Delete delegates for a user", true) {
83
84                         @Override
85                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
86                                 Result<Void> r = context.deleteDelegate(trans, req, resp);
87                                 switch(r.status) {
88                                         case OK:
89                                                 resp.setStatus(HttpStatus.OK_200); 
90                                                 break;
91                                         default:
92                                                 context.error(trans,resp,r);
93                                 }                               
94                         }                       
95                 });
96                 
97                 /**
98                  * DELETE a delegate
99                  */
100                 authzAPI.route(DELETE, "/authz/delegate/:user_name",API.VOID,new Code(facade,"Delete a Delegate", true) {
101
102                         @Override
103                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
104                                 Result<Void> r = context.deleteDelegate(trans, pathParam(req, "user_name"));
105                                 switch(r.status) {
106                                         case OK:
107                                                 resp.setStatus(HttpStatus.OK_200); 
108                                                 break;
109                                         default:
110                                                 context.error(trans,resp,r);
111                                 }                               
112                         }                       
113                 });
114                 
115                 /**
116                  * Read who is delegating for User
117                  */
118                 authzAPI.route(GET, "/authz/delegates/user/:user",API.DELGS,new Code(facade,"Get Delegates by User", true) {
119
120                         @Override
121                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
122                                 Result<Void> r = context.getDelegatesByUser(trans, pathParam(req, "user"), resp);
123                                 switch(r.status) {
124                                         case OK:
125                                                 resp.setStatus(HttpStatus.OK_200); 
126                                                 break;
127                                         default:
128                                                 context.error(trans,resp,r);
129                                 }                               
130                         }                       
131                 });
132
133                 /**
134                  * Read for whom the User is delegating
135                  */
136                 authzAPI.route(GET, "/authz/delegates/delegate/:delegate",API.DELGS,new Code(facade,"Get Delegates by Delegate", true) {
137
138                         @Override
139                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
140                                 Result<Void> r = context.getDelegatesByDelegate(trans, pathParam(req, "delegate"), resp);
141                                 switch(r.status) {
142                                         case OK:
143                                                 resp.setStatus(HttpStatus.OK_200); 
144                                                 break;
145                                         default:
146                                                 context.error(trans,resp,r);
147                                 }                               
148                         }                       
149                 });
150
151         }
152 }