[AAF-21] Initial code import
[aaf/authz.git] / authz-gw / src / main / java / com / att / authz / gw / api / API_Proxy.java
diff --git a/authz-gw/src/main/java/com/att/authz/gw/api/API_Proxy.java b/authz-gw/src/main/java/com/att/authz/gw/api/API_Proxy.java
new file mode 100644 (file)
index 0000000..14d8c41
--- /dev/null
@@ -0,0 +1,156 @@
+/*******************************************************************************\r
+ * ============LICENSE_START====================================================\r
+ * * org.onap.aai\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * Copyright © 2017 Amdocs\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package com.att.authz.gw.api;\r
+\r
+import java.net.ConnectException;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import com.att.aft.dme2.internal.jetty.http.HttpStatus;\r
+import com.att.authz.env.AuthzTrans;\r
+import com.att.authz.gw.GwAPI;\r
+import com.att.authz.gw.GwCode;\r
+import com.att.authz.gw.facade.GwFacade;\r
+import com.att.authz.gw.mapper.Mapper.API;\r
+import com.att.cadi.CadiException;\r
+import com.att.cadi.client.Future;\r
+import com.att.cadi.client.Rcli;\r
+import com.att.cadi.client.Retryable;\r
+import com.att.cadi.config.Config;\r
+import com.att.cssa.rserv.HttpMethods;\r
+import com.att.inno.env.APIException;\r
+import com.att.inno.env.Env;\r
+import com.att.inno.env.TimeTaken;\r
+\r
+/**\r
+ * API Apis.. using Redirect for mechanism\r
+ * \r
+ *\r
+ */\r
+public class API_Proxy {\r
+\r
+       /**\r
+        * Normal Init level APIs\r
+        * \r
+        * @param gwAPI\r
+        * @param facade\r
+        * @throws Exception\r
+        */\r
+       public static void init(final GwAPI gwAPI, GwFacade facade) throws Exception {\r
+               \r
+               String aafurl = gwAPI.env.getProperty(Config.AAF_URL);\r
+               if(aafurl==null) {\r
+               } else {\r
+\r
+                       ////////\r
+                       // Transferring APIs\r
+                       ///////\r
+                       gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy GET", true) {\r
+                               @Override\r
+                               public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
+                                       TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
+                                       try {\r
+                                               gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
+                                                       @Override\r
+                                                       public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
+                                                               Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
+                                                               ft.get(10000); // Covers return codes and err messages\r
+                                                               return null;\r
+                                                       }\r
+                                               });\r
+                                       \r
+                                       } catch (CadiException | APIException e) {\r
+                                               trans.error().log(e);\r
+                                       } finally {\r
+                                               tt.done();\r
+                                       }\r
+                               }\r
+                       });\r
+                       \r
+                       gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy POST", true) {\r
+                               @Override\r
+                               public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
+                                       TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
+                                       try {\r
+                                               gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
+                                                       @Override\r
+                                                       public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
+                                                               Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);\r
+                                                               ft.get(10000); // Covers return codes and err messages\r
+                                                               return null;\r
+                                                       }\r
+                                               });\r
+                                       } catch (CadiException | APIException e) {\r
+                                               trans.error().log(e);\r
+                                       } finally {\r
+                                               tt.done();\r
+                                       }\r
+                               }\r
+                       });\r
+                       \r
+                       gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy PUT", true) {\r
+                               @Override\r
+                               public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
+                                       TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
+                                       try {\r
+                                               gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
+                                                       @Override\r
+                                                       public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
+                                                               Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
+                                                               ft.get(10000); // Covers return codes and err messages\r
+                                                               return null;\r
+                                                       }\r
+                                               });\r
+                                       } catch (CadiException | APIException e) {\r
+                                               trans.error().log(e);\r
+                                       } finally {\r
+                                               tt.done();\r
+                                       }\r
+                               }\r
+                       });\r
+                       \r
+                       gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new GwCode(facade,"Proxy DELETE", true) {\r
+                               @Override\r
+                               public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {\r
+                                       TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);\r
+                                       try {\r
+                                               gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {\r
+                                                       @Override\r
+                                                       public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {\r
+                                                               Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);\r
+                                                               ft.get(10000); // Covers return codes and err messages\r
+                                                               return null;\r
+                                                       }\r
+                                               });\r
+                                       } catch (CadiException | APIException e) {\r
+                                               trans.error().log(e);\r
+                                       } finally {\r
+                                               tt.done();\r
+                                       }\r
+                               }\r
+                       });\r
+               }\r
+       }\r
+}\r