--- /dev/null
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ * * http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ * * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package org.json;\r
+\r
+/*\r
+Copyright (c) 2002 JSON.org\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining a copy\r
+of this software and associated documentation files (the "Software"), to deal\r
+in the Software without restriction, including without limitation the rights\r
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+copies of the Software, and to permit persons to whom the Software is\r
+furnished to do so, subject to the following conditions:\r
+\r
+The above copyright notice and this permission notice shall be included in all\r
+copies or substantial portions of the Software.\r
+\r
+The Software shall be used for Good, not Evil.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+SOFTWARE.\r
+*/\r
+\r
+import java.util.Iterator;\r
+\r
+/**\r
+ * Convert an HTTP header to a JSONObject and back.\r
+ * @author JSON.org\r
+ * @version 2010-12-24\r
+ */\r
+public class HTTP {\r
+\r
+ /** Carriage return/line feed. */\r
+ public static final String CRLF = "\r\n";\r
+\r
+ /**\r
+ * Convert an HTTP header string into a JSONObject. It can be a request\r
+ * header or a response header. A request header will contain\r
+ * <pre>{\r
+ * Method: "POST" (for example),\r
+ * "Request-URI": "/" (for example),\r
+ * "HTTP-Version": "HTTP/1.1" (for example)\r
+ * }</pre>\r
+ * A response header will contain\r
+ * <pre>{\r
+ * "HTTP-Version": "HTTP/1.1" (for example),\r
+ * "Status-Code": "200" (for example),\r
+ * "Reason-Phrase": "OK" (for example)\r
+ * }</pre>\r
+ * In addition, the other parameters in the header will be captured, using\r
+ * the HTTP field names as JSON names, so that <pre>\r
+ * Date: Sun, 26 May 2002 18:06:04 GMT\r
+ * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s\r
+ * Cache-Control: no-cache</pre>\r
+ * become\r
+ * <pre>{...\r
+ * Date: "Sun, 26 May 2002 18:06:04 GMT",\r
+ * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",\r
+ * "Cache-Control": "no-cache",\r
+ * ...}</pre>\r
+ * It does no further checking or conversion. It does not parse dates.\r
+ * It does not do '%' transforms on URLs.\r
+ * @param string An HTTP header string.\r
+ * @return A JSONObject containing the elements and attributes\r
+ * of the XML string.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONObject toJSONObject(String string) throws JSONException {\r
+ JSONObject jo = new JSONObject();\r
+ HTTPTokener x = new HTTPTokener(string);\r
+ String token;\r
+\r
+ token = x.nextToken();\r
+ if (token.toUpperCase().startsWith("HTTP")) {\r
+\r
+// Response\r
+\r
+ jo.put("HTTP-Version", token);\r
+ jo.put("Status-Code", x.nextToken());\r
+ jo.put("Reason-Phrase", x.nextTo('\0'));\r
+ x.next();\r
+\r
+ } else {\r
+\r
+// Request\r
+\r
+ jo.put("Method", token);\r
+ jo.put("Request-URI", x.nextToken());\r
+ jo.put("HTTP-Version", x.nextToken());\r
+ }\r
+\r
+// Fields\r
+\r
+ while (x.more()) {\r
+ String name = x.nextTo(':');\r
+ x.next(':');\r
+ jo.put(name, x.nextTo('\0'));\r
+ x.next();\r
+ }\r
+ return jo;\r
+ }\r
+\r
+\r
+ /**\r
+ * Convert a JSONObject into an HTTP header. A request header must contain\r
+ * <pre>{\r
+ * Method: "POST" (for example),\r
+ * "Request-URI": "/" (for example),\r
+ * "HTTP-Version": "HTTP/1.1" (for example)\r
+ * }</pre>\r
+ * A response header must contain\r
+ * <pre>{\r
+ * "HTTP-Version": "HTTP/1.1" (for example),\r
+ * "Status-Code": "200" (for example),\r
+ * "Reason-Phrase": "OK" (for example)\r
+ * }</pre>\r
+ * Any other members of the JSONObject will be output as HTTP fields.\r
+ * The result will end with two CRLF pairs.\r
+ * @param jo A JSONObject\r
+ * @return An HTTP header string.\r
+ * @throws JSONException if the object does not contain enough\r
+ * information.\r
+ */\r
+ public static String toString(JSONObject jo) throws JSONException {\r
+ Iterator<String> keys = jo.keys();\r
+ String string;\r
+ StringBuffer sb = new StringBuffer();\r
+ if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {\r
+ sb.append(jo.getString("HTTP-Version"));\r
+ sb.append(' ');\r
+ sb.append(jo.getString("Status-Code"));\r
+ sb.append(' ');\r
+ sb.append(jo.getString("Reason-Phrase"));\r
+ } else if (jo.has("Method") && jo.has("Request-URI")) {\r
+ sb.append(jo.getString("Method"));\r
+ sb.append(' ');\r
+ sb.append('"');\r
+ sb.append(jo.getString("Request-URI"));\r
+ sb.append('"');\r
+ sb.append(' ');\r
+ sb.append(jo.getString("HTTP-Version"));\r
+ } else {\r
+ throw new JSONException("Not enough material for an HTTP header.");\r
+ }\r
+ sb.append(CRLF);\r
+ while (keys.hasNext()) {\r
+ string = keys.next().toString();\r
+ if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&\r
+ !"Reason-Phrase".equals(string) && !"Method".equals(string) &&\r
+ !"Request-URI".equals(string) && !jo.isNull(string)) {\r
+ sb.append(string);\r
+ sb.append(": ");\r
+ sb.append(jo.getString(string));\r
+ sb.append(CRLF);\r
+ }\r
+ }\r
+ sb.append(CRLF);\r
+ return sb.toString();\r
+ }\r
+}\r