cca9118c5fb71de8ee56f5bc067155341bd98293
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / Cookie.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 public class Cookie {\r
27 \r
28     /**\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
39      */\r
40     public static String escape(String string) {\r
41         char         c;\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
46             c = s.charAt(i);\r
47             if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {\r
48                 sb.append('%');\r
49                 sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));\r
50                 sb.append(Character.forDigit((char)(c & 0x0f), 16));\r
51             } else {\r
52                 sb.append(c);\r
53             }\r
54         }\r
55         return sb.toString();\r
56     }\r
57 \r
58 \r
59     /**\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
68      * a JSONObject.\r
69      * @param string The cookie specification string.\r
70      * @return A JSONObject containing "name", "value", and possibly other\r
71      *  members.\r
72      * @throws JSONException\r
73      */\r
74     public static JSONObject toJSONObject(String string) throws JSONException {\r
75         String         name;\r
76         JSONObject     jo = new JSONObject();\r
77         Object         value;\r
78         JSONTokener x = new JSONTokener(string);\r
79         jo.put("name", x.nextTo('='));\r
80         x.next('=');\r
81         jo.put("value", x.nextTo(';'));\r
82         x.next();\r
83         while (x.more()) {\r
84             name = unescape(x.nextTo("=;"));\r
85             if (x.next() != '=') {\r
86                 if (name.equals("secure")) {\r
87                     value = Boolean.TRUE;\r
88                 } else {\r
89                     throw x.syntaxError("Missing '=' in cookie parameter.");\r
90                 }\r
91             } else {\r
92                 value = unescape(x.nextTo(';'));\r
93                 x.next();\r
94             }\r
95             jo.put(name, value);\r
96         }\r
97         return jo;\r
98     }\r
99 \r
100 \r
101     /**\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
110      */\r
111     public static String toString(JSONObject jo) throws JSONException {\r
112         StringBuffer sb = new StringBuffer();\r
113 \r
114         sb.append(escape(jo.getString("name")));\r
115         sb.append("=");\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
120         }\r
121         if (jo.has("domain")) {\r
122             sb.append(";domain=");\r
123             sb.append(escape(jo.getString("domain")));\r
124         }\r
125         if (jo.has("path")) {\r
126             sb.append(";path=");\r
127             sb.append(escape(jo.getString("path")));\r
128         }\r
129         if (jo.optBoolean("secure")) {\r
130             sb.append(";secure");\r
131         }\r
132         return sb.toString();\r
133     }\r
134 \r
135     /**\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>&nbsp;<small>(plus)</small> and\r
140      *      <code>%</code><i>hh</i> sequences.\r
141      * @return The unescaped string.\r
142      */\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
148             if (c == '+') {\r
149                 c = ' ';\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
155                     i += 2;\r
156                 }\r
157             }\r
158             sb.append(c);\r
159         }\r
160         return sb.toString();\r
161     }\r
162 }\r