Disclaimer: This blog post is only applicable for the SAP Cloud SDK version of at most 2.19.2. We plan to continuously migrate these blog posts into our List of Tutorials. Feel free to check out our updated Tutorials on the SAP Cloud SDK.
mvn archetype:generate -DarchetypeGroupId=com.sap.cloud.s4hana.archetypes \
-DarchetypeArtifactId=scp-cf-tomee -DarchetypeVersion=LATEST
mvn package
mvn tomee:run
# or: mvn tomee:debug
cf push
HelloWorldServlet
. You can generate project files with maven, too:# in main directory:
mvn install
# install is required to make eclipse:eclipse or idea:idea work
mvn eclipse:eclipse
mvn idea:idea
MultichainService
class to facilitate access to the SAP Cloud Platform Blockchain MultiChain service. It makes use of other facilities provided by the SAP Cloud SDK: ScpCfServiceInfo
and reusable HTTP clients.application/pom.xml
file:...
<dependency>
<groupId>com.sap.cloud.s4hana</groupId>
<artifactId>s4hana-all</artifactId>
</dependency>
<!-- Dependency added for SAP Cloud Platform MultiChain service access facilitation -->
<dependency>
<groupId>com.sap.cloud.s4hana.services</groupId>
<artifactId>scp-blockchain</artifactId>
</dependency>
...
HelloWorldServlet
class to a new servlet named, for example, MultichainServlet. Modify the @WebServlet
annotation to register the servlet on a new path, say @WebServlet("/multichain")
. Then change the doGet
method to read:String key = request.getParameter("key");
if (Strings.isNullOrEmpty(key)) {
response.setStatus(HttpStatus.SC_BAD_REQUEST);
} else {
try {
MultichainService multichainService = MultichainService.create();
String valueHex = request.getParameter("valueHex");
final Object result;
if (valueHex != null) {
result = multichainService.invoke(
"publish", "root", key, valueHex);
logger.debug("Publish result: {}", result);
} else {
result = multichainService.invoke(
"liststreamkeyitems", "root", key);
logger.debug("listtreamkeyitems result: {}", result);
}
response.getWriter().write(new Gson().toJson(result));
} catch (Exception e) {
final String msg =
"Failed to access MultiChain service: " + e.getMessage();
logger.error(msg, e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().println(msg);
}
}
key
parameter is read from the request. Then an instance of the MultichainService
class is created that facilitates access to the MultiChain API. This class is available in the com.sap.cloud.s4hana.services:scp-blockchain
dependency added above in this blog and you should have added it when following this blog from top to bottom.valueHex
parameter is read. It's presence determines the fact that a write operation is requested. If the parameter is not present in the request, then a read operation is undertaken.publish
allows writing a value entry under the given key in the given stream. Here we use the 'root' stream, which is available by default in any MultiChain installation. Be aware that the MultiChain API requires data to be in hexadecimal format - this is very fitting for the main use of this blockchain data facility: Writing hashes to the chain. Hashes are regularly printed in human-readable hex code, instead of their raw byte values. The result of this operation will be the id of the created transaction.liststreamkeyitems
gets all entries under a specific key in a specific stream. It will list everything ever written under the key together with the id of the transaction that was created by a write operation. See below for an example of the output. But let's produce the output ourselves, too: Now you can compile and run the code for a quick initial test (in application subdirectory):mvn package
export ALLOW_MOCKED_AUTH_HEADER=true
# or windows: set ALLOW_MOCKED_AUTH_HEADER=true
# VCAP_SERVICES needs mocking too if you test locally.
# Get the values for the locations marked <EDIT> from the SAP Cloud Platform Cockpit in the application screen under environment variables:
export VCAP_SERVICES='{ "multichain": [ { "name": "multichain_test", "instance_name": "multichain_test", "binding_name": null, "credentials": { "api_key": "<EDIT>", "url": "<EDIT>" }, "syslog_drain_url": null, "volume_mounts": [], "label": "multichain", "provider": null, "plan": "dev", "tags": [ "blockchain", "multichain" ] } ] }'
# on windows: set VCAP_SERVICES={ "multichain": [ { "name": "multichain_test", "instance_name": "multichain_test", "binding_name": null, "credentials": { "api_key": "<EDIT>", "url": "<EDIT>" }, "syslog_drain_url": null, "volume_mounts": [], "label": "multichain", "provider": null, "plan": "dev", "tags": [ "blockchain", "multichain" ] } ] }
mvn tomee:run
# or: mvn tomee:debug
cf push
# show your apps in current space - this assumes you deployed the skeleton as recommended above
cf apps
cf set-env <enter your app name> ALLOW_MOCKED_AUTH_HEADER true
# after the change of the env variable you need to restart the application:
cf restart <enter your app name>
?key=k1&valueHex=aa
String input = "test text";
final String hexEncoded =
BaseEncoding.base16().encode(input.getBytes("UTF-8"));
String decoded =
new String(BaseEncoding.base16().decode(hexEncoded), Charsets.UTF_8);
assertEquals(input, decoded);
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.services.scp.blockchain.multichain.MultichainService;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/multichain")
public class MultichainServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static final Logger logger = CloudLoggerFactory.getLogger(MultichainServlet.class);
@Override
protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
throws IOException
{
logger.info("I am running!");
String key = request.getParameter("key");
if (Strings.isNullOrEmpty(key)) {
response.setStatus(HttpStatus.SC_BAD_REQUEST);
} else {
try {
MultichainService multichainService = MultichainService.create();
String valueHex = request.getParameter("valueHex");
final Object result;
if (valueHex != null) {
result = multichainService.invoke(
"publish", "root", key, valueHex);
logger.debug("Publish result: {}", result);
} else {
result = multichainService.invoke(
"liststreamkeyitems", "root", key);
logger.debug("listtreamkeyitems result: {}", result);
}
response.getWriter().write(new Gson().toJson(result));
} catch (Exception e) {
final String msg =
"Failed to access MultiChain service: " + e.getMessage();
logger.error(msg, e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().println(msg);
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
13 | |
13 | |
11 | |
10 | |
10 | |
10 | |
8 | |
8 |