HttpApache.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package epson.print.ecclient;
  2. import epson.common.Constants;
  3. import epson.common.httpclient.IAHttpClient;
  4. import epson.print.Util.EPLog;
  5. import java.net.URL;
  6. import org.apache.commons.lang.CharEncoding;
  7. public class HttpApache extends HttpAccess {
  8. private volatile boolean mCanceled = false;
  9. private IAHttpClient mHttpClient = null;
  10. public void setDisplay(DebugDisplay debugDisplay) {
  11. this.mDisplay = debugDisplay;
  12. }
  13. private void dispHttpRes(String str) {
  14. if (this.mDisplay != null) {
  15. DebugDisplay debugDisplay = this.mDisplay;
  16. debugDisplay.addResText(str + Constants.BREAK_LINE);
  17. }
  18. EPLog.m316v("epson_connect", str);
  19. }
  20. private void dispHttpCmd(URL url) {
  21. if (this.mDisplay != null) {
  22. DebugDisplay debugDisplay = this.mDisplay;
  23. debugDisplay.addResText("=> " + url.toString() + Constants.BREAK_LINE);
  24. }
  25. EPLog.m316v("epson_connect", url.toString());
  26. }
  27. public int HttpGet(String str) {
  28. this.mResString = null;
  29. this.mHttpRetCode = 0;
  30. dispHttpRes("[get]::" + str);
  31. try {
  32. this.mHttpClient = new IAHttpClient();
  33. IAHttpClient.HttpResponse execute = this.mHttpClient.execute(new IAHttpClient.HttpGet(str));
  34. this.mResString = execute.getEntity().toString(CharEncoding.UTF_8);
  35. this.mHttpRetCode = execute.getResponseCode();
  36. dispHttpRes("get return :: <" + this.mHttpRetCode + "> '" + this.mResString + "'\n");
  37. return 0;
  38. } catch (Exception e) {
  39. EPLog.w("epson_connect", "error in HttpGet: " + e.toString());
  40. return -1100;
  41. }
  42. }
  43. public int HttpPost(String str, String str2) {
  44. this.mResString = null;
  45. this.mHttpRetCode = 0;
  46. dispHttpRes("[post]:: " + str + " :: " + str2);
  47. try {
  48. IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(str);
  49. this.mHttpClient = new IAHttpClient();
  50. httpPost.setChunked(false);
  51. httpPost.setContentType("application/json");
  52. httpPost.setContentEncoding(CharEncoding.UTF_8);
  53. httpPost.setEntity(str2.getBytes(CharEncoding.UTF_8));
  54. IAHttpClient.HttpResponse execute = this.mHttpClient.execute(httpPost);
  55. this.mResString = execute.getEntity().toString(CharEncoding.UTF_8);
  56. this.mHttpRetCode = execute.getResponseCode();
  57. dispHttpRes("post return :: <" + this.mHttpRetCode + "> '" + this.mResString + "'\n");
  58. return 0;
  59. } catch (Exception e) {
  60. EPLog.w("epson_connect", "error in HttpPost: " + e.toString());
  61. return -1100;
  62. }
  63. }
  64. public int PostFile(String str, String str2, String str3, String str4, int i, int i2) {
  65. this.mResString = null;
  66. this.mHttpRetCode = 0;
  67. dispHttpRes("[post file]:: " + str + " :: file: <" + str4 + "> offset: <" + i + "> dataSize: <" + i2 + ">");
  68. if (this.mCanceled) {
  69. return -2;
  70. }
  71. try {
  72. IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(str);
  73. httpPost.setEntityFile(str4, i, i2);
  74. httpPost.setContentType(str3);
  75. httpPost.setContentLength(Integer.valueOf(i2));
  76. this.mHttpClient = new IAHttpClient();
  77. IAHttpClient.HttpResponse executeFile = this.mHttpClient.executeFile(httpPost);
  78. int responseCode = executeFile.getResponseCode();
  79. if (responseCode != 200) {
  80. EPLog.w("epson_connect", "error in PostFile: status = <" + responseCode + ">");
  81. return -1100;
  82. }
  83. this.mResString = executeFile.getEntity().toString(CharEncoding.UTF_8);
  84. this.mHttpRetCode = executeFile.getResponseCode();
  85. dispHttpRes("post file return :: <" + this.mHttpRetCode + "> '" + this.mResString + "'\n");
  86. return 0;
  87. } catch (Exception e) {
  88. EPLog.w("epson_connect", "exception in HttpPost: " + e.toString());
  89. return -1100;
  90. }
  91. }
  92. public int GetFile(String str, String str2) {
  93. dispHttpRes("[get file]::" + str + " file <" + str2 + ">");
  94. this.mResString = null;
  95. this.mHttpRetCode = 0;
  96. if (this.mCanceled) {
  97. return -2;
  98. }
  99. try {
  100. IAHttpClient iAHttpClient = new IAHttpClient();
  101. IAHttpClient.HttpGet httpGet = new IAHttpClient.HttpGet(str);
  102. httpGet.setEntityFile(str2, 0, 0);
  103. IAHttpClient.HttpResponse executeFile = iAHttpClient.executeFile(httpGet);
  104. if (200 != executeFile.getResponseCode()) {
  105. EPLog.w("epson_connect", "error in HttpGet(): ");
  106. return -1100;
  107. }
  108. this.mHttpRetCode = executeFile.getResponseCode();
  109. dispHttpRes("getFile return :: <" + this.mHttpRetCode + ">\n");
  110. return 0;
  111. } catch (Exception e) {
  112. EPLog.w("epson_connect", "error in HttpGet: " + e.toString());
  113. return -1100;
  114. }
  115. }
  116. public void cancel() {
  117. EPLog.i("epson_connect", "cancel called()");
  118. this.mCanceled = true;
  119. shutdown();
  120. }
  121. public void resetCancel() {
  122. this.mCanceled = false;
  123. }
  124. private void shutdown() {
  125. IAHttpClient iAHttpClient = this.mHttpClient;
  126. if (iAHttpClient != null) {
  127. iAHttpClient.disconnect();
  128. }
  129. }
  130. }