Newer
Older
DevelopRx / app / src / main / java / org / ntlab / developrx / ObservableHttpConnection.java
package org.ntlab.developrx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

/**
 * Created by matsumoto_k on 2017/11/16.
 */

abstract public class ObservableHttpConnection {
    private HttpURLConnection httpURLConnection = null;
    private String baseUrl = "";
    private String pathParams = "";
    private String formParams = "";
    private String queryParams = "";
    private String method = "";

    private PublishSubject<String> publishSubject = PublishSubject.create();

    public ObservableHttpConnection(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void connection() {
        Observable.just(baseUrl)
                .subscribeOn(Schedulers.computation())
                .flatMap(url -> Observable.just(getHttp(new URL(baseUrl))))
                .subscribe(response -> {
                    publishSubject.onNext(response);
                });
    }

    public String getHttp(URL url) {
        try {
            if (queryParams.isEmpty() || queryParams.isEmpty()) {
                httpURLConnection = (HttpURLConnection) new URL(baseUrl + pathParams).openConnection();
            } else {
                httpURLConnection = (HttpURLConnection) new URL(baseUrl + pathParams + "?" + queryParams).openConnection();
            }
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setConnectTimeout(15000);
            httpURLConnection.setRequestMethod(method);
            if (formParams.length() > 0) {
                httpURLConnection.setDoOutput(true);
                OutputStream out = httpURLConnection.getOutputStream();
                out.write(formParams.getBytes("UTF-8"));
                out.flush();
                out.close();
                formParams = "";
            }
            pathParams = "";
            queryParams = "";
            httpURLConnection.connect();
            return toString(httpURLConnection.getInputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String toString(InputStream inputStream) {
        Reader reader = null;
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return writer.toString();
    }

    public void doGet() {
        method = "GET";
        connection();
    }

    public void doPost() {
        method = "POST";
        connection();
    }

    public void doPut() {
        method = "PUT";
        connection();
    }

    public void doDelete() {
        method = "DELETE";
        connection();
    }

    public void addQueryParam(String key, String value) {
        if (queryParams.length() > 0) {
            queryParams += ("&");
        }
        queryParams += (key + "=" + value);
    }

    public void addFormParam(String key, String value) {
        if (formParams.length() > 0) {
            formParams += "&";
        }
        formParams += (key + "=" + value);
    }

    public void addPathParam(String param) {
        pathParams += "/" + param;
    }

    public PublishSubject<String> getPublishSubject() {
        return publishSubject;
    }
}