Search in sources :

Example 11 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.

the class SeafConnection method prepareApiGetRequest.

private HttpRequest prepareApiGetRequest(String apiPath, Map<String, ?> params) throws IOException {
    HttpRequest req = HttpRequest.get(account.server + apiPath, params, false);
    setRequestCommon(req);
    return req;
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest)

Example 12 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.

the class SeafConnection method getBlockDownloadLink.

/**
 * get file server link for downloading a block
 *
 * @param repoID
 * @param fileId
 * @param blockId
 * @return
 * @throws SeafException
 * @throws IOException
 */
private String getBlockDownloadLink(String repoID, String fileId, String blockId) throws SeafException, IOException {
    try {
        String apiPath = String.format("api2/repos/%s/files/%s/blks/%s/download-link/", repoID, fileId, blockId);
        HttpRequest req = prepareApiGetRequest(apiPath, null);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        return new String(req.bytes(), "UTF-8");
    } catch (SeafException | IOException e) {
        throw e;
    } catch (HttpRequestException e) {
        throw getSeafExceptionFromHttpRequestException(e);
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) IOException(java.io.IOException)

Example 13 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.

the class SeafConnection method getStarredFiles.

public String getStarredFiles() throws SeafException {
    try {
        // HttpRequest req = prepareApiGetRequest("api2/starredfiles/");
        HttpRequest req = prepareApiGetRequest("api/v2.1/starred-items/");
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        return new String(req.bytes(), "UTF-8");
    } catch (SeafException e) {
        throw e;
    } catch (HttpRequestException e) {
        throw getSeafExceptionFromHttpRequestException(e);
    } catch (IOException e) {
        throw SeafException.networkException;
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) IOException(java.io.IOException)

Example 14 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.

the class SeafConnection method realLogin.

/**
 * Login into the server
 * @return true if login success, false otherwise
 * @throws SeafException
 */
private boolean realLogin(String passwd, String authToken, boolean rememberDevice) throws SeafException {
    boolean withAuthToken = false;
    HttpRequest req = null;
    try {
        req = prepareApiPostRequest("api2/auth-token/", false, null);
        if (!TextUtils.isEmpty(authToken)) {
            req.header("X-Seafile-OTP", authToken);
            withAuthToken = true;
        // Log.d(DEBUG_TAG, "authToken " + authToken);
        }
        if (!TextUtils.isEmpty(account.sessionKey)) {
            req.header("X-SEAFILE-S2FA", account.sessionKey);
        }
        if (rememberDevice) {
            req.header("X-SEAFILE-2FA-TRUST-DEVICE", 1);
        }
        req.form("username", account.email);
        req.form("password", passwd);
        String appVersion = "";
        Context context = SeadroidApplication.getAppContext();
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            appVersion = pInfo.versionName;
        } catch (NameNotFoundException e) {
        // ignore
        }
        String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
        req.form("platform", "android");
        req.form("device_id", deviceId);
        req.form("device_name", Build.MODEL);
        req.form("client_version", appVersion);
        req.form("platform_version", Build.VERSION.RELEASE);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK, withAuthToken);
        String sessionKey = req.header("x-seafile-s2fa");
        if (!TextUtils.isEmpty(sessionKey)) {
            account.sessionKey = sessionKey;
        }
        String contentAsString = new String(req.bytes(), "UTF-8");
        JSONObject obj = Utils.parseJsonObject(contentAsString);
        if (obj == null)
            return false;
        account.token = obj.getString("token");
        return true;
    } catch (SeafException e) {
        throw e;
    } catch (HttpRequestException e) {
        throw getSeafExceptionFromHttpRequestException(e);
    } catch (IOException e) {
        e.printStackTrace();
        throw SeafException.networkException;
    } catch (JSONException e) {
        throw SeafException.illFormatException;
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) Context(android.content.Context) JSONObject(org.json.JSONObject) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 15 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.

the class SeafConnection method prepareApiPutRequest.

private HttpRequest prepareApiPutRequest(String apiPath, Map<String, ?> params) throws IOException {
    HttpRequest req = HttpRequest.put(account.server + apiPath, params, false);
    setRequestCommon(req);
    return req;
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest)

Aggregations

HttpRequest (com.github.kevinsawicki.http.HttpRequest)86 HttpRequestException (com.github.kevinsawicki.http.HttpRequest.HttpRequestException)29 IOException (java.io.IOException)25 JSONObject (org.json.JSONObject)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 File (java.io.File)8 TimerTask (java.util.TimerTask)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 Pair (android.util.Pair)6 URL (java.net.URL)5 JSONException (org.json.JSONException)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 JSONObject (com.alibaba.fastjson.JSONObject)4 ZipFile (net.lingala.zip4j.ZipFile)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 IPEntity (com.example.li.springboot_crawler_demo.utils.img.entity.IPEntity)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 Matcher (java.util.regex.Matcher)2 JSONArray (org.json.JSONArray)2