From b306d7ba2cbb3b59701ef33954d3ce48f7484f1d Mon Sep 17 00:00:00 2001 From: Luis Sanchez Date: Tue, 18 Apr 2017 14:36:16 -0400 Subject: [PATCH] [FAB-3221] Java cc getFunctionAndParameters() ChaincodeStub: - add getFunction() - add getParameters() - rename getArgsAsStrings() to getStringArgs() Change-Id: Iea537353f3c40b205b8f5fb0213a85733b5dbdab Signed-off-by: Luis Sanchez --- .../fabric/shim/ChaincodeStub.java | 31 ++++++++++++++++--- .../fabric/shim/ChaincodeStubImpl.java | 20 +++++++++++- .../src/main/java/example/LinkExample.java | 5 ++- .../src/main/java/example/SimpleSample.java | 15 ++++----- .../src/main/java/example/Example02.java | 13 ++++---- .../src/main/java/example/Example04.java | 7 ++--- .../src/main/java/example/Example05.java | 7 ++--- .../src/main/java/example/Example06.java | 4 +-- .../src/main/java/example/EventSender.java | 5 ++- 9 files changed, 69 insertions(+), 38 deletions(-) diff --git a/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java b/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java index dd0e53e85ed..645790e74cc 100644 --- a/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java +++ b/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java @@ -16,11 +16,10 @@ package org.hyperledger.fabric.shim; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.stream.Collectors.toList; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent; import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response; @@ -41,9 +40,31 @@ public interface ChaincodeStub { * {@link Chaincode#init(ChaincodeStub)} or * {@link Chaincode#invoke(ChaincodeStub)}. * - * @return a list of arguments cast to a UTF-8 string + * @return a list of arguments cast to UTF-8 strings */ - List getArgsAsStrings(); + List getStringArgs(); + + /** + * A convenience method that returns the first argument of the chaincode + * invocation for use as a function name. + * + * The bytes of the first argument are decoded as a UTF-8 string. + * + * @return the function name + */ + String getFunction(); + + /** + * A convenience method that returns all except the first argument of the + * chaincode invocation for use as the parameters to the function returned + * by #{@link ChaincodeStub#getFunction()}. + * + * The bytes of the arguments are decoded as a UTF-8 strings and returned as + * a list of string parameters.. + * + * @return a list of parameters + */ + List getParameters(); /** * Returns the transaction id @@ -134,7 +155,7 @@ default Response invokeChaincode(String chaincodeName, List args) { * @return */ default Response invokeChaincodeWithStringArgs(String chaincodeName, List args, String channel) { - return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(StandardCharsets.UTF_8)).collect(Collectors.toList()), channel); + return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(UTF_8)).collect(toList()), channel); } /** diff --git a/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStubImpl.java b/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStubImpl.java index 4684b097e48..4fc390df4ae 100644 --- a/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStubImpl.java +++ b/core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStubImpl.java @@ -16,6 +16,8 @@ package org.hyperledger.fabric.shim; +import static java.util.stream.Collectors.toList; + import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -54,10 +56,26 @@ public List getArgs() { * @see org.hyperledger.fabric.shim.ChaincodeStub#getArgsAsStrings() */ @Override - public List getArgsAsStrings() { + public List getStringArgs() { return args.stream().map(x -> x.toStringUtf8()).collect(Collectors.toList()); } + /* (non-Javadoc) + * @see org.hyperledger.fabric.shim.ChaincodeStub#getFunction() + */ + @Override + public String getFunction() { + return getStringArgs().size() > 0 ? getStringArgs().get(0) : null; + } + + /* (non-Javadoc) + * @see org.hyperledger.fabric.shim.ChaincodeStub#getParameters() + */ + @Override + public List getParameters() { + return getStringArgs().stream().skip(1).collect(toList()); + } + /* (non-Javadoc) * @see org.hyperledger.fabric.shim.ChaincodeStub#setEvent(java.lang.String, byte[]) */ diff --git a/examples/chaincode/java/LinkExample/src/main/java/example/LinkExample.java b/examples/chaincode/java/LinkExample/src/main/java/example/LinkExample.java index b1d27d05edc..c38daedecdb 100644 --- a/examples/chaincode/java/LinkExample/src/main/java/example/LinkExample.java +++ b/examples/chaincode/java/LinkExample/src/main/java/example/LinkExample.java @@ -43,9 +43,8 @@ public Response init(ChaincodeStub stub) { @Override public Response invoke(ChaincodeStub stub) { try { - final List argList = stub.getArgsAsStrings(); - final String function = argList.get(0); - final List args = argList.subList(0, argList.size()); + final String function = stub.getFunction(); + final List args = stub.getParameters(); switch (function) { case "init": diff --git a/examples/chaincode/java/SimpleSample/src/main/java/example/SimpleSample.java b/examples/chaincode/java/SimpleSample/src/main/java/example/SimpleSample.java index 1011bee2150..ac000dbd5d0 100644 --- a/examples/chaincode/java/SimpleSample/src/main/java/example/SimpleSample.java +++ b/examples/chaincode/java/SimpleSample/src/main/java/example/SimpleSample.java @@ -22,8 +22,6 @@ import static org.hyperledger.fabric.shim.ChaincodeHelper.newInternalServerErrorResponse; import static org.hyperledger.fabric.shim.ChaincodeHelper.newSuccessResponse; -import java.util.List; - import javax.json.Json; import org.apache.commons.logging.Log; @@ -44,19 +42,18 @@ public class SimpleSample extends ChaincodeBase { @Override public Response init(ChaincodeStub stub) { - final List args = stub.getArgsAsStrings(); - if(!args.get(0).equals("init")) { - return newBadRequestResponse(format("Unknown function: %s", args.get(0))); + final String function = stub.getFunction(); + if(!function.equals("init")) { + return newBadRequestResponse(format("Unknown function: %s", function)); } - return init(stub, args.stream().skip(1).toArray(String[]::new)); + return init(stub, stub.getParameters().stream().toArray(String[]::new)); } @Override public Response invoke(ChaincodeStub stub) { try { - final List argList = stub.getArgsAsStrings(); - final String function = argList.get(0); - final String[] args = argList.stream().skip(1).toArray(String[]::new); + final String function = stub.getFunction(); + final String[] args = stub.getParameters().stream().toArray(String[]::new); switch (function) { case "transfer": diff --git a/examples/chaincode/java/chaincode_example02/src/main/java/example/Example02.java b/examples/chaincode/java/chaincode_example02/src/main/java/example/Example02.java index a1752f21f6d..6e9f5a569f4 100644 --- a/examples/chaincode/java/chaincode_example02/src/main/java/example/Example02.java +++ b/examples/chaincode/java/chaincode_example02/src/main/java/example/Example02.java @@ -39,12 +39,12 @@ public class Example02 extends ChaincodeBase { @Override public Response init(ChaincodeStub stub) { try { - final List args = stub.getArgsAsStrings(); - switch (args.get(0)) { + final String function = stub.getFunction(); + switch (function) { case "init": - return init(stub, args.stream().skip(1).toArray(String[]::new)); + return init(stub, stub.getParameters().stream().toArray(String[]::new)); default: - return newBadRequestResponse(format("Unknown function: %s", args.get(0))); + return newBadRequestResponse(format("Unknown function: %s", function)); } } catch (NumberFormatException e) { return newBadRequestResponse(e.toString()); @@ -59,9 +59,8 @@ public Response init(ChaincodeStub stub) { public Response invoke(ChaincodeStub stub) { try { - final List argList = stub.getArgsAsStrings(); - final String function = argList.get(0); - final String[] args = argList.stream().skip(1).toArray(String[]::new); + final String function = stub.getFunction(); + final String[] args = stub.getParameters().stream().toArray(String[]::new); switch (function) { case "invoke": diff --git a/examples/chaincode/java/chaincode_example04/src/main/java/example/Example04.java b/examples/chaincode/java/chaincode_example04/src/main/java/example/Example04.java index 8d6ff7fc122..53b754f8b9b 100644 --- a/examples/chaincode/java/chaincode_example04/src/main/java/example/Example04.java +++ b/examples/chaincode/java/chaincode_example04/src/main/java/example/Example04.java @@ -37,7 +37,7 @@ public Response init(ChaincodeStub stub) { // expects to be called with: { "init", key, value } try { - final List args = stub.getArgsAsStrings(); + final List args = stub.getStringArgs(); if(args.size() != 3) { return newBadRequestResponse("Incorrect number of arguments. Expecting \"init\" plus 2 more."); } @@ -59,9 +59,8 @@ public Response init(ChaincodeStub stub) { public Response invoke(ChaincodeStub stub) { // expects to be called with: { "invoke"|"query", chaincodeName, key } try { - final List argList = stub.getArgsAsStrings(); - final String function = argList.get(0); - final String[] args = argList.stream().skip(1).toArray(String[]::new); + final String function = stub.getFunction(); + final String[] args = stub.getParameters().stream().toArray(String[]::new); switch (function) { case "invoke": diff --git a/examples/chaincode/java/chaincode_example05/src/main/java/example/Example05.java b/examples/chaincode/java/chaincode_example05/src/main/java/example/Example05.java index 1383d72f53d..1ef1ce94230 100644 --- a/examples/chaincode/java/chaincode_example05/src/main/java/example/Example05.java +++ b/examples/chaincode/java/chaincode_example05/src/main/java/example/Example05.java @@ -35,7 +35,7 @@ public Response init(ChaincodeStub stub) { // expects to be called with: { "init", key, value } try { - final List args = stub.getArgsAsStrings(); + final List args = stub.getStringArgs(); if(args.size() != 3) { return newBadRequestResponse("Incorrect number of arguments. Expecting \"init\" plus 2 more."); } @@ -57,9 +57,8 @@ public Response init(ChaincodeStub stub) { public Response invoke(ChaincodeStub stub) { // expects to be called with: { "invoke"|"query", chaincodeName, key } try { - final List argList = stub.getArgsAsStrings(); - final String function = argList.get(0); - final String[] args = argList.stream().skip(1).toArray(String[]::new); + final String function = stub.getFunction(); + final String[] args = stub.getParameters().stream().toArray(String[]::new); switch (function) { case "invoke": diff --git a/examples/chaincode/java/chaincode_example06/src/main/java/example/Example06.java b/examples/chaincode/java/chaincode_example06/src/main/java/example/Example06.java index 8c1aaa05da0..f60cca5f20c 100644 --- a/examples/chaincode/java/chaincode_example06/src/main/java/example/Example06.java +++ b/examples/chaincode/java/chaincode_example06/src/main/java/example/Example06.java @@ -32,10 +32,10 @@ public Response init(ChaincodeStub stub) { @Override public Response invoke(ChaincodeStub stub) { - if(stub.getArgsAsStrings().isEmpty()) + if(stub.getArgs().isEmpty()) return newBadRequestResponse(format("No arguments specified.")); - switch (stub.getArgsAsStrings().get(0)) { + switch (stub.getStringArgs().get(0)) { case "runtimeException": throw new RuntimeException("Exception thrown as requested."); default: diff --git a/examples/chaincode/java/eventsender/src/main/java/example/EventSender.java b/examples/chaincode/java/eventsender/src/main/java/example/EventSender.java index 99228d29121..6bdcee70d21 100644 --- a/examples/chaincode/java/eventsender/src/main/java/example/EventSender.java +++ b/examples/chaincode/java/eventsender/src/main/java/example/EventSender.java @@ -44,12 +44,11 @@ public Response init(ChaincodeStub stub) { public Response invoke(ChaincodeStub stub) { try { - final List argList = stub.getArgsAsStrings(); - final String function = argList.get(0); + final String function = stub.getFunction(); switch (function) { case "invoke": - return doInvoke(stub, argList.stream().skip(1).collect(toList())); + return doInvoke(stub, stub.getParameters()); case "query": return doQuery(stub); default: