90d754ec67f04dbbb84c8d24a5205e3dfa604a88
[aaf/authz.git] / authz-gw / src / main / java / org / onap / aaf / authz / gw / api / API_Proxy.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.authz.gw.api;\r
24 \r
25 import java.net.ConnectException;\r
26 \r
27 import javax.servlet.http.HttpServletRequest;\r
28 import javax.servlet.http.HttpServletResponse;\r
29 \r
30 import org.onap.aaf.authz.env.AuthzTrans;\r
31 import org.onap.aaf.authz.gw.GwAPI;\r
32 import org.onap.aaf.authz.gw.GwCode;\r
33 import org.onap.aaf.authz.gw.facade.GwFacade;\r
34 import org.onap.aaf.authz.gw.mapper.Mapper.API;\r
35 import org.onap.aaf.cssa.rserv.HttpMethods;\r
36 \r
37 import com.att.aft.dme2.internal.jetty.http.HttpStatus;\r
38 import org.onap.aaf.cadi.CadiException;\r
39 import org.onap.aaf.cadi.client.Future;\r
40 import org.onap.aaf.cadi.client.Rcli;\r
41 import org.onap.aaf.cadi.client.Retryable;\r
42 import org.onap.aaf.cadi.config.Config;\r
43 import org.onap.aaf.inno.env.APIException;\r
44 import org.onap.aaf.inno.env.Env;\r
45 import org.onap.aaf.inno.env.TimeTaken;\r
46 \r
47 /**\r
48  * API Apis.. using Redirect for mechanism\r
49  * \r
50  *\r
51  */\r
52 public class API_Proxy {\r
53 \r
54         /**\r
55          * Normal Init level APIs\r
56          * \r
57          * @param gwAPI\r
58          * @param facade\r
59          * @throws Exception\r
60          */\r
61         public static void init(final GwAPI gwAPI, GwFacade facade) throws Exception {\r
62                 \r
63                 String aafurl = gwAPI.env.getProperty(Config.AAF_URL);\r
64                 if(aafurl==null) {\r
65                 } else {\r
66 \r
67                         ////////\r
68                         // Transferring APIs\r
69                         ///////\r
70                         gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy GET", true) {\r
71                                 @Override\r
72                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
73                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
74                                         try {\r
75                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
76                                                         @Override\r
77                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
78                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
79                                                                 ft.get(10000); // Covers return codes and err messages\r
80                                                                 return null;\r
81                                                         }\r
82                                                 });\r
83                                         \r
84                                         } catch (CadiException | APIException e) {\r
85                                                 trans.error().log(e);\r
86                                         } finally {\r
87                                                 tt.done();\r
88                                         }\r
89                                 }\r
90                         });\r
91                         \r
92                         gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy POST", true) {\r
93                                 @Override\r
94                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
95                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
96                                         try {\r
97                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
98                                                         @Override\r
99                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
100                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);\r
101                                                                 ft.get(10000); // Covers return codes and err messages\r
102                                                                 return null;\r
103                                                         }\r
104                                                 });\r
105                                         } catch (CadiException | APIException e) {\r
106                                                 trans.error().log(e);\r
107                                         } finally {\r
108                                                 tt.done();\r
109                                         }\r
110                                 }\r
111                         });\r
112                         \r
113                         gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy PUT", true) {\r
114                                 @Override\r
115                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
116                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
117                                         try {\r
118                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
119                                                         @Override\r
120                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
121                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
122                                                                 ft.get(10000); // Covers return codes and err messages\r
123                                                                 return null;\r
124                                                         }\r
125                                                 });\r
126                                         } catch (CadiException | APIException e) {\r
127                                                 trans.error().log(e);\r
128                                         } finally {\r
129                                                 tt.done();\r
130                                         }\r
131                                 }\r
132                         });\r
133                         \r
134                         gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy DELETE", true) {\r
135                                 @Override\r
136                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
137                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
138                                         try {\r
139                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
140                                                         @Override\r
141                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
142                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
143                                                                 ft.get(10000); // Covers return codes and err messages\r
144                                                                 return null;\r
145                                                         }\r
146                                                 });\r
147                                         } catch (CadiException | APIException e) {\r
148                                                 trans.error().log(e);\r
149                                         } finally {\r
150                                                 tt.done();\r
151                                         }\r
152                                 }\r
153                         });\r
154                 }\r
155         }\r
156 }\r