JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / org / json / Cookie.java
1 package org.json;
2
3 /*
4 Copyright (c) 2002 JSON.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 The Software shall be used for Good, not Evil.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27 /**
28  * Convert a web browser cookie specification to a JSONObject and back. JSON and
29  * Cookies are both notations for name/value pairs.
30  * 
31  * @author JSON.org
32  * @version 2015-12-09
33  */
34 public class Cookie
35 {
36
37   /**
38    * Produce a copy of a string in which the characters '+', '%', '=', ';' and
39    * control characters are replaced with "%hh". This is a gentle form of URL
40    * encoding, attempting to cause as little distortion to the string as
41    * possible. The characters '=' and ';' are meta characters in cookies. By
42    * convention, they are escaped using the URL-encoding. This is only a
43    * convention, not a standard. Often, cookies are expected to have encoded
44    * values. We encode '=' and ';' because we must. We encode '%' and '+'
45    * because they are meta characters in URL encoding.
46    * 
47    * @param string
48    *          The source string.
49    * @return The escaped result.
50    */
51   public static String escape(String string)
52   {
53     char c;
54     String s = string.trim();
55     int length = s.length();
56     StringBuilder sb = new StringBuilder(length);
57     for (int i = 0; i < length; i += 1)
58     {
59       c = s.charAt(i);
60       if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';')
61       {
62         sb.append('%');
63         sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16));
64         sb.append(Character.forDigit((char) (c & 0x0f), 16));
65       }
66       else
67       {
68         sb.append(c);
69       }
70     }
71     return sb.toString();
72   }
73
74   /**
75    * Convert a cookie specification string into a JSONObject. The string will
76    * contain a name value pair separated by '='. The name and the value will be
77    * unescaped, possibly converting '+' and '%' sequences. The cookie properties
78    * may follow, separated by ';', also represented as name=value (except the
79    * secure property, which does not have a value). The name will be stored
80    * under the key "name", and the value will be stored under the key "value".
81    * This method does not do checking or validation of the parameters. It only
82    * converts the cookie string into a JSONObject.
83    * 
84    * @param string
85    *          The cookie specification string.
86    * @return A JSONObject containing "name", "value", and possibly other
87    *         members.
88    * @throws JSONException
89    */
90   public static JSONObject toJSONObject(String string) throws JSONException
91   {
92     String name;
93     JSONObject jo = new JSONObject();
94     Object value;
95     JSONTokener x = new JSONTokener(string);
96     jo.put("name", x.nextTo('='));
97     x.next('=');
98     jo.put("value", x.nextTo(';'));
99     x.next();
100     while (x.more())
101     {
102       name = unescape(x.nextTo("=;"));
103       if (x.next() != '=')
104       {
105         if (name.equals("secure"))
106         {
107           value = Boolean.TRUE;
108         }
109         else
110         {
111           throw x.syntaxError("Missing '=' in cookie parameter.");
112         }
113       }
114       else
115       {
116         value = unescape(x.nextTo(';'));
117         x.next();
118       }
119       jo.put(name, value);
120     }
121     return jo;
122   }
123
124   /**
125    * Convert a JSONObject into a cookie specification string. The JSONObject
126    * must contain "name" and "value" members. If the JSONObject contains
127    * "expires", "domain", "path", or "secure" members, they will be appended to
128    * the cookie specification string. All other members are ignored.
129    * 
130    * @param jo
131    *          A JSONObject
132    * @return A cookie specification string
133    * @throws JSONException
134    */
135   public static String toString(JSONObject jo) throws JSONException
136   {
137     StringBuilder sb = new StringBuilder();
138
139     sb.append(escape(jo.getString("name")));
140     sb.append("=");
141     sb.append(escape(jo.getString("value")));
142     if (jo.has("expires"))
143     {
144       sb.append(";expires=");
145       sb.append(jo.getString("expires"));
146     }
147     if (jo.has("domain"))
148     {
149       sb.append(";domain=");
150       sb.append(escape(jo.getString("domain")));
151     }
152     if (jo.has("path"))
153     {
154       sb.append(";path=");
155       sb.append(escape(jo.getString("path")));
156     }
157     if (jo.optBoolean("secure"))
158     {
159       sb.append(";secure");
160     }
161     return sb.toString();
162   }
163
164   /**
165    * Convert <code>%</code><i>hh</i> sequences to single characters, and convert
166    * plus to space.
167    * 
168    * @param string
169    *          A string that may contain
170    *          <code>+</code>&nbsp;<small>(plus)</small> and
171    *          <code>%</code><i>hh</i> sequences.
172    * @return The unescaped string.
173    */
174   public static String unescape(String string)
175   {
176     int length = string.length();
177     StringBuilder sb = new StringBuilder(length);
178     for (int i = 0; i < length; ++i)
179     {
180       char c = string.charAt(i);
181       if (c == '+')
182       {
183         c = ' ';
184       }
185       else if (c == '%' && i + 2 < length)
186       {
187         int d = JSONTokener.dehexchar(string.charAt(i + 1));
188         int e = JSONTokener.dehexchar(string.charAt(i + 2));
189         if (d >= 0 && e >= 0)
190         {
191           c = (char) (d * 16 + e);
192           i += 2;
193         }
194       }
195       sb.append(c);
196     }
197     return sb.toString();
198   }
199 }