Search in sources :

Example 11 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class HttpService method post.

public BaseResModel<K> post(BaseReqModel<T> req, String url, Class<K> clazz) {
    String res;
    BaseResModel<K> resModel = new BaseResModel<K>();
    try {
        req.sign();
        res = doPost(req, url);
    } catch (GlobalException e) {
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new GlobalException(ResultInfoEnum.SYSTEM_ERROR);
    }
    if (res != null && res.length() > 0) {
        try {
            resModel = JSON.parseObject(res, resModel.getClass());
            String bodystr = JSON.toJSONString(resModel.getBody());
            boolean checkJson = JsonUtil.getJSONType(bodystr);
            resModel.setBody(checkJson ? JSON.parseObject(bodystr, clazz) : null);
        } catch (Exception e) {
            throw new GlobalException(ResultInfoEnum.DATA_CONVERSION_ERROR);
        }
        // Signature verfication, data transfer
        if (resModel == null) {
            throw new GlobalException(ResultInfoEnum.INVALID_RESPONSE_ERROR);
        }
        boolean result = false;
        try {
            result = resModel.verify();
        } catch (Exception e) {
            throw new GlobalException(ResultInfoEnum.RES_MAC_ERROR);
        }
        if (!result) {
            throw new GlobalException(ResultInfoEnum.RES_MAC_ERROR);
        }
        if (resModel.getHeader().getCode() != 0) {
            throw new GlobalException(resModel.getHeader().getMsg());
        } else {
        }
        return resModel;
    } else {
        throw new GlobalException(ResultInfoEnum.SYSTEM_ERROR);
    }
}
Also used : BaseResModel(com.bsnbase.sdk.entity.base.BaseResModel) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) JsonException(javax.json.JsonException)

Example 12 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class HttpService method arrayPost.

public BaseResArrayModel<K> arrayPost(BaseReqModel<T> req, String url, Class<K> clazz) {
    String res;
    BaseResArrayModel<K> resModel = new BaseResArrayModel<K>();
    try {
        req.sign();
        res = doPost(req, url);
    } catch (Exception e) {
        e.printStackTrace();
        throw new GlobalException(ResultInfoEnum.SYSTEM_ERROR);
    }
    if (res != null && res.length() > 0) {
        try {
            resModel = JSON.parseObject(res, resModel.getClass());
            String bodystr = JSON.toJSONString(resModel.getBody());
            resModel.setBody(JSON.parseArray(bodystr, clazz));
        } catch (JsonException e) {
            throw new GlobalException(ResultInfoEnum.DATA_CONVERSION_ERROR);
        }
        // Signature verfication, data transfer
        if (resModel == null) {
            throw new GlobalException(ResultInfoEnum.INVALID_RESPONSE_ERROR);
        }
        boolean result = false;
        try {
            result = resModel.verify();
        } catch (Exception e) {
            throw new GlobalException(ResultInfoEnum.RES_MAC_ERROR);
        }
        if (!result) {
            throw new GlobalException(ResultInfoEnum.RES_MAC_ERROR);
        }
        if (resModel.getHeader().getCode() != 0) {
            throw new GlobalException(resModel.getHeader().getMsg());
        } else {
        }
        return resModel;
    } else {
        throw new GlobalException(ResultInfoEnum.SYSTEM_ERROR);
    }
}
Also used : JsonException(javax.json.JsonException) BaseResArrayModel(com.bsnbase.sdk.entity.base.BaseResArrayModel) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) JsonException(javax.json.JsonException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Example 13 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class StoreUtils method generateCSR.

/**
 * csr generation
 *
 * @param name
 * @param appCode
 * @return
 */
public static UserCertInfo generateCSR(String name, String appCode) {
    try {
        String DN = "CN=" + Common.getCNName(name, appCode) + ",OU=client";
        AlgorithmTypeEnum algorithmTypeEnum = AlgorithmTypeEnum.fromAlgorithmTypeEnum(Config.config.getAppInfo().getAlgorithmType());
        AlgorithmTypeContext algorithmTypeContext = new AlgorithmTypeContext(algorithmTypeEnum);
        UserCertInfo certInfo = algorithmTypeContext.getAlgorithmTypeHandle().getUserCertInfo(DN);
        if (Objects.isNull(certInfo)) {
            throw new GlobalException(ResultInfoEnum.ALGORITHM_TYPE_ERROR);
        }
        return certInfo;
    } catch (Exception e) {
        logger.error("Get CSR Exception", e);
        throw new GlobalException("Get CSR Exception");
    }
}
Also used : AlgorithmTypeEnum(com.bsnbase.sdk.util.enums.AlgorithmTypeEnum) AlgorithmTypeContext(com.bsnbase.sdk.util.algorithm.AlgorithmTypeContext) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Example 14 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class TransData method getTransdata.

public static String getTransdata(TransactionRequest request, TransactionUser user) throws Exception {
    ProposalPackage.Proposal proposal = proposal(request, user);
    byte[] proposalBytes = convertToBytes(proposal);
    byte[] signBytes;
    try {
        AlgorithmTypeEnum algorithmTypeEnum = AlgorithmTypeEnum.fromAlgorithmTypeEnum(com.bsnbase.sdk.entity.config.Config.config.getAppInfo().getAlgorithmType());
        signBytes = FabricTransUtil.getTransSign(algorithmTypeEnum, user.getPrivateKey(), proposalBytes);
    } catch (Exception e) {
        throw new GlobalException(ResultInfoEnum.TRANSACTION_SIGNING_ERROR);
    }
    ProposalPackage.SignedProposal.Builder sig = ProposalPackage.SignedProposal.newBuilder();
    sig.setProposalBytes(getByteString(proposalBytes));
    sig.setSignature(getByteString(signBytes));
    byte[] signProposalBytes = convertToBytes(sig.build());
    return Base64.getEncoder().encodeToString(signProposalBytes);
}
Also used : AlgorithmTypeEnum(com.bsnbase.sdk.util.enums.AlgorithmTypeEnum) ProposalPackage(org.hyperledger.fabric.protos.peer.ProposalPackage) IOException(java.io.IOException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Example 15 with GlobalException

use of com.bsnbase.sdk.util.exception.GlobalException in project PCNGateway-Java-SDK by BSNDA.

the class BaseReqModel method sign.

/**
 * The request gateway calls the "sign" method
 */
@Override
public void sign() throws Exception {
    if (this.header == null) {
        throw new GlobalException(ResultInfoEnum.REQUEST_HEADER_ERROR);
    }
    String signValue = this.header.getHeaderString();
    if (this.body != null) {
        signValue += this.body.getEncryptionValue();
    }
    AlgorithmTypeEnum algorithmTypeEnum = AlgorithmTypeEnum.fromAlgorithmTypeEnum(Config.config.getAppInfo().getAlgorithmType());
    AlgorithmTypeContext algorithmTypeContext = new AlgorithmTypeContext(algorithmTypeEnum);
    String sign = algorithmTypeContext.getAlgorithmTypeHandle().sign(Config.config.getPrk(), signValue);
    this.mac = sign;
}
Also used : AlgorithmTypeEnum(com.bsnbase.sdk.util.enums.AlgorithmTypeEnum) AlgorithmTypeContext(com.bsnbase.sdk.util.algorithm.AlgorithmTypeContext) GlobalException(com.bsnbase.sdk.util.exception.GlobalException)

Aggregations

GlobalException (com.bsnbase.sdk.util.exception.GlobalException)15 AlgorithmTypeEnum (com.bsnbase.sdk.util.enums.AlgorithmTypeEnum)5 IOException (java.io.IOException)5 JsonException (javax.json.JsonException)3 BaseReqModel (com.bsnbase.sdk.entity.base.BaseReqModel)2 BaseResModel (com.bsnbase.sdk.entity.base.BaseResModel)2 AlgorithmTypeContext (com.bsnbase.sdk.util.algorithm.AlgorithmTypeContext)2 HttpService (com.bsnbase.sdk.util.common.HttpService)2 BigInteger (java.math.BigInteger)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)2 BaseResArrayModel (com.bsnbase.sdk.entity.base.BaseResArrayModel)1 ReqKeyUploadBody (com.bsnbase.sdk.entity.req.cita.ReqKeyUploadBody)1 ReqKeyEscrowNo (com.bsnbase.sdk.entity.req.fabric.ReqKeyEscrowNo)1 ReqTrans (com.bsnbase.sdk.entity.req.fiscobcos.ReqTrans)1 ResGetBlockHeight (com.bsnbase.sdk.entity.resp.cita.ResGetBlockHeight)1 ResKeyEscrow (com.bsnbase.sdk.entity.resp.cita.ResKeyEscrow)1 ResKeyEscrowNo (com.bsnbase.sdk.entity.resp.fabric.ResKeyEscrowNo)1 ResUserInfo (com.bsnbase.sdk.entity.resp.fabric.ResUserInfo)1 ResGetBlockHeight (com.bsnbase.sdk.entity.resp.fiscobcos.ResGetBlockHeight)1