Update project structure to org.onap.aaf
[aaf/authz.git] / authz-core / src / main / java / org / onap / aaf / cssa / rserv / Route.java
diff --git a/authz-core/src/main/java/org/onap/aaf/cssa/rserv/Route.java b/authz-core/src/main/java/org/onap/aaf/cssa/rserv/Route.java
new file mode 100644 (file)
index 0000000..9d9253d
--- /dev/null
@@ -0,0 +1,142 @@
+/*******************************************************************************\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 java.io.IOException;\r
+import java.util.List;\r
+\r
+import javax.servlet.ServletException;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.onap.aaf.inno.env.Env;\r
+import org.onap.aaf.inno.env.TimeTaken;\r
+import org.onap.aaf.inno.env.Trans;\r
+\r
+public class Route<TRANS extends Trans> {\r
+       public final String auditText;\r
+       public final HttpMethods meth;\r
+       public final String path;\r
+       \r
+       private Match match;\r
+       // package on purpose\r
+       private final TypedCode<TRANS> content;\r
+       private final boolean isGet;\r
+       \r
+       public Route(HttpMethods meth, String path) {\r
+               this.path = path;\r
+               auditText = meth.name() + ' ' + path;\r
+               this.meth = meth; // Note: Using Spark def for now.\r
+               isGet = meth.compareTo(HttpMethods.GET) == 0;\r
+               match = new Match(path);\r
+               content = new TypedCode<TRANS>();\r
+       }\r
+       \r
+       public void add(HttpCode<TRANS,?> code, String ... others) {\r
+               code.match = match;\r
+               content.add(code, others);\r
+       }\r
+       \r
+//     public void add(HttpCode<TRANS,?> code, Class<?> cls, String version, String ... others) {\r
+//             code.match = match;\r
+//             content.add(code, cls, version, others);\r
+//     }\r
+//\r
+       public HttpCode<TRANS,?> getCode(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {\r
+               // Type is associated with Accept for GET (since it is what is being returned\r
+               // We associate the rest with ContentType.\r
+               // FYI, thought about this a long time before implementing this way.\r
+               String compare;\r
+//             String special[]; // todo, expose Charset (in special) to outside\r
+               if(isGet) {\r
+                       compare = req.getHeader("Accept"); // Accept is used for read, as we want to agree on what caller is ready to handle\r
+               } else {\r
+                       compare = req.getContentType(); // Content type used to declare what data is being created, updated or deleted (might be used for key)\r
+               }\r
+\r
+               Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> hl = content.prep(trans, compare);\r
+               if(hl==null) {\r
+                       resp.setStatus(406); // NOT_ACCEPTABLE\r
+               } else {\r
+                       if(isGet) { // Set Content Type to expected content\r
+                               if("*".equals(hl.x) || "*/*".equals(hl.x)) {// if wild-card, then choose first kind of type\r
+                                       resp.setContentType(content.first());\r
+                               } else {\r
+                                       resp.setContentType(hl.x);\r
+                               }\r
+                       }\r
+                       return hl.y.x;\r
+               }\r
+               return null;\r
+       }\r
+       \r
+       public Route<TRANS> matches(String method, String path) {\r
+               return meth.name().equalsIgnoreCase(method) && match.match(path)?this:null;\r
+       }\r
+       \r
+       public TimeTaken start(Trans trans, String auditText, HttpCode<TRANS,?> code, String type) {\r
+               StringBuilder sb = new StringBuilder(auditText);\r
+               sb.append(", ");\r
+               sb.append(code.desc());\r
+               sb.append(", Content: ");\r
+               sb.append(type);\r
+               return trans.start(sb.toString(), Env.SUB);\r
+       }\r
+\r
+       // Package on purpose.. for "find/Create" routes only\r
+       boolean resolvesTo(HttpMethods hm, String p) {\r
+               return(path.equals(p) && hm.equals(meth));\r
+       }\r
+       \r
+       public String toString() {\r
+               return auditText + ' ' + content; \r
+       }\r
+\r
+       public String report(HttpCode<TRANS, ?> code) {\r
+               StringBuilder sb = new StringBuilder();\r
+               sb.append(auditText);\r
+               sb.append(' ');\r
+               content.relatedTo(code, sb);\r
+               return sb.toString();\r
+       }\r
+\r
+       public RouteReport api() {\r
+               RouteReport tr = new RouteReport();\r
+               tr.meth = meth;\r
+               tr.path = path;\r
+               content.api(tr);\r
+               return tr;\r
+       }\r
+\r
+\r
+       /**\r
+        * contentRelatedTo (For reporting) list routes that will end up at a specific Code\r
+        * @return\r
+        */\r
+       public String contentRelatedTo(HttpCode<TRANS, ?> code) {\r
+               StringBuilder sb = new StringBuilder(path);\r
+               sb.append(' ');\r
+               content.relatedTo(code, sb);\r
+               return sb.toString();\r
+       }\r
+}\r