[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-gw / src / main / java / com / att / 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 com.att.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 com.att.aft.dme2.internal.jetty.http.HttpStatus;\r
31 import com.att.authz.env.AuthzTrans;\r
32 import com.att.authz.gw.GwAPI;\r
33 import com.att.authz.gw.GwCode;\r
34 import com.att.authz.gw.facade.GwFacade;\r
35 import com.att.authz.gw.mapper.Mapper.API;\r
36 import com.att.cadi.CadiException;\r
37 import com.att.cadi.client.Future;\r
38 import com.att.cadi.client.Rcli;\r
39 import com.att.cadi.client.Retryable;\r
40 import com.att.cadi.config.Config;\r
41 import com.att.cssa.rserv.HttpMethods;\r
42 import com.att.inno.env.APIException;\r
43 import com.att.inno.env.Env;\r
44 import com.att.inno.env.TimeTaken;\r
45 \r
46 /**\r
47  * API Apis.. using Redirect for mechanism\r
48  * \r
49  *\r
50  */\r
51 public class API_Proxy {\r
52 \r
53         /**\r
54          * Normal Init level APIs\r
55          * \r
56          * @param gwAPI\r
57          * @param facade\r
58          * @throws Exception\r
59          */\r
60         public static void init(final GwAPI gwAPI, GwFacade facade) throws Exception {\r
61                 \r
62                 String aafurl = gwAPI.env.getProperty(Config.AAF_URL);\r
63                 if(aafurl==null) {\r
64                 } else {\r
65 \r
66                         ////////\r
67                         // Transferring APIs\r
68                         ///////\r
69                         gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy GET", true) {\r
70                                 @Override\r
71                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
72                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
73                                         try {\r
74                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
75                                                         @Override\r
76                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
77                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
78                                                                 ft.get(10000); // Covers return codes and err messages\r
79                                                                 return null;\r
80                                                         }\r
81                                                 });\r
82                                         \r
83                                         } catch (CadiException | APIException e) {\r
84                                                 trans.error().log(e);\r
85                                         } finally {\r
86                                                 tt.done();\r
87                                         }\r
88                                 }\r
89                         });\r
90                         \r
91                         gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy POST", true) {\r
92                                 @Override\r
93                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
94                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
95                                         try {\r
96                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
97                                                         @Override\r
98                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
99                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);\r
100                                                                 ft.get(10000); // Covers return codes and err messages\r
101                                                                 return null;\r
102                                                         }\r
103                                                 });\r
104                                         } catch (CadiException | APIException e) {\r
105                                                 trans.error().log(e);\r
106                                         } finally {\r
107                                                 tt.done();\r
108                                         }\r
109                                 }\r
110                         });\r
111                         \r
112                         gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy PUT", true) {\r
113                                 @Override\r
114                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
115                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
116                                         try {\r
117                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
118                                                         @Override\r
119                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
120                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
121                                                                 ft.get(10000); // Covers return codes and err messages\r
122                                                                 return null;\r
123                                                         }\r
124                                                 });\r
125                                         } catch (CadiException | APIException e) {\r
126                                                 trans.error().log(e);\r
127                                         } finally {\r
128                                                 tt.done();\r
129                                         }\r
130                                 }\r
131                         });\r
132                         \r
133                         gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy DELETE", true) {\r
134                                 @Override\r
135                                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
136                                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
137                                         try {\r
138                                                 gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
139                                                         @Override\r
140                                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
141                                                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
142                                                                 ft.get(10000); // Covers return codes and err messages\r
143                                                                 return null;\r
144                                                         }\r
145                                                 });\r
146                                         } catch (CadiException | APIException e) {\r
147                                                 trans.error().log(e);\r
148                                         } finally {\r
149                                                 tt.done();\r
150                                         }\r
151                                 }\r
152                         });\r
153                 }\r
154         }\r
155 }\r