[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / CookieList.java
diff --git a/datarouter-prov/src/main/java/org/json/CookieList.java b/datarouter-prov/src/main/java/org/json/CookieList.java
new file mode 100644 (file)
index 0000000..89b7816
--- /dev/null
@@ -0,0 +1,112 @@
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\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.json;\r
+\r
+/*\r
+Copyright (c) 2002 JSON.org\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining a copy\r
+of this software and associated documentation files (the "Software"), to deal\r
+in the Software without restriction, including without limitation the rights\r
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+copies of the Software, and to permit persons to whom the Software is\r
+furnished to do so, subject to the following conditions:\r
+\r
+The above copyright notice and this permission notice shall be included in all\r
+copies or substantial portions of the Software.\r
+\r
+The Software shall be used for Good, not Evil.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+SOFTWARE.\r
+*/\r
+\r
+import java.util.Iterator;\r
+\r
+/**\r
+ * Convert a web browser cookie list string to a JSONObject and back.\r
+ * @author JSON.org\r
+ * @version 2010-12-24\r
+ */\r
+public class CookieList {\r
+\r
+    /**\r
+     * Convert a cookie list into a JSONObject. A cookie list is a sequence\r
+     * of name/value pairs. The names are separated from the values by '='.\r
+     * The pairs are separated by ';'. The names and the values\r
+     * will be unescaped, possibly converting '+' and '%' sequences.\r
+     *\r
+     * To add a cookie to a cooklist,\r
+     * cookielistJSONObject.put(cookieJSONObject.getString("name"),\r
+     *     cookieJSONObject.getString("value"));\r
+     * @param string  A cookie list string\r
+     * @return A JSONObject\r
+     * @throws JSONException\r
+     */\r
+    public static JSONObject toJSONObject(String string) throws JSONException {\r
+        JSONObject jo = new JSONObject();\r
+        JSONTokener x = new JSONTokener(string);\r
+        while (x.more()) {\r
+            String name = Cookie.unescape(x.nextTo('='));\r
+            x.next('=');\r
+            jo.put(name, Cookie.unescape(x.nextTo(';')));\r
+            x.next();\r
+        }\r
+        return jo;\r
+    }\r
+\r
+\r
+    /**\r
+     * Convert a JSONObject into a cookie list. A cookie list is a sequence\r
+     * of name/value pairs. The names are separated from the values by '='.\r
+     * The pairs are separated by ';'. The characters '%', '+', '=', and ';'\r
+     * in the names and values are replaced by "%hh".\r
+     * @param jo A JSONObject\r
+     * @return A cookie list string\r
+     * @throws JSONException\r
+     */\r
+    public static String toString(JSONObject jo) throws JSONException {\r
+        boolean      b = false;\r
+        Iterator<String> keys = jo.keys();\r
+        String       string;\r
+        StringBuffer sb = new StringBuffer();\r
+        while (keys.hasNext()) {\r
+            string = keys.next().toString();\r
+            if (!jo.isNull(string)) {\r
+                if (b) {\r
+                    sb.append(';');\r
+                }\r
+                sb.append(Cookie.escape(string));\r
+                sb.append("=");\r
+                sb.append(Cookie.escape(jo.getString(string)));\r
+                b = true;\r
+            }\r
+        }\r
+        return sb.toString();\r
+    }\r
+}\r