Initial code import
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / option / QueryOptions.java
1 /**
2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.openo.msb.wrapper.consul.option;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static org.openo.msb.wrapper.consul.option.Options.optionallyAdd;
21
22 import java.math.BigInteger;
23
24 import javax.ws.rs.client.WebTarget;
25
26 import com.google.common.base.Optional;
27
28 /**
29  * Container for common query options used by the Consul API.
30  */
31
32 public abstract class QueryOptions implements ParamAdder {
33
34     public static final QueryOptions BLANK = ImmutableQueryOptions.builder().build();
35
36     public abstract Optional<String> getWait();
37     public abstract Optional<String> getToken();
38     public abstract Optional<BigInteger> getIndex();
39     public abstract Optional<String> getNear();
40
41   
42     public ConsistencyMode getConsistencyMode() {
43         return ConsistencyMode.DEFAULT;
44     }
45
46
47     public boolean isBlocking() {
48         return getWait().isPresent();
49     }
50
51
52     public boolean hasToken() {
53         return getToken().isPresent();
54     }
55
56
57     void validate() {
58         if (isBlocking()) {
59             checkArgument(getIndex().isPresent(), "If wait is specified, index must also be specified");
60         }
61     }
62
63     public static ImmutableQueryOptions.Builder blockSeconds(int seconds, BigInteger index) {
64         return blockBuilder("s", seconds, index);
65     }
66
67     public static ImmutableQueryOptions.Builder blockMinutes(int minutes, BigInteger index) {
68         return blockBuilder("m", minutes, index);
69     }
70
71     private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, BigInteger index) {
72         return ImmutableQueryOptions.builder()
73                 .wait(String.format("%s%s", qty, identifier))
74                 .index(index);
75     }
76
77     @Override
78     public WebTarget apply(WebTarget input) {
79
80         WebTarget added = input;
81         switch (getConsistencyMode()) {
82             case CONSISTENT:
83                 added = added.queryParam("consistent", "");
84                 break;
85             case STALE:
86                 added = added.queryParam("stale", "");
87                 break;
88         }
89
90         if (isBlocking()) {
91             added = added.queryParam("wait", getWait().get())
92                     .queryParam("index", String.valueOf(getIndex().get()));
93         }
94
95         added = optionallyAdd(added, "token", getToken());
96         added = optionallyAdd(added, "near", getToken());
97
98         return added;
99     }
100 }