Search in sources :

Example 71 with HttpRequest

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

the class SeafConnection method createNewDir.

public Pair<String, String> createNewDir(String repoID, String parentDir, String dirName) throws SeafException {
    HttpRequest req = null;
    try {
        String fullPath = Utils.pathJoin(parentDir, dirName);
        final String encodeUriComponent = encodeUriComponent(fullPath).replaceAll("\\+", "%20");
        Map<String, Object> params = Maps.newHashMap();
        params.put("p", encodeUriComponent);
        params.put("reloaddir", "true");
        req = prepareApiPostRequest("api2/repos/" + repoID + "/dir/", true, params, false);
        req.form("operation", "mkdir");
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        String newDirID = req.header("oid");
        if (newDirID == null) {
            return null;
        }
        String content = new String(req.bytes(), "UTF-8");
        if (content.length() == 0) {
            return null;
        }
        return new Pair<String, String>(newDirID, content);
    } catch (SeafException e) {
        throw e;
    } catch (UnsupportedEncodingException e) {
        throw SeafException.encodingException;
    } catch (HttpRequestException e) {
        throw getSeafExceptionFromHttpRequestException(e);
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) JSONObject(org.json.JSONObject) Pair(android.util.Pair)

Example 72 with HttpRequest

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

the class SeafConnection method getServerInfo.

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

Example 73 with HttpRequest

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

the class SeafConnection method searchLibraries.

public String searchLibraries(String query, int page) throws SeafException {
    try {
        Map<String, Object> params = Maps.newHashMap();
        params.put("q", encodeUriComponent(query));
        if (page > 0)
            params.put("per_page", page);
        HttpRequest req = prepareApiGetRequest("api2/search/", params);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        String result = new String(req.bytes(), "UTF-8");
        return result;
    } 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) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 74 with HttpRequest

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

the class SeafConnection method getDirents.

/**
 * Get the contents of a directory.
 * @param repoID
 * @param path
 * @param cachedDirID The local cached dirID.
 * @return A non-null Pair of (dirID, content). If the local cache is up to date, the "content" is null.
 * @throws SeafException
 */
public Pair<String, String> getDirents(String repoID, String path, String cachedDirID) throws SeafException {
    try {
        String apiPath = String.format("api2/repos/%s/dir/", repoID);
        Map<String, Object> params = Maps.newHashMap();
        params.put("p", encodeUriComponent(path));
        if (cachedDirID != null) {
            params.put("oid", cachedDirID);
        }
        HttpRequest req = prepareApiGetRequest(apiPath, params);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        String dirID = req.header("oid");
        String content;
        if (dirID == null) {
            throw SeafException.unknownException;
        }
        if (dirID.equals(cachedDirID)) {
            // local cache is valid
            // Log.d(DEBUG_TAG, String.format("dir %s is cached", path));
            content = null;
        } else {
            /*Log.d(DEBUG_TAG,
                      String.format("dir %s will be downloaded from server, latest %s, local cache %s",
                                    path, dirID, cachedDirID != null ? cachedDirID : "null"));*/
            byte[] rawBytes = req.bytes();
            if (rawBytes == null) {
                throw SeafException.unknownException;
            }
            content = new String(rawBytes, "UTF-8");
        }
        return new Pair<String, String>(dirID, content);
    } catch (SeafException e) {
        throw e;
    } catch (UnsupportedEncodingException e) {
        throw SeafException.encodingException;
    } catch (HttpRequestException e) {
        throw getSeafExceptionFromHttpRequestException(e);
    } catch (IOException e) {
        throw SeafException.networkException;
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) JSONObject(org.json.JSONObject) IOException(java.io.IOException) Pair(android.util.Pair)

Example 75 with HttpRequest

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

the class SeafConnection method star.

public void star(String repoID, String path) throws SeafException {
    try {
        HttpRequest req = prepareApiPostRequest("api2/starredfiles/", true, null);
        req.form("repo_id", repoID);
        req.form("p", path);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_CREATED);
    } catch (SeafException e) {
        throw e;
    } catch (HttpRequestException e) {
        throw getSeafExceptionFromHttpRequestException(e);
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException)

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