Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.microprofile.config.ConfigProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -64,10 +65,11 @@ public class RemoteKerConnection {

/**
*
* @param transferProcess The EDC transfer process this connection belongs to. Set to null for non-EDC connections.
* @param transferProcess The EDC transfer process this connection belongs to.
* Set to null for non-EDC connections.
*/
public RemoteKerConnection(MessageDispatcher dispatcher, KnowledgeEngineRuntimeConnectionDetails kerConnectionDetails,
TransferProcess transferProcess) {
public RemoteKerConnection(MessageDispatcher dispatcher,
KnowledgeEngineRuntimeConnectionDetails kerConnectionDetails, TransferProcess transferProcess) {
this.dispatcher = dispatcher;
this.remoteKerConnectionDetails = kerConnectionDetails;
this.transferProcess = transferProcess;
Expand Down Expand Up @@ -134,7 +136,7 @@ private void updateRemoteKerDataFromPeer() {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(new URI(this.remoteKerUri + "/runtimedetails"));
if (this.isEdcConnection())
requestBuilder = requestBuilder.setHeader("Authorization", this.transferProcess.authToken());

HttpRequest request = requestBuilder.GET().build();
HttpResponse<String> response = this.httpClient.send(request, BodyHandlers.ofString());
if (response.statusCode() == 200) {
Expand Down Expand Up @@ -227,9 +229,13 @@ public boolean representsKnowledgeBase(URI knowledgeBaseId) {
* @param kerDetails
*/
public void updateKerDetails(KnowledgeEngineRuntimeDetails kerDetails) {
// TODO implement checks?
this.remoteKerDetails = kerDetails;
dispatcher.notifySmartConnectorsChanged();
if (this.isAvailable()) {
LOG.trace("Updated KER '{}' Ker Details to {}.", this.getRemoteKerUri());
this.remoteKerDetails = kerDetails;
dispatcher.notifySmartConnectorsChanged();
} else {
LOG.trace("Not updated KER '{}' Ker Details due to unavailability.", this.getRemoteKerUri());
}
}

public void start() {
Expand All @@ -241,11 +247,12 @@ public void stop() {
try {
String ker_id = URLEncoder.encode(dispatcher.getMyKnowledgeEngineRuntimeDetails().getRuntimeId(),
StandardCharsets.UTF_8);

HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(new URI(this.remoteKerUri + "/runtimedetails/" + ker_id));

HttpRequest.Builder requestBuilder = HttpRequest
.newBuilder(new URI(this.remoteKerUri + "/runtimedetails/" + ker_id));
if (this.isEdcConnection())
requestBuilder = requestBuilder.headers("Authorization", this.transferProcess.authToken());

HttpRequest request = requestBuilder.DELETE().build();
HttpResponse<String> response = this.httpClient.send(request, BodyHandlers.ofString());
if (response.statusCode() == 204) {
Expand Down Expand Up @@ -290,39 +297,51 @@ public void sendToRemoteSmartConnector(KnowledgeMessage message) throws IOExcept
throw new IOException("KER " + this.remoteKerUri + " is currently unavailable. Trying again later.");
}

HttpResponse<String> response;
try {
String jsonMessage = objectMapper.writeValueAsString(MessageConverter.toJson(message));

HttpRequest.Builder requestBuilder = HttpRequest
.newBuilder(new URI(this.remoteKerUri + getPathForMessageType(message)))
.headers("Content-Type", "application/json");
if (this.isEdcConnection())
requestBuilder = requestBuilder.setHeader("Authorization", this.transferProcess.authToken());

HttpRequest request = requestBuilder.POST(BodyPublishers.ofString(jsonMessage)).build();
HttpResponse<String> response = this.httpClient.send(request, BodyHandlers.ofString());
response = this.httpClient.send(request, BodyHandlers.ofString());
} catch (URISyntaxException | InterruptedException | IOException | IllegalArgumentException e) {
this.remoteKerDetails = null;
int time = this.errorOccurred();
LOG.warn("Ignoring KER {} for {} minutes. Error '{}' occurred.", this.remoteKerUri, time, e.getMessage());
this.dispatcher.notifySmartConnectorsChanged();
throw new IOException(e);
}

if (response != null) {
// NOTE: 200 when using EDC, 202 otherwise
if (response.statusCode() == 200 || response.statusCode() == 202) {
if (response.statusCode() == 200 || response.statusCode() == 202) {
this.noError();
LOG.trace("Successfully sent message {} to {}", message.getMessageId(), this.remoteKerUri);
} else if (response.statusCode() >= 400 && response.statusCode() < 500) {
// these error status codes are not severe enough to set this KER to temporarily
// unavailable.
throw new IOException("Message not accepted by remote host, status code " + response.statusCode()
+ ", body " + response.body());
} else {
this.remoteKerDetails = null;
int time = this.errorOccurred();
LOG.warn("Ignoring KER {} for {} minutes. Failed to send message {} to {}, got response {}: {}",
this.remoteKerUri, time, message.getMessageId(), this.remoteKerUri, response.statusCode(),
response.body());
String msg = MessageFormatter.basicArrayFormat(
"Ignoring KER {} for {} minutes. Error statuscode '{}' occurred.",
new Object[] { this.remoteKerUri, time, response.statusCode() });
LOG.warn(msg);
this.dispatcher.notifySmartConnectorsChanged();
throw new IOException("Message not accepted by remote host, status code " + response.statusCode()
+ ", body " + response.body());
throw new IOException(msg);
}
} catch (URISyntaxException | InterruptedException | IOException | IllegalArgumentException e) {
this.remoteKerDetails = null;
int time = this.errorOccurred();
LOG.warn("Ignoring KER {} for {} minutes. Error '{}' occurred.", this.remoteKerUri, time,
e.getMessage());
this.dispatcher.notifySmartConnectorsChanged();
throw new IOException(e);
} else {
assert false;
LOG.error("Response should not be null when sending message to remote KER.");
}

}

public void sendMyKerDetailsToPeer(KnowledgeEngineRuntimeDetails details) {
Expand All @@ -339,7 +358,7 @@ public void sendMyKerDetailsToPeer(KnowledgeEngineRuntimeDetails details) {

if (this.isEdcConnection())
requestBuilder = requestBuilder.headers("Authorization", this.transferProcess.authToken());

HttpRequest request = requestBuilder.POST(BodyPublishers.ofString(jsonMessage)).build();
HttpResponse<String> response = this.httpClient.send(request, BodyHandlers.ofString());
if (response.statusCode() == 200) {
Expand All @@ -357,8 +376,7 @@ public void sendMyKerDetailsToPeer(KnowledgeEngineRuntimeDetails details) {
this.remoteKerDetails = null;
int time = this.errorOccurred();
this.dispatcher.notifySmartConnectorsChanged();
LOG.warn(
"Ignoring KER {} for {} minutes. Failed to send updated KnowledgeEngineRuntimeDetails due to '{}'",
LOG.warn("Ignoring KER {} for {} minutes. Failed to send updated KnowledgeEngineRuntimeDetails due to '{}'",
this.remoteKerUri, time, e.getMessage());
LOG.debug("", e);
}
Expand Down
Loading