use of rx.Subscriber in project PhotoNoter by yydcdut.
the class RxUser method getQQ.
public Observable<IUser> getQQ() {
return Observable.create(new Observable.OnSubscribe<IUser>() {
@Override
public void call(Subscriber<? super IUser> subscriber) {
if (mQQUser == null) {
String name = mSharedPreferences.getString(Q_NAME, NAME_DEFAULT);
String netImagePath = mSharedPreferences.getString(Q_NET_IMAGE_PATH, Q_NET_IMAGE_PATH_DEFAULT);
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(netImagePath)) {
subscriber.onError(new RxException("没有登录!!!"));
return;
} else {
mQQUser = new QQUser(name, netImagePath);
}
}
subscriber.onNext(mQQUser);
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}
use of rx.Subscriber in project PhotoNoter by yydcdut.
the class RxUser method getEvernote.
public Observable<IUser> getEvernote() {
return Observable.create(new Observable.OnSubscribe<IUser>() {
@Override
public void call(Subscriber<? super IUser> subscriber) {
if (mEvernoteUser == null) {
String name = mSharedPreferences.getString(EVERNOTE_NAME, NAME_DEFAULT);
if (TextUtils.isEmpty(name)) {
subscriber.onError(new RxException("没有登录!!!"));
} else {
mEvernoteUser = new EvernoteUser(name);
}
}
subscriber.onNext(mEvernoteUser);
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}
use of rx.Subscriber 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);
}
};
}
});
}
use of rx.Subscriber in project azure-tools-for-java by Microsoft.
the class AzureDockerUtils method refreshDockerSubscriptions.
public static Map<String, AzureDockerSubscription> refreshDockerSubscriptions(AzureManager azureAuthManager) {
Map<String, AzureDockerSubscription> subsMap = new HashMap<>();
try {
if (DEBUG)
System.out.format("Get AzureDockerHostsManage subscription details: %s\n", new Date().toString());
SubscriptionManager subscriptionManager = azureAuthManager.getSubscriptionManager();
List<SubscriptionDetail> subscriptions = subscriptionManager.getSubscriptionDetails();
if (subscriptions != null) {
if (DEBUG)
System.out.format("Get AzureDockerHostsManage Docker subscription details: %s\n", new Date().toString());
Observable.from(subscriptions).flatMap(subscriptionDetail -> {
return Observable.create(new Observable.OnSubscribe<AzureDockerSubscription>() {
@Override
public void call(Subscriber<? super AzureDockerSubscription> dockerSubscriptionSubscriber) {
if (subscriptionDetail.isSelected()) {
AzureDockerSubscription dockerSubscription = new AzureDockerSubscription();
dockerSubscription.id = subscriptionDetail.getSubscriptionId();
try {
if (DEBUG)
System.out.format("\tGet AzureDockerHostsManage Docker subscription: %s at %s\n", dockerSubscription.id, new Date().toString());
dockerSubscription.tid = subscriptionDetail.getTenantId();
dockerSubscription.name = subscriptionDetail.getSubscriptionName();
dockerSubscription.azureClient = azureAuthManager.getAzure(dockerSubscription.id);
dockerSubscription.keyVaultClient = azureAuthManager.getKeyVaultClient(subscriptionDetail.getTenantId());
dockerSubscription.isSelected = true;
if (AzureDockerUtils.hasServicePrincipalAzureManager(azureAuthManager)) {
dockerSubscription.userId = null;
dockerSubscription.servicePrincipalId = azureAuthManager.getCurrentUserId();
} else {
dockerSubscription.userId = azureAuthManager.getCurrentUserId();
dockerSubscription.servicePrincipalId = null;
}
dockerSubscriptionSubscriber.onNext(dockerSubscription);
} catch (Exception e) {
e.printStackTrace();
DefaultLoader.getUIHelper().showError(e.getMessage(), "Error Loading Subscription Details for " + dockerSubscription.id);
}
}
dockerSubscriptionSubscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<AzureDockerSubscription>() {
@Override
public void call(AzureDockerSubscription dockerSubscription) {
subsMap.put(dockerSubscription.id, dockerSubscription);
}
});
}
if (DEBUG)
System.out.format("Get AzureDockerHostsManage locations: %s\n", new Date().toString());
List<Subscription> azureSubscriptionList = azureAuthManager.getSubscriptions();
for (Subscription subscription : azureSubscriptionList) {
AzureDockerSubscription dockerSubscription = subsMap.get(subscription.subscriptionId());
if (dockerSubscription != null) {
List<String> locations = subscription.listLocations().stream().sorted(Comparator.comparing(Location::displayName)).map(o -> o.name().toLowerCase()).collect(Collectors.toList());
dockerSubscription.locations = locations;
}
}
} catch (Exception e) {
e.printStackTrace();
DefaultLoader.getUIHelper().showError(e.getMessage(), "Error loading subscription details");
}
return subsMap;
}
use of rx.Subscriber in project azure-tools-for-java by Microsoft.
the class AzureDockerCertVaultOps method getVault.
public static AzureDockerCertVault getVault(AzureDockerCertVault certVault, KeyVaultClient keyVaultClient) throws AzureDockerException {
if (certVault == null || keyVaultClient == null || certVault.uri == null) {
throw new AzureDockerException("Unexpected argument values; azureClient, vault name and resourceGroupName cannot be null");
}
String vaultUri = certVault.uri;
try {
SecretBundle secret = keyVaultClient.getSecret(vaultUri, SECRETENTRY_DOCKERHOSTNAMES);
if (secret != null) {
certVault.hostName = secret.value();
} else {
certVault.hostName = null;
return null;
}
} catch (Exception e) {
return null;
}
//Execute Key Vault Secret read in parallel
Map<String, String> secretNamesAndValueMap = new HashMap<>();
Observable.from(DOCKERHOST_SECRETS).flatMap(secretName -> {
return Observable.create(new Observable.OnSubscribe<Pair<String, String>>() {
@Override
public void call(Subscriber<? super Pair<String, String>> subscriber) {
keyVaultClient.getSecretAsync(vaultUri, secretName, new ServiceCallback<SecretBundle>() {
@Override
public void failure(Throwable throwable) {
subscriber.onCompleted();
}
@Override
public void success(SecretBundle secretBundle) {
if (secretBundle != null) {
subscriber.onNext(new Pair<>(secretName, secretBundle.value()));
}
subscriber.onCompleted();
}
});
}
}).subscribeOn(Schedulers.io());
}, 5).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<Pair<String, String>>() {
@Override
public void call(Pair<String, String> secretNameAndValue) {
secretNamesAndValueMap.put(secretNameAndValue.first(), secretNameAndValue.second());
}
});
String currentSecretValue;
currentSecretValue = secretNamesAndValueMap.get("vmUsername");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.vmUsername = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("vmPwd");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.vmPwd = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("sshKey");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.sshKey = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("sshPubKey");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.sshPubKey = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("tlsCACert");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.tlsCACert = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("tlsCAKey");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.tlsCAKey = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("tlsClientCert");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.tlsClientCert = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("tlsClientKey");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.tlsClientKey = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("tlsServerCert");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.tlsServerCert = currentSecretValue;
}
currentSecretValue = secretNamesAndValueMap.get("tlsServerKey");
if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
certVault.tlsServerKey = currentSecretValue;
}
return certVault;
}
Aggregations