AT&T 2.0.19 Code drop, stage 3
[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) throws Exception {
63                 
64                 String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null);
65                 if(aafurl==null) {
66                 } else {
67                         ////////
68                         // Transferring APIs
69                         // But DO NOT transfer BasicAuth case... wastes resources.
70                         ///////
71                         final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade);
72                         
73                         gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) {
74                                 @Override
75                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
76                                         if("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) {
77                                                 bac.handle(trans, req, resp);
78                                         } else {
79                                                 TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
80                                                 try {
81                                                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
82                                                                 @Override
83                                                                 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
84                                                                         Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
85                                                                         ft.get(10000); // Covers return codes and err messages
86                                                                         return null;
87                                                                 }
88                                                         });
89                                                 
90                                                 } catch (CadiException | APIException e) {
91                                                         trans.error().log(e);
92                                                 } finally {
93                                                         tt.done();
94                                                 }
95                                         }
96                                 }
97                         });
98                         
99                         gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) {
100                                 @Override
101                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
102                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
103                                         try {
104                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
105                                                         @Override
106                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
107                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);
108                                                                 ft.get(10000); // Covers return codes and err messages
109                                                                 return null;
110                                                         }
111                                                 });
112                                         } catch (CadiException | APIException e) {
113                                                 trans.error().log(e);
114                                         } finally {
115                                                 tt.done();
116                                         }
117                                 }
118                         });
119                         
120                         gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) {
121                                 @Override
122                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
123                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
124                                         try {
125                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
126                                                         @Override
127                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
128                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
129                                                                 ft.get(10000); // Covers return codes and err messages
130                                                                 return null;
131                                                         }
132                                                 });
133                                         } catch (CadiException | APIException e) {
134                                                 trans.error().log(e);
135                                         } finally {
136                                                 tt.done();
137                                         }
138                                 }
139                         });
140                         
141                         gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) {
142                                 @Override
143                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
144                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
145                                         try {
146                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
147                                                         @Override
148                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
149                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
150                                                                 ft.get(10000); // Covers return codes and err messages
151                                                                 return null;
152                                                         }
153                                                 });
154                                         } catch (CadiException | APIException e) {
155                                                 trans.error().log(e);
156                                         } finally {
157                                                 tt.done();
158                                         }
159                                 }
160                         });
161                 }
162         }
163 }