Search in sources :

Example 1 with StandardUserAgentInterceptor

use of org.thoughtcrime.securesms.net.StandardUserAgentInterceptor in project Signal-Android by signalapp.

the class SubmitDebugLogRepository method uploadContent.

@WorkerThread
@NonNull
private String uploadContent(@NonNull String contentType, @NonNull RequestBody requestBody) throws IOException {
    try {
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new StandardUserAgentInterceptor()).dns(SignalServiceNetworkAccess.DNS).build();
        Response response = client.newCall(new Request.Builder().url(API_ENDPOINT).get().build()).execute();
        ResponseBody body = response.body();
        if (!response.isSuccessful() || body == null) {
            throw new IOException("Unsuccessful response: " + response);
        }
        JSONObject json = new JSONObject(body.string());
        String url = json.getString("url");
        JSONObject fields = json.getJSONObject("fields");
        String item = fields.getString("key");
        MultipartBody.Builder post = new MultipartBody.Builder();
        Iterator<String> keys = fields.keys();
        post.addFormDataPart("Content-Type", contentType);
        while (keys.hasNext()) {
            String key = keys.next();
            post.addFormDataPart(key, fields.getString(key));
        }
        post.addFormDataPart("file", "file", requestBody);
        Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
        if (!postResponse.isSuccessful()) {
            throw new IOException("Bad response: " + postResponse);
        }
        return API_ENDPOINT + "/" + item;
    } catch (JSONException e) {
        Log.w(TAG, "Error during upload.", e);
        throw new IOException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) JSONObject(org.json.JSONObject) StandardUserAgentInterceptor(org.thoughtcrime.securesms.net.StandardUserAgentInterceptor) MultipartBody(okhttp3.MultipartBody) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 2 with StandardUserAgentInterceptor

use of org.thoughtcrime.securesms.net.StandardUserAgentInterceptor in project Signal-Android by WhisperSystems.

the class SubmitDebugLogRepository method uploadContent.

@WorkerThread
@NonNull
private String uploadContent(@NonNull String contentType, @NonNull RequestBody requestBody) throws IOException {
    try {
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new StandardUserAgentInterceptor()).dns(SignalServiceNetworkAccess.DNS).build();
        Response response = client.newCall(new Request.Builder().url(API_ENDPOINT).get().build()).execute();
        ResponseBody body = response.body();
        if (!response.isSuccessful() || body == null) {
            throw new IOException("Unsuccessful response: " + response);
        }
        JSONObject json = new JSONObject(body.string());
        String url = json.getString("url");
        JSONObject fields = json.getJSONObject("fields");
        String item = fields.getString("key");
        MultipartBody.Builder post = new MultipartBody.Builder();
        Iterator<String> keys = fields.keys();
        post.addFormDataPart("Content-Type", contentType);
        while (keys.hasNext()) {
            String key = keys.next();
            post.addFormDataPart(key, fields.getString(key));
        }
        post.addFormDataPart("file", "file", requestBody);
        Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
        if (!postResponse.isSuccessful()) {
            throw new IOException("Bad response: " + postResponse);
        }
        return API_ENDPOINT + "/" + item;
    } catch (JSONException e) {
        Log.w(TAG, "Error during upload.", e);
        throw new IOException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) JSONObject(org.json.JSONObject) StandardUserAgentInterceptor(org.thoughtcrime.securesms.net.StandardUserAgentInterceptor) MultipartBody(okhttp3.MultipartBody) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)2 WorkerThread (androidx.annotation.WorkerThread)2 IOException (java.io.IOException)2 MultipartBody (okhttp3.MultipartBody)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 ResponseBody (okhttp3.ResponseBody)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 StandardUserAgentInterceptor (org.thoughtcrime.securesms.net.StandardUserAgentInterceptor)2