1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.client.armeria.legacy;
17  
18  import java.net.UnknownHostException;
19  import java.util.concurrent.ScheduledExecutorService;
20  
21  import com.linecorp.armeria.client.ClientBuilder;
22  import com.linecorp.armeria.client.ClientRequestContext;
23  import com.linecorp.armeria.client.encoding.DecodingClient;
24  import com.linecorp.armeria.client.endpoint.EndpointGroup;
25  import com.linecorp.armeria.common.HttpHeaderNames;
26  import com.linecorp.armeria.common.HttpRequest;
27  import com.linecorp.centraldogma.client.CentralDogma;
28  import com.linecorp.centraldogma.client.armeria.AbstractArmeriaCentralDogmaBuilder;
29  import com.linecorp.centraldogma.client.armeria.ArmeriaCentralDogmaBuilder;
30  import com.linecorp.centraldogma.internal.client.ReplicationLagTolerantCentralDogma;
31  import com.linecorp.centraldogma.internal.thrift.CentralDogmaService.AsyncIface;
32  
33  /**
34   * Builds a legacy {@link CentralDogma} client based on Thrift.
35   *
36   * @deprecated Use {@link ArmeriaCentralDogmaBuilder}.
37   */
38  @Deprecated
39  public class LegacyCentralDogmaBuilder extends AbstractArmeriaCentralDogmaBuilder<LegacyCentralDogmaBuilder> {
40      /**
41       * Returns a newly-created {@link CentralDogma} instance.
42       *
43       * @throws UnknownHostException if failed to resolve the host names from the DNS servers
44       */
45      public CentralDogma build() throws UnknownHostException {
46          final EndpointGroup endpointGroup = endpointGroup();
47          final String scheme = "tbinary+" + (isUseTls() ? "https" : "http");
48          final ClientBuilder builder =
49                  newClientBuilder(scheme, endpointGroup, cb -> {
50                      cb.decorator(DecodingClient.newDecorator())
51                        .rpcDecorator(LegacyCentralDogmaTimeoutScheduler::new);
52                  }, "/cd/thrift/v1");
53  
54          final String authorization = "Bearer " + accessToken();
55          builder.decorator((delegate, ctx, req) -> {
56              if (!req.headers().contains(HttpHeaderNames.AUTHORIZATION)) {
57                  // To prevent CSRF attack, we add 'Authorization' header to every request.
58                  final HttpRequest newReq = req.withHeaders(req.headers()
59                                                                .toBuilder()
60                                                                .set(HttpHeaderNames.AUTHORIZATION, authorization)
61                                                                .build());
62                  return delegate.execute(ctx, newReq);
63              }
64              return delegate.execute(ctx, req);
65          });
66  
67          final ScheduledExecutorService blockingTaskExecutor = blockingTaskExecutor();
68  
69          final int maxRetriesOnReplicationLag = maxNumRetriesOnReplicationLag();
70          final CentralDogma dogma = new LegacyCentralDogma(blockingTaskExecutor,
71                                                            builder.build(AsyncIface.class), endpointGroup);
72          if (maxRetriesOnReplicationLag <= 0) {
73              return dogma;
74          } else {
75              return new ReplicationLagTolerantCentralDogma(
76                      blockingTaskExecutor, dogma, maxRetriesOnReplicationLag,
77                      retryIntervalOnReplicationLagMillis(),
78                      () -> {
79                          // FIXME(trustin): Note that this will always return `null` due to a known limitation
80                          //                 in Armeria: https://github.com/line/armeria/issues/760
81                          final ClientRequestContext ctx = ClientRequestContext.currentOrNull();
82                          return ctx != null ? ctx.remoteAddress() : null;
83                      });
84          }
85      }
86  }