Skip to content

Commit 326fd52

Browse files
committed
add loggging and add multiple ASTM ahndler support
1 parent f978818 commit 326fd52

File tree

11 files changed

+119
-32
lines changed

11 files changed

+119
-32
lines changed

astm-http-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.itech</groupId>
66
<artifactId>astm-http-lib</artifactId>
7-
<version>2.1.0</version>
7+
<version>2.2.0</version>
88

99
<properties>
1010
<java.version>21</java.version>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.itech.ahb.lib.astm;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NonNull;
6+
import lombok.RequiredArgsConstructor;
7+
import org.itech.ahb.lib.astm.servlet.ASTMHandler;
8+
import org.itech.ahb.lib.common.HandleStatus;
9+
10+
@Data
11+
@AllArgsConstructor
12+
@RequiredArgsConstructor
13+
public class ASTMHandlerResponse {
14+
15+
@NonNull
16+
String response;
17+
18+
@NonNull
19+
HandleStatus status;
20+
21+
Boolean communicateResponse = false;
22+
23+
@NonNull
24+
ASTMHandler handler;
25+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.itech.ahb.lib.astm.servlet;
22

3+
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
34
import org.itech.ahb.lib.common.ASTMMessage;
4-
import org.itech.ahb.lib.common.HandleStatus;
55

66
public interface ASTMHandler {
7-
HandleStatus handle(ASTMMessage message);
7+
String getName();
8+
9+
ASTMHandlerResponse handle(ASTMMessage message);
810

911
boolean matches(ASTMMessage message);
1012
}
Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.itech.ahb.lib.astm.servlet;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
35
import java.util.HashMap;
46
import java.util.List;
57
import java.util.Map;
68
import java.util.Map.Entry;
9+
import java.util.Optional;
710
import lombok.extern.slf4j.Slf4j;
11+
import org.apache.commons.lang3.tuple.Pair;
12+
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
813
import org.itech.ahb.lib.common.ASTMMessage;
914
import org.itech.ahb.lib.common.HandleStatus;
1015

@@ -24,35 +29,49 @@ public ASTMHandlerMarshaller(List<ASTMHandler> handlers, MarshallerMode mode) {
2429
this.mode = mode;
2530
}
2631

27-
public HandleStatus handle(ASTMMessage message) {
28-
Map<ASTMMessage, ASTMHandler> messageHandlers = new HashMap<>();
32+
public ASTMMarshallerResponse handle(ASTMMessage message) {
33+
Map<ASTMMessage, List<ASTMHandler>> messageHandlersMap = new HashMap<>();
2934
log.debug("finding a handler for astm message: " + message.hashCode());
3035
for (ASTMHandler handler : handlers) {
3136
if (handler.matches(message)) {
32-
log.debug("handler found for astm message: " + message.hashCode());
33-
messageHandlers.put(message, handler);
37+
log.debug("handler: '" + handler.getName() + "' found for astm message: " + message.hashCode());
38+
List<ASTMHandler> matchingMessageHandlers = messageHandlersMap.getOrDefault(message, new ArrayList<>());
39+
matchingMessageHandlers.add(handler);
40+
messageHandlersMap.put(message, matchingMessageHandlers);
3441
if (mode == MarshallerMode.FIRST) {
42+
log.debug("marshall mode is FIRST, proceeding with a single handler");
3543
break;
3644
}
3745
}
3846
}
39-
if (!messageHandlers.containsKey(message)) {
47+
if (!messageHandlersMap.containsKey(message)) {
4048
log.warn("astm message received but no handler was configured to handle the message");
49+
log.debug("finished handling astm messages");
50+
return new ASTMMarshallerResponse();
4151
}
4252

53+
List<ASTMHandlerResponse> handleResponses = new ArrayList<>();
4354
log.debug("handling astm message...");
44-
for (Entry<ASTMMessage, ASTMHandler> messageHandler : messageHandlers.entrySet()) {
45-
try {
46-
HandleStatus status = messageHandler.getValue().handle(messageHandler.getKey());
47-
log.debug("finished handling http astm message");
48-
return status;
49-
} catch (RuntimeException e) {
50-
log.error("unexpected error occurred during handling astm message: " + messageHandler.getKey(), e);
51-
return HandleStatus.FAIL;
52-
// TODO add some handle exception handling. retry queue? db save?
55+
for (Entry<ASTMMessage, List<ASTMHandler>> matchingMessageHandlers : messageHandlersMap.entrySet()) {
56+
for (ASTMHandler messageHandler : matchingMessageHandlers.getValue()) {
57+
try {
58+
ASTMHandlerResponse handleResponse = messageHandler.handle(matchingMessageHandlers.getKey());
59+
log.debug("'" + messageHandler.getName() + "' finished handling http astm message");
60+
handleResponses.add(handleResponse);
61+
} catch (RuntimeException e) {
62+
log.error(
63+
"unexpected error occurred during '" +
64+
messageHandler.getName() +
65+
"' handling astm message: " +
66+
matchingMessageHandlers.getKey(),
67+
e
68+
);
69+
handleResponses.add(new ASTMHandlerResponse("", HandleStatus.FAIL, false, messageHandler));
70+
// TODO add some handle exception handling. retry queue? db save?
71+
// handler.handleError();
72+
}
5373
}
5474
}
55-
log.debug("finished handling astm messages");
56-
return HandleStatus.UNHANDLED;
75+
return new ASTMMarshallerResponse(handleResponses);
5776
}
5877
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.itech.ahb.lib.astm.servlet;
2+
3+
import java.util.List;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.RequiredArgsConstructor;
7+
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
8+
9+
@Data
10+
@AllArgsConstructor
11+
@RequiredArgsConstructor
12+
public class ASTMMarshallerResponse {
13+
14+
List<ASTMHandlerResponse> responses;
15+
}

astm-http-lib/src/main/java/org/itech/ahb/lib/astm/servlet/ASTMReceiveThread.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.io.IOException;
44
import java.net.Socket;
5+
import java.util.List;
6+
import java.util.Optional;
57
import lombok.extern.slf4j.Slf4j;
8+
import org.apache.commons.lang3.tuple.Pair;
9+
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
610
import org.itech.ahb.lib.common.ASTMMessage;
711
import org.itech.ahb.lib.common.HandleStatus;
812
import org.itech.ahb.lib.common.exception.ASTMCommunicationException;
@@ -31,15 +35,25 @@ public ASTMReceiveThread(Communicator communicator, ASTMHandlerMarshaller astmHa
3135
public void run() {
3236
log.trace("thread started to receive ASTM message");
3337
try {
38+
ASTMMessage message;
3439
try {
35-
ASTMMessage message = communicator.receiveProtocol();
36-
HandleStatus status = astmHandlerMarshaller.handle(message);
37-
log.debug("astm HandleStatus is: " + status);
38-
if (status != HandleStatus.SUCCESS) {
39-
log.error("message was not handled successfully");
40-
}
40+
message = communicator.receiveProtocol();
4141
} catch (IllegalStateException | FrameParsingException | ASTMCommunicationException e) {
42-
log.error("an error occurred understanding or handling what was received from the astm sender", e);
42+
log.error("an error occurred understanding what was received from the astm sender", e);
43+
return;
44+
}
45+
//TODO replace Pairs with more informative types
46+
ASTMMarshallerResponse response = astmHandlerMarshaller.handle(message);
47+
if (response.getResponses() == null || response.getResponses().size() == 0) {
48+
log.error("message was unhandled");
49+
} else {
50+
for (ASTMHandlerResponse handlerResponse : response.getResponses()) {
51+
if (handlerResponse.getStatus() != HandleStatus.SUCCESS) {
52+
log.error("message was not handled successfully by: " + handlerResponse.getHandler().getName());
53+
} else {
54+
log.debug("message was handled successfully by: " + handlerResponse.getHandler().getName());
55+
}
56+
}
4357
}
4458
} catch (IOException e) {
4559
log.error("error occurred communicating with astm sender", e);

astm-http-lib/src/main/java/org/itech/ahb/lib/astm/servlet/DefaultForwardingASTMToHTTPHandler.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import java.net.http.HttpResponse;
99
import java.util.Base64;
1010
import lombok.extern.slf4j.Slf4j;
11+
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
1112
import org.itech.ahb.lib.common.ASTMMessage;
1213
import org.itech.ahb.lib.common.DefaultASTMMessage;
1314
import org.itech.ahb.lib.common.HandleStatus;
15+
import org.itech.ahb.lib.util.LogUtil;
1416

1517
@Slf4j
1618
public class DefaultForwardingASTMToHTTPHandler implements ASTMHandler {
@@ -32,7 +34,7 @@ public DefaultForwardingASTMToHTTPHandler(URI forwardingUri, String username, ch
3234
}
3335

3436
@Override
35-
public HandleStatus handle(ASTMMessage message) {
37+
public ASTMHandlerResponse handle(ASTMMessage message) {
3638
HttpClient client = HttpClient.newHttpClient();
3739
log.debug("creating request to forward to http server at " + forwardingUri.toString());
3840
Builder requestBuilder = HttpRequest.newBuilder() //
@@ -50,17 +52,25 @@ public HandleStatus handle(ASTMMessage message) {
5052
try {
5153
log.debug("forwarding request to http server at " + forwardingUri.toString());
5254
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
55+
log.debug("received " + response.statusCode() + "response from http server at " + forwardingUri.toString());
56+
log.trace("response: " + LogUtil.convertForDisplay(response.body()));
5357
if (response.statusCode() == 200) {
54-
return HandleStatus.SUCCESS;
58+
return new ASTMHandlerResponse(response.body(), HandleStatus.SUCCESS, false, this);
5559
}
60+
return new ASTMHandlerResponse(response.body(), HandleStatus.FAIL, false, this);
5661
} catch (IOException | InterruptedException e) {
5762
log.error("error occurred communicating with http server at " + forwardingUri.toString(), e);
5863
}
59-
return HandleStatus.FAIL;
64+
return new ASTMHandlerResponse("", HandleStatus.FAIL, false, this);
6065
}
6166

6267
@Override
6368
public boolean matches(ASTMMessage message) {
6469
return message instanceof DefaultASTMMessage;
6570
}
71+
72+
@Override
73+
public String getName() {
74+
return "Forwarding ASTM to HTTP Handler";
75+
}
6676
}

astm-http-lib/src/main/java/org/itech/ahb/lib/common/HandleStatus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public enum HandleStatus {
44
SUCCESS,
55
FAIL,
6+
@Deprecated
67
UNHANDLED
78
}

astm-http-lib/src/main/java/org/itech/ahb/lib/http/servlet/DefaultForwardingHTTPToASTMHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public HandleStatus handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfo
8989
// the communicator must remain open to receive the line contention. The thread
9090
// will close the socket
9191
closeSocket = false;
92-
ASTMReceiveThread receiveThread = new ASTMReceiveThread(communicator, astmHandlerMarshaller);
92+
ASTMReceiveThread receiveThread = new ASTMReceiveThread(communicator, socket, astmHandlerMarshaller);
9393
receiveThread.start();
9494

9595
if (message.getMessageLength() == 0) {

astm-http-lib/src/main/java/org/itech/ahb/lib/http/servlet/HTTPHandlerMarshaller.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public HandleStatus handle(ASTMMessage message) {
3131
return handle(message, Set.of());
3232
}
3333

34+
//TODO rework this like ASTMHandler to allow multiple handlers and more informative responses
3435
public HandleStatus handle(ASTMMessage message, Set<HttpForwardingHandlerInfo> handlersInfos) {
3536
Map<ASTMMessage, HTTPHandler> messageHandlers = new HashMap<>();
3637
log.debug("finding a handler for astm http message: " + message.hashCode());

0 commit comments

Comments
 (0)