package epson.print.ecclient;

import org.apache.commons.lang.CharEncoding;

import java.net.URL;

import epson.common.Constants;
import epson.common.httpclient.IAHttpClient;
import epson.print.Util.EPLog;

public class HttpApache extends HttpAccess {
    private volatile boolean mCanceled = false;
    private IAHttpClient mHttpClient = null;

    public void setDisplay(DebugDisplay debugDisplay) {
        mDisplay = debugDisplay;
    }

    private void dispHttpRes(String str) {
        if (this.mDisplay != null) {
            DebugDisplay debugDisplay = mDisplay;
            debugDisplay.addResText(str + Constants.BREAK_LINE);
        }
        EPLog.v("epson_connect", str);
    }

    private void dispHttpCmd(URL url) {
        if (this.mDisplay != null) {
            DebugDisplay debugDisplay = mDisplay;
            debugDisplay.addResText("=> " + url.toString() + Constants.BREAK_LINE);
        }
        EPLog.v("epson_connect", url.toString());
    }

    public int HttpGet(String str) {
        mResString = null;
        mHttpRetCode = 0;
        dispHttpRes("[get]::" + str);
        try {
            mHttpClient = new IAHttpClient();
            IAHttpClient.HttpResponse execute = mHttpClient.execute(new IAHttpClient.HttpGet(str));
            mResString = execute.getEntity().toString(CharEncoding.UTF_8);
            mHttpRetCode = execute.getResponseCode();
            dispHttpRes("get return :: <" + mHttpRetCode + "> '" + mResString + "'\n");
            return 0;
        } catch (Exception e) {
            EPLog.w("epson_connect", "error in HttpGet: " + e.toString());
            return -1100;
        }
    }

    public int HttpPost(String str, String str2) {
        mResString = null;
        mHttpRetCode = 0;
        dispHttpRes("[post]:: " + str + " :: " + str2);
        try {
            IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(str);
            mHttpClient = new IAHttpClient();
            httpPost.setChunked(false);
            httpPost.setContentType("application/json");
            httpPost.setContentEncoding(CharEncoding.UTF_8);
            httpPost.setEntity(str2.getBytes(CharEncoding.UTF_8));
            IAHttpClient.HttpResponse execute = mHttpClient.execute(httpPost);
            mResString = execute.getEntity().toString(CharEncoding.UTF_8);
            mHttpRetCode = execute.getResponseCode();
            dispHttpRes("post return :: <" + mHttpRetCode + "> '" + mResString + "'\n");
            return 0;
        } catch (Exception e) {
            EPLog.w("epson_connect", "error in HttpPost: " + e.toString());
            return -1100;
        }
    }

    public int PostFile(String str, String str2, String str3, String str4, int i, int i2) {
        mResString = null;
        mHttpRetCode = 0;
        dispHttpRes("[post file]:: " + str + " :: file: <" + str4 + "> offset: <" + i + "> dataSize: <" + i2 + ">");
        if (this.mCanceled) {
            return -2;
        }
        try {
            IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(str);
            httpPost.setEntityFile(str4, i, i2);
            httpPost.setContentType(str3);
            httpPost.setContentLength(Integer.valueOf(i2));
            mHttpClient = new IAHttpClient();
            IAHttpClient.HttpResponse executeFile = mHttpClient.executeFile(httpPost);
            int responseCode = executeFile.getResponseCode();
            if (responseCode != 200) {
                EPLog.w("epson_connect", "error in PostFile: status = <" + responseCode + ">");
                return -1100;
            }
            mResString = executeFile.getEntity().toString(CharEncoding.UTF_8);
            mHttpRetCode = executeFile.getResponseCode();
            dispHttpRes("post file return :: <" + mHttpRetCode + "> '" + mResString + "'\n");
            return 0;
        } catch (Exception e) {
            EPLog.w("epson_connect", "exception in HttpPost: " + e.toString());
            return -1100;
        }
    }

    public int GetFile(String str, String str2) {
        dispHttpRes("[get file]::" + str + " file <" + str2 + ">");
        mResString = null;
        mHttpRetCode = 0;
        if (this.mCanceled) {
            return -2;
        }
        try {
            IAHttpClient iAHttpClient = new IAHttpClient();
            IAHttpClient.HttpGet httpGet = new IAHttpClient.HttpGet(str);
            httpGet.setEntityFile(str2, 0, 0);
            IAHttpClient.HttpResponse executeFile = iAHttpClient.executeFile(httpGet);
            if (200 != executeFile.getResponseCode()) {
                EPLog.w("epson_connect", "error in HttpGet(): ");
                return -1100;
            }
            mHttpRetCode = executeFile.getResponseCode();
            dispHttpRes("getFile return :: <" + mHttpRetCode + ">\n");
            return 0;
        } catch (Exception e) {
            EPLog.w("epson_connect", "error in HttpGet: " + e.toString());
            return -1100;
        }
    }

    public void cancel() {
        EPLog.i("epson_connect", "cancel called()");
        mCanceled = true;
        shutdown();
    }

    public void resetCancel() {
        mCanceled = false;
    }

    private void shutdown() {
        IAHttpClient iAHttpClient = mHttpClient;
        if (iAHttpClient != null) {
            iAHttpClient.disconnect();
        }
    }
}
