[DMAAP-48] Initial code import
[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 /*\r
26 Copyright (c) 2002 JSON.org\r
27 \r
28 Permission is hereby granted, free of charge, to any person obtaining a copy\r
29 of this software and associated documentation files (the "Software"), to deal\r
30 in the Software without restriction, including without limitation the rights\r
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
32 copies of the Software, and to permit persons to whom the Software is\r
33 furnished to do so, subject to the following conditions:\r
34 \r
35 The above copyright notice and this permission notice shall be included in all\r
36 copies or substantial portions of the Software.\r
37 \r
38 The Software shall be used for Good, not Evil.\r
39 \r
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
41 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
42 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
43 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
44 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
45 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
46 SOFTWARE.\r
47 */\r
48 \r
49 import java.util.Iterator;\r
50 \r
51 /**\r
52  * Convert a web browser cookie list string to a JSONObject and back.\r
53  * @author JSON.org\r
54  * @version 2010-12-24\r
55  */\r
56 public class CookieList {\r
57 \r
58     /**\r
59      * Convert a cookie list into a JSONObject. A cookie list is a sequence\r
60      * of name/value pairs. The names are separated from the values by '='.\r
61      * The pairs are separated by ';'. The names and the values\r
62      * will be unescaped, possibly converting '+' and '%' sequences.\r
63      *\r
64      * To add a cookie to a cooklist,\r
65      * cookielistJSONObject.put(cookieJSONObject.getString("name"),\r
66      *     cookieJSONObject.getString("value"));\r
67      * @param string  A cookie list string\r
68      * @return A JSONObject\r
69      * @throws JSONException\r
70      */\r
71     public static JSONObject toJSONObject(String string) throws JSONException {\r
72         JSONObject jo = new JSONObject();\r
73         JSONTokener x = new JSONTokener(string);\r
74         while (x.more()) {\r
75             String name = Cookie.unescape(x.nextTo('='));\r
76             x.next('=');\r
77             jo.put(name, Cookie.unescape(x.nextTo(';')));\r
78             x.next();\r
79         }\r
80         return jo;\r
81     }\r
82 \r
83 \r
84     /**\r
85      * Convert a JSONObject into a cookie list. A cookie list is a sequence\r
86      * of name/value pairs. The names are separated from the values by '='.\r
87      * The pairs are separated by ';'. The characters '%', '+', '=', and ';'\r
88      * in the names and values are replaced by "%hh".\r
89      * @param jo A JSONObject\r
90      * @return A cookie list string\r
91      * @throws JSONException\r
92      */\r
93     public static String toString(JSONObject jo) throws JSONException {\r
94         boolean      b = false;\r
95         Iterator<String> keys = jo.keys();\r
96         String       string;\r
97         StringBuffer sb = new StringBuffer();\r
98         while (keys.hasNext()) {\r
99             string = keys.next().toString();\r
100             if (!jo.isNull(string)) {\r
101                 if (b) {\r
102                     sb.append(';');\r
103                 }\r
104                 sb.append(Cookie.escape(string));\r
105                 sb.append("=");\r
106                 sb.append(Cookie.escape(jo.getString(string)));\r
107                 b = true;\r
108             }\r
109         }\r
110         return sb.toString();\r
111     }\r
112 }\r