Skip to content

Commit

Permalink
HttpStreamRequestImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Nov 29, 2023
1 parent c39a3c0 commit 900c447
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.split.android.client.network;

public abstract class BaseHttpResponseImpl implements BaseHttpResponse {
private static final int HTTP_OK = 200;

private static final int HTTP_OK = 200;
protected static final int HTTP_MULTIPLE_CHOICES = 300;
protected static final int HTTP_UNAUTHORIZED = 401;
protected static final int HTTP_BAD_REQUEST = 400;
Expand All @@ -15,7 +16,7 @@ protected BaseHttpResponseImpl(int httpStatus) {

@Override
public boolean isSuccess() {
return mHttpStatus >= HTTP_OK && mHttpStatus< HTTP_MULTIPLE_CHOICES;
return mHttpStatus >= HTTP_OK && mHttpStatus < HTTP_MULTIPLE_CHOICES;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.split.android.client.network;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -14,27 +16,27 @@
import java.util.Map;

import io.split.android.client.utils.logger.Logger;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.google.common.base.Preconditions.checkNotNull;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

public class HttpStreamRequestImpl implements HttpStreamRequest {
private static final MediaType JSON
= MediaType.get("application/json; charset=utf-8");
private OkHttpClient mOkHttpClient;
private URI mUri;
private Map<String, String> mHeaders;
private Response mOkHttpResponse;

private final URI mUri;
private final Map<String, String> mHeaders;
private final SSLSocketFactory mSslSocketFactory;
private HttpURLConnection mConnection;
private BufferedReader mResponseBufferedReader;
private InputStream mResponseInputStream;

HttpStreamRequestImpl(@NonNull OkHttpClient okHttpClient, @NonNull URI uri,
@NonNull Map<String, String> headers) {
mOkHttpClient = checkNotNull(okHttpClient);
private HttpStreamRequestImpl(@NonNull URI uri,
@NonNull Map<String, String> headers,
@Nullable SSLSocketFactory sslSocketFactory) {
mUri = checkNotNull(uri);
mHeaders = new HashMap<>(checkNotNull(headers));
mSslSocketFactory = sslSocketFactory;
}

@Override
Expand All @@ -50,39 +52,47 @@ public void addHeader(String name, String value) {
@Override
public void close() {
Logger.d("Closing streaming connection");
if (mOkHttpResponse != null) {
if (mOkHttpResponse.body() != null) {
try {
mOkHttpResponse.close();
mOkHttpResponse.body().close();
} catch (Exception e) {
Logger.d("Unknown error closing streaming connection: " + e.getLocalizedMessage());
}
if (mResponseInputStream != null) {
try {
mResponseInputStream.close();
} catch (IOException e) {
Logger.d("Unknown error closing streaming connection: " + e.getLocalizedMessage());
} catch (Exception e) {
Logger.d("Unknown error closing stream: " + e.getLocalizedMessage());
}
if (mResponseBufferedReader != null) {
try {
mResponseBufferedReader.close();
} catch (IOException e) {
Logger.d("Buffer already closed");
} catch (Exception e) {
Logger.d("Unknown error closing buffer: " + e.getLocalizedMessage());
}
}
if (mResponseBufferedReader != null) {
try {
mResponseBufferedReader.close();
} catch (IOException e) {
Logger.d("Buffer already closed");
} catch (Exception e) {
Logger.d("Unknown error closing buffer: " + e.getLocalizedMessage());
}
}
mConnection.disconnect();
Logger.d("Streaming connection closed");
}

private HttpStreamResponse getRequest() throws HttpException {
URL url;
HttpStreamResponse response;
try {
url = mUri.toURL();
Request.Builder requestBuilder = new Request.Builder()
.url(url);
addHeaders(requestBuilder);
Request okHttpRequest = requestBuilder.build();
mConnection = (HttpURLConnection) url.openConnection();
addHeaders(mConnection);

if (mSslSocketFactory != null) {
Logger.d("Setting SSL socket factory in stream request");
if (mConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) mConnection).setSSLSocketFactory(mSslSocketFactory);
} else {
Logger.d("Failed to set SSL socket factory in stream request. Connection is not SSL");
}
}

mOkHttpResponse = mOkHttpClient.newCall(okHttpRequest).execute();
response = buildResponse(mOkHttpResponse);
mConnection.connect();
response = buildResponse(mConnection);

} catch (MalformedURLException e) {
throw new HttpException("URL is malformed: " + e.getLocalizedMessage());
Expand All @@ -94,18 +104,25 @@ private HttpStreamResponse getRequest() throws HttpException {
return response;
}

private void addHeaders(Request.Builder request) {
private void addHeaders(HttpURLConnection request) {
if (mHeaders == null) {
return;
}

for (Map.Entry<String, String> entry : mHeaders.entrySet()) {
request.header(entry.getKey(), entry.getValue());
request.setRequestProperty(entry.getKey(), entry.getValue());
}
}

private HttpStreamResponse buildResponse(Response okHttpResponse) throws IOException {
int responseCode = okHttpResponse.code();
if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < 300 && okHttpResponse.body() != null) {
mResponseBufferedReader = new BufferedReader(new InputStreamReader(
okHttpResponse.body().byteStream()));
return new HttpStreamResponseImpl(responseCode, mResponseBufferedReader);
private HttpStreamResponse buildResponse(HttpURLConnection connection) throws IOException {
int responseCode = connection.getResponseCode();
if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_MULT_CHOICE) {
mResponseInputStream = connection.getInputStream();
if (mResponseInputStream != null) {
mResponseBufferedReader = new BufferedReader(new InputStreamReader(mResponseInputStream));

return new HttpStreamResponseImpl(responseCode, mResponseBufferedReader);
}
}
return new HttpStreamResponseImpl(responseCode);
}
Expand Down

0 comments on commit 900c447

Please sign in to comment.