All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ultreia.java4all.http.HResponse Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
package io.ultreia.java4all.http;

/*-
 * #%L
 * Http :: Api
 * %%
 * Copyright (C) 2017 - 2020 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * Created by tchemit on 14/04/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class HResponse implements Closeable {

    private final HttpRequestBase httpRequestBase;
    private final HttpResponse response;
    private final Integer statusCode;

    private final String responseAsString;

    private final Gson gson;
    private final Header[] responseHeaders;

    HResponse(HttpRequestBase httpRequestBase, HttpResponse response, Integer statusCode, String responseAsString, Gson gson, Header... responseHeaders) {
        this.httpRequestBase = httpRequestBase;
        this.response = response;
        this.statusCode = statusCode;
        this.responseAsString = responseAsString;
        this.gson = gson;
        this.responseHeaders = responseHeaders;
    }

    public HttpResponse getResponse() {
        return response;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    @Override
    public String toString() {
        return responseAsString;
    }

    public Header[] getResponseHeaders() {
        return responseHeaders;
    }

    @Override
    public void close() throws IOException {

        try {
            if (response != null && response.getEntity() != null) {
                response.getEntity().getContent().close();
            }
        } finally {

            // Release the connection.
            httpRequestBase.releaseConnection();

        }

    }

    public Document toHtml() {
        return Jsoup.parse(responseAsString);
    }

    public int toInt() {
        return Integer.parseInt(responseAsString.trim().replaceAll("\"", ""));
    }

    public long toLong() {
        return Long.parseLong(responseAsString.trim().replaceAll("\"", ""));
    }

    public boolean toBoolean() {
        return Boolean.parseBoolean(responseAsString.trim().replaceAll("\"", ""));
    }

    public float toFloat() {
        return Float.parseFloat(responseAsString.trim().replaceAll("\"", ""));
    }

    public double toDouble() {
        return Double.parseDouble(responseAsString.trim().replaceAll("\"", ""));
    }

    public Map toJson() {
        return toJson(new TypeToken>(){}.getType());
    }

    public  T toJson(Type type) {
        return gson.fromJson(responseAsString, type);
    }

    public  Optional toOptional(Class type) {
        return toJson(token(Optional.class, type));
    }

    public  Set toSet(Class type) {
        return toJson(token(Set.class, type));
    }

    public  ImmutableSet toImmutableSet(Class type) {
        return ImmutableSet.copyOf(toSet(type));
    }

    public  ImmutableList toImmutableList(Class type) {
        return ImmutableList.copyOf(toList(type));
    }

    public  List toList(Class type) {
        return toJson(token(List.class, type));
    }

    private Type token(Class main, Class type) {
        return TypeToken.getParameterized(main, type).getType();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy