2 * Copyright 2016 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
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
23 * http://www.apache.org/licenses/LICENSE-2.0
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.
32 package org.openo.msb.wrapper.consul.option;
34 import static com.google.common.base.Preconditions.checkArgument;
35 import static org.openo.msb.wrapper.consul.option.Options.optionallyAdd;
37 import java.math.BigInteger;
39 import javax.ws.rs.client.WebTarget;
41 import com.google.common.base.Optional;
44 * Container for common query options used by the Consul API.
47 public abstract class QueryOptions implements ParamAdder {
49 public static final QueryOptions BLANK = ImmutableQueryOptions.builder().build();
51 public abstract Optional<String> getWait();
52 public abstract Optional<String> getToken();
53 public abstract Optional<BigInteger> getIndex();
54 public abstract Optional<String> getNear();
57 public ConsistencyMode getConsistencyMode() {
58 return ConsistencyMode.DEFAULT;
62 public boolean isBlocking() {
63 return getWait().isPresent();
67 public boolean hasToken() {
68 return getToken().isPresent();
74 checkArgument(getIndex().isPresent(), "If wait is specified, index must also be specified");
78 public static ImmutableQueryOptions.Builder blockSeconds(int seconds, BigInteger index) {
79 return blockBuilder("s", seconds, index);
82 public static ImmutableQueryOptions.Builder blockMinutes(int minutes, BigInteger index) {
83 return blockBuilder("m", minutes, index);
86 private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, BigInteger index) {
87 return ImmutableQueryOptions.builder()
88 .wait(String.format("%s%s", qty, identifier))
93 public WebTarget apply(WebTarget input) {
95 WebTarget added = input;
96 switch (getConsistencyMode()) {
98 added = added.queryParam("consistent", "");
101 added = added.queryParam("stale", "");
106 added = added.queryParam("wait", getWait().get())
107 .queryParam("index", String.valueOf(getIndex().get()));
110 added = optionallyAdd(added, "token", getToken());
111 added = optionallyAdd(added, "near", getToken());