EvernoteRequest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.epson.iprint.storage.evernote;
  2. import android.content.Context;
  3. import com.epson.iprint.storage.StorageItem;
  4. import com.epson.iprint.storage.StorageServiceClient;
  5. import com.evernote.client.android.EvernoteSession;
  6. import com.evernote.edam.error.EDAMNotFoundException;
  7. import com.evernote.edam.error.EDAMSystemException;
  8. import com.evernote.edam.error.EDAMUserException;
  9. import com.evernote.edam.notestore.NoteFilter;
  10. import com.evernote.edam.notestore.NoteList;
  11. import com.evernote.edam.type.Data;
  12. import com.evernote.edam.type.Note;
  13. import com.evernote.edam.type.NoteSortOrder;
  14. import com.evernote.edam.type.Notebook;
  15. import com.evernote.edam.type.Resource;
  16. import com.evernote.edam.type.ResourceAttributes;
  17. import com.evernote.thrift.TException;
  18. import com.google.common.primitives.UnsignedBytes;
  19. import epson.common.Utils;
  20. import epson.print.Util.EPLog;
  21. import epson.server.service.EvernoteClient;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.security.MessageDigest;
  29. import java.security.NoSuchAlgorithmException;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.Vector;
  34. public class EvernoteRequest {
  35. private static final String NOTE_PREFIX = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\"><en-note>";
  36. private static final String NOTE_SUFFIX = "</en-note>";
  37. private static final String TAG = "EvernoteRequest";
  38. private static final String URI_FILE_DEMI = "/";
  39. private static final String URI_FILE_FEED = "root";
  40. private Context mContext = null;
  41. public EvernoteRequest(Context context) {
  42. this.mContext = context;
  43. }
  44. public StorageItem getFeedItem() {
  45. StorageItem storageItem = new StorageItem();
  46. storageItem.path = URI_FILE_FEED;
  47. storageItem.type = StorageItem.ItemType.DIRECTORY;
  48. return storageItem;
  49. }
  50. public List<StorageItem> getItemList(StorageItem storageItem) throws IOException, TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {
  51. EvernoteSession evernoteSession = EvernoteClient.getEvernoteSession(this.mContext);
  52. if (storageItem.type == StorageItem.ItemType.DIRECTORY) {
  53. ArrayList arrayList = new ArrayList();
  54. if (URI_FILE_FEED.equals(storageItem.path)) {
  55. for (Notebook next : evernoteSession.getEvernoteClientFactory().getNoteStoreClient().listNotebooks()) {
  56. StorageItem storageItem2 = new StorageItem();
  57. storageItem2.path = next.getGuid();
  58. storageItem2.name = next.getName();
  59. storageItem2.type = StorageItem.ItemType.DIRECTORY;
  60. storageItem2.userInfo = next;
  61. arrayList.add(storageItem2);
  62. }
  63. } else {
  64. NoteFilter noteFilter = new NoteFilter();
  65. noteFilter.setOrder(NoteSortOrder.UPDATED.getValue());
  66. noteFilter.setAscending(false);
  67. noteFilter.setNotebookGuid(storageItem.path);
  68. Map<String, Integer> notebookCounts = evernoteSession.getEvernoteClientFactory().getNoteStoreClient().findNoteCounts(noteFilter, false).getNotebookCounts();
  69. if (notebookCounts != null) {
  70. Integer num = notebookCounts.get(storageItem.path);
  71. if (num == null) {
  72. return new ArrayList();
  73. }
  74. NoteList findNotes = evernoteSession.getEvernoteClientFactory().getNoteStoreClient().findNotes(noteFilter, 0, num.intValue());
  75. if (findNotes == null) {
  76. return new ArrayList();
  77. }
  78. for (Note resources : findNotes.getNotes()) {
  79. List<Resource> resources2 = resources.getResources();
  80. if (resources2 != null) {
  81. for (Resource next2 : resources2) {
  82. if (next2.getAttributes().getFileName() != null) {
  83. StorageItem storageItem3 = new StorageItem();
  84. storageItem3.path = next2.getGuid();
  85. storageItem3.name = next2.getAttributes().getFileName();
  86. storageItem3.type = StorageItem.ItemType.FILE;
  87. storageItem3.userInfo = next2;
  88. arrayList.add(storageItem3);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return arrayList;
  96. }
  97. throw new IOException("Invalid ItemType");
  98. }
  99. private StorageItem getUploadFolder() throws TException, EDAMUserException, EDAMSystemException {
  100. StorageItem storageItem = null;
  101. for (Notebook next : EvernoteClient.getEvernoteSession(this.mContext).getEvernoteClientFactory().getNoteStoreClient().listNotebooks()) {
  102. if ("Epson iPrint".equals(next.getName())) {
  103. storageItem = new StorageItem();
  104. storageItem.path = next.getGuid();
  105. storageItem.name = next.getName();
  106. storageItem.type = StorageItem.ItemType.DIRECTORY;
  107. storageItem.userInfo = next;
  108. }
  109. }
  110. return storageItem;
  111. }
  112. private StorageItem newUploadFolder() throws TException, EDAMUserException, EDAMSystemException {
  113. EvernoteSession evernoteSession = EvernoteClient.getEvernoteSession(this.mContext);
  114. Notebook notebook = new Notebook();
  115. notebook.setName("Epson iPrint");
  116. Notebook createNotebook = evernoteSession.getEvernoteClientFactory().getNoteStoreClient().createNotebook(notebook);
  117. StorageItem storageItem = new StorageItem();
  118. storageItem.path = createNotebook.getGuid();
  119. storageItem.name = createNotebook.getName();
  120. storageItem.type = StorageItem.ItemType.DIRECTORY;
  121. storageItem.userInfo = createNotebook;
  122. return storageItem;
  123. }
  124. class UploadHandler {
  125. private boolean canceled = false;
  126. UploadHandler() {
  127. }
  128. private StorageServiceClient.ProcessError upload(StorageItem storageItem, String str, String str2) throws FileNotFoundException, NoSuchAlgorithmException, TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {
  129. EvernoteSession evernoteSession = EvernoteClient.getEvernoteSession(EvernoteRequest.this.mContext);
  130. File file = new File(str);
  131. Resource resource = new Resource();
  132. resource.setData(EvernoteRequest.readFileAsData(file.getAbsolutePath()));
  133. resource.setMime(Utils.getExtType(Utils.getExtention(file.getAbsolutePath())));
  134. ResourceAttributes resourceAttributes = new ResourceAttributes();
  135. resourceAttributes.setFileName(str2);
  136. resource.setAttributes(resourceAttributes);
  137. Note note = new Note();
  138. note.setTitle(str2);
  139. note.setCreated(System.currentTimeMillis());
  140. note.setUpdated(System.currentTimeMillis());
  141. note.setActive(true);
  142. note.setResourcesIsSet(true);
  143. note.setNotebookGuid(((Notebook) storageItem.userInfo).getGuid());
  144. Vector vector = new Vector();
  145. vector.add(resource);
  146. note.setResources(vector);
  147. note.setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\"><en-note><p>Epson iPrint scan data:</p><en-media type=\"" + resource.getMime() + "\" hash=\"" + EvernoteRequest.bytesToHex(resource.getData().getBodyHash()) + "\"/>" + "</en-note>");
  148. evernoteSession.getEvernoteClientFactory().getNoteStoreClient().createNote(note);
  149. return this.canceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.NONE;
  150. }
  151. private void cancel() {
  152. this.canceled = true;
  153. EPLog.d(EvernoteRequest.TAG, "Upload Canceled!!");
  154. }
  155. }
  156. class DownloadHandler {
  157. private boolean canceled = false;
  158. DownloadHandler() {
  159. }
  160. private StorageServiceClient.ProcessError download(String str, String str2) throws TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException, IOException {
  161. Resource resource = EvernoteClient.getEvernoteSession(EvernoteRequest.this.mContext).getEvernoteClientFactory().getNoteStoreClient().getResource(str, true, false, true, false);
  162. resource.getAttributes().getFileName();
  163. Data data = resource.getData();
  164. FileOutputStream fileOutputStream = new FileOutputStream(new File(str2));
  165. try {
  166. fileOutputStream.write(data.getBody());
  167. try {
  168. fileOutputStream.close();
  169. } catch (IOException unused) {
  170. }
  171. return this.canceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.NONE;
  172. } catch (IOException e) {
  173. throw e;
  174. } catch (Throwable th) {
  175. try {
  176. fileOutputStream.close();
  177. } catch (IOException unused2) {
  178. }
  179. throw th;
  180. }
  181. }
  182. private void cancel() {
  183. this.canceled = true;
  184. }
  185. }
  186. public StorageServiceClient.ProcessError handleException(Exception exc) {
  187. EPLog.e(TAG, "handleException");
  188. String message = exc.getMessage();
  189. if (message == null && exc.getCause() != null) {
  190. message = exc.getCause().getMessage();
  191. }
  192. if (message != null) {
  193. EPLog.e(TAG, message);
  194. }
  195. EvernoteClient.logout(this.mContext);
  196. return StorageServiceClient.ProcessError.ERROR;
  197. }
  198. private UploadHandler getUploadHandler() {
  199. return new UploadHandler();
  200. }
  201. private DownloadHandler getDownloadHandler() {
  202. return new DownloadHandler();
  203. }
  204. private static Data readFileAsData(String str) throws FileNotFoundException, NoSuchAlgorithmException {
  205. FileInputStream fileInputStream = new FileInputStream(str);
  206. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  207. byte[] bArr = new byte[10240];
  208. while (true) {
  209. try {
  210. int read = fileInputStream.read(bArr);
  211. if (read >= 0) {
  212. byteArrayOutputStream.write(bArr, 0, read);
  213. }
  214. } catch (IOException e) {
  215. e.printStackTrace();
  216. } catch (Throwable th) {
  217. try {
  218. fileInputStream.close();
  219. } catch (IOException unused) {
  220. }
  221. throw th;
  222. }
  223. try {
  224. break;
  225. } catch (IOException unused2) {
  226. }
  227. }
  228. fileInputStream.close();
  229. byte[] byteArray = byteArrayOutputStream.toByteArray();
  230. Data data = new Data();
  231. data.setSize(byteArray.length);
  232. data.setBodyHash(MessageDigest.getInstance("MD5").digest(byteArray));
  233. data.setBody(byteArray);
  234. return data;
  235. }
  236. private static String bytesToHex(byte[] bArr) {
  237. StringBuilder sb = new StringBuilder();
  238. for (byte b : bArr) {
  239. byte b2 = b & UnsignedBytes.MAX_VALUE;
  240. if (b2 < 16) {
  241. sb.append('0');
  242. }
  243. sb.append(Integer.toHexString(b2));
  244. }
  245. return sb.toString();
  246. }
  247. }