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
26 public class Cookie {
\r
29 * Produce a copy of a string in which the characters '+', '%', '=', ';'
\r
30 * and control characters are replaced with "%hh". This is a gentle form
\r
31 * of URL encoding, attempting to cause as little distortion to the
\r
32 * string as possible. The characters '=' and ';' are meta characters in
\r
33 * cookies. By convention, they are escaped using the URL-encoding. This is
\r
34 * only a convention, not a standard. Often, cookies are expected to have
\r
35 * encoded values. We encode '=' and ';' because we must. We encode '%' and
\r
36 * '+' because they are meta characters in URL encoding.
\r
37 * @param string The source string.
\r
38 * @return The escaped result.
\r
40 public static String escape(String string) {
\r
42 String s = string.trim();
\r
43 StringBuffer sb = new StringBuffer();
\r
44 int length = s.length();
\r
45 for (int i = 0; i < length; i += 1) {
\r
47 if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
\r
49 sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
\r
50 sb.append(Character.forDigit((char)(c & 0x0f), 16));
\r
55 return sb.toString();
\r
60 * Convert a cookie specification string into a JSONObject. The string
\r
61 * will contain a name value pair separated by '='. The name and the value
\r
62 * will be unescaped, possibly converting '+' and '%' sequences. The
\r
63 * cookie properties may follow, separated by ';', also represented as
\r
64 * name=value (except the secure property, which does not have a value).
\r
65 * The name will be stored under the key "name", and the value will be
\r
66 * stored under the key "value". This method does not do checking or
\r
67 * validation of the parameters. It only converts the cookie string into
\r
69 * @param string The cookie specification string.
\r
70 * @return A JSONObject containing "name", "value", and possibly other
\r
72 * @throws JSONException
\r
74 public static JSONObject toJSONObject(String string) throws JSONException {
\r
76 JSONObject jo = new JSONObject();
\r
78 JSONTokener x = new JSONTokener(string);
\r
79 jo.put("name", x.nextTo('='));
\r
81 jo.put("value", x.nextTo(';'));
\r
84 name = unescape(x.nextTo("=;"));
\r
85 if (x.next() != '=') {
\r
86 if (name.equals("secure")) {
\r
87 value = Boolean.TRUE;
\r
89 throw x.syntaxError("Missing '=' in cookie parameter.");
\r
92 value = unescape(x.nextTo(';'));
\r
95 jo.put(name, value);
\r
102 * Convert a JSONObject into a cookie specification string. The JSONObject
\r
103 * must contain "name" and "value" members.
\r
104 * If the JSONObject contains "expires", "domain", "path", or "secure"
\r
105 * members, they will be appended to the cookie specification string.
\r
106 * All other members are ignored.
\r
107 * @param jo A JSONObject
\r
108 * @return A cookie specification string
\r
109 * @throws JSONException
\r
111 public static String toString(JSONObject jo) throws JSONException {
\r
112 StringBuffer sb = new StringBuffer();
\r
114 sb.append(escape(jo.getString("name")));
\r
116 sb.append(escape(jo.getString("value")));
\r
117 if (jo.has("expires")) {
\r
118 sb.append(";expires=");
\r
119 sb.append(jo.getString("expires"));
\r
121 if (jo.has("domain")) {
\r
122 sb.append(";domain=");
\r
123 sb.append(escape(jo.getString("domain")));
\r
125 if (jo.has("path")) {
\r
126 sb.append(";path=");
\r
127 sb.append(escape(jo.getString("path")));
\r
129 if (jo.optBoolean("secure")) {
\r
130 sb.append(";secure");
\r
132 return sb.toString();
\r
136 * Convert <code>%</code><i>hh</i> sequences to single characters, and
\r
137 * convert plus to space.
\r
138 * @param string A string that may contain
\r
139 * <code>+</code> <small>(plus)</small> and
\r
140 * <code>%</code><i>hh</i> sequences.
\r
141 * @return The unescaped string.
\r
143 public static String unescape(String string) {
\r
144 int length = string.length();
\r
145 StringBuffer sb = new StringBuffer();
\r
146 for (int i = 0; i < length; ++i) {
\r
147 char c = string.charAt(i);
\r
150 } else if (c == '%' && i + 2 < length) {
\r
151 int d = JSONTokener.dehexchar(string.charAt(i + 1));
\r
152 int e = JSONTokener.dehexchar(string.charAt(i + 2));
\r
153 if (d >= 0 && e >= 0) {
\r
154 c = (char)(d * 16 + e);
\r
160 return sb.toString();
\r