Search in sources :

Example 11 with UnknownHttpResponseException

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);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Example 12 with UnknownHttpResponseException

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;
    }
}
Also used : Response(okhttp3.Response) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SQLiteDatabaseCorruptException(android.database.sqlite.SQLiteDatabaseCorruptException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) DB(com.ichi2.libanki.DB) ResponseBody(okhttp3.ResponseBody) NonNull(androidx.annotation.NonNull)

Example 13 with UnknownHttpResponseException

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();
    }
}
Also used : HostNum(com.ichi2.libanki.sync.HostNum) FullSyncer(com.ichi2.libanki.sync.FullSyncer) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) JSONException(com.ichi2.utils.JSONException) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) FullSyncer(com.ichi2.libanki.sync.FullSyncer) Syncer(com.ichi2.libanki.sync.Syncer) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) Collection(com.ichi2.libanki.Collection) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) JSONObject(com.ichi2.utils.JSONObject) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException)

Example 14 with UnknownHttpResponseException

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;
}
Also used : JSONException(com.ichi2.utils.JSONException) HostNum(com.ichi2.libanki.sync.HostNum) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) JSONException(com.ichi2.utils.JSONException) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) SuppressLint(android.annotation.SuppressLint) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Example 15 with UnknownHttpResponseException

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();
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Uri(android.net.Uri) HashMap(java.util.HashMap) Random(java.util.Random) BufferedOutputStream(java.io.BufferedOutputStream) RequestBody(okhttp3.RequestBody) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) Map(java.util.Map) API_VERSION(com.ichi2.libanki.Consts.API_VERSION) Connection(com.ichi2.async.Connection) Response(okhttp3.Response) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException) Utils(com.ichi2.libanki.Utils) MediaType(okhttp3.MediaType) OutputStream(java.io.OutputStream) Request(okhttp3.Request) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) StringWriter(java.io.StringWriter) FileOutputStream(java.io.FileOutputStream) CustomSyncServer(com.ichi2.anki.web.CustomSyncServer) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) JSONObject(com.ichi2.utils.JSONObject) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Timber(timber.log.Timber) TimeUnit(java.util.concurrent.TimeUnit) ANKI_CHINA_BASE(com.ichi2.libanki.Consts.ANKI_CHINA_BASE) SSLException(javax.net.ssl.SSLException) Nullable(androidx.annotation.Nullable) OkHttpClient(okhttp3.OkHttpClient) SharedPreferences(android.content.SharedPreferences) AnkiDroidApp(com.ichi2.anki.AnkiDroidApp) Consts(com.ichi2.libanki.Consts) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedReader(java.io.BufferedReader) HttpUrl(okhttp3.HttpUrl) VersionUtils(com.ichi2.utils.VersionUtils) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) Response(okhttp3.Response) StringWriter(java.io.StringWriter) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

IOException (java.io.IOException)23 JSONObject (com.ichi2.utils.JSONObject)22 Response (okhttp3.Response)20 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)8 NoEnoughServerSpaceException (com.ichi2.anki.exception.NoEnoughServerSpaceException)7 File (java.io.File)7 JSONException (com.ichi2.utils.JSONException)6 FileInputStream (java.io.FileInputStream)6 MediaSyncException (com.ichi2.anki.exception.MediaSyncException)5 HostNum (com.ichi2.libanki.sync.HostNum)5 RemoteServer (com.ichi2.libanki.sync.RemoteServer)5 HttpSyncer (com.ichi2.libanki.sync.HttpSyncer)4 ZipFile (java.util.zip.ZipFile)4 JSONArray (com.ichi2.utils.JSONArray)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SharedPreferences (android.content.SharedPreferences)2 SQLiteDatabaseCorruptException (android.database.sqlite.SQLiteDatabaseCorruptException)2 Uri (android.net.Uri)2 Pair (android.util.Pair)2