Skip to content

Commit 01180a9

Browse files
committed
add multiple handlers support to HTTP to ASTM
1 parent da2c22e commit 01180a9

15 files changed

+133
-46
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.2.1</version>
7+
<version>2.3.0</version>
88

99
<properties>
1010
<java.version>21</java.version>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import lombok.RequiredArgsConstructor;
77
import org.itech.ahb.lib.astm.servlet.ASTMHandler;
88
import org.itech.ahb.lib.common.HandleStatus;
9+
import org.itech.ahb.lib.common.HandlerResponse;
910

1011
@Data
1112
@AllArgsConstructor
1213
@RequiredArgsConstructor
13-
public class ASTMHandlerResponse {
14+
public class ASTMHandlerResponse implements HandlerResponse {
1415

1516
@NonNull
1617
String response;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.itech.ahb.lib.astm.servlet;
2+
3+
//TODO add the capability to add info about forwarding (for example listen on sevaral sockets, and each socket could have a defined forwarding info)
4+
public class ASTMForwardingHandlerInfo {}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Map;
77
import java.util.Map.Entry;
8+
import java.util.Set;
89
import lombok.extern.slf4j.Slf4j;
910
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
1011
import org.itech.ahb.lib.common.ASTMMessage;
@@ -27,6 +28,10 @@ public ASTMHandlerMarshaller(List<ASTMHandler> handlers, MarshallerMode mode) {
2728
}
2829

2930
public ASTMMarshallerResponse handle(ASTMMessage message) {
31+
return handle(message, Set.of());
32+
}
33+
34+
public ASTMMarshallerResponse handle(ASTMMessage message, Set<ASTMForwardingHandlerInfo> handlersInfos) {
3035
Map<ASTMMessage, List<ASTMHandler>> messageHandlersMap = new HashMap<>();
3136
log.debug("finding a handler for astm message: " + message.hashCode());
3237
for (ASTMHandler handler : handlers) {
@@ -43,7 +48,6 @@ public ASTMMarshallerResponse handle(ASTMMessage message) {
4348
}
4449
if (!messageHandlersMap.containsKey(message)) {
4550
log.warn("astm message received but no handler was configured to handle the message");
46-
log.debug("finished handling astm messages");
4751
return new ASTMMarshallerResponse();
4852
}
4953

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import lombok.Data;
66
import lombok.RequiredArgsConstructor;
77
import org.itech.ahb.lib.astm.ASTMHandlerResponse;
8+
import org.itech.ahb.lib.common.MarshallerResponse;
89

910
@Data
1011
@AllArgsConstructor
1112
@RequiredArgsConstructor
12-
public class ASTMMarshallerResponse {
13+
public class ASTMMarshallerResponse implements MarshallerResponse {
1314

1415
List<ASTMHandlerResponse> responses;
1516
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.itech.ahb.lib.common;
2+
3+
public interface HandlerResponse {
4+
HandleStatus getStatus();
5+
Boolean getCommunicateResponse();
6+
String getResponse();
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.itech.ahb.lib.common;
2+
3+
public interface MarshallerResponse {}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public DefaultForwardingHTTPToASTMHandler(
4040
}
4141

4242
@Override
43-
public HandleStatus handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfos) throws FrameParsingException {
43+
public HTTPHandlerResponse handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfos)
44+
throws FrameParsingException {
4445
Socket socket = null;
4546
boolean success = false;
4647
boolean closeSocket = true;
@@ -49,8 +50,8 @@ public HandleStatus handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfo
4950
int forwardingPort = this.defaultForwardingPort;
5051
ASTMVersion forwardingProtocol = this.defaultForwardingProtocol;
5152
for (HTTPHandlerInfo handlerInfo : handlerInfos) {
52-
if (handlerInfo instanceof HttpForwardingHandlerInfo) {
53-
HttpForwardingHandlerInfo httpForwardingHandlerInfo = (HttpForwardingHandlerInfo) handlerInfo;
53+
if (handlerInfo instanceof HTTPForwardingHandlerInfo) {
54+
HTTPForwardingHandlerInfo httpForwardingHandlerInfo = (HTTPForwardingHandlerInfo) handlerInfo;
5455
forwardingAddress = StringUtils.isBlank(httpForwardingHandlerInfo.getForwardAddress())
5556
? forwardingAddress
5657
: httpForwardingHandlerInfo.getForwardAddress();
@@ -99,7 +100,7 @@ public HandleStatus handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfo
99100
}
100101
} catch (IOException | ASTMCommunicationException e) {
101102
log.error("error occurred communicating with astm server at " + forwardingAddress + ":" + forwardingPort, e);
102-
return HandleStatus.FAIL;
103+
return new HTTPHandlerResponse("", HandleStatus.FAIL, false, this);
103104
} finally {
104105
if (closeSocket) {
105106
if (socket != null && !socket.isClosed()) {
@@ -115,11 +116,16 @@ public HandleStatus handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfo
115116
}
116117
}
117118
}
118-
return success ? HandleStatus.SUCCESS : HandleStatus.FAIL;
119+
return new HTTPHandlerResponse("", success ? HandleStatus.SUCCESS : HandleStatus.FAIL, false, this);
119120
}
120121

121122
@Override
122123
public boolean matches(ASTMMessage message) {
123124
return message instanceof DefaultASTMMessage;
124125
}
126+
127+
@Override
128+
public String getName() {
129+
return "Default HTTP to ASTM Handler";
130+
}
125131
}

astm-http-lib/src/main/java/org/itech/ahb/lib/http/servlet/HttpForwardingHandlerInfo.java renamed to astm-http-lib/src/main/java/org/itech/ahb/lib/http/servlet/HTTPForwardingHandlerInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.itech.ahb.lib.astm.servlet.ASTMServlet.ASTMVersion;
55

66
@Data
7-
public class HttpForwardingHandlerInfo implements HTTPHandlerInfo {
7+
public class HTTPForwardingHandlerInfo implements HTTPHandlerInfo {
88

99
private String forwardAddress;
1010
private int forwardPort;

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

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

33
import java.util.Set;
44
import org.itech.ahb.lib.common.ASTMMessage;
5-
import org.itech.ahb.lib.common.HandleStatus;
65
import org.itech.ahb.lib.common.exception.FrameParsingException;
76

87
public interface HTTPHandler {
98
boolean matches(ASTMMessage message);
109

11-
HandleStatus handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfo) throws FrameParsingException;
10+
HTTPHandlerResponse handle(ASTMMessage message, Set<HTTPHandlerInfo> handlerInfo) throws FrameParsingException;
11+
12+
String getName();
1213
}

0 commit comments

Comments
 (0)