Update project structure to org.onap.aaf
[aaf/authz.git] / authz-core / src / main / java / org / onap / aaf / cssa / rserv / HttpCode.java
diff --git a/authz-core/src/main/java/org/onap/aaf/cssa/rserv/HttpCode.java b/authz-core/src/main/java/org/onap/aaf/cssa/rserv/HttpCode.java
new file mode 100644 (file)
index 0000000..49a4ba1
--- /dev/null
@@ -0,0 +1,111 @@
+/*******************************************************************************\r
+ * ============LICENSE_START====================================================\r
+ * * org.onap.aaf\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\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 org.onap.aaf.cssa.rserv;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.onap.aaf.inno.env.Trans;\r
+\r
+/**\r
+ * HTTP Code element, which responds to the essential "handle Method".\r
+ * \r
+ * Use Native HttpServletRe[quest|sponse] calls for questions like QueryParameters (getParameter, etc)\r
+ * \r
+ * Use local "pathParam" method to obtain in an optimized manner the path parameter, which must be interpreted by originating string\r
+ * \r
+ * i.e. my/path/:id/:other/*\r
+ * \r
+ *\r
+ * @param <TRANS>\r
+ * @param <T>\r
+ */\r
+public abstract class HttpCode<TRANS extends Trans, CONTEXT> {\r
+       protected CONTEXT context;\r
+       private String desc;\r
+       protected String [] roles;\r
+       private boolean all;\r
+       \r
+       // Package by design... Set by Route when linked\r
+       Match match;\r
+       \r
+       public HttpCode(CONTEXT context, String description, String ... roles) {\r
+               this.context = context;\r
+               desc = description;\r
+               \r
+               // Evaluate for "*" once...\r
+               all = false;\r
+               for(String srole : roles) {\r
+                       if("*".equals(srole)) {\r
+                               all = true;\r
+                               break;\r
+                       }\r
+               }\r
+               this.roles = all?null:roles;\r
+       }\r
+       \r
+       public abstract void handle(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws Exception;\r
+       \r
+       public String desc() {\r
+               return desc;\r
+       }\r
+       \r
+       /**\r
+        * Get the variable element out of the Path Parameter, as set by initial Code\r
+        * \r
+        * @param req\r
+        * @param key\r
+        * @return\r
+        */\r
+       public String pathParam(HttpServletRequest req, String key) {\r
+               return match.param(req.getPathInfo(), key);\r
+       }\r
+\r
+       // Note: get Query Params from Request\r
+       \r
+       /**\r
+        * Check for Authorization when set.\r
+        * \r
+        * If no Roles set, then accepts all users\r
+        * \r
+        * @param req\r
+        * @return\r
+        */\r
+       public boolean isAuthorized(HttpServletRequest req) {\r
+               if(all)return true;\r
+               if(roles!=null) {\r
+                       for(String srole : roles) {\r
+                               if(req.isUserInRole(srole)) return true;\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
+       \r
+       public boolean no_cache() {\r
+               return false;\r
+       }\r
+       \r
+       public String toString() {\r
+               return desc;\r
+       }\r
+}\r