package epson.print.ecclient;

import epson.common.Constants;
import epson.common.httpclient.IAHttpClient;
import epson.print.Util.EPLog;
import java.net.URL;
import org.apache.commons.lang.CharEncoding;

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

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

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

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

    public int HttpGet(String str) {
        this.mResString = null;
        this.mHttpRetCode = 0;
        dispHttpRes("[get]::" + str);
        try {
            this.mHttpClient = new IAHttpClient();
            IAHttpClient.HttpResponse execute = this.mHttpClient.execute(new IAHttpClient.HttpGet(str));
            this.mResString = execute.getEntity().toString(CharEncoding.UTF_8);
            this.mHttpRetCode = execute.getResponseCode();
            dispHttpRes("get return :: <" + this.mHttpRetCode + "> '" + this.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) {
        this.mResString = null;
        this.mHttpRetCode = 0;
        dispHttpRes("[post]:: " + str + " :: " + str2);
        try {
            IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(str);
            this.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 = this.mHttpClient.execute(httpPost);
            this.mResString = execute.getEntity().toString(CharEncoding.UTF_8);
            this.mHttpRetCode = execute.getResponseCode();
            dispHttpRes("post return :: <" + this.mHttpRetCode + "> '" + this.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) {
        this.mResString = null;
        this.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));
            this.mHttpClient = new IAHttpClient();
            IAHttpClient.HttpResponse executeFile = this.mHttpClient.executeFile(httpPost);
            int responseCode = executeFile.getResponseCode();
            if (responseCode != 200) {
                EPLog.w("epson_connect", "error in PostFile: status = <" + responseCode + ">");
                return -1100;
            }
            this.mResString = executeFile.getEntity().toString(CharEncoding.UTF_8);
            this.mHttpRetCode = executeFile.getResponseCode();
            dispHttpRes("post file return :: <" + this.mHttpRetCode + "> '" + this.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 + ">");
        this.mResString = null;
        this.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;
            }
            this.mHttpRetCode = executeFile.getResponseCode();
            dispHttpRes("getFile return :: <" + this.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()");
        this.mCanceled = true;
        shutdown();
    }

    public void resetCancel() {
        this.mCanceled = false;
    }

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