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

Canary oct updates #67

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- PROJECT NAME -->
<name>Flexy Canary Connector</name>
<!-- PROJECT VERSION -->
<version>1.0.3</version>
<version>1.0.4</version>
<!-- PROJECT GROUP ID (PARENT PACKAGE) -->
<groupId>com.hms_networks.americas.sc</groupId>
<!-- PROJECT ARTIFACT ID (ROOT PACKAGE NAME) -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.hms_networks.americas.sc.extensions.system.http.SCHttpAuthException;
import com.hms_networks.americas.sc.extensions.system.http.SCHttpConnectionException;
import com.hms_networks.americas.sc.extensions.system.http.SCHttpEwonException;
import com.hms_networks.americas.sc.extensions.system.http.SCHttpUnknownException;
import com.hms_networks.americas.sc.extensions.system.http.requests.SCHttpPostRequestInfo;
import java.io.IOException;

Expand All @@ -18,19 +17,28 @@
*
* @author HMS Networks, MU Americas Solution Center
* @since 1.0.0
* @version 1.0.1
*/
public class CanaryApiRequestSender {

/**
* Send and parse an API POST request with the given information.
* Send and parse an API POST request with the given information. HTTP POST exceptions will be
* caught and logged and will result in an {@link CanaryApiResponseStatus } error response.
*
* @param request the {@link SCHttpPostRequestInfo} to hold all request information
* @return the status of the request
* @since 1.0.0
*/
public static CanaryApiResponseStatus processRequest(SCHttpPostRequestInfo request) {
CanaryApiResponseStatus status;
String responseBodyString = apiRequest(request);
String responseBodyString;

try {
responseBodyString = request.doRequest();
} catch (Exception e) {
logApiRequestException(e, request.getUrl());
return CanaryApiResponseStatus.ERROR;
}

// Parse response body for useful information
status = handleResponseBodyString(responseBodyString, request.getUrl());
Expand Down Expand Up @@ -72,40 +80,35 @@ private static CanaryApiResponseStatus handleResponseBodyString(
}

/**
* Send an API POST request with the given information.
* Check the type of the exception passed as a parameter. Log a specific message for that type.
*
* @param request {@link SCHttpPostRequestInfo} to hold all request information
* @return true if the request was successful
* @since 1.0.0
* @param exception {@link Exception} from HTTP POST request
* @param url request url that generated the exception
* @since 1.0.1
*/
private static String apiRequest(SCHttpPostRequestInfo request) {
String responseBodyString = "";
String url = request.getUrl();

try {
responseBodyString = request.doRequest();
} catch (EWException e) {
requestHttpsError(e, "Ewon exception during HTTP request to " + url + ".");
} catch (IOException e) {
requestHttpsError(e, "IO exception during HTTP request to " + url + ".");
} catch (SCHttpEwonException e) {
requestHttpsError(e, "Ewon exception during the HTTP request to " + url + ".");
} catch (SCHttpAuthException e) {
requestHttpsError(e, "Auth exception during the HTTP request to " + url + ".");
} catch (SCHttpConnectionException e) {
requestHttpsError(e, "Connection error during the HTTP request to " + url + ".");
} catch (SCHttpUnknownException e) {
requestHttpsError(e, "Unknown exception during the HTTP request to " + url + ".");
private static void logApiRequestException(Exception exception, String url) {

if (exception instanceof EWException) {
requestHttpsError(exception, "Ewon exception during HTTP request to " + url + ".");
} else if (exception instanceof IOException) {
requestHttpsError(exception, "IO exception during HTTP request to " + url + ".");
} else if (exception instanceof SCHttpEwonException) {
requestHttpsError(exception, "Ewon exception during the HTTP request to " + url + ".");
} else if (exception instanceof SCHttpAuthException) {
requestHttpsError(exception, "Auth exception during the HTTP request to " + url + ".");
} else if (exception instanceof SCHttpConnectionException) {
requestHttpsError(exception, "Connection error during the HTTP request to " + url + ".");
} else {
requestHttpsError(exception, "Unknown exception during the HTTP request to " + url + ".");
}

return responseBodyString;
}

/**
* Parse the Canary API response JSON for usable information.
*
* @param response the Canary API response JSON
* @param connectionUrl the URL that generated the response
* @param messageStatus the current {@link CanaryApiResponseStatus} of the API response
* @return true if the request was successful
* @since 1.0.0
*/
Expand Down Expand Up @@ -148,7 +151,7 @@ private static void processResponseStatus(CanaryApiResponseStatus status) {
Logger.LOG_CRITICAL("Unknown error detected. Request will be resent.");
} else if (status == CanaryApiResponseStatus.BAD_TOKENS) {
Logger.LOG_DEBUG("API Session tokens expired, refreshing session tokens.");
SessionManager.sendKeepAliveOrRefreshToken();
SessionManager.handleSessionTokenError();
} else if (status == CanaryApiResponseStatus.ERROR) {
Logger.LOG_CRITICAL("API error detected. Request will be resent.");
} else if (status == CanaryApiResponseStatus.ERROR_WAIT_FOR_EXPIRE) {
Expand All @@ -163,7 +166,8 @@ private static void processResponseStatus(CanaryApiResponseStatus status) {
* Parse the response JSON for generic response components.
*
* @param response JSON to read in and parse
* @return true if the response indicates success
* @param status the current {@link CanaryApiResponseStatus} of the API response
* @return updated {@link CanaryApiResponseStatus} based on the response content
* @throws JSONException on errors reading the JSON object
* @since 1.0.0
*/
Expand Down Expand Up @@ -265,6 +269,7 @@ private static void logApiError(String apiErrorMessage, int apiErrorMessageCount
* Parse the response JSON for user token response components.
*
* @param response JSON to read in and parse
* @param status the current {@link CanaryApiResponseStatus} of the API response
* @return true if the response indicates success
* @throws JSONException on errors reading the JSON object
* @since 1.0.0
Expand All @@ -286,7 +291,8 @@ private static CanaryApiResponseStatus checkUserTokenResponseJson(
* Parse the response JSON for session response components.
*
* @param response JSON to read in and parse
* @return true if the response indicates success
* @param status the current {@link CanaryApiResponseStatus} of the API response
* @return updated {@link CanaryApiResponseStatus} based on the content of the response
* @throws JSONException on errors reading the JSON object
* @since 1.0.0
*/
Expand All @@ -298,6 +304,7 @@ private static CanaryApiResponseStatus checkSessionTokenResponseJson(
if (response.has(SESSION_TOKEN_JSON_FIELD_NAME)) {
String sessionToken = response.getString(SESSION_TOKEN_JSON_FIELD_NAME);
SessionManager.setCurrentSessionToken(sessionToken);
SessionManager.updateTokenExpiration();
status = CanaryApiResponseStatus.GOOD_REQUEST;
}
return status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void run() {
if (!removed) {
Logger.LOG_WARN("Unable to remove payload from queue");
}
// for success, make sure data polling is not blocked
CanaryConnectorMain.getInstance().setDataPollingBlocked(false);
} else if (requestStatus == CanaryApiResponseStatus.ERROR_WAIT_FOR_EXPIRE) {
Logger.LOG_WARN(
"Waiting for existing sessions to expire before sending more data to Canary");
Expand All @@ -60,9 +62,17 @@ public void run() {
Logger.LOG_SERIOUS("An error occurred while waiting for existing sessions to expire.");
Logger.LOG_EXCEPTION(e);
}
} else if (requestStatus == CanaryApiResponseStatus.ERROR) {
Logger.LOG_WARN(
"Stopping reads of historical data due to an error sending a message to Canary");
// When payloads fail to send, historical data polling should be blocked
CanaryConnectorMain.getInstance().setDataPollingBlocked(true);
} else {
Logger.LOG_WARN("Unable to send payload to Canary");
}
} else {
// If there are no payloads to send, make sure data polling is not blocked
CanaryConnectorMain.getInstance().setDataPollingBlocked(false);
}

// thread finished
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/hms_networks/sc/canary/api/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @author HMS Networks, MU Americas Solution Center
* @since 1.0.0
* @version 1.0.1
*/
public class SessionManager {

Expand Down Expand Up @@ -39,6 +40,15 @@ public static void sendKeepAliveOrRefreshToken() {
}
}

/**
* Bypass session expiration check and force a new session token.
*
* @since 1.0.1
*/
public static void handleSessionTokenError() {
getSessionToken();
}

/**
* This method should be called to revoke the Canary API tokens when shutting down the connector.
*
Expand Down Expand Up @@ -81,7 +91,6 @@ private static void sendKeepAlive() {
SCHttpPostRequestInfo request =
CanaryApiRequestBuilder.getKeepAliveRequest(
getCurrentUserToken(), getCurrentSessionToken());
updateTokenExpiration();
CanaryApiRequestSender.processRequest(request);
}

Expand All @@ -103,7 +112,6 @@ private static void getUserToken() {
private static void getSessionToken() {
SCHttpPostRequestInfo request =
CanaryApiRequestBuilder.getSessionTokenRequest(getCurrentUserToken());
updateTokenExpiration();
CanaryApiRequestSender.processRequest(request);
}

Expand Down
2 changes: 1 addition & 1 deletion starting-files/jvmrun
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-heapsize 25M -classpath /usr/flexy-canary-connector-1.0.3-full.jar -emain com.hms_networks.sc.canary.CanaryConnectorMain
-heapsize 25M -classpath /usr/flexy-canary-connector-1.0.4-full.jar -emain com.hms_networks.sc.canary.CanaryConnectorMain
8 changes: 8 additions & 0 deletions web-docs/docs/02-CHANGELOG.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ sidebar_label: Change Log
toc_max_heading_level: 2
---

## Version 1.0.4
### Bug Fixes
- Fixed excessive memory usage during offline periods by halting historical data reading when
there is no connection.
- Fixed session management bug where session keep alive requests were sent with expired session token.
### Other
- Updates to Docs

## Version 1.0.3
### Features
- Added optional session configuration fields to allow for more control over the session settings
Expand Down
Loading