package epson.common.httpclient;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import epson.print.CommonDefine;
import epson.print.Util.EPLog;

public class IAHttpClient {
    private static final String CONTENT_ENCODING = "Content-Encoding";
    private static final String CONTENT_LENGTH = "Content-Length";
    private static final String CONTENT_TYPE = "Content-Type";
    private static final String REQUEST_GET = "GET";
    private static final String REQUEST_POST = "POST";
    private static final String TAG = "IAHttpClient";
    private static final String TRANSFER_ENCODING = "Transfer-Encoding";
    private final int BUFFER_SIZE = 1024;
    HttpURLConnection conn = null;
    private Object connLockObj = new Object();
    private int connectionTimeout = 30000;
    private boolean followRedirects = true;
    private int readTimeout = 30000;

    public interface HttpStatus {
        public static final int SC_ACCEPTED = 202;
        public static final int SC_BAD_GATEWAY = 502;
        public static final int SC_BAD_REQUEST = 400;
        public static final int SC_CONFLICT = 409;
        public static final int SC_CONTINUE = 100;
        public static final int SC_CREATED = 201;
        public static final int SC_EXPECTATION_FAILED = 417;
        public static final int SC_FAILED_DEPENDENCY = 424;
        public static final int SC_FORBIDDEN = 403;
        public static final int SC_GATEWAY_TIMEOUT = 504;
        public static final int SC_GONE = 410;
        public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
        public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
        public static final int SC_INSUFFICIENT_STORAGE = 507;
        public static final int SC_INTERNAL_SERVER_ERROR = 500;
        public static final int SC_LENGTH_REQUIRED = 411;
        public static final int SC_LOCKED = 423;
        public static final int SC_METHOD_FAILURE = 420;
        public static final int SC_METHOD_NOT_ALLOWED = 405;
        public static final int SC_MOVED_PERMANENTLY = 301;
        public static final int SC_MOVED_TEMPORARILY = 302;
        public static final int SC_MULTIPLE_CHOICES = 300;
        public static final int SC_MULTI_STATUS = 207;
        public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
        public static final int SC_NOT_ACCEPTABLE = 406;
        public static final int SC_NOT_FOUND = 404;
        public static final int SC_NOT_IMPLEMENTED = 501;
        public static final int SC_NOT_MODIFIED = 304;
        public static final int SC_NO_CONTENT = 204;
        public static final int SC_OK = 200;
        public static final int SC_PARTIAL_CONTENT = 206;
        public static final int SC_PAYMENT_REQUIRED = 402;
        public static final int SC_PRECONDITION_FAILED = 412;
        public static final int SC_PROCESSING = 102;
        public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
        public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
        public static final int SC_REQUEST_TIMEOUT = 408;
        public static final int SC_REQUEST_TOO_LONG = 413;
        public static final int SC_REQUEST_URI_TOO_LONG = 414;
        public static final int SC_RESET_CONTENT = 205;
        public static final int SC_SEE_OTHER = 303;
        public static final int SC_SERVICE_UNAVAILABLE = 503;
        public static final int SC_SWITCHING_PROTOCOLS = 101;
        public static final int SC_TEMPORARY_REDIRECT = 307;
        public static final int SC_UNAUTHORIZED = 401;
        public static final int SC_UNPROCESSABLE_ENTITY = 422;
        public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
        public static final int SC_USE_PROXY = 305;
    }

    public void setConnectionTimeout(int i) {
        connectionTimeout = i;
    }

    public void setSoTimeout(int i) {
        readTimeout = i;
    }

    public void setFollowRedirects(boolean z) {
        followRedirects = z;
    }

    private HttpURLConnection connect(HttpBase httpBase) throws IOException {
        HttpURLConnection httpURLConnection;
        synchronized (this.connLockObj) {
            conn = (HttpURLConnection) httpBase.getURI().openConnection();
            conn.setConnectTimeout(this.connectionTimeout);
            conn.setReadTimeout(this.readTimeout);
            conn.setInstanceFollowRedirects(this.followRedirects);
            httpURLConnection = conn;
        }
        return httpURLConnection;
    }

    public void disconnect() {
        synchronized (this.connLockObj) {
            if (this.conn != null) {
                conn.disconnect();
            }
        }
    }

    public synchronized HttpResponse execute(HttpGet httpGet) throws IOException {
        HttpResponse parseResponse;
        new HttpResponse();
        try {
            conn = connect(httpGet);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            makeHeader(this.conn, httpGet);
            conn.connect();
            parseResponse = parseResponse(this.conn, true);
            disconnect();
        } catch (IOException e) {
            throw e;
        } catch (Throwable th) {
            disconnect();
            throw th;
        }
        return parseResponse;
    }

    public synchronized epson.common.httpclient.IAHttpClient.HttpResponse execute(epson.common.httpclient.IAHttpClient.HttpPost r4) throws java.io.IOException {

        throw new UnsupportedOperationException("Method not decompiled: epson.common.httpclient.IAHttpClient.execute(epson.common.httpclient.IAHttpClient$HttpPost):epson.common.httpclient.IAHttpClient$HttpResponse");
    }

    public epson.common.httpclient.IAHttpClient.HttpResponse executeFile(epson.common.httpclient.IAHttpClient.HttpGet r7) throws java.io.IOException {

        throw new UnsupportedOperationException("Method not decompiled: epson.common.httpclient.IAHttpClient.executeFile(epson.common.httpclient.IAHttpClient$HttpGet):epson.common.httpclient.IAHttpClient$HttpResponse");
    }

    public epson.common.httpclient.IAHttpClient.HttpResponse executeFile(epson.common.httpclient.IAHttpClient.HttpPost r10) throws java.io.IOException {

        throw new UnsupportedOperationException("Method not decompiled: epson.common.httpclient.IAHttpClient.executeFile(epson.common.httpclient.IAHttpClient$HttpPost):epson.common.httpclient.IAHttpClient$HttpResponse");
    }

    private void makeHeader(HttpURLConnection httpURLConnection, HttpBase httpBase) {
        if (httpBase.getContentType() != null) {
            httpURLConnection.setRequestProperty("Content-Type", httpBase.getContentType());
        }
        if (httpBase.getContentEncoding() != null) {
            httpURLConnection.setRequestProperty("Content-Encoding", httpBase.getContentEncoding());
        }
        if (httpBase.getChunked() != null && httpBase.getChunked().equals(true)) {
            httpURLConnection.setRequestProperty("Transfer-Encoding", "chunked");
        }
    }

    private HttpResponse parseResponse(HttpURLConnection httpURLConnection, boolean z) throws IOException {
        InputStream inputStream;
        HttpResponse httpResponse = new HttpResponse();
        httpResponse.setResponseCode(httpURLConnection.getResponseCode());
        httpResponse.setContentLength(Integer.valueOf(httpURLConnection.getContentLength()));
        if (httpURLConnection.getContentEncoding() != null) {
            httpResponse.setContentEncoding(httpURLConnection.getContentEncoding());
        }
        if (httpURLConnection.getContentType() != null) {
            httpResponse.setContentType(httpURLConnection.getContentType());
        }
        EPLog.i(TAG, "ResponseCode = " + httpResponse.getResponseCode() + " Content-Type = " + httpResponse.getContentType() + " Content-Length = " + httpResponse.getContentLength());
        if (z) {
            switch (httpResponse.getResponseCode() / 100) {
                case 1:
                case 2:
                case 3:
                    inputStream = httpURLConnection.getInputStream();
                    break;
                default:
                    inputStream = httpURLConnection.getErrorStream();
                    break;
            }
            if (inputStream != null) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                try {
                    byte[] bArr = new byte[1024];
                    while (true) {
                        int read = inputStream.read(bArr);
                        if (read <= 0) {
                            httpResponse.setEntity(byteArrayOutputStream.toByteArray());
                            try {
                                byteArrayOutputStream.close();
                            } catch (IOException unused) {
                            }
                        } else {
                            byteArrayOutputStream.write(bArr, 0, read);
                        }
                    }
                } catch (FileNotFoundException e) {
                    EPLog.e(TAG, e.getMessage());
                } catch (Throwable th) {
                    try {
                        byteArrayOutputStream.close();
                    } catch (IOException unused2) {
                    }
                    throw th;
                }
            }
        }
        return httpResponse;
    }

    public static class HttpGet extends HttpBase {
        public /* bridge */ /* synthetic */ Boolean getChunked() {
            return super.getChunked();
        }

        public /* bridge */ /* synthetic */ String getContentEncoding() {
            return super.getContentEncoding();
        }

        public /* bridge */ /* synthetic */ Integer getContentLength() {
            return super.getContentLength();
        }

        public /* bridge */ /* synthetic */ String getContentType() {
            return super.getContentType();
        }

        public /* bridge */ /* synthetic */ ByteArrayOutputStream getEntity() throws IOException {
            return super.getEntity();
        }

        public /* bridge */ /* synthetic */ int getEntityFileDataOffset() {
            return super.getEntityFileDataOffset();
        }

        public /* bridge */ /* synthetic */ int getEntityFileDataSize() {
            return super.getEntityFileDataSize();
        }

        public /* bridge */ /* synthetic */ String getEntityFileName() {
            return super.getEntityFileName();
        }

        public /* bridge */ /* synthetic */ URL getURI() {
            return super.getURI();
        }

        public /* bridge */ /* synthetic */ void setChunked(Boolean bool) {
            super.setChunked(bool);
        }

        public /* bridge */ /* synthetic */ void setContentEncoding(String str) {
            super.setContentEncoding(str);
        }

        public /* bridge */ /* synthetic */ void setContentLength(Integer num) {
            super.setContentLength(num);
        }

        public /* bridge */ /* synthetic */ void setContentType(String str) {
            super.setContentType(str);
        }

        public /* bridge */ /* synthetic */ void setEntity(byte[] bArr) {
            super.setEntity(bArr);
        }

        public /* bridge */ /* synthetic */ void setEntityFile(String str, int i, int i2) {
            super.setEntityFile(str, i, i2);
        }

        public HttpGet(String str) {
            super(str);
        }
    }

    public static class HttpPost extends HttpBase {
        public /* bridge */ /* synthetic */ Boolean getChunked() {
            return super.getChunked();
        }

        public /* bridge */ /* synthetic */ String getContentEncoding() {
            return super.getContentEncoding();
        }

        public /* bridge */ /* synthetic */ Integer getContentLength() {
            return super.getContentLength();
        }

        public /* bridge */ /* synthetic */ String getContentType() {
            return super.getContentType();
        }

        public /* bridge */ /* synthetic */ ByteArrayOutputStream getEntity() throws IOException {
            return super.getEntity();
        }

        public /* bridge */ /* synthetic */ int getEntityFileDataOffset() {
            return super.getEntityFileDataOffset();
        }

        public /* bridge */ /* synthetic */ int getEntityFileDataSize() {
            return super.getEntityFileDataSize();
        }

        public /* bridge */ /* synthetic */ String getEntityFileName() {
            return super.getEntityFileName();
        }

        public /* bridge */ /* synthetic */ URL getURI() {
            return super.getURI();
        }

        public /* bridge */ /* synthetic */ void setChunked(Boolean bool) {
            super.setChunked(bool);
        }

        public /* bridge */ /* synthetic */ void setContentEncoding(String str) {
            super.setContentEncoding(str);
        }

        public /* bridge */ /* synthetic */ void setContentLength(Integer num) {
            super.setContentLength(num);
        }

        public /* bridge */ /* synthetic */ void setContentType(String str) {
            super.setContentType(str);
        }

        public /* bridge */ /* synthetic */ void setEntity(byte[] bArr) {
            super.setEntity(bArr);
        }

        public /* bridge */ /* synthetic */ void setEntityFile(String str, int i, int i2) {
            super.setEntityFile(str, i, i2);
        }

        public HttpPost(String str) {
            super(str);
        }

        public static byte[] getUrlEncodedFormEntity(List<BasicNameValuePair> list, String str) throws UnsupportedEncodingException {
            StringBuilder sb = new StringBuilder();
            boolean z = true;
            for (BasicNameValuePair next : list) {
                if (z) {
                    z = false;
                } else {
                    sb.append("&");
                }
                sb.append(URLEncoder.encode(next.getName(), str));
                sb.append(CommonDefine.EQUAL_MARK);
                sb.append(URLEncoder.encode(next.getValue(), str));
            }
            return sb.toString().getBytes(str);
        }
    }

    public static class HttpResponse extends HttpBase {
        private int responseCode = 0;

        public /* bridge */ /* synthetic */ Boolean getChunked() {
            return super.getChunked();
        }

        public /* bridge */ /* synthetic */ String getContentEncoding() {
            return super.getContentEncoding();
        }

        public /* bridge */ /* synthetic */ Integer getContentLength() {
            return super.getContentLength();
        }

        public /* bridge */ /* synthetic */ String getContentType() {
            return super.getContentType();
        }

        public /* bridge */ /* synthetic */ ByteArrayOutputStream getEntity() throws IOException {
            return super.getEntity();
        }

        public /* bridge */ /* synthetic */ int getEntityFileDataOffset() {
            return super.getEntityFileDataOffset();
        }

        public /* bridge */ /* synthetic */ int getEntityFileDataSize() {
            return super.getEntityFileDataSize();
        }

        public /* bridge */ /* synthetic */ String getEntityFileName() {
            return super.getEntityFileName();
        }

        public /* bridge */ /* synthetic */ URL getURI() {
            return super.getURI();
        }

        public /* bridge */ /* synthetic */ void setChunked(Boolean bool) {
            super.setChunked(bool);
        }

        public /* bridge */ /* synthetic */ void setContentEncoding(String str) {
            super.setContentEncoding(str);
        }

        public /* bridge */ /* synthetic */ void setContentLength(Integer num) {
            super.setContentLength(num);
        }

        public /* bridge */ /* synthetic */ void setContentType(String str) {
            super.setContentType(str);
        }

        public /* bridge */ /* synthetic */ void setEntity(byte[] bArr) {
            super.setEntity(bArr);
        }

        public /* bridge */ /* synthetic */ void setEntityFile(String str, int i, int i2) {
            super.setEntityFile(str, i, i2);
        }

        public int getResponseCode() {
            return responseCode;
        }

        public void setResponseCode(int i) {
            responseCode = i;
        }
    }

    static abstract class HttpBase {
        private Boolean chunked = null;
        private String contentEncoding;
        private Integer contentLength = null;
        private String contentType;
        private byte[] entityBuffer;
        private int entityFileDataOffset;
        private int entityFileDataSize;
        private String entityFileName;
        private URL url = null;

        public HttpBase() {
        }

        public HttpBase(String str) {
            try {
                url = new URL(str);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

        public URL getURI() {
            return url;
        }

        public String getContentType() {
            return contentType;
        }

        public void setContentType(String str) {
            contentType = str;
        }

        public String getContentEncoding() {
            return contentEncoding;
        }

        public void setContentEncoding(String str) {
            contentEncoding = str;
        }

        public Boolean getChunked() {
            return chunked;
        }

        public void setChunked(Boolean bool) {
            chunked = bool;
        }

        public Integer getContentLength() {
            return contentLength;
        }

        public void setContentLength(Integer num) {
            contentLength = num;
        }

        public void setEntity(byte[] bArr) {
            entityBuffer = bArr;
        }

        public ByteArrayOutputStream getEntity() throws IOException {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(this.entityBuffer.length);
            byteArrayOutputStream.write(this.entityBuffer);
            return byteArrayOutputStream;
        }

        public String getEntityFileName() {
            return entityFileName;
        }

        public int getEntityFileDataOffset() {
            return entityFileDataOffset;
        }

        public int getEntityFileDataSize() {
            return entityFileDataSize;
        }

        public void setEntityFile(String str, int i, int i2) {
            entityFileName = str;
            entityFileDataOffset = i;
            entityFileDataSize = i2;
        }
    }
}
