Fix license issues in dmaap dr
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / CookieList.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.json;\r
24 \r
25 import java.util.Iterator;\r
26 \r
27 \r
28 public class CookieList {\r
29 \r
30     /**\r
31      * Convert a cookie list into a JSONObject. A cookie list is a sequence\r
32      * of name/value pairs. The names are separated from the values by '='.\r
33      * The pairs are separated by ';'. The names and the values\r
34      * will be unescaped, possibly converting '+' and '%' sequences.\r
35      *\r
36      * To add a cookie to a cooklist,\r
37      * cookielistJSONObject.put(cookieJSONObject.getString("name"),\r
38      *     cookieJSONObject.getString("value"));\r
39      * @param string  A cookie list string\r
40      * @return A JSONObject\r
41      * @throws JSONException\r
42      */\r
43     public static JSONObject toJSONObject(String string) throws JSONException {\r
44         JSONObject jo = new JSONObject();\r
45         JSONTokener x = new JSONTokener(string);\r
46         while (x.more()) {\r
47             String name = Cookie.unescape(x.nextTo('='));\r
48             x.next('=');\r
49             jo.put(name, Cookie.unescape(x.nextTo(';')));\r
50             x.next();\r
51         }\r
52         return jo;\r
53     }\r
54 \r
55 \r
56     /**\r
57      * Convert a JSONObject into a cookie list. A cookie list is a sequence\r
58      * of name/value pairs. The names are separated from the values by '='.\r
59      * The pairs are separated by ';'. The characters '%', '+', '=', and ';'\r
60      * in the names and values are replaced by "%hh".\r
61      * @param jo A JSONObject\r
62      * @return A cookie list string\r
63      * @throws JSONException\r
64      */\r
65     public static String toString(JSONObject jo) throws JSONException {\r
66         boolean      b = false;\r
67         Iterator<String> keys = jo.keys();\r
68         String       string;\r
69         StringBuffer sb = new StringBuffer();\r
70         while (keys.hasNext()) {\r
71             string = keys.next().toString();\r
72             if (!jo.isNull(string)) {\r
73                 if (b) {\r
74                     sb.append(';');\r
75                 }\r
76                 sb.append(Cookie.escape(string));\r
77                 sb.append("=");\r
78                 sb.append(Cookie.escape(jo.getString(string)));\r
79                 b = true;\r
80             }\r
81         }\r
82         return sb.toString();\r
83     }\r
84 }\r