001 package org.apache.tapestry.json;
002
003 /*
004 Copyright (c) 2002 JSON.org
005
006 Permission is hereby granted, free of charge, to any person obtaining a copy
007 of this software and associated documentation files (the "Software"), to deal
008 in the Software without restriction, including without limitation the rights
009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 copies of the Software, and to permit persons to whom the Software is
011 furnished to do so, subject to the following conditions:
012
013 The above copyright notice and this permission notice shall be included in all
014 copies or substantial portions of the Software.
015
016 The Software shall be used for Good, not Evil.
017
018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
024 SOFTWARE.
025 */
026
027 import java.text.ParseException;
028
029 /**
030 * Convert a web browser cookie specification to a JSONObject and back. JSON and
031 * Cookies are both notations for name/value pairs.
032 *
033 * @author JSON.org
034 * @version 0.1
035 */
036 public final class Cookie
037 {
038
039 /* defeat instantiation */
040 private Cookie()
041 {
042 }
043
044 /**
045 * Produce a copy of a string in which the characters '+', '%', '=', ';' and
046 * control characters are replaced with "%hh". This is a gentle form of URL
047 * encoding, attempting to cause as little distortion to the string as
048 * possible. The characters '=' and ';' are meta characters in cookies. By
049 * convention, they are escaped using the URL-encoding. This is only a
050 * convention, not a standard. Often, cookies are expected to have encoded
051 * values. We encode '=' and ';' because we must. We encode '%' and '+'
052 * because they are meta characters in URL encoding.
053 *
054 * @param string
055 * The source string.
056 * @return The escaped result.
057 */
058 public static String escape(String string)
059 {
060 char c;
061 String s = string.trim();
062 StringBuffer sb = new StringBuffer();
063 int len = s.length();
064 for(int i = 0; i < len; i += 1)
065 {
066 c = s.charAt(i);
067 if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';')
068 {
069 sb.append('%');
070 sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16));
071 sb.append(Character.forDigit((char) (c & 0x0f), 16));
072 }
073 else
074 {
075 sb.append(c);
076 }
077 }
078 return sb.toString();
079 }
080
081 /**
082 * Convert a cookie specification string into a JSONObject. The string will
083 * contain a name value pair separated by '='. The name and the value will
084 * be unescaped, possibly converting '+' and '%' sequences. The cookie
085 * properties may follow, separated by ';', also represented as name=value
086 * (except the secure property, which does not have a value). The name will
087 * be stored under the key "name", and the value will be stored under the
088 * key "value". This method does not do checking or validation of the
089 * parameters. It only converts the cookie string into a JSONObject.
090 *
091 * @param string
092 * The cookie specification string.
093 * @return A JSONObject containing "name", "value", and possibly other
094 * members.
095 * @throws ParseException
096 */
097 public static JSONObject toJSONObject(String string)
098 throws ParseException
099 {
100 String n;
101 JSONObject o = new JSONObject();
102 Object v;
103 JSONTokener x = new JSONTokener(string);
104 o.put("name", x.nextTo('='));
105 x.next('=');
106 o.put("value", x.nextTo(';'));
107 x.next();
108 while(x.more())
109 {
110 n = JSONTokener.unescape(x.nextTo("=;"));
111 if (x.next() != '=')
112 {
113 if (n.equals("secure"))
114 {
115 v = Boolean.TRUE;
116 }
117 else
118 {
119 throw x.syntaxError("Missing '=' in cookie parameter.");
120 }
121 }
122 else
123 {
124 v = JSONTokener.unescape(x.nextTo(';'));
125 x.next();
126 }
127 o.put(n, v);
128 }
129 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 StringBuffer sb = new StringBuffer();
145
146 sb.append(escape(o.getString("name")));
147 sb.append("=");
148 sb.append(escape(o.getString("value")));
149 if (o.has("expires"))
150 {
151 sb.append(";expires=");
152 sb.append(o.getString("expires"));
153 }
154 if (o.has("domain"))
155 {
156 sb.append(";domain=");
157 sb.append(escape(o.getString("domain")));
158 }
159 if (o.has("path"))
160 {
161 sb.append(";path=");
162 sb.append(escape(o.getString("path")));
163 }
164 if (o.optBoolean("secure"))
165 {
166 sb.append(";secure");
167 }
168 return sb.toString();
169 }
170 }