1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
27 import java.util.Iterator;
\r
32 /** Carriage return/line feed. */
\r
33 public static final String CRLF = "\r\n";
\r
36 * Convert an HTTP header string into a JSONObject. It can be a request
\r
37 * header or a response header. A request header will contain
\r
39 * Method: "POST" (for example),
\r
40 * "Request-URI": "/" (for example),
\r
41 * "HTTP-Version": "HTTP/1.1" (for example)
\r
43 * A response header will contain
\r
45 * "HTTP-Version": "HTTP/1.1" (for example),
\r
46 * "Status-Code": "200" (for example),
\r
47 * "Reason-Phrase": "OK" (for example)
\r
49 * In addition, the other parameters in the header will be captured, using
\r
50 * the HTTP field names as JSON names, so that <pre>
\r
51 * Date: Sun, 26 May 2002 18:06:04 GMT
\r
52 * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
\r
53 * Cache-Control: no-cache</pre>
\r
56 * Date: "Sun, 26 May 2002 18:06:04 GMT",
\r
57 * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
\r
58 * "Cache-Control": "no-cache",
\r
60 * It does no further checking or conversion. It does not parse dates.
\r
61 * It does not do '%' transforms on URLs.
\r
62 * @param string An HTTP header string.
\r
63 * @return A JSONObject containing the elements and attributes
\r
64 * of the XML string.
\r
65 * @throws JSONException
\r
67 public static JSONObject toJSONObject(String string) throws JSONException {
\r
68 JSONObject jo = new JSONObject();
\r
69 HTTPTokener x = new HTTPTokener(string);
\r
72 token = x.nextToken();
\r
73 if (token.toUpperCase().startsWith("HTTP")) {
\r
77 jo.put("HTTP-Version", token);
\r
78 jo.put("Status-Code", x.nextToken());
\r
79 jo.put("Reason-Phrase", x.nextTo('\0'));
\r
86 jo.put("Method", token);
\r
87 jo.put("Request-URI", x.nextToken());
\r
88 jo.put("HTTP-Version", x.nextToken());
\r
94 String name = x.nextTo(':');
\r
96 jo.put(name, x.nextTo('\0'));
\r
104 * Convert a JSONObject into an HTTP header. A request header must contain
\r
106 * Method: "POST" (for example),
\r
107 * "Request-URI": "/" (for example),
\r
108 * "HTTP-Version": "HTTP/1.1" (for example)
\r
110 * A response header must contain
\r
112 * "HTTP-Version": "HTTP/1.1" (for example),
\r
113 * "Status-Code": "200" (for example),
\r
114 * "Reason-Phrase": "OK" (for example)
\r
116 * Any other members of the JSONObject will be output as HTTP fields.
\r
117 * The result will end with two CRLF pairs.
\r
118 * @param jo A JSONObject
\r
119 * @return An HTTP header string.
\r
120 * @throws JSONException if the object does not contain enough
\r
123 public static String toString(JSONObject jo) throws JSONException {
\r
124 Iterator<String> keys = jo.keys();
\r
126 StringBuffer sb = new StringBuffer();
\r
127 if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
\r
128 sb.append(jo.getString("HTTP-Version"));
\r
130 sb.append(jo.getString("Status-Code"));
\r
132 sb.append(jo.getString("Reason-Phrase"));
\r
133 } else if (jo.has("Method") && jo.has("Request-URI")) {
\r
134 sb.append(jo.getString("Method"));
\r
137 sb.append(jo.getString("Request-URI"));
\r
140 sb.append(jo.getString("HTTP-Version"));
\r
142 throw new JSONException("Not enough material for an HTTP header.");
\r
145 while (keys.hasNext()) {
\r
146 string = keys.next().toString();
\r
147 if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&
\r
148 !"Reason-Phrase".equals(string) && !"Method".equals(string) &&
\r
149 !"Request-URI".equals(string) && !jo.isNull(string)) {
\r
152 sb.append(jo.getString(string));
\r
157 return sb.toString();
\r