Commit 4d84c1eb authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Add getContentType(...) and getDefaultHttpClient() method.

parent 0c75c04f
package chat.rocket.android.helper; package chat.rocket.android.helper;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull;
import com.facebook.stetho.okhttp3.StethoInterceptor; import com.facebook.stetho.okhttp3.StethoInterceptor;
import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.rest.CookieInterceptor; import chat.rocket.android.api.rest.CookieInterceptor;
import chat.rocket.android.api.rest.DefaultCookieProvider; import chat.rocket.android.api.rest.DefaultCookieProvider;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/** /**
* Helper class for OkHttp client. * Helper class for OkHttp client.
*/ */
public class OkHttpHelper { public class OkHttpHelper {
private static OkHttpClient defaultHttpClient;
private static OkHttpClient httpClientForUploadFile; private static OkHttpClient httpClientForUploadFile;
private static OkHttpClient httpClientForDownloadFile; private static OkHttpClient httpClientForDownloadFile;
private static OkHttpClient httpClientForWS; private static OkHttpClient httpClientForWS;
private static String contentType;
public static OkHttpClient getDefaultHttpClient() {
if (defaultHttpClient == null) {
defaultHttpClient = new OkHttpClient();
}
return defaultHttpClient;
}
public static OkHttpClient getClientForDownloadFile(Context context) { public static OkHttpClient getClientForDownloadFile(Context context) {
if (httpClientForDownloadFile == null) { if (httpClientForDownloadFile == null) {
...@@ -48,4 +63,28 @@ public class OkHttpHelper { ...@@ -48,4 +63,28 @@ public class OkHttpHelper {
} }
return httpClientForWS; return httpClientForWS;
} }
public static String getContentType(String uri) {
Request request = new Request.Builder()
.url(uri)
.head()
.build();
getDefaultHttpClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException ioException) {
ioException.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull final Response response)
throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code: " + response);
} else {
contentType = response.header("Content-Type");
}
}
});
return contentType;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment