Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional proxy authentication. Improved connection closing. #18

Merged
merged 2 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {

allprojects {
group = 'com.launchdarkly'
version = "1.1.1"
version = "1.2.0-SNAPSHOT"
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
Expand Down
50 changes: 43 additions & 7 deletions src/main/java/com/launchdarkly/eventsource/EventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class EventSource implements ConnectionHandler, Closeable {
private final OkHttpClient client;
private volatile Call call;
private final Random jitter = new Random();
private BufferedSource bufferedSource = null;

EventSource(Builder builder) {
this.uri = builder.uri;
Expand All @@ -52,14 +53,20 @@ public class EventSource implements ConnectionHandler, Closeable {
this.executor = Executors.newCachedThreadPool(threadFactory);
this.handler = new AsyncEventHandler(this.executor, builder.handler);
this.readyState = new AtomicReference<>(RAW);
this.client = builder.client.newBuilder()

OkHttpClient.Builder clientBuilder = builder.client.newBuilder()
.connectionPool(new ConnectionPool(1, 1, TimeUnit.SECONDS))
.readTimeout(0, TimeUnit.SECONDS)
.writeTimeout(0, TimeUnit.SECONDS)
.connectTimeout(0, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.proxy(builder.proxy)
.build();
.proxy(builder.proxy);

if (builder.proxyAuthenticator != null) {
clientBuilder.proxyAuthenticator(builder.proxyAuthenticator);
}

this.client = clientBuilder.build();
}

public void start() {
Expand Down Expand Up @@ -95,6 +102,9 @@ public void close() throws IOException {
}
}
executor.shutdownNow();
if (bufferedSource != null) {
bufferedSource.close();
}

if (client != null) {
if (client.connectionPool() != null) {
Expand Down Expand Up @@ -143,9 +153,12 @@ private void connect() {
} catch (Exception e) {
handler.onError(e);
}
BufferedSource bs = Okio.buffer(response.body().source());
if (bufferedSource != null) {
bufferedSource.close();
}
bufferedSource = Okio.buffer(response.body().source());
EventParser parser = new EventParser(uri, handler, EventSource.this);
for (String line; !Thread.currentThread().isInterrupted() && (line = bs.readUtf8LineStrict()) != null; ) {
for (String line; !Thread.currentThread().isInterrupted() && (line = bufferedSource.readUtf8LineStrict()) != null; ) {
parser.line(line);
}
} else {
Expand Down Expand Up @@ -255,6 +268,7 @@ public static final class Builder {
private final EventHandler handler;
private Headers headers = Headers.of();
private Proxy proxy;
private Authenticator proxyAuthenticator = null;
private OkHttpClient client = new OkHttpClient();

public Builder(EventHandler handler, URI uri) {
Expand Down Expand Up @@ -304,8 +318,30 @@ public Builder client(OkHttpClient client) {
* @return the builder
*/
public Builder proxy(String proxyHost, int proxyPort) {
proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
return this;
proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
return this;
}

/**
* Set the {@link Proxy} to be used to make the EventSource connection.
*
* @param proxy the proxy
* @return the builder
*/
public Builder proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}

/**
* Sets the Proxy Authentication mechanism if needed. Defaults to no auth.
*
* @param proxyAuthenticator
* @return
*/
public Builder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.proxyAuthenticator = proxyAuthenticator;
return this;
}

public EventSource build() {
Expand Down