c77e9a8504f77fb1a755170bba60d70148376f9e
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / api / API_Proxy.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.locate.api;
23
24 import java.net.ConnectException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.locate.AAF_Locate;
32 import org.onap.aaf.auth.locate.BasicAuthCode;
33 import org.onap.aaf.auth.locate.LocateCode;
34 import org.onap.aaf.auth.locate.facade.LocateFacade;
35 import org.onap.aaf.auth.locate.mapper.Mapper.API;
36 import org.onap.aaf.auth.rserv.HttpMethods;
37 import org.onap.aaf.cadi.CadiException;
38 import org.onap.aaf.cadi.client.Future;
39 import org.onap.aaf.cadi.client.Rcli;
40 import org.onap.aaf.cadi.client.Retryable;
41 import org.onap.aaf.cadi.config.Config;
42 import org.onap.aaf.cadi.oauth.OAuth2Principal;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.Env;
45 import org.onap.aaf.misc.env.TimeTaken;
46
47 /**
48  * API Apis.. using Redirect for mechanism
49  *
50  * @author Jonathan
51  *
52  */
53 public class API_Proxy {
54
55     /**
56      * Normal Init level APIs
57      *
58      * @param gwAPI
59      * @param facade
60      * @throws Exception
61      */
62     public static void init(final AAF_Locate gwAPI, LocateFacade facade) {
63
64         String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null);
65         if (aafurl!=null) {
66             ////////
67             // Transferring APIs
68             // But DO NOT transfer BasicAuth case... wastes resources.
69             ///////
70             final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade);
71
72             gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) {
73                 @Override
74                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
75                     if ("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) {
76                         bac.handle(trans, req, resp);
77                     } else {
78                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
79                         try {
80                             gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
81                                 @Override
82                                 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
83                                     Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
84                                     ft.get(10000); // Covers return codes and err messages
85                                     return null;
86                                 }
87                             });
88
89                         } catch (CadiException | APIException e) {
90                             trans.error().log(e);
91                         } finally {
92                             tt.done();
93                         }
94                     }
95                 }
96             });
97
98             gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) {
99                 @Override
100                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
101                     TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
102                     try {
103                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
104                             @Override
105                             public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
106                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);
107                                 ft.get(10000); // Covers return codes and err messages
108                                 return null;
109                             }
110                         });
111                     } catch (CadiException | APIException e) {
112                         trans.error().log(e);
113                     } finally {
114                         tt.done();
115                     }
116                 }
117             });
118
119             gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) {
120                 @Override
121                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
122                     TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
123                     try {
124                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
125                             @Override
126                             public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
127                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
128                                 ft.get(10000); // Covers return codes and err messages
129                                 return null;
130                             }
131                         });
132                     } catch (CadiException | APIException e) {
133                         trans.error().log(e);
134                     } finally {
135                         tt.done();
136                     }
137                 }
138             });
139
140             gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) {
141                 @Override
142                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
143                     TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
144                     try {
145                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
146                             @Override
147                             public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
148                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
149                                 ft.get(10000); // Covers return codes and err messages
150                                 return null;
151                             }
152                         });
153                     } catch (CadiException | APIException e) {
154                         trans.error().log(e);
155                     } finally {
156                         tt.done();
157                     }
158                 }
159             });
160         }
161     }
162 }