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