|
|
@@ -0,0 +1,141 @@
|
|
|
+package com.knacki.mimou.bridge;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.CharBuffer;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.InvalidParameterException;
|
|
|
+import java.util.logging.Level;
|
|
|
+import java.util.logging.Logger;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by thibal on 1/12/18.
|
|
|
+ */
|
|
|
+public class XHRController {
|
|
|
+ public final Method method;
|
|
|
+ public final String url;
|
|
|
+ protected int timeo = 5;
|
|
|
+ protected String responseType;
|
|
|
+
|
|
|
+ private static final Logger log = Logger.getLogger(XHRController.class.getName());
|
|
|
+
|
|
|
+ public XHRController(String method, String url) throws InvalidParameterException {
|
|
|
+ this.method = parseMethod(method);
|
|
|
+ this.url = url;
|
|
|
+ }
|
|
|
+
|
|
|
+ public XHRController setTimeo(int timeo) {
|
|
|
+ this.timeo = timeo;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public XHRController setResponseType(String respType) {
|
|
|
+ responseType = respType;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void makeRequest(String payload, JsInterface.TypedCallback<Response> callback) {
|
|
|
+ HttpURLConnection con = null;
|
|
|
+
|
|
|
+ Logger.getAnonymousLogger().severe("REQUEST " +this.url);
|
|
|
+ try {
|
|
|
+ con = (HttpURLConnection) (new URL(this.url).openConnection());
|
|
|
+ con.setDoOutput(true);
|
|
|
+ con.setRequestMethod(this.method.str);
|
|
|
+ if (payload != null)
|
|
|
+ con.getOutputStream().write(payload.getBytes());
|
|
|
+ InputStream in = new BufferedInputStream(con.getInputStream());
|
|
|
+ callback.onResult(readStream(con, in));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.log(Level.WARNING, "Cannot " +this.method.str +" " +this.url +": " +e.getMessage(), e);
|
|
|
+ callback.onResult(new Response(e));
|
|
|
+ } finally {
|
|
|
+ if (con != null)
|
|
|
+ con.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Response readStream(HttpURLConnection con, InputStream stream) throws IOException {
|
|
|
+ final int BUFFER_LEN = 10;
|
|
|
+ int readCount;
|
|
|
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ do {
|
|
|
+ byte[] byteRead = new byte[BUFFER_LEN];
|
|
|
+ readCount = stream.read(byteRead);
|
|
|
+ if (readCount > 0)
|
|
|
+ buffer.write(byteRead, 0, readCount);
|
|
|
+ } while (readCount == BUFFER_LEN);
|
|
|
+ return new Response((short) con.getResponseCode(), con.getResponseMessage(), buffer.toString("UTF-8"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Method parseMethod(String s) {
|
|
|
+ s = s.toUpperCase();
|
|
|
+ if (Method.GET.equals(s)) {
|
|
|
+ return Method.GET;
|
|
|
+ }
|
|
|
+ if (Method.POST.equals(s)) {
|
|
|
+ return Method.POST;
|
|
|
+ }
|
|
|
+ if (Method.PUT.equals(s)) {
|
|
|
+ return Method.PUT;
|
|
|
+ }
|
|
|
+ if (Method.DELETE.equals(s)) {
|
|
|
+ return Method.DELETE;
|
|
|
+ }
|
|
|
+ if (Method.HEAD.equals(s)) {
|
|
|
+ return Method.HEAD;
|
|
|
+ }
|
|
|
+ if (Method.OPTIONS.equals(s)) {
|
|
|
+ return Method.OPTIONS;
|
|
|
+ }
|
|
|
+ if (Method.TRACE.equals(s)) {
|
|
|
+ return Method.TRACE;
|
|
|
+ }
|
|
|
+ throw new InvalidParameterException("unknown method: " +s);
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum Method {
|
|
|
+ GET("GET"),
|
|
|
+ POST("POST"),
|
|
|
+ PUT("PUT"),
|
|
|
+ DELETE("DELETE"),
|
|
|
+ HEAD("HEAD"),
|
|
|
+ OPTIONS("OPTIONS"),
|
|
|
+ TRACE("TRACE");
|
|
|
+
|
|
|
+ public final String str;
|
|
|
+
|
|
|
+ Method(String s) {
|
|
|
+ this.str = s;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean equals(String s) {
|
|
|
+ return this.str.equals(s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class Response {
|
|
|
+ public final short statusCode;
|
|
|
+ public final String statusText;
|
|
|
+ public final String response;
|
|
|
+
|
|
|
+ Response(Exception e) {
|
|
|
+ statusCode = 0;
|
|
|
+ statusText = e.getMessage();
|
|
|
+ response = e.getMessage();
|
|
|
+ }
|
|
|
+
|
|
|
+ Response(short statusCode, String statusText, String response) {
|
|
|
+ this.statusCode = statusCode;
|
|
|
+ this.statusText = statusText;
|
|
|
+ this.response = response;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|