Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
<!--Test Dependencies-->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
4 changes: 2 additions & 2 deletions samples/example-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>com.exceptionless</groupId>
<artifactId>example-app</artifactId>
<version>1.0-beta1</version>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>com.exceptionless</groupId>
<artifactId>exceptionless-client</artifactId>
<version>1.0</version>
<version>1.0-beta1</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
package com.exceptionless.sample.app;

import com.exceptionless.exceptionlessclient.ExceptionlessClient;
import com.exceptionless.exceptionlessclient.models.EventPluginContext;

public class Main {
public static void main(String[] args) {
ExceptionlessClient client =
ExceptionlessClient.from(
System.getenv("EXCEPTIONLESS_SAMPLE_APP_API_KEY"),
System.getenv("EXCEPTIONLESS_SAMPLE_APP_SERVER_URL"));

client.getConfigurationManager().useSessions();

client.submitSessionStart();
private static final ExceptionlessClient client =
ExceptionlessClient.from(
System.getenv("EXCEPTIONLESS_SAMPLE_APP_API_KEY"),
System.getenv("EXCEPTIONLESS_SAMPLE_APP_SERVER_URL"));

public static void sampleEventSubmissions() {
client.submitException(new RuntimeException("Test exception"));
client.submitUnhandledException(new RuntimeException("Test exception"),"Test submission method");
client.submitUnhandledException(
new RuntimeException("Test exception"), "Test submission method");
client.submitFeatureUsage("Test feature");
client.submitLog("Test log");
client.submitNotFound("Test resource");
}

client.submitSessionEnd("Test user id");
public static void sampleUseOfSessions() {
client.getConfigurationManager().useSessions();
client.submitEvent(
EventPluginContext.from(client.createSessionStart().userIdentity("test-user").build()));
client.submitSessionEnd("test-user");
}

public static void sampleUseOfUpdatingEmailAndDescription() {
client.submitEvent(
EventPluginContext.from(
client.createLog("test-log").referenceId("test-reference-id").build()));
client.updateEmailAndDescription("test-reference-id", "test@email.com", "test-description");
}

public static void main(String[] args) {
sampleEventSubmissions();
sampleUseOfUpdatingEmailAndDescription();
sampleUseOfSessions();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.exceptionless.exceptionlessclient.exceptions;

public class SettingsClientException extends RuntimeException{
public SettingsClientException(Throwable cause) {
super(cause);
}

public SettingsClientException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.exceptionless.exceptionlessclient.exceptions;

public class SubmissionClientException extends RuntimeException {
public SubmissionClientException(Throwable cause) {
super(cause);
}

public SubmissionClientException(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
@Value
@NonFinal
public class SettingsResponse {
@Builder.Default Boolean success = false;
int code;
String body;
ServerSettings settings;
Exception exception;
String message;

public Boolean isSuccess() {
return success;
public boolean isSuccess() {
return code >= 200 && code <= 299;
}

public boolean isNotModified() {
return code == 304;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@
@Value
@NonFinal
public class SubmissionResponse {
int statusCode;
String message;
int code;
String body;

public boolean isSuccess() {
return statusCode >= 200 && statusCode <= 299;
return code >= 200 && code <= 299;
}

public boolean isBadRequest() {
return statusCode == 400;
return code == 400;
}

public boolean isServiceUnavailable() {
return statusCode == 503;
return code == 503;
}

public boolean isPaymentRequired() {
return statusCode == 402;
return code == 402;
}

public boolean unableToAuthenticate() {
return statusCode == 401 || statusCode == 403;
return code == 401 || code == 403;
}

public boolean isNotFound() {
return statusCode == 404;
return code == 404;
}

public boolean isRequestEntityTooLarge() {
return statusCode == 413;
return code == 413;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.exceptionless.exceptionlessclient.queue;

import com.exceptionless.exceptionlessclient.configuration.Configuration;
import com.exceptionless.exceptionlessclient.exceptions.SubmissionException;
import com.exceptionless.exceptionlessclient.exceptions.SubmissionClientException;
import com.exceptionless.exceptionlessclient.models.Event;
import com.exceptionless.exceptionlessclient.models.storage.StorageItem;
import com.exceptionless.exceptionlessclient.models.submission.SubmissionResponse;
Expand Down Expand Up @@ -83,7 +83,7 @@ private boolean shouldSuspendProcessing() {
}

@VisibleForTesting
Boolean isProcessingCurrentlySuspended(){
Boolean isProcessingCurrentlySuspended() {
return shouldSuspendProcessing();
}

Expand Down Expand Up @@ -113,7 +113,7 @@ private boolean shouldDiscard() {

@Override
public void process() {
synchronized (this){
synchronized (this) {
if (processingQueue) {
LOG.trace("Currently processing queue; Returning...");
return;
Expand All @@ -136,11 +136,11 @@ public void process() {
SubmissionResponse response = submissionClient.postEvents(events);
processSubmissionResponse(response, storedEvents);
eventPosted(response, events);
} catch (SubmissionException e) {
LOG.error("Error processing queue", e);
} catch (SubmissionClientException e) {
LOG.error("Error submitting events from queue", e);
suspendProcessing();
} finally {
synchronized (this){
synchronized (this) {
processingQueue = false;
}
}
Expand Down Expand Up @@ -176,7 +176,10 @@ private void processSubmissionResponse(
}

if (response.isNotFound() || response.isBadRequest()) {
LOG.error(String.format("Error while trying to submit data: %s", response.getMessage()));
LOG.error(
String.format(
"Error while trying to submit data, Code:%s, Body:%s",
response.getCode(), response.getBody()));
suspendProcessing(Duration.ofMinutes(4));
removeEvents(storedEvents);
return;
Expand All @@ -195,7 +198,9 @@ private void processSubmissionResponse(
return;
}

LOG.error(String.format("Error submitting events: %s", response.getMessage()));
LOG.error(
String.format(
"Error submitting events, Code: %s, Body: %s", response.getCode(), response.getBody()));
suspendProcessing();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
package com.exceptionless.exceptionlessclient.settings;

import com.exceptionless.exceptionlessclient.configuration.Configuration;
import com.exceptionless.exceptionlessclient.exceptions.SettingsClientException;
import com.exceptionless.exceptionlessclient.models.submission.SettingsResponse;
import com.exceptionless.exceptionlessclient.utils.Utils;
import com.exceptionless.exceptionlessclient.utils.VisibleForTesting;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Builder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class DefaultSettingsClient implements SettingsClientIF {
private final Configuration configuration;
private final HttpClient httpClient;
private final OkHttpClient httpClient;

@Builder
public DefaultSettingsClient(Configuration configuration) {
this.configuration = configuration;
this.httpClient = HttpClient.newHttpClient();
this.httpClient =
new OkHttpClient()
.newBuilder()
.connectTimeout(Duration.ofMillis(configuration.getSettingsClientTimeoutInMillis()))
.build();
}

@VisibleForTesting
DefaultSettingsClient(Configuration configuration, HttpClient httpClient) {
DefaultSettingsClient(Configuration configuration, OkHttpClient httpClient) {
this.configuration = configuration;
this.httpClient = httpClient;
}

@Override
public SettingsResponse getSettings(long version) {
try {
URI uri =
new URI(
String.format(
"%s/api/v2/projects/config?v=%s&access_token=%s",
configuration.getServerUrl(), version, configuration.getApiKey()));

HttpRequest request =
HttpRequest.newBuilder()
.uri(uri)
.GET()
.header("User-Agent", Configuration.USER_AGENT)
.timeout(Duration.ofMillis(configuration.getSettingsClientTimeoutInMillis()))
Request request =
new Request.Builder()
.url(
String.format(
"%s/api/v2/projects/config?v=%s&access_token=%s",
configuration.getServerUrl(), version, configuration.getApiKey()))
.get()
.build();

HttpResponse<String> response =
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
Response response = httpClient.newCall(request).execute();

if (response.statusCode() != 200) {
return SettingsResponse.builder().success(false).message(response.body()).build();
ResponseBody body = response.body();
String bodyStr = body == null ? null : body.string();
if (bodyStr == null) {
return SettingsResponse.builder().code(response.code()).body("").build();
}
if (response.code() / 100 != 2) {
return SettingsResponse.builder().code(response.code()).body(bodyStr).build();
}

ServerSettings serverSettings =
Utils.JSON_MAPPER.readValue(response.body(), new TypeReference<ServerSettings>() {});

return SettingsResponse.builder().success(true).settings(serverSettings).build();
Utils.JSON_MAPPER.readValue(bodyStr, new TypeReference<ServerSettings>() {});
return SettingsResponse.builder()
.code(response.code())
.body(bodyStr)
.settings(serverSettings)
.build();
} catch (Exception e) {
return SettingsResponse.builder().success(false).exception(e).message(e.getMessage()).build();
throw new SettingsClientException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.exceptionless.exceptionlessclient.settings;

import com.exceptionless.exceptionlessclient.exceptions.SettingsClientException;
import com.exceptionless.exceptionlessclient.models.storage.StorageItem;
import com.exceptionless.exceptionlessclient.models.submission.SettingsResponse;
import com.exceptionless.exceptionlessclient.storage.StorageProviderIF;
Expand Down Expand Up @@ -66,20 +67,39 @@ public void updateSettings() {

try {
long currentVersion = getVersion();
LOG.info(String.format("Checking for updated settings from: v%s", currentVersion));
LOG.info(String.format("Checking for updated settings from v%s", currentVersion));

SettingsResponse response = settingsClient.getSettings(currentVersion);
if (!response.isSuccess()) {
LOG.warn(String.format("Unable to update settings: %s", response.getMessage()));
if (shouldNotUpdate(response)) {
return;
}

ServerSettings prevValue = getSavedServerSettings();
storageProvider.getSettings().save(response.getSettings());
propertyChangeSupport.firePropertyChange("settings", prevValue, response.getSettings());
} catch (SettingsClientException e) {
LOG.error(String.format("Error retrieving settings for v%s", getVersion()), e);
} finally {
synchronized (this) {
updatingSettings = false;
}
}
}

private boolean shouldNotUpdate(SettingsResponse response) {
if (response.isNotModified()) {
LOG.info("No need to update, settings are not modified");
return true;
}
if (!response.isSuccess()) {
LOG.warn(String.format("Unable to update settings: %s", response.getBody()));
return true;
}
if (response.getSettings() == null) {
LOG.warn("Not settings returned by server!");
return true;
}

return false;
}
}
Loading