TH2 Added sendRaw implementation in FixServerSessionsContainer
MODIFY
BackEnd/Service/service-fix/src/main/java/com/exactpro/sf/services/fix/FixServerSessionsContainer.java
BackEnd/Service/service-fix/src/main/java/com/exactpro/sf/services/fix/FixServerSessionsContainer.java
package com.exactpro.sf.services.fix;
+import com.exactpro.sf.common.messages.IMessage;
+import com.exactpro.sf.common.util.SendMessageFailedException;
+import com.exactpro.sf.services.ISession;
+import org.apache.commons.lang3.StringUtils;
+import org.quickfixj.CharsetSupport;
+
+import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
-
-import org.apache.commons.lang3.StringUtils;
-
-import com.exactpro.sf.common.messages.IMessage;
-import com.exactpro.sf.common.util.SendMessageFailedException;
-import com.exactpro.sf.services.ISession;
+import java.util.function.Supplier;
public class FixServerSessionsContainer implements ISession {
@@ -42,32 +44,8 @@ public class FixServerSessionsContainer implements ISession {
@Override
public IMessage send(Object message) throws InterruptedException {
- Map<ISession, Exception> sentErrors = new HashMap<>();
- Collection<ISession> connectedSessions = application.getSessions();
- if (connectedSessions.isEmpty()) {
- throw new SendMessageFailedException("No sessions to send message: " + message);
- }
-
- for (ISession session : connectedSessions) {
- try {
- session.send(message);
- } catch (Exception e) {
- sentErrors.put(session, e);
- }
- }
-
- if (!sentErrors.isEmpty()) {
- StringBuilder errorBuilder = new StringBuilder("Not all message successfully sent: ");
- for (Entry<ISession, Exception> error : sentErrors.entrySet()) {
- Exception ex = error.getValue();
- String errorMsg = ex.getCause() == null ? ex.getMessage() : String.format("%s; cause: %s", ex.getMessage(), ex.getCause());
- errorBuilder.append(StringUtils.LF).append(String.format("%s : [%s]", error.getKey(), errorMsg));
- }
- SendMessageFailedException exception = new SendMessageFailedException(errorBuilder.toString());
- sentErrors.values().forEach(exception::addSuppressed);
- throw exception;
- }
-
+ tryActionToSessions(() -> "No sessions to send message: " + message,
+ session -> session.send(message));
return message instanceof IMessage ? (IMessage)message : null;
}
@@ -79,6 +57,12 @@ public class FixServerSessionsContainer implements ISession {
return message instanceof IMessage ? (IMessage)message : null;
}
+ @Override
+ public void sendRaw(byte[] rawData) throws InterruptedException {
+ tryActionToSessions(() -> "No sessions to send raw message: " + CharsetSupport.getCharsetInstance().decode(ByteBuffer.wrap(rawData)),
+ session -> session.sendRaw(rawData));
+ }
+
@Override
public void close() {
for(ISession session : application.getSessions()) {
@@ -106,4 +90,37 @@ public class FixServerSessionsContainer implements ISession {
((FIXSession)session).addExpectedSenderNum(seq);
}
}
+
+ private void tryActionToSessions(Supplier<String> onEmptyMessage, SessionAction actionForSession) {
+ Collection<ISession> connectedSessions = application.getSessions();
+
+ if (connectedSessions.isEmpty()) {
+ throw new SendMessageFailedException(onEmptyMessage.get());
+ }
+
+ Map<ISession, Exception> sentErrors = new HashMap<>();
+ for (ISession session : connectedSessions) {
+ try {
+ actionForSession.accept(session);
+ } catch (Exception e) {
+ sentErrors.put(session, e);
+ }
+ }
+
+ if (!sentErrors.isEmpty()) {
+ StringBuilder errorBuilder = new StringBuilder("Not all message successfully sent: ");
+ for (Entry<ISession, Exception> error : sentErrors.entrySet()) {
+ Exception ex = error.getValue();
+ String errorMsg = ex.getCause() == null ? ex.getMessage() : String.format("%s; cause: %s", ex.getMessage(), ex.getCause());
+ errorBuilder.append(StringUtils.LF).append(String.format("%s : [%s]", error.getKey(), errorMsg));
+ }
+ SendMessageFailedException exception = new SendMessageFailedException(errorBuilder.toString());
+ sentErrors.values().forEach(exception::addSuppressed);
+ throw exception;
+ }
+ }
+
+ private interface SessionAction {
+ void accept(ISession session) throws Exception;
+ }
}

TH2 #1449. Added sendRaw implementation in FixServerSessionsContainer

Change-Id: I1eea3a450feb8728a150bcd456b4250485a99727