IAHttpClient.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. package epson.common.httpclient;
  2. import epson.print.CommonDefine;
  3. import epson.print.Util.EPLog;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.net.URLEncoder;
  13. import java.util.List;
  14. public class IAHttpClient {
  15. private static final String CONTENT_ENCODING = "Content-Encoding";
  16. private static final String CONTENT_LENGTH = "Content-Length";
  17. private static final String CONTENT_TYPE = "Content-Type";
  18. private static final String REQUEST_GET = "GET";
  19. private static final String REQUEST_POST = "POST";
  20. private static final String TAG = "IAHttpClient";
  21. private static final String TRANSFER_ENCODING = "Transfer-Encoding";
  22. private final int BUFFER_SIZE = 1024;
  23. HttpURLConnection conn = null;
  24. private Object connLockObj = new Object();
  25. private int connectionTimeout = 30000;
  26. private boolean followRedirects = true;
  27. private int readTimeout = 30000;
  28. public interface HttpStatus {
  29. public static final int SC_ACCEPTED = 202;
  30. public static final int SC_BAD_GATEWAY = 502;
  31. public static final int SC_BAD_REQUEST = 400;
  32. public static final int SC_CONFLICT = 409;
  33. public static final int SC_CONTINUE = 100;
  34. public static final int SC_CREATED = 201;
  35. public static final int SC_EXPECTATION_FAILED = 417;
  36. public static final int SC_FAILED_DEPENDENCY = 424;
  37. public static final int SC_FORBIDDEN = 403;
  38. public static final int SC_GATEWAY_TIMEOUT = 504;
  39. public static final int SC_GONE = 410;
  40. public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
  41. public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
  42. public static final int SC_INSUFFICIENT_STORAGE = 507;
  43. public static final int SC_INTERNAL_SERVER_ERROR = 500;
  44. public static final int SC_LENGTH_REQUIRED = 411;
  45. public static final int SC_LOCKED = 423;
  46. public static final int SC_METHOD_FAILURE = 420;
  47. public static final int SC_METHOD_NOT_ALLOWED = 405;
  48. public static final int SC_MOVED_PERMANENTLY = 301;
  49. public static final int SC_MOVED_TEMPORARILY = 302;
  50. public static final int SC_MULTIPLE_CHOICES = 300;
  51. public static final int SC_MULTI_STATUS = 207;
  52. public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
  53. public static final int SC_NOT_ACCEPTABLE = 406;
  54. public static final int SC_NOT_FOUND = 404;
  55. public static final int SC_NOT_IMPLEMENTED = 501;
  56. public static final int SC_NOT_MODIFIED = 304;
  57. public static final int SC_NO_CONTENT = 204;
  58. public static final int SC_OK = 200;
  59. public static final int SC_PARTIAL_CONTENT = 206;
  60. public static final int SC_PAYMENT_REQUIRED = 402;
  61. public static final int SC_PRECONDITION_FAILED = 412;
  62. public static final int SC_PROCESSING = 102;
  63. public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
  64. public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
  65. public static final int SC_REQUEST_TIMEOUT = 408;
  66. public static final int SC_REQUEST_TOO_LONG = 413;
  67. public static final int SC_REQUEST_URI_TOO_LONG = 414;
  68. public static final int SC_RESET_CONTENT = 205;
  69. public static final int SC_SEE_OTHER = 303;
  70. public static final int SC_SERVICE_UNAVAILABLE = 503;
  71. public static final int SC_SWITCHING_PROTOCOLS = 101;
  72. public static final int SC_TEMPORARY_REDIRECT = 307;
  73. public static final int SC_UNAUTHORIZED = 401;
  74. public static final int SC_UNPROCESSABLE_ENTITY = 422;
  75. public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
  76. public static final int SC_USE_PROXY = 305;
  77. }
  78. public void setConnectionTimeout(int i) {
  79. connectionTimeout = i;
  80. }
  81. public void setSoTimeout(int i) {
  82. readTimeout = i;
  83. }
  84. public void setFollowRedirects(boolean z) {
  85. followRedirects = z;
  86. }
  87. private HttpURLConnection connect(HttpBase httpBase) throws IOException {
  88. HttpURLConnection httpURLConnection;
  89. synchronized (this.connLockObj) {
  90. conn = (HttpURLConnection) httpBase.getURI().openConnection();
  91. conn.setConnectTimeout(this.connectionTimeout);
  92. conn.setReadTimeout(this.readTimeout);
  93. conn.setInstanceFollowRedirects(this.followRedirects);
  94. httpURLConnection = conn;
  95. }
  96. return httpURLConnection;
  97. }
  98. public void disconnect() {
  99. synchronized (this.connLockObj) {
  100. if (this.conn != null) {
  101. conn.disconnect();
  102. }
  103. }
  104. }
  105. public synchronized HttpResponse execute(HttpGet httpGet) throws IOException {
  106. HttpResponse parseResponse;
  107. new HttpResponse();
  108. try {
  109. conn = connect(httpGet);
  110. conn.setRequestMethod("GET");
  111. conn.setDoInput(true);
  112. makeHeader(this.conn, httpGet);
  113. conn.connect();
  114. parseResponse = parseResponse(this.conn, true);
  115. disconnect();
  116. } catch (IOException e) {
  117. throw e;
  118. } catch (Throwable th) {
  119. disconnect();
  120. throw th;
  121. }
  122. return parseResponse;
  123. }
  124. public synchronized epson.common.httpclient.IAHttpClient.HttpResponse execute(epson.common.httpclient.IAHttpClient.HttpPost r4) throws java.io.IOException {
  125. throw new UnsupportedOperationException("Method not decompiled: epson.common.httpclient.IAHttpClient.execute(epson.common.httpclient.IAHttpClient$HttpPost):epson.common.httpclient.IAHttpClient$HttpResponse");
  126. }
  127. public epson.common.httpclient.IAHttpClient.HttpResponse executeFile(epson.common.httpclient.IAHttpClient.HttpGet r7) throws java.io.IOException {
  128. throw new UnsupportedOperationException("Method not decompiled: epson.common.httpclient.IAHttpClient.executeFile(epson.common.httpclient.IAHttpClient$HttpGet):epson.common.httpclient.IAHttpClient$HttpResponse");
  129. }
  130. public epson.common.httpclient.IAHttpClient.HttpResponse executeFile(epson.common.httpclient.IAHttpClient.HttpPost r10) throws java.io.IOException {
  131. throw new UnsupportedOperationException("Method not decompiled: epson.common.httpclient.IAHttpClient.executeFile(epson.common.httpclient.IAHttpClient$HttpPost):epson.common.httpclient.IAHttpClient$HttpResponse");
  132. }
  133. private void makeHeader(HttpURLConnection httpURLConnection, HttpBase httpBase) {
  134. if (httpBase.getContentType() != null) {
  135. httpURLConnection.setRequestProperty("Content-Type", httpBase.getContentType());
  136. }
  137. if (httpBase.getContentEncoding() != null) {
  138. httpURLConnection.setRequestProperty("Content-Encoding", httpBase.getContentEncoding());
  139. }
  140. if (httpBase.getChunked() != null && httpBase.getChunked().equals(true)) {
  141. httpURLConnection.setRequestProperty("Transfer-Encoding", "chunked");
  142. }
  143. }
  144. private HttpResponse parseResponse(HttpURLConnection httpURLConnection, boolean z) throws IOException {
  145. InputStream inputStream;
  146. HttpResponse httpResponse = new HttpResponse();
  147. httpResponse.setResponseCode(httpURLConnection.getResponseCode());
  148. httpResponse.setContentLength(Integer.valueOf(httpURLConnection.getContentLength()));
  149. if (httpURLConnection.getContentEncoding() != null) {
  150. httpResponse.setContentEncoding(httpURLConnection.getContentEncoding());
  151. }
  152. if (httpURLConnection.getContentType() != null) {
  153. httpResponse.setContentType(httpURLConnection.getContentType());
  154. }
  155. EPLog.i(TAG, "ResponseCode = " + httpResponse.getResponseCode() + " Content-Type = " + httpResponse.getContentType() + " Content-Length = " + httpResponse.getContentLength());
  156. if (z) {
  157. switch (httpResponse.getResponseCode() / 100) {
  158. case 1:
  159. case 2:
  160. case 3:
  161. inputStream = httpURLConnection.getInputStream();
  162. break;
  163. default:
  164. inputStream = httpURLConnection.getErrorStream();
  165. break;
  166. }
  167. if (inputStream != null) {
  168. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  169. try {
  170. byte[] bArr = new byte[1024];
  171. while (true) {
  172. int read = inputStream.read(bArr);
  173. if (read <= 0) {
  174. httpResponse.setEntity(byteArrayOutputStream.toByteArray());
  175. try {
  176. byteArrayOutputStream.close();
  177. } catch (IOException unused) {
  178. }
  179. } else {
  180. byteArrayOutputStream.write(bArr, 0, read);
  181. }
  182. }
  183. } catch (FileNotFoundException e) {
  184. EPLog.m307e(TAG, e.getMessage());
  185. } catch (Throwable th) {
  186. try {
  187. byteArrayOutputStream.close();
  188. } catch (IOException unused2) {
  189. }
  190. throw th;
  191. }
  192. }
  193. }
  194. return httpResponse;
  195. }
  196. public static class HttpGet extends HttpBase {
  197. public /* bridge */ /* synthetic */ Boolean getChunked() {
  198. return super.getChunked();
  199. }
  200. public /* bridge */ /* synthetic */ String getContentEncoding() {
  201. return super.getContentEncoding();
  202. }
  203. public /* bridge */ /* synthetic */ Integer getContentLength() {
  204. return super.getContentLength();
  205. }
  206. public /* bridge */ /* synthetic */ String getContentType() {
  207. return super.getContentType();
  208. }
  209. public /* bridge */ /* synthetic */ ByteArrayOutputStream getEntity() throws IOException {
  210. return super.getEntity();
  211. }
  212. public /* bridge */ /* synthetic */ int getEntityFileDataOffset() {
  213. return super.getEntityFileDataOffset();
  214. }
  215. public /* bridge */ /* synthetic */ int getEntityFileDataSize() {
  216. return super.getEntityFileDataSize();
  217. }
  218. public /* bridge */ /* synthetic */ String getEntityFileName() {
  219. return super.getEntityFileName();
  220. }
  221. public /* bridge */ /* synthetic */ URL getURI() {
  222. return super.getURI();
  223. }
  224. public /* bridge */ /* synthetic */ void setChunked(Boolean bool) {
  225. super.setChunked(bool);
  226. }
  227. public /* bridge */ /* synthetic */ void setContentEncoding(String str) {
  228. super.setContentEncoding(str);
  229. }
  230. public /* bridge */ /* synthetic */ void setContentLength(Integer num) {
  231. super.setContentLength(num);
  232. }
  233. public /* bridge */ /* synthetic */ void setContentType(String str) {
  234. super.setContentType(str);
  235. }
  236. public /* bridge */ /* synthetic */ void setEntity(byte[] bArr) {
  237. super.setEntity(bArr);
  238. }
  239. public /* bridge */ /* synthetic */ void setEntityFile(String str, int i, int i2) {
  240. super.setEntityFile(str, i, i2);
  241. }
  242. public HttpGet(String str) {
  243. super(str);
  244. }
  245. }
  246. public static class HttpPost extends HttpBase {
  247. public /* bridge */ /* synthetic */ Boolean getChunked() {
  248. return super.getChunked();
  249. }
  250. public /* bridge */ /* synthetic */ String getContentEncoding() {
  251. return super.getContentEncoding();
  252. }
  253. public /* bridge */ /* synthetic */ Integer getContentLength() {
  254. return super.getContentLength();
  255. }
  256. public /* bridge */ /* synthetic */ String getContentType() {
  257. return super.getContentType();
  258. }
  259. public /* bridge */ /* synthetic */ ByteArrayOutputStream getEntity() throws IOException {
  260. return super.getEntity();
  261. }
  262. public /* bridge */ /* synthetic */ int getEntityFileDataOffset() {
  263. return super.getEntityFileDataOffset();
  264. }
  265. public /* bridge */ /* synthetic */ int getEntityFileDataSize() {
  266. return super.getEntityFileDataSize();
  267. }
  268. public /* bridge */ /* synthetic */ String getEntityFileName() {
  269. return super.getEntityFileName();
  270. }
  271. public /* bridge */ /* synthetic */ URL getURI() {
  272. return super.getURI();
  273. }
  274. public /* bridge */ /* synthetic */ void setChunked(Boolean bool) {
  275. super.setChunked(bool);
  276. }
  277. public /* bridge */ /* synthetic */ void setContentEncoding(String str) {
  278. super.setContentEncoding(str);
  279. }
  280. public /* bridge */ /* synthetic */ void setContentLength(Integer num) {
  281. super.setContentLength(num);
  282. }
  283. public /* bridge */ /* synthetic */ void setContentType(String str) {
  284. super.setContentType(str);
  285. }
  286. public /* bridge */ /* synthetic */ void setEntity(byte[] bArr) {
  287. super.setEntity(bArr);
  288. }
  289. public /* bridge */ /* synthetic */ void setEntityFile(String str, int i, int i2) {
  290. super.setEntityFile(str, i, i2);
  291. }
  292. public HttpPost(String str) {
  293. super(str);
  294. }
  295. public static byte[] getUrlEncodedFormEntity(List<BasicNameValuePair> list, String str) throws UnsupportedEncodingException {
  296. StringBuilder sb = new StringBuilder();
  297. boolean z = true;
  298. for (BasicNameValuePair next : list) {
  299. if (z) {
  300. z = false;
  301. } else {
  302. sb.append("&");
  303. }
  304. sb.append(URLEncoder.encode(next.getName(), str));
  305. sb.append(CommonDefine.EQUAL_MARK);
  306. sb.append(URLEncoder.encode(next.getValue(), str));
  307. }
  308. return sb.toString().getBytes(str);
  309. }
  310. }
  311. public static class HttpResponse extends HttpBase {
  312. private int responseCode = 0;
  313. public /* bridge */ /* synthetic */ Boolean getChunked() {
  314. return super.getChunked();
  315. }
  316. public /* bridge */ /* synthetic */ String getContentEncoding() {
  317. return super.getContentEncoding();
  318. }
  319. public /* bridge */ /* synthetic */ Integer getContentLength() {
  320. return super.getContentLength();
  321. }
  322. public /* bridge */ /* synthetic */ String getContentType() {
  323. return super.getContentType();
  324. }
  325. public /* bridge */ /* synthetic */ ByteArrayOutputStream getEntity() throws IOException {
  326. return super.getEntity();
  327. }
  328. public /* bridge */ /* synthetic */ int getEntityFileDataOffset() {
  329. return super.getEntityFileDataOffset();
  330. }
  331. public /* bridge */ /* synthetic */ int getEntityFileDataSize() {
  332. return super.getEntityFileDataSize();
  333. }
  334. public /* bridge */ /* synthetic */ String getEntityFileName() {
  335. return super.getEntityFileName();
  336. }
  337. public /* bridge */ /* synthetic */ URL getURI() {
  338. return super.getURI();
  339. }
  340. public /* bridge */ /* synthetic */ void setChunked(Boolean bool) {
  341. super.setChunked(bool);
  342. }
  343. public /* bridge */ /* synthetic */ void setContentEncoding(String str) {
  344. super.setContentEncoding(str);
  345. }
  346. public /* bridge */ /* synthetic */ void setContentLength(Integer num) {
  347. super.setContentLength(num);
  348. }
  349. public /* bridge */ /* synthetic */ void setContentType(String str) {
  350. super.setContentType(str);
  351. }
  352. public /* bridge */ /* synthetic */ void setEntity(byte[] bArr) {
  353. super.setEntity(bArr);
  354. }
  355. public /* bridge */ /* synthetic */ void setEntityFile(String str, int i, int i2) {
  356. super.setEntityFile(str, i, i2);
  357. }
  358. public int getResponseCode() {
  359. return responseCode;
  360. }
  361. public void setResponseCode(int i) {
  362. responseCode = i;
  363. }
  364. }
  365. static abstract class HttpBase {
  366. private Boolean chunked = null;
  367. private String contentEncoding;
  368. private Integer contentLength = null;
  369. private String contentType;
  370. private byte[] entityBuffer;
  371. private int entityFileDataOffset;
  372. private int entityFileDataSize;
  373. private String entityFileName;
  374. private URL url = null;
  375. public HttpBase() {
  376. }
  377. public HttpBase(String str) {
  378. try {
  379. url = new URL(str);
  380. } catch (MalformedURLException e) {
  381. e.printStackTrace();
  382. }
  383. }
  384. public URL getURI() {
  385. return url;
  386. }
  387. public String getContentType() {
  388. return contentType;
  389. }
  390. public void setContentType(String str) {
  391. contentType = str;
  392. }
  393. public String getContentEncoding() {
  394. return contentEncoding;
  395. }
  396. public void setContentEncoding(String str) {
  397. contentEncoding = str;
  398. }
  399. public Boolean getChunked() {
  400. return chunked;
  401. }
  402. public void setChunked(Boolean bool) {
  403. chunked = bool;
  404. }
  405. public Integer getContentLength() {
  406. return contentLength;
  407. }
  408. public void setContentLength(Integer num) {
  409. contentLength = num;
  410. }
  411. public void setEntity(byte[] bArr) {
  412. entityBuffer = bArr;
  413. }
  414. public ByteArrayOutputStream getEntity() throws IOException {
  415. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(this.entityBuffer.length);
  416. byteArrayOutputStream.write(this.entityBuffer);
  417. return byteArrayOutputStream;
  418. }
  419. public String getEntityFileName() {
  420. return entityFileName;
  421. }
  422. public int getEntityFileDataOffset() {
  423. return entityFileDataOffset;
  424. }
  425. public int getEntityFileDataSize() {
  426. return entityFileDataSize;
  427. }
  428. public void setEntityFile(String str, int i, int i2) {
  429. entityFileName = str;
  430. entityFileDataOffset = i;
  431. entityFileDataSize = i2;
  432. }
  433. }
  434. }