From 86d899ff6b3fbd7a64e23eefbb8058f6eb28431e Mon Sep 17 00:00:00 2001 From: nex Date: Tue, 21 Jul 2020 14:39:13 +0800 Subject: [PATCH] Add access token support --- .../org/bdware/bdledger/api/grpc/Client.java | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/bdware/bdledger/api/grpc/Client.java b/src/main/java/org/bdware/bdledger/api/grpc/Client.java index aeac332..d67ecd5 100644 --- a/src/main/java/org/bdware/bdledger/api/grpc/Client.java +++ b/src/main/java/org/bdware/bdledger/api/grpc/Client.java @@ -1,5 +1,7 @@ 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.Common.TransactionType; 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 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; + private ManagedChannel channel; + private NodeGrpc.NodeFutureStub nodeFutureStub; + private NodeGrpc.NodeBlockingStub nodeBlockingStub; + private LedgerGrpc.LedgerFutureStub ledgerFutureStub; + private LedgerGrpc.LedgerBlockingStub ledgerBlockingStub; + private QueryGrpc.QueryFutureStub queryFutureStub; + 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) { - 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(); + nodeFutureStub = NodeGrpc.newFutureStub(channel); nodeBlockingStub = NodeGrpc.newBlockingStub(channel); ledgerFutureStub = LedgerGrpc.newFutureStub(channel); ledgerBlockingStub = LedgerGrpc.newBlockingStub(channel); queryFutureStub = QueryGrpc.newFutureStub(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); + } } /**