[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / Cookie.java
diff --git a/datarouter-prov/src/main/java/org/json/Cookie.java b/datarouter-prov/src/main/java/org/json/Cookie.java
new file mode 100644 (file)
index 0000000..67e4f17
--- /dev/null
@@ -0,0 +1,191 @@
+/*******************************************************************************\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
+/**\r
+ * Convert a web browser cookie specification to a JSONObject and back.\r
+ * JSON and Cookies are both notations for name/value pairs.\r
+ * @author JSON.org\r
+ * @version 2010-12-24\r
+ */\r
+public class Cookie {\r
+\r
+    /**\r
+     * Produce a copy of a string in which the characters '+', '%', '=', ';'\r
+     * and control characters are replaced with "%hh". This is a gentle form\r
+     * of URL encoding, attempting to cause as little distortion to the\r
+     * string as possible. The characters '=' and ';' are meta characters in\r
+     * cookies. By convention, they are escaped using the URL-encoding. This is\r
+     * only a convention, not a standard. Often, cookies are expected to have\r
+     * encoded values. We encode '=' and ';' because we must. We encode '%' and\r
+     * '+' because they are meta characters in URL encoding.\r
+     * @param string The source string.\r
+     * @return       The escaped result.\r
+     */\r
+    public static String escape(String string) {\r
+        char         c;\r
+        String       s = string.trim();\r
+        StringBuffer sb = new StringBuffer();\r
+        int          length = s.length();\r
+        for (int i = 0; i < length; i += 1) {\r
+            c = s.charAt(i);\r
+            if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {\r
+                sb.append('%');\r
+                sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));\r
+                sb.append(Character.forDigit((char)(c & 0x0f), 16));\r
+            } else {\r
+                sb.append(c);\r
+            }\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+\r
+    /**\r
+     * Convert a cookie specification string into a JSONObject. The string\r
+     * will contain a name value pair separated by '='. The name and the value\r
+     * will be unescaped, possibly converting '+' and '%' sequences. The\r
+     * cookie properties may follow, separated by ';', also represented as\r
+     * name=value (except the secure property, which does not have a value).\r
+     * The name will be stored under the key "name", and the value will be\r
+     * stored under the key "value". This method does not do checking or\r
+     * validation of the parameters. It only converts the cookie string into\r
+     * a JSONObject.\r
+     * @param string The cookie specification string.\r
+     * @return A JSONObject containing "name", "value", and possibly other\r
+     *  members.\r
+     * @throws JSONException\r
+     */\r
+    public static JSONObject toJSONObject(String string) throws JSONException {\r
+        String         name;\r
+        JSONObject     jo = new JSONObject();\r
+        Object         value;\r
+        JSONTokener x = new JSONTokener(string);\r
+        jo.put("name", x.nextTo('='));\r
+        x.next('=');\r
+        jo.put("value", x.nextTo(';'));\r
+        x.next();\r
+        while (x.more()) {\r
+            name = unescape(x.nextTo("=;"));\r
+            if (x.next() != '=') {\r
+                if (name.equals("secure")) {\r
+                    value = Boolean.TRUE;\r
+                } else {\r
+                    throw x.syntaxError("Missing '=' in cookie parameter.");\r
+                }\r
+            } else {\r
+                value = unescape(x.nextTo(';'));\r
+                x.next();\r
+            }\r
+            jo.put(name, value);\r
+        }\r
+        return jo;\r
+    }\r
+\r
+\r
+    /**\r
+     * Convert a JSONObject into a cookie specification string. The JSONObject\r
+     * must contain "name" and "value" members.\r
+     * If the JSONObject contains "expires", "domain", "path", or "secure"\r
+     * members, they will be appended to the cookie specification string.\r
+     * All other members are ignored.\r
+     * @param jo A JSONObject\r
+     * @return A cookie specification string\r
+     * @throws JSONException\r
+     */\r
+    public static String toString(JSONObject jo) throws JSONException {\r
+        StringBuffer sb = new StringBuffer();\r
+\r
+        sb.append(escape(jo.getString("name")));\r
+        sb.append("=");\r
+        sb.append(escape(jo.getString("value")));\r
+        if (jo.has("expires")) {\r
+            sb.append(";expires=");\r
+            sb.append(jo.getString("expires"));\r
+        }\r
+        if (jo.has("domain")) {\r
+            sb.append(";domain=");\r
+            sb.append(escape(jo.getString("domain")));\r
+        }\r
+        if (jo.has("path")) {\r
+            sb.append(";path=");\r
+            sb.append(escape(jo.getString("path")));\r
+        }\r
+        if (jo.optBoolean("secure")) {\r
+            sb.append(";secure");\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+    /**\r
+     * Convert <code>%</code><i>hh</i> sequences to single characters, and\r
+     * convert plus to space.\r
+     * @param string A string that may contain\r
+     *      <code>+</code>&nbsp;<small>(plus)</small> and\r
+     *      <code>%</code><i>hh</i> sequences.\r
+     * @return The unescaped string.\r
+     */\r
+    public static String unescape(String string) {\r
+        int length = string.length();\r
+        StringBuffer sb = new StringBuffer();\r
+        for (int i = 0; i < length; ++i) {\r
+            char c = string.charAt(i);\r
+            if (c == '+') {\r
+                c = ' ';\r
+            } else if (c == '%' && i + 2 < length) {\r
+                int d = JSONTokener.dehexchar(string.charAt(i + 1));\r
+                int e = JSONTokener.dehexchar(string.charAt(i + 2));\r
+                if (d >= 0 && e >= 0) {\r
+                    c = (char)(d * 16 + e);\r
+                    i += 2;\r
+                }\r
+            }\r
+            sb.append(c);\r
+        }\r
+        return sb.toString();\r
+    }\r
+}\r