Fix license issues in dmaap dr
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / HTTP.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.json;\r
24 \r
25 \r
26 \r
27 import java.util.Iterator;\r
28 \r
29 \r
30 public class HTTP {\r
31 \r
32     /** Carriage return/line feed. */\r
33     public static final String CRLF = "\r\n";\r
34 \r
35     /**\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
38      * <pre>{\r
39      *    Method: "POST" (for example),\r
40      *    "Request-URI": "/" (for example),\r
41      *    "HTTP-Version": "HTTP/1.1" (for example)\r
42      * }</pre>\r
43      * A response header will contain\r
44      * <pre>{\r
45      *    "HTTP-Version": "HTTP/1.1" (for example),\r
46      *    "Status-Code": "200" (for example),\r
47      *    "Reason-Phrase": "OK" (for example)\r
48      * }</pre>\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
54      * become\r
55      * <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
59      * ...}</pre>\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
66      */\r
67     public static JSONObject toJSONObject(String string) throws JSONException {\r
68         JSONObject     jo = new JSONObject();\r
69         HTTPTokener    x = new HTTPTokener(string);\r
70         String         token;\r
71 \r
72         token = x.nextToken();\r
73         if (token.toUpperCase().startsWith("HTTP")) {\r
74 \r
75 // Response\r
76 \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
80             x.next();\r
81 \r
82         } else {\r
83 \r
84 // Request\r
85 \r
86             jo.put("Method", token);\r
87             jo.put("Request-URI", x.nextToken());\r
88             jo.put("HTTP-Version", x.nextToken());\r
89         }\r
90 \r
91 // Fields\r
92 \r
93         while (x.more()) {\r
94             String name = x.nextTo(':');\r
95             x.next(':');\r
96             jo.put(name, x.nextTo('\0'));\r
97             x.next();\r
98         }\r
99         return jo;\r
100     }\r
101 \r
102 \r
103     /**\r
104      * Convert a JSONObject into an HTTP header. A request header must contain\r
105      * <pre>{\r
106      *    Method: "POST" (for example),\r
107      *    "Request-URI": "/" (for example),\r
108      *    "HTTP-Version": "HTTP/1.1" (for example)\r
109      * }</pre>\r
110      * A response header must contain\r
111      * <pre>{\r
112      *    "HTTP-Version": "HTTP/1.1" (for example),\r
113      *    "Status-Code": "200" (for example),\r
114      *    "Reason-Phrase": "OK" (for example)\r
115      * }</pre>\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
121      *  information.\r
122      */\r
123     public static String toString(JSONObject jo) throws JSONException {\r
124         Iterator<String> keys = jo.keys();\r
125         String       string;\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
129             sb.append(' ');\r
130             sb.append(jo.getString("Status-Code"));\r
131             sb.append(' ');\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
135             sb.append(' ');\r
136             sb.append('"');\r
137             sb.append(jo.getString("Request-URI"));\r
138             sb.append('"');\r
139             sb.append(' ');\r
140             sb.append(jo.getString("HTTP-Version"));\r
141         } else {\r
142             throw new JSONException("Not enough material for an HTTP header.");\r
143         }\r
144         sb.append(CRLF);\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
150                 sb.append(string);\r
151                 sb.append(": ");\r
152                 sb.append(jo.getString(string));\r
153                 sb.append(CRLF);\r
154             }\r
155         }\r
156         sb.append(CRLF);\r
157         return sb.toString();\r
158     }\r
159 }\r