use of org.matrix.androidsdk.core.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MXMediaDownloadWorkerTask method getAntivirusServerPublicKey.
/**
* Get the public key of the antivirus server
*
* @return either empty string if server does not provide the public key, null in case of error, or the public server key
*/
@Nullable
private String getAntivirusServerPublicKey() {
if (mMediaScanRestClient == null) {
// Error
Log.e(LOG_TAG, "Mandatory mMediaScanRestClient is null");
return null;
}
// Make async request sync with a CountDownLatch
// It is easier than adding a method to get the server public key synchronously with Call<T>.execute()
final CountDownLatch latch = new CountDownLatch(1);
final String[] publicServerKey = new String[1];
mMediaScanRestClient.getServerPublicKey(new ApiCallback<String>() {
@Override
public void onSuccess(String serverPublicKey) {
publicServerKey[0] = serverPublicKey;
latch.countDown();
}
@Override
public void onNetworkError(Exception e) {
latch.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
latch.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
latch.countDown();
}
});
try {
latch.await(30, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
}
return publicServerKey[0];
}
use of org.matrix.androidsdk.core.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MatrixMessageListFragment method redactEvent.
/**
* Redact an event from its event id.
*
* @param eventId the event id.
*/
protected void redactEvent(final String eventId) {
// Do nothing on success, the event will be hidden when the redaction event comes down the event stream
mMatrixMessagesFragment.redact(eventId, new ApiCallback<Event>() {
@Override
public void onSuccess(final Event redactedEvent) {
if (null != redactedEvent) {
getUiHandler().post(new Runnable() {
@Override
public void run() {
// create a dummy redacted event to manage the redaction.
// some redacted events are not removed from the history but they are pruned.
Event redacterEvent = new Event();
redacterEvent.roomId = redactedEvent.roomId;
redacterEvent.redacts = redactedEvent.eventId;
redacterEvent.setType(Event.EVENT_TYPE_REDACTION);
onEvent(redacterEvent, EventTimeline.Direction.FORWARDS, mRoom.getState());
if (null != mEventSendingListener) {
try {
mEventSendingListener.onMessageRedacted(redactedEvent);
} catch (Exception e) {
Log.e(LOG_TAG, "redactEvent fails : " + e.getMessage(), e);
}
}
}
});
}
}
private void onError() {
if (null != getActivity()) {
Toast.makeText(getActivity(), R.string.could_not_redact, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNetworkError(Exception e) {
onError();
}
@Override
public void onMatrixError(MatrixError e) {
onError();
}
@Override
public void onUnexpectedError(Exception e) {
onError();
}
});
}
use of org.matrix.androidsdk.core.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MediaScanRestClient method getServerPublicKey.
/**
* Get the current public curve25519 key that the AV server is advertising.
* Read the value from cache if any
*
* @param callback on success callback containing the server public key
*/
public void getServerPublicKey(final ApiCallback<String> callback) {
if (mMxStore == null) {
callback.onUnexpectedError(new Exception("MxStore not configured"));
return;
}
// Check in cache
String keyFromCache = mMxStore.getAntivirusServerPublicKey();
if (keyFromCache != null) {
callback.onSuccess(keyFromCache);
} else {
mApi.getServerPublicKey().enqueue(new RestAdapterCallback<>("getServerPublicKey", null, new SimpleApiCallback<MediaScanPublicKeyResult>(callback) {
@Override
public void onSuccess(MediaScanPublicKeyResult info) {
// Store the key in cache for next times
mMxStore.setAntivirusServerPublicKey(info.mCurve25519PublicKey);
// Note: for some reason info.mCurve25519PublicKey may be null
if (info.mCurve25519PublicKey != null) {
callback.onSuccess(info.mCurve25519PublicKey);
} else {
callback.onUnexpectedError(new Exception("Unable to get server public key from Json"));
}
}
@Override
public void onMatrixError(MatrixError e) {
// Old Antivirus scanner instance will return a 404
if (e.mStatus == HttpURLConnection.HTTP_NOT_FOUND) {
// On 404 consider the public key is not available, so do not encrypt body
mMxStore.setAntivirusServerPublicKey("");
callback.onSuccess("");
} else {
super.onMatrixError(e);
}
}
}, null));
}
}
use of org.matrix.androidsdk.core.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MediaScanRestClient method scanEncryptedFile.
/**
* Scan an encrypted file.
*
* @param encryptedMediaScanBody the encryption information required to decrypt the content before scanning it.
* @param callback on success callback containing a MediaScanResult object
*/
public void scanEncryptedFile(final EncryptedMediaScanBody encryptedMediaScanBody, final ApiCallback<MediaScanResult> callback) {
// Encrypt encryptedMediaScanBody if the server support it
getServerPublicKey(new SimpleApiCallback<String>(callback) {
@Override
public void onSuccess(String serverPublicKey) {
Call<MediaScanResult> request;
// Encrypt the data, if antivirus server supports it
if (!TextUtils.isEmpty(serverPublicKey)) {
try {
OlmPkEncryption olmPkEncryption = new OlmPkEncryption();
olmPkEncryption.setRecipientKey(serverPublicKey);
String data = JsonUtility.getCanonicalizedJsonString(encryptedMediaScanBody);
OlmPkMessage message = olmPkEncryption.encrypt(data);
EncryptedMediaScanEncryptedBody encryptedMediaScanEncryptedBody = new EncryptedMediaScanEncryptedBody();
encryptedMediaScanEncryptedBody.encryptedBodyFileInfo = new EncryptedBodyFileInfo(message);
request = mApi.scanEncrypted(encryptedMediaScanEncryptedBody);
} catch (OlmException e) {
// should not happen. Send the error to the caller
request = null;
callback.onUnexpectedError(e);
}
} else {
// No public key on this server, do not encrypt data
request = mApi.scanEncrypted(encryptedMediaScanBody);
}
if (request != null) {
request.enqueue(new RestAdapterCallback<>("scanEncryptedFile", null, new SimpleApiCallback<MediaScanResult>(callback) {
@Override
public void onSuccess(MediaScanResult scanResult) {
callback.onSuccess(scanResult);
}
@Override
public void onMatrixError(MatrixError e) {
// Check whether the provided encrypted_body could not be decrypted.
if (e.mStatus == HttpURLConnection.HTTP_FORBIDDEN) {
MediaScanError mcsError;
try {
mcsError = JsonUtils.getGson(false).fromJson(e.mErrorBodyAsString, MediaScanError.class);
} catch (Exception exc) {
mcsError = null;
}
if (mcsError != null && MediaScanError.MCS_BAD_DECRYPTION.equals(mcsError.reason)) {
// The client should request again the public key of the server.
resetServerPublicKey();
}
}
super.onMatrixError(e);
}
}, null));
}
}
});
}
use of org.matrix.androidsdk.core.model.MatrixError in project matrix-android-sdk by matrix-org.
the class ThirdPidRestClient method submitValidationToken.
/**
* Request the ownership validation of an email address or a phone number previously set
* by {@link ThirdPidRestClient#requestEmailValidationToken(ThreePid, String, ApiCallback)}
*
* @param medium the medium of the 3pid
* @param token the token generated by the requestEmailValidationToken call
* @param clientSecret the client secret which was supplied in the requestEmailValidationToken call
* @param sid the sid for the session
* @param callback asynchronous callback response
*/
public void submitValidationToken(final String medium, final String token, final String clientSecret, final String sid, final ApiCallback<Boolean> callback) {
RequestOwnershipParams params = RequestOwnershipParams.Companion.with(clientSecret, sid, token);
mApi.requestOwnershipValidationV2(medium, params).enqueue(new RestAdapterCallback<>("submitValidationToken", null, new SimpleApiCallback<SuccessResult>(callback) {
@Override
public void onSuccess(SuccessResult info) {
callback.onSuccess(info.success);
}
@Override
public void onMatrixError(MatrixError e) {
if (e.mStatus == HttpURLConnection.HTTP_NOT_FOUND) /*404*/
{
// Use legacy request
submitValidationTokenLegacy(medium, token, clientSecret, sid, callback);
} else {
super.onMatrixError(e);
}
}
}, null));
}
Aggregations