1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 import java.util.Iterator;
\r
28 public class CookieList {
\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
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
43 public static JSONObject toJSONObject(String string) throws JSONException {
\r
44 JSONObject jo = new JSONObject();
\r
45 JSONTokener x = new JSONTokener(string);
\r
47 String name = Cookie.unescape(x.nextTo('='));
\r
49 jo.put(name, Cookie.unescape(x.nextTo(';')));
\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
65 public static String toString(JSONObject jo) throws JSONException {
\r
67 Iterator<String> keys = jo.keys();
\r
69 StringBuffer sb = new StringBuffer();
\r
70 while (keys.hasNext()) {
\r
71 string = keys.next().toString();
\r
72 if (!jo.isNull(string)) {
\r
76 sb.append(Cookie.escape(string));
\r
78 sb.append(Cookie.escape(jo.getString(string)));
\r
82 return sb.toString();
\r