Search in sources :

Example 1 with GUID

use of weborb.v3types.GUID in project Android-SDK by Backendless.

the class Files method uploadFromStream.

public BackendlessFile uploadFromStream(OutputStreamRouter outputStreamRouter, String name, String path, boolean overwrite) throws Exception {
    HttpURLConnection connection = null;
    String CRLF = "\r\n";
    String boundary = (new GUID()).toString();
    try {
        String urlStr = Backendless.getUrl() + '/' + Backendless.getApplicationId() + '/' + Backendless.getSecretKey() + "/files/" + encodeURL(path) + "/" + encodeURL(name);
        if (overwrite)
            urlStr = urlStr + "?" + OVERWRITE_PARAMETER_NAME + "=" + overwrite;
        java.net.URL url = new URL(urlStr);
        connection = (HttpURLConnection) url.openConnection();
        connection.setChunkedStreamingMode(DEFAULT_CHUNK_SIZE);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        for (String key : HeadersManager.getInstance().getHeaders().keySet()) connection.addRequestProperty(key, HeadersManager.getInstance().getHeaders().get(key));
        try (OutputStream outputStream = connection.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true)) {
            writer.append("--").append(boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(name).append("\"").append(CRLF);
            writer.append("Content-Type: application/octet-stream").append(CRLF);
            writer.append("Content-Transfer-Encoding: binary").append(CRLF);
            writer.append(CRLF).flush();
            outputStreamRouter.writeStream(outputStream);
            outputStream.flush();
            writer.append(CRLF).flush();
            writer.append("--").append(boundary).append("--").append(CRLF);
        }
        if (connection.getResponseCode() != 200) {
            Scanner scanner = new Scanner(connection.getErrorStream());
            scanner.useDelimiter("\\Z");
            String response = scanner.next();
            scanner.close();
            Matcher matcher = SERVER_ERROR_PATTERN.matcher(response);
            String message = null;
            String code = null;
            while (matcher.find()) {
                message = matcher.group(MESSAGE_POSITION);
                code = matcher.group(CODE_POSITION);
            }
            throw new BackendlessException(code == null ? String.valueOf(connection.getResponseCode()) : code, message);
        } else {
            Scanner scanner = new Scanner(connection.getInputStream());
            scanner.useDelimiter("\\Z");
            String response = scanner.next();
            scanner.close();
            Matcher matcher = SERVER_RESULT_PATTERN.matcher(response);
            String fileUrl = null;
            while (matcher.find()) fileUrl = matcher.group(MESSAGE_POSITION);
            return new BackendlessFile(fileUrl);
        }
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(ExceptionMessage.FILE_UPLOAD_ERROR, e);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(ExceptionMessage.FILE_UPLOAD_ERROR, e);
    } catch (IOException e) {
        throw new BackendlessException(ExceptionMessage.FILE_UPLOAD_ERROR, e.getMessage());
    } finally {
        if (connection != null)
            connection.disconnect();
    }
}
Also used : BackendlessException(com.backendless.exceptions.BackendlessException) Scanner(java.util.Scanner) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) URL(java.net.URL) GUID(weborb.v3types.GUID) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) BackendlessFile(com.backendless.files.BackendlessFile)

Aggregations

BackendlessException (com.backendless.exceptions.BackendlessException)1 BackendlessFile (com.backendless.files.BackendlessFile)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Scanner (java.util.Scanner)1 Matcher (java.util.regex.Matcher)1 GUID (weborb.v3types.GUID)1