--- /dev/null
+/*******************************************************************************\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.util.List;\r
+\r
+import org.onap.aaf.inno.env.Trans;\r
+\r
+\r
+\r
+/**\r
+ * A Class to hold Service "ContentTypes", and to match incoming "Accept" types from HTTP.\r
+ * \r
+ * This is a multi-use class built to use the same Parser for ContentTypes and Accept.\r
+ * \r
+ * Thus, you would create and use "Content.Type" within your service, and use it to match\r
+ * Accept Strings. What is returned is an Integer (for faster processing), which can be\r
+ * used in a switch statement to act on match different Actions. The server should\r
+ * know which behaviors match.\r
+ * \r
+ * "bestMatch" returns an integer for the best match, or -1 if no matches.\r
+ *\r
+ *\r
+ */\r
+public abstract class Content<TRANS extends Trans> {\r
+ public static final String Q = "q";\r
+ protected abstract Pair<String,Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>> types(HttpCode<TRANS,?> code, String str);\r
+ protected abstract boolean props(Pair<String, Pair<HttpCode<TRANS,?>,List<Pair<String,Object>>>> type, String tag, String value);\r
+\r
+ /**\r
+ * Parse a Content-Type/Accept. As found, call "types" and "props", which do different\r
+ * things depending on if it's a Content-Type or Accepts. \r
+ * \r
+ * For Content-Type, it builds a tree suitable for Comparison\r
+ * For Accepts, it compares against the tree, and builds an acceptable type list\r
+ * \r
+ * Since this parse code is used for every incoming HTTP transaction, I have removed the implementation\r
+ * that uses String.split, and replaced with integers evaluating the Byte array. This results\r
+ * in only the necessary strings created, resulting in 1/3 better speed, and less \r
+ * Garbage collection.\r
+ * \r
+ * @param trans\r
+ * @param code\r
+ * @param cntnt\r
+ * @return\r
+ */\r
+ protected boolean parse(HttpCode<TRANS,?> code, String cntnt) {\r
+ byte bytes[] = cntnt.getBytes();\r
+ boolean contType=false,contProp=true;\r
+ int cis,cie=-1,cend;\r
+ int sis,sie,send;\r
+ do {\r
+ cis = cie+1;\r
+ cie = cntnt.indexOf(',',cis);\r
+ cend = cie<0?bytes.length:cie;\r
+ // Start SEMIS\r
+ sie=cis-1;\r
+ Pair<String, Pair<HttpCode<TRANS,?>, List<Pair<String, Object>>>> me = null;\r
+ do {\r
+ sis = sie+1;\r
+ sie = cntnt.indexOf(';',sis);\r
+ send = sie>cend || sie<0?cend:sie;\r
+ if(me==null) {\r
+ String semi = new String(bytes,sis,send-sis);\r
+ // trans.checkpoint(semi);\r
+ // Look at first entity within comma group\r
+ // Is this an acceptable Type?\r
+ me=types(code, semi);\r
+ if(me==null) {\r
+ sie=-1; // skip the rest of the processing... not a type\r
+ } else {\r
+ contType=true;\r
+ }\r
+ } else { // We've looped past the first Semi, now process as properties\r
+ // If there are additional elements (more entities within Semi Colons)\r
+ // apply Propertys\r
+ int eq = cntnt.indexOf('=',sis);\r
+ if(eq>sis && eq<send) {\r
+ String tag = new String(bytes,sis,eq-sis);\r
+ String value = new String(bytes,eq+1,send-(eq+1));\r
+ // trans.checkpoint(" Prop " + tag + "=" + value);\r
+ boolean bool = props(me,tag,value);\r
+ if(!bool) {\r
+ contProp=false;\r
+ }\r
+ }\r
+ }\r
+ // End Property\r
+ } while(sie<=cend && sie>=cis);\r
+ // End SEMIS\r
+ } while(cie>=0);\r
+ return contType && contProp; // for use in finds, True if a type found AND all props matched\r
+ }\r
+ \r
+}\r