use of com.ichi2.anki.exception.UnknownHttpResponseException in project Anki-Android by ankidroid.
the class RemoteMediaServer method mediaChanges.
// args: lastUsn
public JSONArray mediaChanges(int lastUsn) throws UnknownHttpResponseException, MediaSyncException {
try {
mPostVars = HashUtil.HashMapInit(1);
mPostVars.put("sk", mSKey);
Response resp = super.req("mediaChanges", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("lastUsn", lastUsn))));
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, JSONArray.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project Anki-Android by ankidroid.
the class FullSyncer method download.
@NonNull
public ConnectionResultType download() throws UnknownHttpResponseException {
InputStream cont;
ResponseBody body = null;
try {
Response ret = super.req("download");
if (ret == null || ret.body() == null) {
return null;
}
body = ret.body();
cont = body.byteStream();
} catch (IllegalArgumentException e1) {
if (body != null) {
body.close();
}
throw new RuntimeException(e1);
}
String path;
if (mCol != null) {
Timber.i("Closing collection for full sync");
// Usual case where collection is non-null
path = mCol.getPath();
mCol.close();
mCol = null;
} else {
// Allow for case where collection is completely unreadable
Timber.w("Collection was unexpectedly null when doing full sync download");
path = CollectionHelper.getCollectionPath(AnkiDroidApp.getInstance());
}
String tpath = path + ".tmp";
try {
super.writeToFile(cont, tpath);
Timber.d("Full Sync - Downloaded temp file");
FileInputStream fis = new FileInputStream(tpath);
if ("upgradeRequired".equals(super.stream2String(fis, 15))) {
Timber.w("Full Sync - 'Upgrade Required' message received");
return UPGRADE_REQUIRED;
}
} catch (FileNotFoundException e) {
Timber.e(e, "Failed to create temp file when downloading collection.");
throw new RuntimeException(e);
} catch (IOException e) {
Timber.e(e, "Full sync failed to download collection.");
return SD_ACCESS_ERROR;
} finally {
body.close();
}
// check the received file is ok
mCon.publishProgress(R.string.sync_check_download_file);
DB tempDb = null;
try {
tempDb = new DB(tpath);
if (!"ok".equalsIgnoreCase(tempDb.queryString("PRAGMA integrity_check"))) {
Timber.e("Full sync - downloaded file corrupt");
return REMOTE_DB_ERROR;
}
} catch (SQLiteDatabaseCorruptException e) {
Timber.e("Full sync - downloaded file corrupt");
return REMOTE_DB_ERROR;
} finally {
if (tempDb != null) {
tempDb.close();
}
}
Timber.d("Full Sync: Downloaded file was not corrupt");
// overwrite existing collection
File newFile = new File(tpath);
if (newFile.renameTo(new File(path))) {
Timber.i("Full Sync Success: Overwritten collection with downloaded file");
return SUCCESS;
} else {
Timber.w("Full Sync: Error overwriting collection with downloaded file");
return OVERWRITE_ERROR;
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class Connection method doInBackgroundSync.
private Payload doInBackgroundSync(Payload data) {
sIsCancellable = true;
Timber.d("doInBackgroundSync()");
// Block execution until any previous background task finishes, or timeout after 5s
boolean ok = CollectionTask.waitToFinish(5);
String hkey = (String) data.data[0];
boolean media = (Boolean) data.data[1];
String conflictResolution = (String) data.data[2];
HostNum hostNum = (HostNum) data.data[3];
long restServerSpace = (long) data.data[4];
// Use safe version that catches exceptions so that full sync is still possible
Collection col = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
boolean colCorruptFullSync = false;
if (!CollectionHelper.getInstance().colIsOpen() || !ok) {
if (conflictResolution != null && "download".equals(conflictResolution)) {
colCorruptFullSync = true;
} else {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
}
try {
CollectionHelper.getInstance().lockCollection();
HttpSyncer server = new RemoteServer(this, hkey, hostNum);
Syncer client = new Syncer(col, server, hostNum);
// run sync and check state
boolean noChanges = false;
if (conflictResolution == null) {
Timber.i("Sync - starting sync");
publishProgress(R.string.sync_prepare_syncing);
Object[] ret = client.sync(this, restServerSpace);
data.message = client.getSyncMsg();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
String retCode = (String) ret[0];
if (!"noChanges".equals(retCode) && !"success".equals(retCode)) {
data.success = false;
data.result = ret;
// Check if there was a sanity check error
if ("sanityCheckError".equals(retCode)) {
// Force full sync next time
col.modSchemaNoCheck();
col.save();
}
return data;
}
// save and note success state
if ("noChanges".equals(retCode)) {
// publishProgress(R.string.sync_no_changes_message);
noChanges = true;
}
restServerSpace = client.getRestSpace();
} else {
try {
// Disable sync cancellation for full-sync
sIsCancellable = false;
server = new FullSyncer(col, hkey, this, hostNum);
if ("upload".equals(conflictResolution)) {
Timber.i("Sync - fullsync - upload collection");
publishProgress(R.string.sync_preparing_full_sync_message);
Object[] ret = server.upload(restServerSpace);
col.reopen();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
if (!ret[0].equals(HttpSyncer.ANKIWEB_STATUS_OK)) {
data.success = false;
data.result = ret;
return data;
}
} else if ("download".equals(conflictResolution)) {
Timber.i("Sync - fullsync - download collection");
publishProgress(R.string.sync_downloading_message);
Object[] ret = server.download();
if (ret == null) {
Timber.w("Sync - fullsync - unknown error");
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
if ("success".equals(ret[0])) {
data.success = true;
col.reopen();
}
if (!"success".equals(ret[0])) {
Timber.w("Sync - fullsync - download failed");
data.success = false;
data.result = ret;
if (!colCorruptFullSync) {
col.reopen();
}
return data;
}
}
} catch (OutOfMemoryError e) {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "OutOfMemoryError" };
return data;
} catch (RuntimeException e) {
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
} else {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
data.result = new Object[] { "IOException", e };
}
data.success = false;
return data;
}
}
// clear undo to avoid non syncing orphans (because undo resets usn too
if (!noChanges) {
col.clearUndo();
}
// then move on to media sync
sIsCancellable = true;
boolean noMediaChanges = false;
boolean enoughServerSpace = true;
String mediaError = null;
if (media) {
server = new RemoteMediaServer(col, hkey, this, hostNum);
MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server, this);
String ret;
try {
// ������ܻ���Ϊ�ϴ��ļ������ռ��С���׳��쳣���ڴ�֮ǰ��Ҫ����ʣ��ռ��С
ret = mediaClient.sync(restServerSpace);
Timber.e("sync media ret is null");
if (ret == null) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
} else {
if ("corruptMediaDB".equals(ret)) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_db_error);
noMediaChanges = true;
}
if ("noChanges".equals(ret)) {
publishProgress(R.string.sync_media_no_changes);
noMediaChanges = true;
}
if ("sanityFailed".equals(ret)) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
} else {
publishProgress(R.string.sync_media_success);
}
}
} catch (RuntimeException e) {
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
}
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error) + "\n\n" + e.getLocalizedMessage();
}
}
if (noChanges && (!media || noMediaChanges)) {
data.success = false;
data.result = new Object[] { "noChanges" };
return data;
} else {
data.success = true;
data.data = new Object[] { conflictResolution, col, mediaError };
return data;
}
} catch (MediaSyncException e) {
Timber.e("Media sync rejected by server");
data.success = false;
data.result = new Object[] { "mediaSyncServerError", e };
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
return data;
} catch (UnknownHttpResponseException e) {
Timber.e(e, "doInBackgroundSync -- unknown response code error");
data.success = false;
int code = e.getResponseCode();
String msg = e.getLocalizedMessage();
data.result = new Object[] { "error", code, msg };
return data;
} catch (NoEnoughServerSpaceException e) {
Timber.e("NoEnoughServerSpaceException ");
e.printStackTrace();
data.success = false;
data.result = new Object[] { "noServerSpace", e.rest, e.need };
return data;
} catch (Exception e) {
// Global error catcher.
// Try to give a human readable error, otherwise print the raw error message
Timber.e(e, "doInBackgroundSync error");
data.success = false;
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
} else {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
data.result = new Object[] { e.getLocalizedMessage(), e };
}
return data;
} finally {
Timber.i("Sync Finished - Closing Collection");
// don't bump mod time unless we explicitly save
if (col != null) {
col.close(false);
}
CollectionHelper.getInstance().unlockCollection();
}
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class Connection method doInBackgroundCommonRequest.
private Payload doInBackgroundCommonRequest(Payload data) {
HostNum hostNum = data.hostNum;
HttpSyncer server = new RemoteServer(this, null, hostNum);
Response ret;
try {
ret = data.RESTType == Payload.REST_TYPE_POST ? server.sendCommonPost(data.method, data.param, data.token) : data.RESTType == Payload.REST_TYPE_GET ? server.sendCommonGet(data.method, data.param, data.token) : server.sendCommonPUT(data.method, data.param, data.token);
} catch (UnknownHttpResponseException e) {
data.success = false;
data.result = new Object[] { "error", e.getResponseCode(), e.getMessage() };
return data;
} catch (Exception e2) {
// Ask user to report all bugs which aren't timeout errors
if (!timeoutOccurred(e2)) {
AnkiDroidApp.sendExceptionReport(e2, "doInBackgroundCommonRequest");
}
data.success = false;
data.result = new Object[] { "connectionError" };
return data;
}
int status_code = -1;
String message = null;
JSONObject bodyData = null;
boolean valid = false;
if (ret != null) {
data.returnType = ret.code();
Timber.d("doInBackgroundCommonRequest - response from server: %d, (%s)", data.returnType, ret.message());
if (data.returnType == 200) {
try {
String bodyStr = ret.body().string();
Timber.d("response body:%s", bodyStr);
JSONObject jo = (new JSONObject(bodyStr));
status_code = jo.getInt("status_code");
message = jo.getString("message");
bodyData = jo;
valid = status_code == 0;
} catch (JSONException e) {
valid = false;
} catch (IllegalStateException | IOException | NullPointerException e) {
// throw new RuntimeException(e);
e.printStackTrace();
}
}
} else {
Timber.e("doInBackgroundCommonRequest - empty response from server");
}
data.success = valid;
data.message = message;
data.statusCode = status_code;
data.result = bodyData;
return data;
}
use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.
the class HttpSyncer method req.
/**
* Note: Return value must be closed
*/
private Response req(String method, InputStream fobj, int comp, JSONObject registerData) throws UnknownHttpResponseException {
File tmpFileBuffer = null;
try {
String bdry = "--" + BOUNDARY;
StringWriter buf = new StringWriter();
// post vars
mPostVars.put("c", comp != 0 ? 1 : 0);
for (String key : mPostVars.keySet()) {
buf.write(bdry + "\r\n");
buf.write(String.format(Locale.US, "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", key, mPostVars.get(key)));
}
tmpFileBuffer = File.createTempFile("syncer", ".tmp", new File(AnkiDroidApp.getCacheStorageDirectory()));
FileOutputStream fos = new FileOutputStream(tmpFileBuffer);
BufferedOutputStream bos = new BufferedOutputStream(fos);
GZIPOutputStream tgt;
// payload as raw data or json
if (fobj != null) {
// header
buf.write(bdry + "\r\n");
buf.write("Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n");
buf.close();
bos.write(buf.toString().getBytes("UTF-8"));
// write file into buffer, optionally compressing
int len;
int totalLen = 0;
BufferedInputStream bfobj = new BufferedInputStream(fobj);
byte[] chunk = new byte[65536];
if (comp != 0) {
tgt = new GZIPOutputStream(bos);
while ((len = bfobj.read(chunk)) >= 0) {
tgt.write(chunk, 0, len);
totalLen += len;
}
tgt.close();
bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true));
} else {
while ((len = bfobj.read(chunk)) >= 0) {
bos.write(chunk, 0, len);
totalLen += len;
}
}
// ������Ⱦ�����ͨ���ݳ���
Timber.d("Write common data length: %d", totalLen);
// ������Ⱦ�����ͨ���ݳ���
Timber.d("Write common data length2: %d", tmpFileBuffer.length());
bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8"));
} else {
buf.close();
bos.write(buf.toString().getBytes("UTF-8"));
bos.write((bdry + "--\r\n").getBytes("UTF-8"));
}
bos.flush();
bos.close();
// connection headers
String url = Uri.parse(syncURL()).buildUpon().appendPath(method).toString();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(parseUrl(url));
Timber.d("request url: %s", url);
requestBuilder.post(new CountingFileRequestBody(tmpFileBuffer, ANKI_POST_TYPE.toString(), num -> {
bytesSent += num;
publishProgress();
}));
Request httpPost = requestBuilder.build();
try {
OkHttpClient httpClient = getHttpClient();
Response httpResponse = httpClient.newCall(httpPost).execute();
// we assume badAuthRaises flag from Anki Desktop always False
// so just throw new RuntimeException if response code not 200 or 403
Timber.d("TLSVersion in use is: %s", (httpResponse.handshake() != null ? httpResponse.handshake().tlsVersion() : "unknown"));
assertOk(httpResponse);
return httpResponse;
} catch (SSLException e) {
Timber.e(e, "SSLException while building HttpClient");
throw new RuntimeException("SSLException while building HttpClient", e);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Timber.e(e, "BasicHttpSyncer.sync: IOException");
throw new RuntimeException(e);
} finally {
if (tmpFileBuffer != null && tmpFileBuffer.exists()) {
tmpFileBuffer.delete();
}
}
}
Aggregations