tidied up FinalizeResponderFlow
This commit is contained in:
parent
1fb18290ca
commit
4c58c0b0a8
@ -28,7 +28,7 @@ import java.util.UUID;
|
||||
|
||||
import static java.util.Objects.*;
|
||||
|
||||
@InitiatingFlow(protocol = "create-chat-protocol")
|
||||
//@InitiatingFlow(protocol = "create-chat-protocol")
|
||||
public class CreateNewChatFlow implements RPCStartableFlow {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(CreateNewChatFlow.class);
|
||||
|
@ -6,6 +6,7 @@ import net.corda.v5.application.flows.InitiatedBy;
|
||||
import net.corda.v5.application.flows.ResponderFlow;
|
||||
import net.corda.v5.application.messaging.FlowSession;
|
||||
import net.corda.v5.base.annotations.Suspendable;
|
||||
import net.corda.v5.base.types.MemberX500Name;
|
||||
import net.corda.v5.ledger.utxo.UtxoLedgerService;
|
||||
import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction;
|
||||
import net.corda.v5.ledger.utxo.transaction.UtxoTransactionValidator;
|
||||
@ -13,8 +14,11 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords;
|
||||
import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
//import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords;
|
||||
//import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty;
|
||||
|
||||
@InitiatedBy(protocol = "finalize-chat-protocol")
|
||||
public class FinalizeChatResponderFlow implements ResponderFlow {
|
||||
@ -25,8 +29,10 @@ public class FinalizeChatResponderFlow implements ResponderFlow {
|
||||
|
||||
@Suspendable
|
||||
@Override
|
||||
public void call(@NotNull FlowSession session) {
|
||||
log.info("AppendChatResponderFlow.call() called");
|
||||
public void call(FlowSession session) {
|
||||
|
||||
log.info("FinalizeChatResponderFlow.call() called");
|
||||
|
||||
try {
|
||||
UtxoTransactionValidator txValidator = ledgerTransaction -> {
|
||||
ChatState state = (ChatState) ledgerTransaction.getOutputContractStates().get(0);
|
||||
@ -45,4 +51,17 @@ public class FinalizeChatResponderFlow implements ResponderFlow {
|
||||
log.warn("Exceptionally finished responder flow", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Suspendable
|
||||
Boolean checkForBannedWords(String str) {
|
||||
List<String> bannedWords = Arrays.asList("banana", "apple", "pear");
|
||||
return bannedWords.stream().anyMatch(str::contains);
|
||||
}
|
||||
|
||||
@Suspendable
|
||||
Boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) {
|
||||
return state.getMessageFrom().equals(otherMember);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,10 @@ import java.util.List;
|
||||
@InitiatingFlow(protocol = "finalize-chat-protocol")
|
||||
public class FinalizeChatSubFlow implements SubFlow<String> {
|
||||
|
||||
// these need to be private + good practice is to declare them at the start of the class
|
||||
private final UtxoSignedTransaction signedTransaction;
|
||||
private final MemberX500Name otherMember;
|
||||
|
||||
public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) {
|
||||
this.signedTransaction = signedTransaction;
|
||||
this.otherMember = otherMember;
|
||||
@ -33,46 +37,47 @@ public class FinalizeChatSubFlow implements SubFlow<String> {
|
||||
@Suspendable
|
||||
public String call() {
|
||||
|
||||
// log.info("AppendChatFlow.call() called");
|
||||
log.info("FinalizeChatFlow.call() called");
|
||||
// log.info("otherMember = " + otherMember);
|
||||
FlowSession session = flowMessaging.initiateFlow(otherMember);
|
||||
|
||||
String retVal;
|
||||
String result;
|
||||
try {
|
||||
List<FlowSession> sessionsList = Arrays.asList(session);
|
||||
log.info("sessionList.size()=" + sessionsList.size());
|
||||
// log.info("sessionList.size()=" + sessionsList.size());
|
||||
|
||||
UtxoSignedTransaction finalizedSignedTransaction = ledgerService.finalize(
|
||||
signedTransaction,
|
||||
sessionsList
|
||||
);
|
||||
|
||||
retVal = finalizedSignedTransaction.getId().toString();
|
||||
log.info("Success! Response: " + retVal);
|
||||
result = finalizedSignedTransaction.getId().toString();
|
||||
log.info("Success! Response: " + result);
|
||||
} catch (Exception e) {
|
||||
log.warn("Finality failed", e);
|
||||
retVal = "Finality failed, " + e.getMessage();
|
||||
result = "Finality failed, " + e.getMessage();
|
||||
}
|
||||
log.info("AppendChatSubFlow call returns=" + retVal);
|
||||
return retVal;
|
||||
// log.info("FinalizeChatSubFlow call returns=" + retVal);
|
||||
return result;
|
||||
}
|
||||
|
||||
public UtxoSignedTransaction getSignedTransaction() {
|
||||
return signedTransaction;
|
||||
}
|
||||
// We don't need getters and setters as the properties should be private
|
||||
|
||||
public void setSignedTransaction(UtxoSignedTransaction signedTransaction) {
|
||||
this.signedTransaction = signedTransaction;
|
||||
}
|
||||
// public UtxoSignedTransaction getSignedTransaction() {
|
||||
// return signedTransaction;
|
||||
// }
|
||||
//
|
||||
// public void setSignedTransaction(UtxoSignedTransaction signedTransaction) {
|
||||
// this.signedTransaction = signedTransaction;
|
||||
// }
|
||||
//
|
||||
// public MemberX500Name getOtherMember() {
|
||||
// return otherMember;
|
||||
// }
|
||||
//
|
||||
// public void setOtherMember(MemberX500Name otherMember) {
|
||||
// this.otherMember = otherMember;
|
||||
// }
|
||||
|
||||
public MemberX500Name getOtherMember() {
|
||||
return otherMember;
|
||||
}
|
||||
|
||||
public void setOtherMember(MemberX500Name otherMember) {
|
||||
this.otherMember = otherMember;
|
||||
}
|
||||
|
||||
public UtxoSignedTransaction signedTransaction;
|
||||
public MemberX500Name otherMember;
|
||||
}
|
||||
|
@ -7,19 +7,26 @@ import net.corda.v5.base.types.MemberX500Name;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
// I think we should avoid static imports, it's an extra level of complexity we don't need to add.
|
||||
// The Kotlin helper functions are more simple because Kotlin supports functions which are in the package but
|
||||
// not in a class.
|
||||
|
||||
public final class ResponderValidationHelpers {
|
||||
public final static List<String> bannedWords = Arrays.asList("banana", "apple", "pear");
|
||||
// public final static List<String> bannedWords = Arrays.asList("banana", "apple", "pear");
|
||||
//
|
||||
// @Suspendable
|
||||
// public static boolean checkForBannedWords(String str) {
|
||||
// return bannedWords.stream().anyMatch(str::contains);
|
||||
// }
|
||||
|
||||
@Suspendable
|
||||
public static boolean checkForBannedWords(String str) {
|
||||
return bannedWords.stream().anyMatch(str::contains);
|
||||
}
|
||||
|
||||
@Suspendable
|
||||
public static boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) {
|
||||
return state.getMessageFrom().equals(otherMember);
|
||||
}
|
||||
// @Suspendable
|
||||
// public static boolean checkMessageFromMatchesCounterparty(ChatState state, MemberX500Name otherMember) {
|
||||
// return state.getMessageFrom().equals(otherMember);
|
||||
// }
|
||||
|
||||
// This class just introduces a scope for some helper functions and should not be instantiated.
|
||||
private ResponderValidationHelpers() {}
|
||||
// private ResponderValidationHelpers() {}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user