| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2015 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 package org.appspot.apprtc.util; | |
| 29 | |
| 30 import java.io.IOException; | |
| 31 import java.io.InputStream; | |
| 32 import java.io.OutputStream; | |
| 33 import java.net.HttpURLConnection; | |
| 34 import java.net.SocketTimeoutException; | |
| 35 import java.net.URL; | |
| 36 import java.util.Scanner; | |
| 37 | |
| 38 /** | |
| 39 * Asynchronous http requests implementation. | |
| 40 */ | |
| 41 public class AsyncHttpURLConnection { | |
| 42 private static final int HTTP_TIMEOUT_MS = 8000; | |
| 43 private static final String HTTP_ORIGIN = "https://apprtc.appspot.com"; | |
| 44 private final String method; | |
| 45 private final String url; | |
| 46 private final String message; | |
| 47 private final AsyncHttpEvents events; | |
| 48 private String contentType; | |
| 49 | |
| 50 /** | |
| 51 * Http requests callbacks. | |
| 52 */ | |
| 53 public interface AsyncHttpEvents { | |
| 54 public void onHttpError(String errorMessage); | |
| 55 public void onHttpComplete(String response); | |
| 56 } | |
| 57 | |
| 58 public AsyncHttpURLConnection(String method, String url, String message, | |
| 59 AsyncHttpEvents events) { | |
| 60 this.method = method; | |
| 61 this.url = url; | |
| 62 this.message = message; | |
| 63 this.events = events; | |
| 64 } | |
| 65 | |
| 66 public void setContentType(String contentType) { | |
| 67 this.contentType = contentType; | |
| 68 } | |
| 69 | |
| 70 public void send() { | |
| 71 Runnable runHttp = new Runnable() { | |
| 72 public void run() { | |
| 73 sendHttpMessage(); | |
| 74 } | |
| 75 }; | |
| 76 new Thread(runHttp).start(); | |
| 77 } | |
| 78 | |
| 79 private void sendHttpMessage() { | |
| 80 try { | |
| 81 HttpURLConnection connection = | |
| 82 (HttpURLConnection) new URL(url).openConnection(); | |
| 83 byte[] postData = new byte[0]; | |
| 84 if (message != null) { | |
| 85 postData = message.getBytes("UTF-8"); | |
| 86 } | |
| 87 connection.setRequestMethod(method); | |
| 88 connection.setUseCaches(false); | |
| 89 connection.setDoInput(true); | |
| 90 connection.setConnectTimeout(HTTP_TIMEOUT_MS); | |
| 91 connection.setReadTimeout(HTTP_TIMEOUT_MS); | |
| 92 // TODO(glaznev) - query request origin from pref_room_server_url_key pref
erences. | |
| 93 connection.addRequestProperty("origin", HTTP_ORIGIN); | |
| 94 boolean doOutput = false; | |
| 95 if (method.equals("POST")) { | |
| 96 doOutput = true; | |
| 97 connection.setDoOutput(true); | |
| 98 connection.setFixedLengthStreamingMode(postData.length); | |
| 99 } | |
| 100 if (contentType == null) { | |
| 101 connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8
"); | |
| 102 } else { | |
| 103 connection.setRequestProperty("Content-Type", contentType); | |
| 104 } | |
| 105 | |
| 106 // Send POST request. | |
| 107 if (doOutput && postData.length > 0) { | |
| 108 OutputStream outStream = connection.getOutputStream(); | |
| 109 outStream.write(postData); | |
| 110 outStream.close(); | |
| 111 } | |
| 112 | |
| 113 // Get response. | |
| 114 int responseCode = connection.getResponseCode(); | |
| 115 if (responseCode != 200) { | |
| 116 events.onHttpError("Non-200 response to " + method + " to URL: " | |
| 117 + url + " : " + connection.getHeaderField(null)); | |
| 118 connection.disconnect(); | |
| 119 return; | |
| 120 } | |
| 121 InputStream responseStream = connection.getInputStream(); | |
| 122 String response = drainStream(responseStream); | |
| 123 responseStream.close(); | |
| 124 connection.disconnect(); | |
| 125 events.onHttpComplete(response); | |
| 126 } catch (SocketTimeoutException e) { | |
| 127 events.onHttpError("HTTP " + method + " to " + url + " timeout"); | |
| 128 } catch (IOException e) { | |
| 129 events.onHttpError("HTTP " + method + " to " + url + " error: " | |
| 130 + e.getMessage()); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 // Return the contents of an InputStream as a String. | |
| 135 private static String drainStream(InputStream in) { | |
| 136 Scanner s = new Scanner(in).useDelimiter("\\A"); | |
| 137 return s.hasNext() ? s.next() : ""; | |
| 138 } | |
| 139 } | |
| OLD | NEW |