root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / org / json / HTTP.java @ 59
History | View | Annotate | Download (5.82 KB)
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 |
import java.util.Iterator; |
28 |
|
29 |
/**
|
30 |
* Convert an HTTP header to a JSONObject and back.
|
31 |
*
|
32 |
* @author JSON.org
|
33 |
* @version 2
|
34 |
*/
|
35 |
public class HTTP { |
36 |
|
37 |
/** Carriage return/line feed. */
|
38 |
public static final String CRLF = "\r\n"; |
39 |
|
40 |
|
41 |
/**
|
42 |
* Convert an HTTP header string into a JSONObject. It can be a request header or a response header. A request header will
|
43 |
* contain
|
44 |
*
|
45 |
* <pre>
|
46 |
* {
|
47 |
* Method: "POST" (for example),
|
48 |
* "Request-URI": "/" (for example),
|
49 |
* "HTTP-Version": "HTTP/1.1" (for example)
|
50 |
* }
|
51 |
* </pre>
|
52 |
*
|
53 |
* A response header will contain
|
54 |
*
|
55 |
* <pre>
|
56 |
* {
|
57 |
* "HTTP-Version": "HTTP/1.1" (for example),
|
58 |
* "Status-Code": "200" (for example),
|
59 |
* "Reason-Phrase": "OK" (for example)
|
60 |
* }
|
61 |
* </pre>
|
62 |
*
|
63 |
* In addition, the other parameters in the header will be captured, using the HTTP field names as JSON names, so that
|
64 |
*
|
65 |
* <pre>
|
66 |
* Date: Sun, 26 May 2002 18:06:04 GMT
|
67 |
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
|
68 |
* Cache-Control: no-cache
|
69 |
* </pre>
|
70 |
*
|
71 |
* become
|
72 |
*
|
73 |
* <pre>
|
74 |
* {...
|
75 |
* Date: "Sun, 26 May 2002 18:06:04 GMT",
|
76 |
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
|
77 |
* "Cache-Control": "no-cache",
|
78 |
* ...}
|
79 |
* </pre>
|
80 |
*
|
81 |
* It does no further checking or conversion. It does not parse dates. It does not do '%' transforms on URLs.
|
82 |
*
|
83 |
* @param string
|
84 |
* An HTTP header string.
|
85 |
* @return A JSONObject containing the elements and attributes of the XML string.
|
86 |
* @throws JSONException
|
87 |
*/
|
88 |
public static JSONObject toJSONObject(final String string) throws JSONException { |
89 |
final JSONObject o = new JSONObject(); |
90 |
final HTTPTokener x = new HTTPTokener(string); |
91 |
String t;
|
92 |
|
93 |
t = x.nextToken(); |
94 |
if (t.toUpperCase().startsWith("HTTP")) { |
95 |
|
96 |
// Response
|
97 |
|
98 |
o.put("HTTP-Version", t);
|
99 |
o.put("Status-Code", x.nextToken());
|
100 |
o.put("Reason-Phrase", x.nextTo('\0')); |
101 |
x.next(); |
102 |
|
103 |
} |
104 |
else {
|
105 |
|
106 |
// Request
|
107 |
|
108 |
o.put("Method", t);
|
109 |
o.put("Request-URI", x.nextToken());
|
110 |
o.put("HTTP-Version", x.nextToken());
|
111 |
} |
112 |
|
113 |
// Fields
|
114 |
|
115 |
while (x.more()) {
|
116 |
final String name = x.nextTo(':'); |
117 |
x.next(':');
|
118 |
o.put(name, x.nextTo('\0'));
|
119 |
x.next(); |
120 |
} |
121 |
return o;
|
122 |
} |
123 |
|
124 |
|
125 |
/**
|
126 |
* Convert a JSONObject into an HTTP header. A request header must contain
|
127 |
*
|
128 |
* <pre>
|
129 |
* {
|
130 |
* Method: "POST" (for example),
|
131 |
* "Request-URI": "/" (for example),
|
132 |
* "HTTP-Version": "HTTP/1.1" (for example)
|
133 |
* }
|
134 |
* </pre>
|
135 |
*
|
136 |
* A response header must contain
|
137 |
*
|
138 |
* <pre>
|
139 |
* {
|
140 |
* "HTTP-Version": "HTTP/1.1" (for example),
|
141 |
* "Status-Code": "200" (for example),
|
142 |
* "Reason-Phrase": "OK" (for example)
|
143 |
* }
|
144 |
* </pre>
|
145 |
*
|
146 |
* Any other members of the JSONObject will be output as HTTP fields. The result will end with two CRLF pairs.
|
147 |
*
|
148 |
* @param o
|
149 |
* A JSONObject
|
150 |
* @return An HTTP header string.
|
151 |
* @throws JSONException
|
152 |
* if the object does not contain enough information.
|
153 |
*/
|
154 |
public static String toString(final JSONObject o) throws JSONException { |
155 |
final Iterator keys = o.keys(); |
156 |
String s;
|
157 |
final StringBuffer sb = new StringBuffer(); |
158 |
if (o.has("Status-Code") && o.has("Reason-Phrase")) { |
159 |
sb.append(o.getString("HTTP-Version"));
|
160 |
sb.append(' ');
|
161 |
sb.append(o.getString("Status-Code"));
|
162 |
sb.append(' ');
|
163 |
sb.append(o.getString("Reason-Phrase"));
|
164 |
} |
165 |
else if (o.has("Method") && o.has("Request-URI")) { |
166 |
sb.append(o.getString("Method"));
|
167 |
sb.append(' ');
|
168 |
sb.append('"');
|
169 |
sb.append(o.getString("Request-URI"));
|
170 |
sb.append('"');
|
171 |
sb.append(' ');
|
172 |
sb.append(o.getString("HTTP-Version"));
|
173 |
} |
174 |
else {
|
175 |
throw new JSONException("Not enough material for an HTTP header."); |
176 |
} |
177 |
sb.append(CRLF); |
178 |
while (keys.hasNext()) {
|
179 |
s = keys.next().toString(); |
180 |
if (!s.equals("HTTP-Version") && !s.equals("Status-Code") && !s.equals("Reason-Phrase") && !s.equals("Method") |
181 |
&& !s.equals("Request-URI") && !o.isNull(s)) {
|
182 |
sb.append(s); |
183 |
sb.append(": ");
|
184 |
sb.append(o.getString(s)); |
185 |
sb.append(CRLF); |
186 |
} |
187 |
} |
188 |
sb.append(CRLF); |
189 |
return sb.toString();
|
190 |
} |
191 |
} |