package org.scribe.model;

import android.util.Log;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.scribe.exceptions.OAuthException;
import org.scribe.utils.MapUtils;
import org.scribe.utils.URLUtils;

/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/3.dex */
class Request {
    private static final String CONTENT_LENGTH = "Content-Length";
    private static final String CONTENT_TYPE = "Content-Type";
    public static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded";
    private String charset;
    private HttpURLConnection connection;
    private String url;
    private Verb verb;
    private String payload = null;
    private byte[] bytePayload = null;
    private boolean connectionKeepAlive = false;
    private Long connectTimeout = null;
    private Long readTimeout = null;
    private Map<String, String> querystringParams = new HashMap();
    private Map<String, String> bodyParams = new HashMap();
    private Map<String, String> headers = new HashMap();

    public Request(Verb verb, String url) {
        this.verb = verb;
        this.url = url;
    }

    public Response send() {
        try {
            createConnection();
            return doSend();
        } catch (UnknownHostException uhe) {
            throw new OAuthException("Could not reach the desired host. Check your network connection.", uhe);
        } catch (IOException ioe) {
            throw new OAuthException("Problems while creating connection.", ioe);
        }
    }

    private void createConnection() throws IOException {
        String effectiveUrl = URLUtils.appendParametersToQueryString(this.url, this.querystringParams);
        if (this.connection == null) {
            System.setProperty("http.keepAlive", this.connectionKeepAlive ? "true" : "false");
            this.connection = (HttpURLConnection) new URL(effectiveUrl).openConnection();
        }
    }

    Response doSend() throws IOException {
        Log.d("Twitter", "====== Twitter doSend ======");
        this.connection.setRequestMethod(this.verb.name());
        this.connection.setConnectTimeout(300);
        addHeaders(this.connection);
        if (this.verb.equals(Verb.PUT) || this.verb.equals(Verb.POST)) {
            addBody(this.connection, getByteBodyContents());
        }
        return new Response(this.connection);
    }

    void addHeaders(HttpURLConnection conn) {
        for (String key : this.headers.keySet()) {
            conn.setRequestProperty(key, this.headers.get(key));
        }
    }

    void addBody(HttpURLConnection conn, byte[] content) throws IOException {
        conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length));
        if (conn.getRequestProperty(CONTENT_TYPE) == null) {
            conn.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
        }
        conn.setDoOutput(true);
        conn.getOutputStream().write(content);
    }

    public void addHeader(String key, String value) {
        this.headers.put(key, value);
    }

    public void addBodyParameter(String key, String value) {
        this.bodyParams.put(key, value);
    }

    public void addQuerystringParameter(String key, String value) {
        this.querystringParams.put(key, value);
    }

    public void addPayload(String payload) {
        this.payload = payload;
    }

    public void addPayload(byte[] payload) {
        this.bytePayload = payload;
    }

    public Map<String, String> getQueryStringParams() {
        try {
            Map params = new HashMap();
            String queryString = new URL(this.url).getQuery();
            params.putAll(MapUtils.queryStringToMap(queryString));
            params.putAll(this.querystringParams);
            return params;
        } catch (MalformedURLException mue) {
            throw new OAuthException("Malformed URL", mue);
        }
    }

    public Map<String, String> getBodyParams() {
        return this.bodyParams;
    }

    public String getUrl() {
        return this.url;
    }

    public String getSanitizedUrl() {
        return this.url.replaceAll("\\?.*", "").replace("\\:\\d{4}", "");
    }

    public String getBodyContents() {
        try {
            return new String(getByteBodyContents(), getCharset());
        } catch (UnsupportedEncodingException uee) {
            throw new OAuthException("Unsupported Charset: " + this.charset, uee);
        }
    }

    byte[] getByteBodyContents() {
        if (this.bytePayload != null) {
            return this.bytePayload;
        }
        String body = this.payload != null ? this.payload : URLUtils.formURLEncodeMap(this.bodyParams);
        try {
            return body.getBytes(getCharset());
        } catch (UnsupportedEncodingException uee) {
            throw new OAuthException("Unsupported Charset: " + getCharset(), uee);
        }
    }

    public Verb getVerb() {
        return this.verb;
    }

    public Map<String, String> getHeaders() {
        return this.headers;
    }

    public String getCharset() {
        return this.charset == null ? Charset.defaultCharset().name() : this.charset;
    }

    public void setConnectTimeout(int duration, TimeUnit unit) {
        this.connectTimeout = Long.valueOf(unit.toMillis(duration));
    }

    public void setReadTimeout(int duration, TimeUnit unit) {
        this.readTimeout = Long.valueOf(unit.toMillis(duration));
    }

    public void setCharset(String charsetName) {
        this.charset = charsetName;
    }

    public void setConnectionKeepAlive(boolean connectionKeepAlive) {
        this.connectionKeepAlive = connectionKeepAlive;
    }

    void setConnection(HttpURLConnection connection) {
        this.connection = connection;
    }

    public String toString() {
        return String.format("@Request(%s %s)", getVerb(), getUrl());
    }
}
