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

prepare 2.0.0 release #41

Merged
merged 36 commits into from
Jan 22, 2020
Merged
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
26dc142
add time threshold for backoff reset
eli-darkly Dec 11, 2018
881473f
Merge branch 'master' into eb/ch15975/backoff-reset
eli-darkly Dec 11, 2018
ca0865d
allow endpoint to be specified as either URI or HttpUrl
eli-darkly Dec 11, 2018
a35c7e6
add @since
eli-darkly Dec 11, 2018
8e4363b
add interface for customizing requests
eli-darkly Dec 11, 2018
54f11e0
Merge pull request #18 from launchdarkly/eb/ch15975/backoff-reset
eli-darkly Dec 13, 2018
7b104a0
Merge pull request #20 from launchdarkly/eb/ch28150/customize-request
eli-darkly Dec 13, 2018
32f80d8
Merge branch 'master' into eb/ch28144/okhttp-url
eli-darkly Dec 13, 2018
1bd8145
Merge pull request #19 from launchdarkly/eb/ch28144/okhttp-url
eli-darkly Dec 13, 2018
52d1732
javadoc fixes
eli-darkly Dec 13, 2018
e82ee5f
add changelog for past releases
eli-darkly Dec 13, 2018
5917b43
Merge branch 'master' of github.com:launchdarkly/okhttp-eventsource
eli-darkly Dec 13, 2018
d6a189b
remove JSR305
eli-darkly Mar 19, 2019
9674ef7
Merge pull request #21 from launchdarkly/eb/ch34379/annotations
eli-darkly Mar 20, 2019
ef15c12
Merge branch 'master' of github.com:launchdarkly/okhttp-eventsource
eli-darkly Mar 21, 2019
6d498fe
Merge branch 'master' of github.com:launchdarkly/okhttp-eventsource
eli-darkly Aug 1, 2019
890eada
replace SSL-specific config method with general-purpose HTTP config m…
eli-darkly Aug 1, 2019
7ba89f4
make helper method static
eli-darkly Aug 1, 2019
4acfb7e
Merge pull request #22 from launchdarkly/eb/ch44325/http-custom-config
eli-darkly Aug 1, 2019
95e4c37
add end-to-end EventSource tests
eli-darkly Aug 1, 2019
c93f4f0
spacing
eli-darkly Aug 1, 2019
0460bfe
Merge pull request #23 from launchdarkly/eb/ch28292/http-tests
eli-darkly Aug 1, 2019
5cd1b37
Merge branch 'master' of github.com:launchdarkly/okhttp-eventsource
eli-darkly Aug 1, 2019
64a67fc
Merge branch 'master' of github.com:launchdarkly/okhttp-eventsource
eli-darkly Aug 22, 2019
9f05a9a
omit default header value if there's a custom value
eli-darkly Oct 12, 2019
2e5354b
avoid trailing period in logger name
eli-darkly Oct 12, 2019
9f83742
Merge pull request #25 from launchdarkly/eb/ch52604/logger-name
eli-darkly Oct 18, 2019
3dd72a6
Merge pull request #24 from launchdarkly/eb/ch52602/override-header
eli-darkly Oct 18, 2019
b77ea93
merge from public after release
LaunchDarklyCI Oct 18, 2019
9874518
add 1.x branch
eli-darkly Jan 18, 2020
7ada28f
update to OkHttp 4.x and Java 8
eli-darkly Jan 18, 2020
bf2f83c
javadoc fixes
eli-darkly Jan 18, 2020
09d9173
Merge branch 'master' into eb/ch62055/okhttp-4.x
eli-darkly Jan 18, 2020
0c3695e
Merge pull request #26 from launchdarkly/eb/ch62055/okhttp-4.x
eli-darkly Jan 22, 2020
8c9ca57
remove EventSource setters, + test improvements
eli-darkly Jan 22, 2020
0c59325
Merge pull request #27 from launchdarkly/eb/ch62489/remove-setters
eli-darkly Jan 22, 2020
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
92 changes: 82 additions & 10 deletions src/main/java/com/launchdarkly/eventsource/EventSource.java
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ public class EventSource implements ConnectionHandler, Closeable {
static final int DEFAULT_BACKOFF_RESET_THRESHOLD_MS = 1000 * 60;

private final String name;
private volatile URI uri;
private volatile HttpUrl url;
private final Headers headers;
private final String method;
@Nullable private final RequestBody body;
@@ -71,7 +71,7 @@ public class EventSource implements ConnectionHandler, Closeable {
EventSource(Builder builder) {
this.name = builder.name;
this.logger = LoggerFactory.getLogger(EventSource.class.getCanonicalName() + "." + name);
this.uri = builder.uri;
this.url = builder.url;
this.headers = addDefaultHeaders(builder.headers);
this.method = builder.method;
this.body = builder.body;
@@ -110,7 +110,7 @@ public void start() {
return;
}
logger.debug("readyState change: " + RAW + " -> " + CONNECTING);
logger.info("Starting EventSource client using URI: " + uri);
logger.info("Starting EventSource client using URI: " + url);
streamExecutor.execute(new Runnable() {
public void run() {
connect();
@@ -164,7 +164,7 @@ public void close() {
Request buildRequest() {
Request.Builder builder = new Request.Builder()
.headers(headers)
.url(uri.toASCIIString())
.url(url)
.method(method, body);

if (lastEventId != null && !lastEventId.isEmpty()) {
@@ -209,7 +209,7 @@ private void connect() {
bufferedSource.close();
}
bufferedSource = Okio.buffer(response.body().source());
EventParser parser = new EventParser(uri, handler, EventSource.this);
EventParser parser = new EventParser(url.uri(), handler, EventSource.this);
for (String line; !Thread.currentThread().isInterrupted() && (line = bufferedSource.readUtf8LineStrict()) != null; ) {
parser.line(line);
}
@@ -350,14 +350,53 @@ public void setLastEventId(String lastEventId) {
this.lastEventId = lastEventId;
}

/**
* Returns the current stream endpoint as an OkHttp HttpUrl.
*
* @since 1.9.0
*/
public HttpUrl getHttpUrl() {
return this.url;
}

/**
* Returns the current stream endpoint as a java.net.URI.
*/
public URI getUri() {
return this.uri;
return this.url.uri();
}

/**
* Changes the stream endpoint. This change will not take effect until the next time the
* EventSource attempts to make a connection.
*
* @param url the new endpoint, as an OkHttp HttpUrl
* @throws IllegalArgumentException if the parameter is null or if the scheme is not HTTP or HTTPS
*
* @since 1.9.0
*/
public void setHttpUrl(HttpUrl url) {
if (url == null) {
throw badUrlException();
}
this.url = url;
}

/**
* Changes the stream endpoint. This change will not take effect until the next time the
* EventSource attempts to make a connection.
*
* @param url the new endpoint, as a java.net.URI
* @throws IllegalArgumentException if the parameter is null or if the scheme is not HTTP or HTTPS
*/
public void setUri(URI uri) {
this.uri = uri;
setHttpUrl(uri == null ? null : HttpUrl.get(uri));
}

private static IllegalArgumentException badUrlException() {
return new IllegalArgumentException("URI/URL must not be null and must be HTTP or HTTPS");
}

/**
* Interface for an object that can modify the network request that the EventSource will make.
* Use this in conjunction with {@link Builder#requestTransformer} if you need to set request
@@ -388,12 +427,15 @@ public static interface RequestTransformer {
public Request transformRequest(Request input);
}

/**
* Builder for {@link EventSource}.
*/
public static final class Builder {
private String name = "";
private long reconnectTimeMs = DEFAULT_RECONNECT_TIME_MS;
private long maxReconnectTimeMs = DEFAULT_MAX_RECONNECT_TIME_MS;
private long backoffResetThresholdMs = DEFAULT_BACKOFF_RESET_THRESHOLD_MS;
private final URI uri;
private final HttpUrl url;
private final EventHandler handler;
private ConnectionErrorHandler connectionErrorHandler = ConnectionErrorHandler.DEFAULT;
private Headers headers = Headers.of();
@@ -409,11 +451,37 @@ public static final class Builder {
.writeTimeout(DEFAULT_WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(true);

/**
* Creates a new builder.
*
* @param handler the event handler
* @param uri the endpoint as a java.net.URI
* @throws IllegalArgumentException if either argument is null, or if the endpoint is not HTTP or HTTPS
*/
public Builder(EventHandler handler, URI uri) {
this.uri = uri;
this.handler = handler;
this(handler, uri == null ? null : HttpUrl.get(uri));
}

/**
* Creates a new builder.
*
* @param handler the event handler
* @param uri the endpoint as an OkHttp HttpUrl
* @throws IllegalArgumentException if either argument is null, or if the endpoint is not HTTP or HTTPS
*
* @since 1.9.0
*/
public Builder(EventHandler handler, HttpUrl url) {
if (handler == null) {
throw new IllegalArgumentException("handler must not be null");
}
if (url == null) {
throw badUrlException();
}
this.url = url;
this.handler = handler;
}

/**
* Set the HTTP method used for this EventSource client to use for requests to establish the EventSource.
*
@@ -613,6 +681,10 @@ public Builder connectionErrorHandler(ConnectionErrorHandler handler) {
return this;
}

/**
* Constructs an {@link EventSource} using the builder's current properties.
* @return the new EventSource instance
*/
public EventSource build() {
if (proxy != null) {
clientBuilder.proxy(proxy);
71 changes: 70 additions & 1 deletion src/test/java/com/launchdarkly/eventsource/EventSourceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.launchdarkly.eventsource;

import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -20,16 +21,84 @@
import static org.mockito.Mockito.mock;

public class EventSourceTest {
private static final URI STREAM_URI = URI.create("http://www.example.com/");
private static final HttpUrl STREAM_HTTP_URL = HttpUrl.parse("http://www.example.com/");
private EventSource eventSource;
private EventSource.Builder builder;

@Before
public void setUp() {
EventHandler eventHandler = mock(EventHandler.class);
builder = new EventSource.Builder(eventHandler, URI.create("http://www.example.com"));
builder = new EventSource.Builder(eventHandler, STREAM_URI);
eventSource = builder.build();
}

@Test
public void hasExpectedUri() {
assertEquals(STREAM_URI, eventSource.getUri());
}

@Test
public void hasExpectedUriWhenInitializedWithHttpUrl() {
EventSource es = new EventSource.Builder(mock(EventHandler.class), STREAM_HTTP_URL).build();
assertEquals(STREAM_URI, es.getUri());
}

@Test
public void hasExpectedHttpUrlWhenInitializedWithUri() {
assertEquals(STREAM_HTTP_URL, eventSource.getHttpUrl());
}

@Test
public void hasExpectedHttpUrlWhenInitializedWithHttpUrl() {
EventSource es = new EventSource.Builder(mock(EventHandler.class), STREAM_HTTP_URL).build();
assertEquals(STREAM_HTTP_URL, es.getHttpUrl());
}

@Test(expected=IllegalArgumentException.class)
public void handlerCannotBeNull() {
new EventSource.Builder(null, STREAM_URI);
}

@Test(expected=IllegalArgumentException.class)
public void uriCannotBeNull() {
new EventSource.Builder(mock(EventHandler.class), (URI)null);
}

@Test(expected=IllegalArgumentException.class)
public void httpUrlCannotBeNull() {
new EventSource.Builder(mock(EventHandler.class), (HttpUrl)null);
}

@Test
public void canSetUri() {
URI uri = URI.create("http://www.other.com/");
eventSource.setUri(uri);
assertEquals(uri, eventSource.getUri());
}

@Test(expected=IllegalArgumentException.class)
public void cannotSetUriToNull() {
eventSource.setUri(null);
}

@Test(expected=IllegalArgumentException.class)
public void cannotSetUriToInvalidScheme() {
eventSource.setUri(URI.create("gopher://example.com/"));
}

@Test
public void canSetHttpUrl() {
HttpUrl url = HttpUrl.parse("http://www.other.com/");
eventSource.setHttpUrl(url);
assertEquals(url, eventSource.getHttpUrl());
}

@Test(expected=IllegalArgumentException.class)
public void cannotSetHttpUrlToNull() {
eventSource.setHttpUrl(null);
}

@Test
public void respectsDefaultMaximumBackoffTime() {
eventSource.setReconnectionTimeMs(2000);