Add access token support

This commit is contained in:
Xiaomin Zhu 2020-07-21 14:39:13 +08:00
parent e7c67dd415
commit 86d899ff6b

View File

@ -1,5 +1,7 @@
package org.bdware.bdledger.api.grpc; package org.bdware.bdledger.api.grpc;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
import org.bdware.bdledger.api.grpc.pb.*; import org.bdware.bdledger.api.grpc.pb.*;
import org.bdware.bdledger.api.grpc.pb.Common.TransactionType; import org.bdware.bdledger.api.grpc.pb.Common.TransactionType;
import org.bdware.bdledger.api.grpc.pb.LedgerOuterClass.*; import org.bdware.bdledger.api.grpc.pb.LedgerOuterClass.*;
@ -30,32 +32,58 @@ public class Client {
private static final Logger logger = Logger.getLogger(Client.class.getName()); private static final Logger logger = Logger.getLogger(Client.class.getName());
private final ManagedChannel channel; private ManagedChannel channel;
private final NodeGrpc.NodeFutureStub nodeFutureStub; private NodeGrpc.NodeFutureStub nodeFutureStub;
private final NodeGrpc.NodeBlockingStub nodeBlockingStub; private NodeGrpc.NodeBlockingStub nodeBlockingStub;
private final LedgerGrpc.LedgerFutureStub ledgerFutureStub; private LedgerGrpc.LedgerFutureStub ledgerFutureStub;
private final LedgerGrpc.LedgerBlockingStub ledgerBlockingStub; private LedgerGrpc.LedgerBlockingStub ledgerBlockingStub;
private final QueryGrpc.QueryFutureStub queryFutureStub; private QueryGrpc.QueryFutureStub queryFutureStub;
private final QueryGrpc.QueryBlockingStub queryBlockingStub; private QueryGrpc.QueryBlockingStub queryBlockingStub;
// private final ManagedChannel channel;
// private final NodeGrpc.NodeFutureStub nodeFutureStub;
// private final NodeGrpc.NodeBlockingStub nodeBlockingStub;
// private final LedgerGrpc.LedgerFutureStub ledgerFutureStub;
// private final LedgerGrpc.LedgerBlockingStub ledgerBlockingStub;
// private final QueryGrpc.QueryFutureStub queryFutureStub;
// private final QueryGrpc.QueryBlockingStub queryBlockingStub;
/** /**
* 构造客户端来访问{@code host:port}的事务账本服务 * 构造客户端来{@code host:port} BDLedger节点发请求
*/ */
public Client(String host, int port) { public Client(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext()); this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(), null);
} }
/** /**
* 用已有的{@link io.grpc.Channel}对象构造客户端来访问事务账本服务 * 构造客户端使用{@code token}作access token来向{@code host:port} BDLedger节点发请求
*/ */
public Client(ManagedChannelBuilder<?> channelBuilder) { public Client(String host, int port, String token) {
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(), token);
}
/**
* 用已有的{@link io.grpc.Channel}对象构造客户端来向BDLedger节点发请求
*/
public Client(ManagedChannelBuilder<?> channelBuilder, String token) {
channel = channelBuilder.build(); channel = channelBuilder.build();
nodeFutureStub = NodeGrpc.newFutureStub(channel); nodeFutureStub = NodeGrpc.newFutureStub(channel);
nodeBlockingStub = NodeGrpc.newBlockingStub(channel); nodeBlockingStub = NodeGrpc.newBlockingStub(channel);
ledgerFutureStub = LedgerGrpc.newFutureStub(channel); ledgerFutureStub = LedgerGrpc.newFutureStub(channel);
ledgerBlockingStub = LedgerGrpc.newBlockingStub(channel); ledgerBlockingStub = LedgerGrpc.newBlockingStub(channel);
queryFutureStub = QueryGrpc.newFutureStub(channel); queryFutureStub = QueryGrpc.newFutureStub(channel);
queryBlockingStub = QueryGrpc.newBlockingStub(channel); queryBlockingStub = QueryGrpc.newBlockingStub(channel);
if (token != null) {
Metadata fixedHeaders = new Metadata();
fixedHeaders.put(Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER), token);
nodeFutureStub = MetadataUtils.attachHeaders(nodeFutureStub, fixedHeaders);
nodeBlockingStub = MetadataUtils.attachHeaders(nodeBlockingStub, fixedHeaders);
ledgerFutureStub = MetadataUtils.attachHeaders(ledgerFutureStub, fixedHeaders);
ledgerBlockingStub = MetadataUtils.attachHeaders(ledgerBlockingStub, fixedHeaders);
queryFutureStub = MetadataUtils.attachHeaders(queryFutureStub, fixedHeaders);
queryBlockingStub = MetadataUtils.attachHeaders(queryBlockingStub, fixedHeaders);
}
} }
/** /**