Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Cookie |
|
| 4.5;4.5 |
1 | package org.apache.tapestry.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 | import java.text.ParseException; | |
28 | ||
29 | /** | |
30 | * Convert a web browser cookie specification to a JSONObject and back. JSON and | |
31 | * Cookies are both notations for name/value pairs. | |
32 | * | |
33 | * @author JSON.org | |
34 | * @version 0.1 | |
35 | */ | |
36 | public final class Cookie | |
37 | { | |
38 | ||
39 | /* defeat instantiation */ | |
40 | private Cookie() | |
41 | 0 | { |
42 | 0 | } |
43 | ||
44 | /** | |
45 | * Produce a copy of a string in which the characters '+', '%', '=', ';' and | |
46 | * control characters are replaced with "%hh". This is a gentle form of URL | |
47 | * encoding, attempting to cause as little distortion to the string as | |
48 | * possible. The characters '=' and ';' are meta characters in cookies. By | |
49 | * convention, they are escaped using the URL-encoding. This is only a | |
50 | * convention, not a standard. Often, cookies are expected to have encoded | |
51 | * values. We encode '=' and ';' because we must. We encode '%' and '+' | |
52 | * because they are meta characters in URL encoding. | |
53 | * | |
54 | * @param string | |
55 | * The source string. | |
56 | * @return The escaped result. | |
57 | */ | |
58 | public static String escape(String string) | |
59 | { | |
60 | char c; | |
61 | 0 | String s = string.trim(); |
62 | 0 | StringBuffer sb = new StringBuffer(); |
63 | 0 | int len = s.length(); |
64 | 0 | for(int i = 0; i < len; i += 1) |
65 | { | |
66 | 0 | c = s.charAt(i); |
67 | 0 | if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') |
68 | { | |
69 | 0 | sb.append('%'); |
70 | 0 | sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16)); |
71 | 0 | sb.append(Character.forDigit((char) (c & 0x0f), 16)); |
72 | } | |
73 | else | |
74 | { | |
75 | 0 | sb.append(c); |
76 | } | |
77 | } | |
78 | 0 | return sb.toString(); |
79 | } | |
80 | ||
81 | /** | |
82 | * Convert a cookie specification string into a JSONObject. The string will | |
83 | * contain a name value pair separated by '='. The name and the value will | |
84 | * be unescaped, possibly converting '+' and '%' sequences. The cookie | |
85 | * properties may follow, separated by ';', also represented as name=value | |
86 | * (except the secure property, which does not have a value). The name will | |
87 | * be stored under the key "name", and the value will be stored under the | |
88 | * key "value". This method does not do checking or validation of the | |
89 | * parameters. It only converts the cookie string into a JSONObject. | |
90 | * | |
91 | * @param string | |
92 | * The cookie specification string. | |
93 | * @return A JSONObject containing "name", "value", and possibly other | |
94 | * members. | |
95 | * @throws ParseException | |
96 | */ | |
97 | public static JSONObject toJSONObject(String string) | |
98 | throws ParseException | |
99 | { | |
100 | String n; | |
101 | 0 | JSONObject o = new JSONObject(); |
102 | Object v; | |
103 | 0 | JSONTokener x = new JSONTokener(string); |
104 | 0 | o.put("name", x.nextTo('=')); |
105 | 0 | x.next('='); |
106 | 0 | o.put("value", x.nextTo(';')); |
107 | 0 | x.next(); |
108 | 0 | while(x.more()) |
109 | { | |
110 | 0 | n = JSONTokener.unescape(x.nextTo("=;")); |
111 | 0 | if (x.next() != '=') |
112 | { | |
113 | 0 | if (n.equals("secure")) |
114 | { | |
115 | 0 | v = Boolean.TRUE; |
116 | } | |
117 | else | |
118 | { | |
119 | 0 | throw x.syntaxError("Missing '=' in cookie parameter."); |
120 | } | |
121 | } | |
122 | else | |
123 | { | |
124 | 0 | v = JSONTokener.unescape(x.nextTo(';')); |
125 | 0 | x.next(); |
126 | } | |
127 | 0 | o.put(n, v); |
128 | } | |
129 | 0 | return o; |
130 | } | |
131 | ||
132 | /** | |
133 | * Convert a JSONObject into a cookie specification string. The JSONObject | |
134 | * must contain "name" and "value" members. If the JSONObject contains | |
135 | * "expires", "domain", "path", or "secure" members, they will be appended | |
136 | * to the cookie specification string. All other members are ignored. | |
137 | * | |
138 | * @param o | |
139 | * A JSONObject | |
140 | * @return A cookie specification string | |
141 | */ | |
142 | public static String toString(JSONObject o) | |
143 | { | |
144 | 0 | StringBuffer sb = new StringBuffer(); |
145 | ||
146 | 0 | sb.append(escape(o.getString("name"))); |
147 | 0 | sb.append("="); |
148 | 0 | sb.append(escape(o.getString("value"))); |
149 | 0 | if (o.has("expires")) |
150 | { | |
151 | 0 | sb.append(";expires="); |
152 | 0 | sb.append(o.getString("expires")); |
153 | } | |
154 | 0 | if (o.has("domain")) |
155 | { | |
156 | 0 | sb.append(";domain="); |
157 | 0 | sb.append(escape(o.getString("domain"))); |
158 | } | |
159 | 0 | if (o.has("path")) |
160 | { | |
161 | 0 | sb.append(";path="); |
162 | 0 | sb.append(escape(o.getString("path"))); |
163 | } | |
164 | 0 | if (o.optBoolean("secure")) |
165 | { | |
166 | 0 | sb.append(";secure"); |
167 | } | |
168 | 0 | return sb.toString(); |
169 | } | |
170 | } |