Init JAVA-API project

Setup Basics blockchain class, pom.xml for maven, Logger, Basic IO Tests.
This commit is contained in:
GME 2018-10-21 11:31:53 +02:00
parent d8c6d78eba
commit 0251ed077d
22 changed files with 514 additions and 0 deletions

83
.gitignore vendored Normal file
View file

@ -0,0 +1,83 @@
# Created by https://www.gitignore.io/api/intellij
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
.idea/sonarlint
# End of https://www.gitignore.io/api/intellij

13
.idea/compiler.xml Normal file
View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="java-api" />
</profile>
</annotationProcessing>
</component>
</project>

6
.idea/encodings.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

2
java-api.iml Normal file
View file

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

BIN
msp/toto/abdel.context Normal file

Binary file not shown.

52
pom.xml Normal file
View file

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.monnethic</groupId>
<artifactId>java-api</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,48 @@
package blockchain.client;
import blockchain.user.UserContext;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.Properties;
public class CAClientWrapper {
private String caUrl;
private Properties properties;
private HFCAClient hfcaClient;
private UserContext adminContext;
public void setAdminContext(UserContext adminContext) {
this.adminContext = adminContext;
}
public HFCAClient getHfcaClient(){
return hfcaClient;
}
public CAClientWrapper(String caUrl, Properties properties) throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException {
this.caUrl=caUrl;
this.properties=properties;
init();
}
private void init() throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
hfcaClient = HFCAClient.createNewInstance(caUrl, properties);
hfcaClient.setCryptoSuite(cryptoSuite);
}
//TODO
public UserContext enrollAdmin(String username, String password) throws Exception{
UserContext userContext = new UserContext();
return null;
}
}

View file

@ -0,0 +1,28 @@
package blockchain.client;
import org.hyperledger.fabric.sdk.Channel;
public class ChannelClientWrapper {
private String name;
private Channel channel;
private FabricClientWrapper fabricClientWrapper;
public String getName(){
return name;
}
public Channel getChannel(){
return channel;
}
public FabricClientWrapper getFabricClientWrapper(){
return fabricClientWrapper;
}
public ChannelClientWrapper(String name, Channel channel, FabricClientWrapper fabricClientWrapper){
this.name=name;
this.channel=channel;
this.fabricClientWrapper=fabricClientWrapper;
}
}

View file

@ -0,0 +1,31 @@
package blockchain.client;
import blockchain.user.UserContext;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import java.lang.reflect.InvocationTargetException;
public class FabricClientWrapper {
private HFClient client;
public HFClient getClient(){
return client;
}
public FabricClientWrapper(UserContext userContext) throws CryptoException, InvalidArgumentException, IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
client = HFClient.createNewInstance();
client.setCryptoSuite(cryptoSuite);
client.setUserContext(userContext);
}
public ChannelClientWrapper createChannelClient(String name) throws InvalidArgumentException {
Channel channel = client.newChannel(name);
ChannelClientWrapper channelClientWrapper = new ChannelClientWrapper(name, channel, this);
return channelClientWrapper;
}
}

View file

@ -0,0 +1,72 @@
package blockchain.user;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.User;
import java.io.Serializable;
import java.util.Set;
public class UserContext implements User, Serializable {
private static final long serialVersionUID = 1L;
protected String name;
protected Set<String> roles;
protected String account;
protected String affiliation;
protected Enrollment enrollment;
protected String mspId;
public void setName(String name){
this.name=name;
}
public void setRoles(Set<String> roles){
this.roles=roles;
}
public void setAccount(String account) {
this.account = account;
}
public void setAffiliation(String affiliation) {
this.affiliation = affiliation;
}
public void setEnrollment(Enrollment enrollment) {
this.enrollment = enrollment;
}
public void setMspId(String mspId) {
this.mspId = mspId;
}
@Override
public String getName() {
return name;
}
@Override
public Set<String> getRoles() {
return roles;
}
@Override
public String getAccount() {
return account;
}
@Override
public String getAffiliation() {
return affiliation;
}
@Override
public Enrollment getEnrollment() {
return enrollment;
}
@Override
public String getMspId() {
return mspId;
}
}

View file

@ -0,0 +1,85 @@
package blockchain.utility;
import blockchain.user.UserContext;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import java.io.*;
public class Util {
private static Logger logger = Logger.getLogger(Util.class);
public static void writeUserContext(UserContext userContext) {
BasicConfigurator.configure();
ObjectOutputStream out = null;
FileOutputStream fileOutputStream = null;
try {
String directoryPath = "msp/" + userContext.getAffiliation();
String filePath = directoryPath + "/" + userContext.getName() + ".context";
File directory = new File(directoryPath);
if (!directory.exists()){
logger.info("Create directory at path "+directoryPath);
directory.mkdirs();
}
File file = new File(filePath);
fileOutputStream = new FileOutputStream(file);
out = new ObjectOutputStream(fileOutputStream);
logger.info("Try write object for user "+userContext.getName());
out.writeObject(userContext);
} catch (IOException e) {
logger.error("1st IOException");
e.printStackTrace();
} finally {
try {
if (out != null){
logger.info("Close ObjectOutputStream");
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null) {
logger.info("Close fileOutputStream");
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static UserContext readUserContext(String affiliation, String username) {
UserContext userContext = null;
FileInputStream fileStream = null;
ObjectInputStream in = null;
try {
String filePath = "msp/" + affiliation + "/" + username + ".context";
File file = new File(filePath);
if (file.exists()) {
fileStream = new FileInputStream(filePath);
in = new ObjectInputStream(fileStream);
userContext = (UserContext) in.readObject();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileStream != null)
fileStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return userContext;
}
}
}

View file

@ -0,0 +1,20 @@
package blockchain.user;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UserContextTest {
@Test
public void TestACreateUser(){
UserContext userContext = new UserContext();
userContext.setName("admin");
userContext.setAffiliation("org1");
userContext.setMspId("MspidOrg1");
userContext.setEnrollment(null);
assertEquals("org1",userContext.getAffiliation());
assertEquals("admin",userContext.getName());
assertEquals(null,userContext.getEnrollment());
}
}

View file

@ -0,0 +1,54 @@
package blockchain.utility;
import blockchain.user.UserContext;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UtilTest {
private static Logger logger = Logger.getLogger(UtilTest.class);
@Test
public void TestAReadNullUser(){
BasicConfigurator.configure();
logger.info("----- START TEST READ NULL USER ----");
UserContext uc = Util.readUserContext("tata","abdel");
if(uc!=null){
logger.info("UserContext is : "+uc.toString());
}else{
logger.info("UserContext doesn't exist");
}
}
@Test
public void TestBWriteUser(){
BasicConfigurator.configure();
logger.info("----- START TEST WRITE NULL USER ----");
UserContext user = new UserContext();
user.setName("abdel");
user.setAffiliation("toto");
user.setEnrollment(null);
user.setMspId("test");
Util.writeUserContext(user);
}
@Test
public void TestCReadCreatedUser(){
BasicConfigurator.configure();
logger.info("----- START TEST READ CREATED USER ----");
UserContext uc = Util.readUserContext("toto","abdel");
if(uc!=null){
logger.info("UserContext is : "+uc.toString());
}else{
logger.info("UserContext doesn't exist");
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.