Search in sources :

Example 1 with TException

use of com.evernote.thrift.TException in project Notes by lguipeng.

the class EvernoteSearchHelper method findAllNotes.

protected List<NotesMetadataList> findAllNotes(Search search, EvernoteNoteStoreClient client, NoteFilter filter) throws Exception {
    List<NotesMetadataList> result = new ArrayList<>();
    final int maxNotes = search.getMaxNotes();
    int offset = search.getOffset();
    int remaining = maxNotes - offset;
    while (remaining > 0) {
        try {
            NotesMetadataList notesMetadata = client.findNotesMetadata(filter, offset, maxNotes, search.getResultSpec());
            remaining = notesMetadata.getTotalNotes() - (notesMetadata.getStartIndex() + notesMetadata.getNotesSize());
            result.add(notesMetadata);
        } catch (EDAMUserException | EDAMSystemException | TException | EDAMNotFoundException e) {
            maybeRethrow(search, e);
            remaining -= search.getPageSize();
        }
        offset += search.getPageSize();
    }
    return result;
}
Also used : TException(com.evernote.thrift.TException) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) ArrayList(java.util.ArrayList) NotesMetadataList(com.evernote.edam.notestore.NotesMetadataList) EDAMUserException(com.evernote.edam.error.EDAMUserException)

Example 2 with TException

use of com.evernote.thrift.TException in project PhotoNoter by yydcdut.

the class RxUser method updateNote2Evernote.

public Observable<Boolean> updateNote2Evernote(String bigPhotoPathWithoutFile, String photoName, String noteTitle, String noteContent) {
    return Observable.create(new Observable.OnSubscribe<List<Notebook>>() {

        @Override
        public void call(Subscriber<? super List<Notebook>> subscriber) {
            if (mEvernoteSession == null) {
                mEvernoteSession = new EvernoteSession.Builder(mContext).setLocale(Locale.SIMPLIFIED_CHINESE).setEvernoteService(EVERNOTE_SERVICE).setSupportAppLinkedNotebooks(SUPPORT_APP_LINKED_NOTEBOOKS).setForceAuthenticationInThirdPartyApp(true).build(BuildConfig.EVERNOTE_CONSUMER_KEY, BuildConfig.EVERNOTE_CONSUMER_SECRET).asSingleton();
            }
            EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
            try {
                List<Notebook> notebookList = noteStoreClient.listNotebooks();
                subscriber.onNext(notebookList);
            } catch (EDAMUserException e) {
                YLog.e(e);
                subscriber.onError(e);
            } catch (EDAMSystemException e) {
                YLog.e(e);
                subscriber.onError(e);
            } catch (TException e) {
                YLog.e(e);
                subscriber.onError(e);
            }
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io()).flatMap(notebooks -> Observable.from(notebooks)).filter(notebook -> notebook.getName().equals(mContext.getResources().getString(R.string.app_name))).lift(new Observable.Operator<String, Notebook>() {

        @Override
        public Subscriber<? super Notebook> call(Subscriber<? super String> subscriber) {
            return new Subscriber<Notebook>() {

                private int mInTimes = 0;

                @Override
                public void onCompleted() {
                    if (mInTimes == 0) {
                        Notebook notebook = new Notebook();
                        notebook.setName(mContext.getResources().getString(R.string.app_name));
                        EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
                        try {
                            Notebook appNoteBook = noteStoreClient.createNotebook(notebook);
                            subscriber.onNext(appNoteBook.getGuid());
                        } catch (EDAMUserException e) {
                            YLog.e(e);
                            subscriber.onError(e);
                        } catch (EDAMSystemException e) {
                            YLog.e(e);
                            subscriber.onError(e);
                        } catch (TException e) {
                            YLog.e(e);
                            subscriber.onError(e);
                        }
                    }
                    subscriber.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(Notebook notebook) {
                    mInTimes++;
                    subscriber.onNext(notebook.getGuid());
                }
            };
        }
    }).map(s -> {
        Note note = new Note();
        note.setNotebookGuid(s);
        return note;
    }).lift(new Observable.Operator<Boolean, Note>() {

        @Override
        public Subscriber<? super Note> call(Subscriber<? super Boolean> subscriber) {
            return new Subscriber<Note>() {

                @Override
                public void onCompleted() {
                    subscriber.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    subscriber.onError(e);
                }

                @Override
                public void onNext(Note note) {
                    note.setTitle(noteTitle);
                    InputStream in = null;
                    // Hash the data in the image file. The hash is used to reference the file in the ENML note content.
                    try {
                        in = new BufferedInputStream(new FileInputStream(bigPhotoPathWithoutFile));
                        FileData data = new FileData(EvernoteUtil.hash(in), new File(bigPhotoPathWithoutFile));
                        ResourceAttributes attributes = new ResourceAttributes();
                        attributes.setFileName(photoName);
                        // Create a new Resource
                        Resource resource = new Resource();
                        resource.setData(data);
                        resource.setMime("image/jpeg");
                        resource.setAttributes(attributes);
                        note.addToResources(resource);
                        // Set the note's ENML content
                        String content = EvernoteUtil.NOTE_PREFIX + noteContent + EvernoteUtil.createEnMediaTag(resource) + EvernoteUtil.NOTE_SUFFIX;
                        note.setContent(content);
                        EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
                        noteStoreClient.createNote(note);
                    } catch (FileNotFoundException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    } catch (IOException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    } catch (EDAMNotFoundException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    } catch (TException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    } catch (EDAMUserException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    } catch (EDAMSystemException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    } finally {
                        FilePathUtils.closeStream(in);
                    }
                    subscriber.onNext(true);
                }
            };
        }
    });
}
Also used : TException(com.evernote.thrift.TException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) Context(android.content.Context) BufferedInputStream(java.io.BufferedInputStream) Singleton(javax.inject.Singleton) Observable(rx.Observable) Inject(javax.inject.Inject) EvernoteUtil(com.evernote.client.android.EvernoteUtil) ResourceAttributes(com.evernote.edam.type.ResourceAttributes) JSONException(org.json.JSONException) Notebook(com.evernote.edam.type.Notebook) JSONObject(org.json.JSONObject) TException(com.evernote.thrift.TException) User(com.evernote.edam.type.User) Locale(java.util.Locale) ContextLife(com.yydcdut.note.injector.ContextLife) FilePathUtils(com.yydcdut.note.utils.FilePathUtils) Schedulers(rx.schedulers.Schedulers) QQToken(com.tencent.connect.auth.QQToken) R(com.yydcdut.note.R) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) WeakReference(java.lang.ref.WeakReference) Tencent(com.tencent.tauth.Tencent) Subscriber(rx.Subscriber) Resource(com.evernote.edam.type.Resource) IUser(com.yydcdut.note.entity.user.IUser) EDAMUserException(com.evernote.edam.error.EDAMUserException) TextUtils(android.text.TextUtils) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) QQUser(com.yydcdut.note.entity.user.QQUser) IOException(java.io.IOException) UserInfo(com.tencent.connect.UserInfo) FileInputStream(java.io.FileInputStream) Note(com.evernote.edam.type.Note) FileData(com.evernote.client.conn.mobile.FileData) UiError(com.tencent.tauth.UiError) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) EvernoteNoteStoreClient(com.evernote.client.android.asyncclient.EvernoteNoteStoreClient) RxException(com.yydcdut.note.model.rx.exception.RxException) List(java.util.List) IUiListener(com.tencent.tauth.IUiListener) SharedPreferences(android.content.SharedPreferences) EvernoteSession(com.evernote.client.android.EvernoteSession) BuildConfig(com.yydcdut.note.BuildConfig) Activity(android.app.Activity) YLog(com.yydcdut.note.utils.YLog) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) EDAMUserException(com.evernote.edam.error.EDAMUserException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) Subscriber(rx.Subscriber) BufferedInputStream(java.io.BufferedInputStream) List(java.util.List) FileData(com.evernote.client.conn.mobile.FileData) Notebook(com.evernote.edam.type.Notebook) EvernoteNoteStoreClient(com.evernote.client.android.asyncclient.EvernoteNoteStoreClient) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(com.evernote.edam.type.Resource) IOException(java.io.IOException) Observable(rx.Observable) FileInputStream(java.io.FileInputStream) ResourceAttributes(com.evernote.edam.type.ResourceAttributes) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) Note(com.evernote.edam.type.Note) File(java.io.File)

Example 3 with TException

use of com.evernote.thrift.TException in project PhotoNoter by yydcdut.

the class EditTextPresenterImpl method doUpdate2Evernote.

private boolean doUpdate2Evernote(String bigPhotoPathWithoutFile, String photoName) {
    mRxUser.initEvernoteSession();
    boolean isSuccess = true;
    try {
        EvernoteNoteStoreClient noteStoreClient = EvernoteSession.getInstance().getEvernoteClientFactory().getNoteStoreClient();
        List<Notebook> notebookList = noteStoreClient.listNotebooks();
        boolean hasNoteBook = false;
        Notebook appNoteBook = null;
        for (Notebook notebook : notebookList) {
            if (notebook.getName().equals(mContext.getResources().getString(R.string.app_name))) {
                hasNoteBook = true;
                appNoteBook = notebook;
            }
        }
        if (!hasNoteBook) {
            Notebook notebook = new Notebook();
            notebook.setName(mContext.getResources().getString(R.string.app_name));
            appNoteBook = noteStoreClient.createNotebook(notebook);
        }
        Note note = new Note();
        note.setTitle(mEditTextView.getNoteTitle());
        if (appNoteBook != null) {
            note.setNotebookGuid(appNoteBook.getGuid());
        }
        InputStream in = null;
        try {
            // Hash the data in the image file. The hash is used to reference the file in the ENML note content.
            in = new BufferedInputStream(new FileInputStream(bigPhotoPathWithoutFile));
            FileData data = null;
            data = new FileData(EvernoteUtil.hash(in), new File(bigPhotoPathWithoutFile));
            ResourceAttributes attributes = new ResourceAttributes();
            attributes.setFileName(photoName);
            // Create a new Resource
            Resource resource = new Resource();
            resource.setData(data);
            resource.setMime("image/jpeg");
            resource.setAttributes(attributes);
            note.addToResources(resource);
            // Set the note's ENML content
            String content = EvernoteUtil.NOTE_PREFIX + mEditTextView.getNoteContent() + EvernoteUtil.createEnMediaTag(resource) + EvernoteUtil.NOTE_SUFFIX;
            note.setContent(content);
            try {
                noteStoreClient.createNote(note);
            } catch (EDAMNotFoundException e) {
                YLog.e(e);
            }
        } catch (IOException e) {
            YLog.e(e);
            isSuccess = false;
        } finally {
            FilePathUtils.closeStream(in);
        }
    } catch (EDAMUserException e) {
        YLog.e(e);
        isSuccess = false;
    } catch (EDAMSystemException e) {
        YLog.e(e);
        isSuccess = false;
    } catch (TException e) {
        YLog.e(e);
        isSuccess = false;
    }
    return isSuccess;
}
Also used : TException(com.evernote.thrift.TException) Notebook(com.evernote.edam.type.Notebook) EvernoteNoteStoreClient(com.evernote.client.android.asyncclient.EvernoteNoteStoreClient) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(com.evernote.edam.type.Resource) IOException(java.io.IOException) EDAMUserException(com.evernote.edam.error.EDAMUserException) FileInputStream(java.io.FileInputStream) ResourceAttributes(com.evernote.edam.type.ResourceAttributes) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) BufferedInputStream(java.io.BufferedInputStream) RxPhotoNote(com.yydcdut.note.model.rx.RxPhotoNote) Note(com.evernote.edam.type.Note) FileData(com.evernote.client.conn.mobile.FileData) File(java.io.File)

Example 4 with TException

use of com.evernote.thrift.TException in project Notes by lguipeng.

the class BootstrapManager method getBootstrapInfo.

/**
   * Makes a web request to get the latest bootstrap information.
   * This is a requirement during the oauth process
   *
   * @return {@link BootstrapInfoWrapper}
   * @throws Exception
   */
BootstrapInfoWrapper getBootstrapInfo() throws Exception {
    Log.d(LOGTAG, "getBootstrapInfo()");
    BootstrapInfo bsInfo = null;
    try {
        if (mBootstrapServerUsed == null) {
            initializeUserStoreAndCheckVersion();
        }
        bsInfo = mEvernoteSession.getEvernoteClientFactory().getUserStoreClient(getUserStoreUrl(mBootstrapServerUsed), null).getBootstrapInfo(mLocale.toString());
        printBootstrapInfo(bsInfo);
    } catch (TException e) {
        Log.e(LOGTAG, "error getting bootstrap info", e);
    }
    return new BootstrapInfoWrapper(mBootstrapServerUsed, bsInfo);
}
Also used : TException(com.evernote.thrift.TException) BootstrapInfo(com.evernote.edam.userstore.BootstrapInfo)

Example 5 with TException

use of com.evernote.thrift.TException in project Notes by lguipeng.

the class FileData method write.

@Override
public void write(TProtocol oprot) throws TException {
    validate();
    oprot.writeStructBegin(STRUCT_DESC);
    if (this.getBodyHash() != null) {
        if (isSetBodyHash()) {
            oprot.writeFieldBegin(BODY_HASH_FIELD_DESC);
            oprot.writeBinary(ByteBuffer.wrap(this.getBodyHash()));
            oprot.writeFieldEnd();
        }
    }
    oprot.writeFieldBegin(SIZE_FIELD_DESC);
    oprot.writeI32(this.getSize());
    oprot.writeFieldEnd();
    if (this.mBodyFile != null && this.mBodyFile.isFile()) {
        oprot.writeFieldBegin(BODY_FIELD_DESC);
        InputStream s = null;
        try {
            s = new FileInputStream(mBodyFile);
            oprot.writeStream(s, this.mBodyFile.length());
        } catch (FileNotFoundException e) {
            throw new TException("Failed to write binary body:" + mBodyFile, e);
        } finally {
            try {
                if (s != null) {
                    s.close();
                }
            } catch (Exception e) {
            }
        }
        oprot.writeFieldEnd();
    }
    oprot.writeFieldStop();
    oprot.writeStructEnd();
}
Also used : TException(com.evernote.thrift.TException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) TException(com.evernote.thrift.TException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

TException (com.evernote.thrift.TException)6 EDAMSystemException (com.evernote.edam.error.EDAMSystemException)4 EDAMUserException (com.evernote.edam.error.EDAMUserException)4 EDAMNotFoundException (com.evernote.edam.error.EDAMNotFoundException)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 SharedPreferences (android.content.SharedPreferences)2 EvernoteNoteStoreClient (com.evernote.client.android.asyncclient.EvernoteNoteStoreClient)2 FileData (com.evernote.client.conn.mobile.FileData)2 Note (com.evernote.edam.type.Note)2 Notebook (com.evernote.edam.type.Notebook)2 Resource (com.evernote.edam.type.Resource)2 ResourceAttributes (com.evernote.edam.type.ResourceAttributes)2 User (com.evernote.edam.type.User)2 EvernoteUser (com.yydcdut.note.entity.user.EvernoteUser)2 IUser (com.yydcdut.note.entity.user.IUser)2 QQUser (com.yydcdut.note.entity.user.QQUser)2 RxException (com.yydcdut.note.model.rx.exception.RxException)2 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2