@@ -1,8 +1,10 @@ | |||||
package com.jd.blockchain.ump; | package com.jd.blockchain.ump; | ||||
import com.jd.blockchain.ump.web.RetrievalConfigListener; | |||||
import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -16,6 +18,7 @@ public class UmpBooter { | |||||
private static final String ARG_HOST = "-h"; | private static final String ARG_HOST = "-h"; | ||||
private static final String CONFIG = "BOOT-INF" + File.separator + "classes" + File.separator + "config.properties"; | private static final String CONFIG = "BOOT-INF" + File.separator + "classes" + File.separator + "config.properties"; | ||||
private static final String CONFIG_APPLICATION = "BOOT-INF" + File.separator + "classes" + File.separator + "application.properties"; | |||||
private static final String CONFIG_PROP_HOST = "server.host"; | private static final String CONFIG_PROP_HOST = "server.host"; | ||||
@@ -44,7 +47,22 @@ public class UmpBooter { | |||||
String[] args = argList.toArray(new String[argList.size()]); | String[] args = argList.toArray(new String[argList.size()]); | ||||
// 启动服务器; | // 启动服务器; | ||||
SpringApplication.run(UmpConfiguration.class, args); | |||||
// SpringApplication.run(UmpConfiguration.class, args); | |||||
InputStream inputStream = UmpBooter.class.getResourceAsStream(File.separator + CONFIG_APPLICATION); | |||||
if (inputStream == null) { | |||||
System.err.println("InputStream is NULL !!!"); | |||||
} | |||||
Properties props = new Properties(); | |||||
try { | |||||
props.load(inputStream); | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
// 启动服务器; | |||||
SpringApplication springApplication = new SpringApplication(UmpConfiguration.class); | |||||
springApplication.addListeners(new RetrievalConfigListener(props)); | |||||
springApplication.run(args); | |||||
} | } | ||||
private static Server server(String[] args) { | private static Server server(String[] args) { | ||||
@@ -2,4 +2,7 @@ server.tomcat.uri-encoding=utf-8 | |||||
spring.mvc.favicon.enabled=false | spring.mvc.favicon.enabled=false | ||||
logging.config=classpath:log4j2-jump.xml | |||||
logging.config=classpath:log4j2-jump.xml | |||||
retrieval.schemaUrl=http://192.168.151.39:8082 | |||||
retrieval.taskUrl=http://192.168.151.39:10005 |
@@ -5,4 +5,7 @@ server.host=0.0.0.0 | |||||
server.port=8080 | server.port=8080 | ||||
# 本地数据库存储位置 | # 本地数据库存储位置 | ||||
db.url=rocksdb://#project#/jumpdb | |||||
db.url=rocksdb://#project#/jumpdb | |||||
schema.retrieval.url=http://192.168.151.39:8082 | |||||
task.retrieval.url=http://192.168.151.39:10005 |
@@ -0,0 +1,73 @@ | |||||
package com.jd.blockchain.ump.model; | |||||
/** | |||||
* @author zhaogw | |||||
* date 2019/7/10 15:31 | |||||
*/ | |||||
public class ApiResult { | |||||
/** | |||||
* 错误码,对应{@link ErrorCode},表示一种错误类型 | |||||
* 如果是成功,则code为1 | |||||
*/ | |||||
private int code; | |||||
/** | |||||
* 具体解释 | |||||
*/ | |||||
private String message; | |||||
/** | |||||
* 返回的结果包装在value中,value可以是单个对象 | |||||
*/ | |||||
private Object value; | |||||
public ApiResult (){ } | |||||
public ApiResult (Object obj){ | |||||
this.value = obj; | |||||
} | |||||
public ApiResult (int code, String message){ | |||||
this.code = code; | |||||
this.message = message; | |||||
} | |||||
public ApiResult(ErrorCode errorCode){ | |||||
this.code = errorCode.getCode(); | |||||
this.message = errorCode.getMsg(); | |||||
} | |||||
public ApiResult(ErrorCode errorCode, Object obj){ | |||||
this.code = errorCode.getCode(); | |||||
this.message = errorCode.getMsg(); | |||||
this.value = obj; | |||||
} | |||||
public ApiResult (int code, String message, Object value){ | |||||
this.code = code; | |||||
this.message = message; | |||||
this.value = value; | |||||
} | |||||
public int getCode() { | |||||
return code; | |||||
} | |||||
public void setCode(int code) { | |||||
this.code = code; | |||||
} | |||||
public String getMessage() { | |||||
return message; | |||||
} | |||||
public void setMessage(String message) { | |||||
this.message = message; | |||||
} | |||||
public Object getValue() { | |||||
return value; | |||||
} | |||||
public void setValue(Object value) { | |||||
this.value = value; | |||||
} | |||||
} |
@@ -0,0 +1,33 @@ | |||||
package com.jd.blockchain.ump.model; | |||||
/** | |||||
* @author zhaogw | |||||
* date 2019/7/10 15:32 | |||||
*/ | |||||
public enum ErrorCode { | |||||
SUCCESS(1,"成功"), | |||||
NO_PERMISSION(2,"权限不足"), | |||||
SERVER_ERROR(3,"服务器异常"), | |||||
AUTH_ERROR(4,"认证失败"), | |||||
PARAMS_ERROR(5,"参数错误"), | |||||
JSON_PARSE_ERROR(6,"Json解析错误"), | |||||
ILLEAGAL_STRING(7,"非法字符串"), | |||||
GEN_KEY_INPUT_LACK(8,"缺少必要的输入参数:name/randomSeed/basePath/password"), | |||||
UNKNOW_ERROR(10000,"未知错误"); | |||||
private int code; | |||||
private String msg; | |||||
ErrorCode(int code,String msg){ | |||||
this.code = code; | |||||
this.msg = msg; | |||||
} | |||||
public int getCode() { | |||||
return code; | |||||
} | |||||
public String getMsg() { | |||||
return msg; | |||||
} | |||||
} |
@@ -101,4 +101,17 @@ public class UmpConstant { | |||||
public static final String PATH_LOCAL_CONFIG = PATH_CONFIG_INIT + File.separator + "local.conf"; | public static final String PATH_LOCAL_CONFIG = PATH_CONFIG_INIT + File.separator + "local.conf"; | ||||
public static final String PATH_LEDGER_INIT_CONFIG = PATH_CONFIG_INIT + File.separator + "ledger.init"; | public static final String PATH_LEDGER_INIT_CONFIG = PATH_CONFIG_INIT + File.separator + "ledger.init"; | ||||
public static final String PEER_HOST_IP = "peerHostIp"; | |||||
public static final String INIT_PORT = "iPort"; | |||||
public static final String CONSENSUS_PORT = "cPort"; | |||||
public static final String DELIMETER_QUESTION = "?"; //逗号分隔符; | |||||
public static final String LEDGER_LIST = "ledger_list"; //the key that save all the ledger hash in the rocksdb; | |||||
public static final int MEMORY_MAP_MAX_COUNT=10000; | |||||
public static final int MEMORY_MAP_REMOVE_COUNT=50; | |||||
public static final String ALL_LEDGER="all_ledger"; | |||||
public static final String DELIMETER_MINUS = "-"; | |||||
public static final String SCHEMA_PREFIX = "schema_"; | |||||
public static final String SCHEMA_RETRIEVAL_URL = "retrieval.schemaUrl"; | |||||
public static final String TASK_RETRIEVAL_URL = "retrieval.taskUrl"; | |||||
} | } |
@@ -0,0 +1,47 @@ | |||||
package com.jd.blockchain.ump.model.penetrate; | |||||
import java.util.List; | |||||
/** | |||||
* 数据账户信息 | |||||
* @author zhaogw | |||||
* date 2019/7/26 14:49 | |||||
*/ | |||||
public class DataAccountSchema { | |||||
private String ledgerHash; | |||||
private String dataAccount; | |||||
private String memo; | |||||
private List<FieldSchema> fieldSchemaList; | |||||
public String getLedgerHash() { | |||||
return ledgerHash; | |||||
} | |||||
public void setLedgerHash(String ledgerHash) { | |||||
this.ledgerHash = ledgerHash; | |||||
} | |||||
public String getDataAccount() { | |||||
return dataAccount; | |||||
} | |||||
public void setDataAccount(String dataAccount) { | |||||
this.dataAccount = dataAccount; | |||||
} | |||||
public String getMemo() { | |||||
return memo; | |||||
} | |||||
public void setMemo(String memo) { | |||||
this.memo = memo; | |||||
} | |||||
public List<FieldSchema> getFieldSchemaList() { | |||||
return fieldSchemaList; | |||||
} | |||||
public void setFieldSchemaList(List<FieldSchema> fieldSchemaList) { | |||||
this.fieldSchemaList = fieldSchemaList; | |||||
} | |||||
} |
@@ -0,0 +1,46 @@ | |||||
package com.jd.blockchain.ump.model.penetrate; | |||||
/** | |||||
* ump中记录的字段信息; | |||||
* @author zhaogw | |||||
* date 2019/7/26 14:50 | |||||
*/ | |||||
public class FieldSchema { | |||||
private String code; | |||||
private String fieldType; | |||||
private boolean isPrimary; | |||||
//备注; | |||||
private String memo; | |||||
public String getCode() { | |||||
return code; | |||||
} | |||||
public void setCode(String code) { | |||||
this.code = code; | |||||
} | |||||
public String getFieldType() { | |||||
return fieldType; | |||||
} | |||||
public void setFieldType(String fieldType) { | |||||
this.fieldType = fieldType; | |||||
} | |||||
public boolean isPrimary() { | |||||
return isPrimary; | |||||
} | |||||
public void setPrimary(boolean primary) { | |||||
isPrimary = primary; | |||||
} | |||||
public String getMemo() { | |||||
return memo; | |||||
} | |||||
public void setMemo(String memo) { | |||||
this.memo = memo; | |||||
} | |||||
} |
@@ -0,0 +1,79 @@ | |||||
package com.jd.blockchain.ump.model.penetrate; | |||||
import com.alibaba.fastjson.annotation.JSONField; | |||||
import java.util.List; | |||||
/** | |||||
* mediator's domain; | |||||
* @author zhaogw | |||||
* date 2019/7/2 18:02 | |||||
*/ | |||||
public class LeaderDomain { | |||||
@JSONField(name="host") | |||||
private String host; | |||||
@JSONField(name="port") | |||||
private String port; | |||||
@JSONField(name="createTime") | |||||
private String createTime; | |||||
@JSONField(name="ledgerSeed") | |||||
private String ledgerSeed; | |||||
@JSONField(name="peerDomainList") | |||||
private List<PeerDomain> peerDomainList; | |||||
@JSONField(name="ledgerHash") | |||||
private String ledgerHash; | |||||
public List<PeerDomain> getPeerDomainList() { | |||||
return peerDomainList; | |||||
} | |||||
public void setPeerDomainList(List<PeerDomain> peerDomainList) { | |||||
this.peerDomainList = peerDomainList; | |||||
} | |||||
public String getCreateTime() { | |||||
return createTime; | |||||
} | |||||
public void setCreateTime(String createTime) { | |||||
this.createTime = createTime; | |||||
} | |||||
public String getLedgerSeed() { | |||||
return ledgerSeed; | |||||
} | |||||
public void setLedgerSeed(String ledgerSeed) { | |||||
this.ledgerSeed = ledgerSeed; | |||||
} | |||||
public String getHost() { | |||||
return host; | |||||
} | |||||
public void setHost(String host) { | |||||
this.host = host; | |||||
} | |||||
public String getPort() { | |||||
return port; | |||||
} | |||||
public void setPort(String port) { | |||||
this.port = port; | |||||
} | |||||
public String getLedgerHash() { | |||||
return ledgerHash; | |||||
} | |||||
public void setLedgerHash(String ledgerHash) { | |||||
this.ledgerHash = ledgerHash; | |||||
} | |||||
} |
@@ -0,0 +1,177 @@ | |||||
package com.jd.blockchain.ump.model.penetrate; | |||||
import com.alibaba.fastjson.annotation.JSONField; | |||||
/** | |||||
* @author zhaogw | |||||
* date 2019/7/2 10:10 | |||||
*/ | |||||
public class PeerDomain { | |||||
@JSONField(name="peerId") | |||||
private int peerId; | |||||
private String basePath; | |||||
@JSONField(name="peerName") | |||||
private String peerName; | |||||
@JSONField(name="host") | |||||
private String host; | |||||
private String serverPort; | |||||
@JSONField(name="initPort") | |||||
private String initPort; | |||||
@JSONField(name="consensusPort") | |||||
private String consensusPort; | |||||
@JSONField(name="visitPort") | |||||
private String visitPort; | |||||
@JSONField(name="mediatorUrl") | |||||
private String mediatorUrl; | |||||
private String dbUri; | |||||
private boolean gatewayBindPeer; | |||||
//the random String from front input by mouse device; | |||||
private String randomSeed; | |||||
@JSONField(name="peerPubKey") | |||||
private String peerPubKey; | |||||
private String peerPrivKey; | |||||
private String peerRawPasswd; | |||||
private String peerPasswd; | |||||
public int getPeerId() { | |||||
return peerId; | |||||
} | |||||
public void setPeerId(int peerId) { | |||||
this.peerId = peerId; | |||||
} | |||||
public String getBasePath() { | |||||
return basePath; | |||||
} | |||||
public void setBasePath(String basePath) { | |||||
this.basePath = basePath; | |||||
} | |||||
public String getPeerName() { | |||||
return peerName; | |||||
} | |||||
public void setPeerName(String peerName) { | |||||
this.peerName = peerName; | |||||
} | |||||
public String getHost() { | |||||
return host; | |||||
} | |||||
public void setHost(String host) { | |||||
this.host = host; | |||||
} | |||||
public String getServerPort() { | |||||
return serverPort; | |||||
} | |||||
public void setServerPort(String serverPort) { | |||||
this.serverPort = serverPort; | |||||
} | |||||
public String getInitPort() { | |||||
return initPort; | |||||
} | |||||
public void setInitPort(String initPort) { | |||||
this.initPort = initPort; | |||||
} | |||||
public String getConsensusPort() { | |||||
return consensusPort; | |||||
} | |||||
public void setConsensusPort(String consensusPort) { | |||||
this.consensusPort = consensusPort; | |||||
} | |||||
public String getVisitPort() { | |||||
return visitPort; | |||||
} | |||||
public void setVisitPort(String visitPort) { | |||||
this.visitPort = visitPort; | |||||
} | |||||
public String getMediatorUrl() { | |||||
return mediatorUrl; | |||||
} | |||||
public void setMediatorUrl(String mediatorUrl) { | |||||
this.mediatorUrl = mediatorUrl; | |||||
} | |||||
public String getDbUri() { | |||||
return dbUri; | |||||
} | |||||
public void setDbUri(String dbUri) { | |||||
this.dbUri = dbUri; | |||||
} | |||||
public boolean isGatewayBindPeer() { | |||||
return gatewayBindPeer; | |||||
} | |||||
public void setGatewayBindPeer(boolean gatewayBindPeer) { | |||||
this.gatewayBindPeer = gatewayBindPeer; | |||||
} | |||||
public String getRandomSeed() { | |||||
return randomSeed; | |||||
} | |||||
public void setRandomSeed(String randomSeed) { | |||||
this.randomSeed = randomSeed; | |||||
} | |||||
public String getPeerPubKey() { | |||||
return peerPubKey; | |||||
} | |||||
public void setPeerPubKey(String peerPubKey) { | |||||
this.peerPubKey = peerPubKey; | |||||
} | |||||
public String getPeerPrivKey() { | |||||
return peerPrivKey; | |||||
} | |||||
public void setPeerPrivKey(String peerPrivKey) { | |||||
this.peerPrivKey = peerPrivKey; | |||||
} | |||||
public String getPeerRawPasswd() { | |||||
return peerRawPasswd; | |||||
} | |||||
public void setPeerRawPasswd(String peerRawPasswd) { | |||||
this.peerRawPasswd = peerRawPasswd; | |||||
} | |||||
public String getPeerPasswd() { | |||||
return peerPasswd; | |||||
} | |||||
public void setPeerPasswd(String peerPasswd) { | |||||
this.peerPasswd = peerPasswd; | |||||
} | |||||
} |
@@ -0,0 +1,90 @@ | |||||
package com.jd.blockchain.ump.model.penetrate; | |||||
import com.alibaba.fastjson.annotation.JSONField; | |||||
import com.jd.blockchain.ump.model.UmpConstant; | |||||
import org.springframework.util.CollectionUtils; | |||||
import java.util.List; | |||||
/** | |||||
* @author zhaogw | |||||
* date 2019/7/19 11:33 | |||||
*/ | |||||
public class SchemaDomain { | |||||
@JSONField(serialize = false) | |||||
private String schemaId; | |||||
@JSONField(serialize = false) | |||||
private String schemaAllId; | |||||
@JSONField(name="ledger") | |||||
private String ledgerHash; | |||||
@JSONField(name="associate_account") | |||||
private String dataAccount; | |||||
@JSONField(serialize = false) | |||||
private List<FieldSchema> fieldSchemaList; | |||||
private String content; | |||||
public String getSchemaId() { | |||||
return schemaId; | |||||
} | |||||
public void setSchemaId(String schemaId) { | |||||
this.schemaId = schemaId; | |||||
} | |||||
public String getLedgerHash() { | |||||
return ledgerHash; | |||||
} | |||||
public void setLedgerHash(String ledgerHash) { | |||||
this.ledgerHash = ledgerHash; | |||||
} | |||||
public String getDataAccount() { | |||||
return dataAccount; | |||||
} | |||||
public void setDataAccount(String dataAccount) { | |||||
this.dataAccount = dataAccount; | |||||
} | |||||
public String getContent() { | |||||
if(CollectionUtils.isEmpty(fieldSchemaList)){ | |||||
throw new IllegalStateException("content is empty! you must choose the field first!"); | |||||
} | |||||
StringBuffer stringBuffer = new StringBuffer(); | |||||
stringBuffer.append("type "+this.schemaId).append("{").append(" "); | |||||
for(FieldSchema fieldSchema : fieldSchemaList){ | |||||
if(fieldSchema.isPrimary()){ | |||||
stringBuffer.append(fieldSchema.getCode()+"(isPrimaryKey: Boolean = true):"+fieldSchema.getFieldType()).append(" "); | |||||
}else { | |||||
stringBuffer.append(fieldSchema.getCode()+":"+fieldSchema.getFieldType()).append(" "); | |||||
} | |||||
} | |||||
stringBuffer.append("}"); | |||||
return stringBuffer.toString(); | |||||
} | |||||
public void setContent(String content) { | |||||
this.content = content; | |||||
} | |||||
public List<FieldSchema> getFieldSchemaList() { | |||||
return fieldSchemaList; | |||||
} | |||||
public void setFieldSchemaList(List<FieldSchema> fieldSchemaList) { | |||||
this.fieldSchemaList = fieldSchemaList; | |||||
} | |||||
public String getSchemaAllId() { | |||||
this.schemaAllId = schemaId+ UmpConstant.DELIMETER_MINUS+ledgerHash.substring(0,6)+ | |||||
UmpConstant.DELIMETER_MINUS+ dataAccount.substring(0,6); | |||||
return schemaAllId; | |||||
} | |||||
} |
@@ -0,0 +1,70 @@ | |||||
package com.jd.blockchain.ump.model.penetrate.store; | |||||
import com.jd.blockchain.ump.model.UmpConstant; | |||||
import java.util.concurrent.BlockingQueue; | |||||
import java.util.concurrent.LinkedBlockingQueue; | |||||
/** | |||||
* 通过枚举的方式来实现内存队列;避免大并发时引起的不必要的多次实例化; | |||||
* | |||||
* @author zhaoguangwei | |||||
* | |||||
*/ | |||||
public enum MemQueue { | |||||
instance; | |||||
private BlockingQueue<String> queue = null; | |||||
private MemQueue() { | |||||
queue = new LinkedBlockingQueue<String>(); | |||||
} | |||||
/** | |||||
* 记录放入内存队列; | |||||
* @param key | |||||
* @return | |||||
*/ | |||||
public boolean put(String key) { | |||||
boolean rtn = false; | |||||
try { | |||||
while (queue.size() >= UmpConstant.MEMORY_MAP_MAX_COUNT) { | |||||
queue.remove(); | |||||
} | |||||
queue.add(key); | |||||
rtn = true; | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
return rtn; | |||||
} | |||||
/** | |||||
* 从内存队列取出一条记录; | |||||
* | |||||
* @return | |||||
*/ | |||||
public String get() { | |||||
String record = null; | |||||
try { | |||||
record = queue.take(); | |||||
} catch (InterruptedException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
return record; | |||||
} | |||||
/** | |||||
* 内存队列清除; | |||||
*/ | |||||
public void clear() { | |||||
queue.clear(); | |||||
} | |||||
/** | |||||
* 获得记录数; | |||||
* | |||||
* @return | |||||
*/ | |||||
public int size() { | |||||
return queue.size(); | |||||
} | |||||
} |
@@ -0,0 +1,49 @@ | |||||
package com.jd.blockchain.ump.model.penetrate.store; | |||||
import com.jd.blockchain.ump.model.UmpConstant; | |||||
import java.util.Map; | |||||
import java.util.concurrent.ConcurrentHashMap; | |||||
import static com.jd.blockchain.ump.model.UmpConstant.MEMORY_MAP_MAX_COUNT; | |||||
import static com.jd.blockchain.ump.model.UmpConstant.MEMORY_MAP_REMOVE_COUNT; | |||||
/** | |||||
* @author zhaogw | |||||
* date 2019/7/17 17:10 | |||||
*/ | |||||
public enum MemStore { | |||||
instance; | |||||
private Map<String,Object> records = null; | |||||
MemStore(){ | |||||
records = new ConcurrentHashMap(); | |||||
} | |||||
public Object get(String key){ | |||||
return records.get(key); | |||||
} | |||||
public Object remove(String key){ | |||||
return records.remove(key); | |||||
} | |||||
public boolean put(String key, Object obj){ | |||||
boolean rtn = false; | |||||
MemQueue.instance.put(key); | |||||
if(records.size()>MEMORY_MAP_MAX_COUNT){ | |||||
//clear 50 records; | |||||
for(int i=0; i< MEMORY_MAP_REMOVE_COUNT; i++){ | |||||
String _key = MemQueue.instance.get(); | |||||
if(_key.equals(UmpConstant.ALL_LEDGER)){ | |||||
//don't remove the all_ledger; | |||||
continue; | |||||
} | |||||
records.remove(_key); | |||||
} | |||||
} | |||||
records.put(key,obj); | |||||
return true; | |||||
} | |||||
} |
@@ -0,0 +1,26 @@ | |||||
/** | |||||
* Copyright: Copyright 2016-2020 JD.COM All Right Reserved | |||||
* FileName: com.jd.blockchain.gateway.service.DataRetrievalService | |||||
* Author: shaozhuguang | |||||
* Department: 区块链研发部 | |||||
* Date: 2019/1/15 下午3:08 | |||||
* Description: | |||||
*/ | |||||
package com.jd.blockchain.ump.service; | |||||
import com.jd.blockchain.ump.model.penetrate.DataAccountSchema; | |||||
/** | |||||
* data account ump store; | |||||
*/ | |||||
public interface DataAccountUmpService { | |||||
/** | |||||
* 整体新增dataAccountSchema,单独某个field不会更新;如果原先库中有记录,则更新为最新内容; | |||||
* @param dataAccountSchema | |||||
*/ | |||||
boolean addDataAccountSchema(DataAccountSchema dataAccountSchema); | |||||
DataAccountSchema deleteDataAcccountSchema(String ledgerHash, String dataAccount); | |||||
DataAccountSchema findDataAccountSchema(String ledgerHash, String dataAccount); | |||||
} |
@@ -0,0 +1,31 @@ | |||||
package com.jd.blockchain.ump.service; | |||||
import com.jd.blockchain.ump.model.penetrate.DataAccountSchema; | |||||
import com.jd.blockchain.ump.model.penetrate.store.MemStore; | |||||
import org.springframework.stereotype.Service; | |||||
import static com.jd.blockchain.ump.model.UmpConstant.SCHEMA_PREFIX; | |||||
import static com.jd.blockchain.utils.BaseConstant.DELIMETER_UNDERLINE; | |||||
/** | |||||
* @author zhaogw | |||||
* date 2019/7/26 15:14 | |||||
*/ | |||||
@Service | |||||
public class DataAccountUmpServiceImpl implements DataAccountUmpService { | |||||
@Override | |||||
public boolean addDataAccountSchema(DataAccountSchema dataAccountSchema) { | |||||
return MemStore.instance.put(SCHEMA_PREFIX+dataAccountSchema.getLedgerHash()+ | |||||
DELIMETER_UNDERLINE+dataAccountSchema.getDataAccount(),dataAccountSchema); | |||||
} | |||||
@Override | |||||
public DataAccountSchema deleteDataAcccountSchema(String ledgerHash, String dataAccount) { | |||||
return (DataAccountSchema)MemStore.instance.remove(SCHEMA_PREFIX+ledgerHash+ DELIMETER_UNDERLINE+dataAccount); | |||||
} | |||||
@Override | |||||
public DataAccountSchema findDataAccountSchema(String ledgerHash, String dataAccount) { | |||||
return (DataAccountSchema)MemStore.instance.get(SCHEMA_PREFIX+ledgerHash+ DELIMETER_UNDERLINE+dataAccount); | |||||
} | |||||
} |
@@ -0,0 +1,28 @@ | |||||
/** | |||||
* Copyright: Copyright 2016-2020 JD.COM All Right Reserved | |||||
* FileName: com.jd.blockchain.gateway.service.DataRetrievalService | |||||
* Author: shaozhuguang | |||||
* Department: 区块链研发部 | |||||
* Date: 2019/1/15 下午3:08 | |||||
* Description: | |||||
*/ | |||||
package com.jd.blockchain.ump.service; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import java.util.Map; | |||||
/** | |||||
* | |||||
* @author shaozhuguang | |||||
* @create 2019/1/15 | |||||
* @since 1.0.0 | |||||
*/ | |||||
public interface DataRetrievalService { | |||||
String retrieval(String url) throws Exception; | |||||
String retrievalPost(String url, JSONObject jsonObject) throws Exception; | |||||
String retrievalPost(String url, String queryString) throws Exception; | |||||
String delete(String url) throws Exception; | |||||
} |
@@ -0,0 +1,45 @@ | |||||
/** | |||||
* Copyright: Copyright 2016-2020 JD.COM All Right Reserved | |||||
* FileName: com.jd.blockchain.gateway.service.DeepQueryServiceImpl | |||||
* Author: shaozhuguang | |||||
* Department: 区块链研发部 | |||||
* Date: 2019/1/15 下午3:09 | |||||
* Description: | |||||
*/ | |||||
package com.jd.blockchain.ump.service; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.jd.blockchain.ump.util.HttpClientPool; | |||||
import org.springframework.stereotype.Component; | |||||
import java.util.Map; | |||||
/** | |||||
* | |||||
* @author shaozhuguang | |||||
* @create 2019/1/15 | |||||
* @since 1.0.0 | |||||
*/ | |||||
@Component | |||||
public class DataRetrievalServiceHandler implements DataRetrievalService { | |||||
@Override | |||||
public String retrieval(String url) throws Exception { | |||||
return HttpClientPool.get(url); | |||||
} | |||||
@Override | |||||
public String retrievalPost(String url, JSONObject jsonObject) throws Exception { | |||||
return HttpClientPool.jsonPost(url,jsonObject.toJSONString()); | |||||
} | |||||
@Override | |||||
public String delete(String url) throws Exception { | |||||
return HttpClientPool.delete(url); | |||||
} | |||||
@Override | |||||
public String retrievalPost(String url, String queryString) throws Exception { | |||||
return HttpClientPool.jsonPost(url,queryString); | |||||
} | |||||
} |
@@ -0,0 +1,51 @@ | |||||
/** | |||||
* Copyright: Copyright 2016-2020 JD.COM All Right Reserved | |||||
* FileName: com.jd.blockchain.gateway.service.DataRetrievalService | |||||
* Author: shaozhuguang | |||||
* Department: 区块链研发部 | |||||
* Date: 2019/1/15 下午3:08 | |||||
* Description: | |||||
*/ | |||||
package com.jd.blockchain.ump.service; | |||||
import com.jd.blockchain.ump.model.penetrate.LeaderDomain; | |||||
import java.util.List; | |||||
/** | |||||
* data store; | |||||
*/ | |||||
public interface UmpStoreService { | |||||
/** | |||||
* get ledger info by ledgerHash; | |||||
* @param ledgerHash | |||||
* @return | |||||
*/ | |||||
LeaderDomain findLedgerInfo(String ledgerHash); | |||||
/** | |||||
* get all ledgers; | |||||
* @return | |||||
*/ | |||||
List <LeaderDomain> findAllLedgers(); | |||||
/** | |||||
* get all states by seed; | |||||
* @param seed | |||||
* @return | |||||
*/ | |||||
List<String> findStates(String seed); | |||||
/** | |||||
* get value by key; | |||||
* @param key | |||||
* @return | |||||
*/ | |||||
Object findRecord(String key); | |||||
void saveLedgerInfo(LeaderDomain leaderDomain); | |||||
void saveRecord(String key, Object obj); | |||||
void saveState4Seed(String key, String state); | |||||
} |
@@ -0,0 +1,76 @@ | |||||
/** | |||||
* Copyright: Copyright 2016-2020 JD.COM All Right Reserved | |||||
* FileName: com.jd.blockchain.gateway.service.DataRetrievalService | |||||
* Author: shaozhuguang | |||||
* Department: 区块链研发部 | |||||
* Date: 2019/1/15 下午3:08 | |||||
* Description: | |||||
*/ | |||||
package com.jd.blockchain.ump.service; | |||||
import com.jd.blockchain.ump.model.UmpConstant; | |||||
import com.jd.blockchain.ump.model.penetrate.LeaderDomain; | |||||
import com.jd.blockchain.ump.model.penetrate.store.MemStore; | |||||
import org.springframework.stereotype.Service; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
/** | |||||
* data store; | |||||
*/ | |||||
@Service | |||||
public class UmpStoreServiceImpl implements UmpStoreService{ | |||||
@Override | |||||
public LeaderDomain findLedgerInfo(String ledgerHash) { | |||||
return (LeaderDomain) MemStore.instance.get(ledgerHash); | |||||
} | |||||
@Override | |||||
public List<LeaderDomain> findAllLedgers() { | |||||
return (List<LeaderDomain>)MemStore.instance.get(UmpConstant.ALL_LEDGER); | |||||
} | |||||
@Override | |||||
public List<String> findStates(String seed) { | |||||
return (List<String>)MemStore.instance.get(seed); | |||||
} | |||||
@Override | |||||
public Object findRecord(String key) { | |||||
return MemStore.instance.get(key); | |||||
} | |||||
@Override | |||||
public void saveLedgerInfo(LeaderDomain leaderDomain) { | |||||
MemStore.instance.put(leaderDomain.getLedgerHash(),leaderDomain); | |||||
//also put it into ALL_LEDGER; | |||||
Object obj = MemStore.instance.get(UmpConstant.ALL_LEDGER); | |||||
List<LeaderDomain> leaderDomains = null; | |||||
if(obj == null){ | |||||
leaderDomains = new ArrayList<>(); | |||||
}else { | |||||
leaderDomains = (List<LeaderDomain>)obj; | |||||
} | |||||
leaderDomains.add(leaderDomain); | |||||
MemStore.instance.put(UmpConstant.ALL_LEDGER,leaderDomains); | |||||
} | |||||
@Override | |||||
public void saveRecord(String key, Object obj) { | |||||
MemStore.instance.put(key,obj); | |||||
} | |||||
@Override | |||||
public void saveState4Seed(String key, String state) { | |||||
Object obj = MemStore.instance.get(key); | |||||
List<String> states = null; | |||||
if(obj == null){ | |||||
states = new ArrayList<>(); | |||||
}else { | |||||
states = (List<String>)obj; | |||||
} | |||||
states.add(state); | |||||
MemStore.instance.put(key,states); | |||||
} | |||||
} |
@@ -8,14 +8,13 @@ | |||||
*/ | */ | ||||
package com.jd.blockchain.ump.util; | package com.jd.blockchain.ump.util; | ||||
import com.alibaba.fastjson.JSONObject; | |||||
import org.apache.http.*; | import org.apache.http.*; | ||||
import org.apache.http.client.ClientProtocolException; | |||||
import org.apache.http.client.HttpRequestRetryHandler; | import org.apache.http.client.HttpRequestRetryHandler; | ||||
import org.apache.http.client.config.RequestConfig; | import org.apache.http.client.config.RequestConfig; | ||||
import org.apache.http.client.entity.UrlEncodedFormEntity; | import org.apache.http.client.entity.UrlEncodedFormEntity; | ||||
import org.apache.http.client.methods.CloseableHttpResponse; | |||||
import org.apache.http.client.methods.HttpGet; | |||||
import org.apache.http.client.methods.HttpPost; | |||||
import org.apache.http.client.methods.HttpRequestBase; | |||||
import org.apache.http.client.methods.*; | |||||
import org.apache.http.client.protocol.HttpClientContext; | import org.apache.http.client.protocol.HttpClientContext; | ||||
import org.apache.http.config.Registry; | import org.apache.http.config.Registry; | ||||
import org.apache.http.config.RegistryBuilder; | import org.apache.http.config.RegistryBuilder; | ||||
@@ -29,14 +28,14 @@ import org.apache.http.entity.StringEntity; | |||||
import org.apache.http.impl.client.CloseableHttpClient; | import org.apache.http.impl.client.CloseableHttpClient; | ||||
import org.apache.http.impl.client.HttpClients; | import org.apache.http.impl.client.HttpClients; | ||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||||
import org.apache.http.message.BasicHeader; | |||||
import org.apache.http.message.BasicNameValuePair; | import org.apache.http.message.BasicNameValuePair; | ||||
import org.apache.http.protocol.HTTP; | |||||
import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | ||||
import javax.net.ssl.SSLException; | import javax.net.ssl.SSLException; | ||||
import javax.net.ssl.SSLHandshakeException; | import javax.net.ssl.SSLHandshakeException; | ||||
import java.io.IOException; | |||||
import java.io.InterruptedIOException; | |||||
import java.io.UnsupportedEncodingException; | |||||
import java.io.*; | |||||
import java.net.UnknownHostException; | import java.net.UnknownHostException; | ||||
import java.nio.charset.Charset; | import java.nio.charset.Charset; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
@@ -286,4 +285,19 @@ public class HttpClientPool { | |||||
EntityUtils.consume(entity); | EntityUtils.consume(entity); | ||||
return result; | return result; | ||||
} | } | ||||
public static String delete(String url) throws IOException { | |||||
HttpDelete httpDelete = new HttpDelete(url); | |||||
config(httpDelete); | |||||
try (CloseableHttpResponse response = httpDelete(url, httpDelete)) { | |||||
return parseResponse(response); | |||||
} | |||||
} | |||||
private static CloseableHttpResponse httpDelete(String url, HttpDelete httpDelete) throws IOException { | |||||
return getHttpClient(url) | |||||
.execute(httpDelete, HttpClientContext.create()); | |||||
} | |||||
} | } |
@@ -0,0 +1,270 @@ | |||||
package com.jd.blockchain.ump.controller; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.jd.blockchain.ump.model.ApiResult; | |||||
import com.jd.blockchain.ump.model.ErrorCode; | |||||
import com.jd.blockchain.ump.model.UmpConstant; | |||||
import com.jd.blockchain.ump.model.penetrate.DataAccountSchema; | |||||
import com.jd.blockchain.ump.model.penetrate.SchemaDomain; | |||||
import com.jd.blockchain.ump.service.DataAccountUmpService; | |||||
import com.jd.blockchain.ump.service.DataRetrievalService; | |||||
import com.jd.blockchain.ump.web.RetrievalConfig; | |||||
import com.jd.blockchain.utils.ConsoleUtils; | |||||
import org.apache.commons.logging.Log; | |||||
import org.apache.commons.logging.LogFactory; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.web.bind.annotation.*; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
/** | |||||
* @Author zhaogw | |||||
* date 2019/07/18 17:01 | |||||
*/ | |||||
@RestController | |||||
@RequestMapping(path = "/schema") | |||||
public class RetrievalController { | |||||
private static final Log log = LogFactory.getLog(RetrievalController.class); | |||||
@Autowired | |||||
private DataRetrievalService dataRetrievalService; | |||||
@Autowired | |||||
private DataAccountUmpService dataAccountUmpService; | |||||
/** | |||||
* add schema by web; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.POST, value = "") | |||||
public Object addSchema4Web(HttpServletRequest request, @RequestBody SchemaDomain schemaDomain) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl() == null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrievalPost(fullQueryUrl,JSONObject.toJSONString(schemaDomain)); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* add schema; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
// @RequestMapping(method = RequestMethod.POST, value = "") | |||||
@Deprecated | |||||
public Object addSchema(HttpServletRequest request,@RequestBody JSONObject jsonObject) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl() == null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrievalPost(fullQueryUrl,jsonObject); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* list schema; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.GET, value = "/list") | |||||
public Object listSchema(HttpServletRequest request) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl() == null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrieval(fullQueryUrl); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* delete schema; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.DELETE, value = "/{schemaId}") | |||||
public Object deleteSchema(HttpServletRequest request) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl() == null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.delete(fullQueryUrl); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* start schema; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.GET, value = "/start/{schemaId}") | |||||
public Object startSchema(HttpServletRequest request) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl()==null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrieval(fullQueryUrl); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* stop schema; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.GET, value = "/stop/{schemaId}") | |||||
public Object stopSchema(HttpServletRequest request) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl() == null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrieval(fullQueryUrl); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* querysql; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.POST, value = "/querysql") | |||||
public Object queryBySql(HttpServletRequest request,@RequestBody String queryString) { | |||||
String result; | |||||
if (RetrievalConfig.getSchemaUrl() == null || RetrievalConfig.getSchemaUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getSchemaUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrievalPost(fullQueryUrl,queryString); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
/** | |||||
* add dataAccountSchema; | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.POST, value = "/addDataAccountSchema") | |||||
public ApiResult addDataAccountSchema( @RequestBody DataAccountSchema dataAccountSchema) { | |||||
try { | |||||
dataAccountUmpService.addDataAccountSchema(dataAccountSchema); | |||||
return new ApiResult(ErrorCode.SUCCESS); | |||||
} catch (Exception e) { | |||||
return new ApiResult(ErrorCode.SERVER_ERROR,e.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* delete dataAccountSchema; | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.GET, value = "/delDataAccountSchema/ledger/{ledgerHash}/account/{dataAccount}") | |||||
public ApiResult deleteDataAccountSchema(@PathVariable(name = "ledgerHash") String ledgerHash, | |||||
@PathVariable(name = "dataAccount") String dataAccount) { | |||||
try { | |||||
DataAccountSchema dataAccountSchema = dataAccountUmpService.deleteDataAcccountSchema(ledgerHash, dataAccount); | |||||
return new ApiResult(ErrorCode.SUCCESS,dataAccountSchema); | |||||
} catch (Exception e) { | |||||
return new ApiResult(ErrorCode.SERVER_ERROR,e.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* find dataAccountSchema; | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.GET, value = "/findDataAccountSchema/ledger/{ledgerHash}/account/{dataAccount}") | |||||
public ApiResult findDataAccountSchema(@PathVariable(name = "ledgerHash") String ledgerHash, | |||||
@PathVariable(name = "dataAccount") String dataAccount) { | |||||
try { | |||||
DataAccountSchema dataAccountSchema = dataAccountUmpService.findDataAccountSchema(ledgerHash, dataAccount); | |||||
return new ApiResult(ErrorCode.SUCCESS,dataAccountSchema); | |||||
} catch (Exception e) { | |||||
return new ApiResult(ErrorCode.SERVER_ERROR,e.getMessage()); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,59 @@ | |||||
package com.jd.blockchain.ump.controller; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.jd.blockchain.ump.model.UmpConstant; | |||||
import com.jd.blockchain.ump.service.DataRetrievalService; | |||||
import com.jd.blockchain.ump.web.RetrievalConfig; | |||||
import com.jd.blockchain.utils.ConsoleUtils; | |||||
import org.apache.commons.logging.Log; | |||||
import org.apache.commons.logging.LogFactory; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.web.bind.annotation.RequestMapping; | |||||
import org.springframework.web.bind.annotation.RequestMethod; | |||||
import org.springframework.web.bind.annotation.RestController; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
/** | |||||
* @Author zhaogw | |||||
* date 2019/07/18 17:01 | |||||
*/ | |||||
@RestController | |||||
@RequestMapping(path = "/tasks") | |||||
public class TaskRetrievalController { | |||||
private static final Log log = LogFactory.getLog(TaskRetrievalController.class); | |||||
// @Value("${task.retrieval.url}") | |||||
// private String taskRetrievalUrl; | |||||
@Autowired | |||||
private DataRetrievalService dataRetrievalService; | |||||
/** | |||||
* get the nums of all tasks; | |||||
* @param request | |||||
* @return | |||||
*/ | |||||
@RequestMapping(method = RequestMethod.GET, value = "") | |||||
public Object listSchema(HttpServletRequest request) { | |||||
String result; | |||||
if (RetrievalConfig.getTaskUrl() == null || RetrievalConfig.getTaskUrl().length() <= 0) { | |||||
result = "{'message':'OK','data':'" + "schema.retrieval.url is empty" + "'}"; | |||||
} else { | |||||
String queryParams = request.getQueryString() == null ? "": request.getQueryString(); | |||||
String fullQueryUrl = new StringBuffer(RetrievalConfig.getTaskUrl()) | |||||
.append(request.getRequestURI()) | |||||
.append(UmpConstant.DELIMETER_QUESTION) | |||||
.append(queryParams) | |||||
.toString(); | |||||
try { | |||||
result = dataRetrievalService.retrieval(fullQueryUrl); | |||||
ConsoleUtils.info("request = {%s} \r\n result = {%s} \r\n", fullQueryUrl, result); | |||||
} catch (Exception e) { | |||||
result = "{'message':'error','data':'" + e.getMessage() + "'}"; | |||||
} | |||||
} | |||||
return JSONObject.parse(result); | |||||
} | |||||
} |
@@ -0,0 +1,62 @@ | |||||
package com.jd.blockchain.ump.web; | |||||
import org.springframework.beans.BeansException; | |||||
import org.springframework.core.io.support.PropertiesLoaderUtils; | |||||
import java.io.IOException; | |||||
import java.io.UnsupportedEncodingException; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
import java.util.Properties; | |||||
import static com.jd.blockchain.ump.model.UmpConstant.SCHEMA_RETRIEVAL_URL; | |||||
import static com.jd.blockchain.ump.model.UmpConstant.TASK_RETRIEVAL_URL; | |||||
public class RetrievalConfig { | |||||
private static Map<String,String> propertiesMap = new HashMap<>(); | |||||
private String schemaUrl; | |||||
private String taskUrl; | |||||
public static void processProperties(Properties props) throws BeansException { | |||||
propertiesMap = new HashMap<String, String>(); | |||||
for (Object key : props.keySet()) { | |||||
String keyStr = key.toString(); | |||||
try { | |||||
// PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下 | |||||
propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"), "utf-8")); | |||||
} catch (UnsupportedEncodingException e) { | |||||
e.printStackTrace(); | |||||
} catch (java.lang.Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
public static void loadAllProperties(String propertyFileName) { | |||||
try { | |||||
Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName); | |||||
processProperties(properties); | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
public static String getProperty(String name) { | |||||
return propertiesMap.get(name).toString(); | |||||
} | |||||
public static Map<String, String> getAllProperty() { | |||||
return propertiesMap; | |||||
} | |||||
public static String getSchemaUrl() { | |||||
return propertiesMap.get(SCHEMA_RETRIEVAL_URL); | |||||
} | |||||
public static String getTaskUrl() { | |||||
return propertiesMap.get(TASK_RETRIEVAL_URL); | |||||
} | |||||
} |
@@ -0,0 +1,20 @@ | |||||
package com.jd.blockchain.ump.web; | |||||
import org.springframework.context.ApplicationListener; | |||||
import org.springframework.context.event.ContextRefreshedEvent; | |||||
import java.util.Properties; | |||||
public class RetrievalConfigListener implements ApplicationListener<ContextRefreshedEvent> { | |||||
private Properties props; | |||||
public RetrievalConfigListener(Properties props){ | |||||
this.props = props; | |||||
} | |||||
@Override | |||||
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { | |||||
RetrievalConfig.processProperties(props); | |||||
} | |||||
} |