Search in sources :

Example 6 with HttpRequest

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

the class SeafConnection method getBlockFromLink.

private File getBlockFromLink(String dlink, FileBlocks fileBlocks, String blkId, String localPath, long fileSize, ProgressMonitor monitor) throws SeafException {
    if (dlink == null)
        return null;
    try {
        HttpRequest req = prepareApiFileGetRequest(dlink);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        if (monitor != null) {
            /*if (req.header(HttpRequest.HEADER_CONTENT_LENGTH) == null) {
                    throw SeafException.illFormatException;
                }
                Long size = Long.parseLong(req.header(HttpRequest.HEADER_CONTENT_LENGTH));*/
            if (req.contentLength() > 0) {
                monitor.onProgressNotify(fileSize, true);
            }
        }
        File block = new File(localPath);
        // Log.d(DEBUG_TAG, "write to " + block.getAbsolutePath());
        if (monitor == null) {
            req.receive(block);
        } else {
            req.bufferSize(DataManager.BUFFER_SIZE);
            req.receive(new MonitoredFileOutputStream(fileBlocks, blkId, block, monitor));
        }
        return block;
    } catch (SeafException e) {
        throw e;
    } catch (UnsupportedEncodingException e) {
        throw SeafException.encodingException;
    } catch (IOException e) {
        e.printStackTrace();
        throw SeafException.networkException;
    } catch (HttpRequestException e) {
        if (e.getCause() instanceof MonitorCancelledException) {
            // Log.d(DEBUG_TAG, "download is cancelled");
            throw SeafException.userCancelledException;
        } else {
            throw getSeafExceptionFromHttpRequestException(e);
        }
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) IOException(java.io.IOException) File(java.io.File)

Example 7 with HttpRequest

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

the class SeafConnection method getBlockUploadLink.

private String getBlockUploadLink(String repoID, List<String> blocksId) throws SeafException {
    try {
        String apiPath;
        apiPath = "api2/repos/" + repoID + "/upload-blks-link/";
        HttpRequest req = prepareApiPostRequest(apiPath, true, null);
        String ids = blocksId.toString().replace("[", "").replace("]", "").replace(" ", "");
        req.form("blklist", ids);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        return new String(req.bytes(), "UTF-8");
    } catch (SeafException e) {
        Log.d(DEBUG_TAG, e.getCode() + e.getMessage());
        throw e;
    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg != null)
            Log.d(DEBUG_TAG, msg);
        else
            Log.d(DEBUG_TAG, "get upload link error", e);
        throw SeafException.unknownException;
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) JSONException(org.json.JSONException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with HttpRequest

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

the class SeafConnection method getUploadLink.

private String getUploadLink(String repoID, boolean update, String dir) throws SeafException {
    try {
        String apiPath;
        if (update) {
            apiPath = "api2/repos/" + repoID + "/update-link/";
        } else {
            apiPath = "api2/repos/" + repoID + "/upload-link/?p=" + Utils.toURLEncoded(dir);
        }
        HttpRequest req;
        req = prepareApiGetRequest(apiPath);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        String result = new String(req.bytes(), "UTF-8");
        // should return "\"http://gonggeng.org:8082/...\"" or "\"https://gonggeng.org:8082/...\"
        if (result.startsWith("\"http")) {
            // remove the starting and trailing quote
            return result.substring(1, result.length() - 1);
        } else
            throw SeafException.unknownException;
    } catch (SeafException e) {
        Log.d(DEBUG_TAG, e.getCode() + e.getMessage());
        throw e;
    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg != null)
            Log.d(DEBUG_TAG, msg);
        else
            Log.d(DEBUG_TAG, "get upload link error", e);
        throw SeafException.unknownException;
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) JSONException(org.json.JSONException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with HttpRequest

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

the class SeafConnection method getFileFromLink.

private File getFileFromLink(String dlink, String path, String localPath, String oid, ProgressMonitor monitor) throws SeafException {
    if (dlink == null)
        return null;
    File file = new File(localPath);
    try {
        int i = dlink.lastIndexOf('/');
        String quoted = dlink.substring(0, i) + "/" + URLEncoder.encode(dlink.substring(i + 1), "UTF-8");
        HttpRequest req = prepareApiFileGetRequest(quoted);
        checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
        if (monitor != null) {
            Long size = -1L;
            if (req.contentLength() > 0) {
                size = Long.valueOf(req.contentLength());
            } else {
                // The req.contentLength() returns an int, which has a max value of
                // 2GB. So if a file size exceeds 2GB, request.contentLength() would
                // return -1. In such case, we parse the content length from the raw
                // header string directly.
                // 
                // See https://github.com/kevinsawicki/http-request/blob/http-request-5.6/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java#L2519-L2521
                String contentLengthheader = req.header(HttpRequest.HEADER_CONTENT_LENGTH);
                // response, e.g. when the server is using chunked transfer encoding.
                if (contentLengthheader != null) {
                    size = Long.parseLong(contentLengthheader);
                }
            }
            if (size > 0) {
                monitor.onProgressNotify(size, false);
            }
        }
        File tmp = DataManager.createTempFile();
        // Log.d(DEBUG_TAG, "write to " + tmp.getAbsolutePath());
        if (monitor == null) {
            req.receive(tmp);
        } else {
            req.bufferSize(MonitoredFileOutputStream.BUFFER_SIZE);
            req.receive(new MonitoredFileOutputStream(tmp, monitor));
        }
        if (!tmp.renameTo(file)) {
            Log.w(DEBUG_TAG, "Rename file error");
            return null;
        }
        return file;
    } catch (SeafException e) {
        throw e;
    } catch (UnsupportedEncodingException e) {
        throw SeafException.encodingException;
    } catch (IOException e) {
        e.printStackTrace();
        throw SeafException.networkException;
    } catch (HttpRequestException e) {
        if (e.getCause() instanceof MonitorCancelledException) {
            // Log.d(DEBUG_TAG, "download is cancelled");
            throw SeafException.userCancelledException;
        } else {
            throw getSeafExceptionFromHttpRequestException(e);
        }
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) IOException(java.io.IOException) File(java.io.File)

Example 10 with HttpRequest

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

the class SeafConnection method getShareLink.

public String getShareLink(String repoID, String path) throws SeafException {
    try {
        Map<String, Object> params = Maps.newHashMap();
        params.put("repo_id", repoID);
        params.put("path", path);
        HttpRequest req = prepareApiGetRequest("api/v2.1/share-links", 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)

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