/** * Retrieves a list of matching keys for the given key. * * GET /v1/kv/{key}?keys * * @param key The key to retrieve. * @return A list of zero to many keys. */ public List<String> getKeys(String key) { return getKeys(key, QueryOptions.BLANK); }
public static List<String> listKeys(KeyValueClient kv, String keyPrefix) { try { return kv.getKeys(keyPrefix); } catch (ConsulException e) { if (e.getCode() == HTTP_NOT_FOUND) { return new ArrayList<>(0); } throw e; } }
@Override public <T> Set<T> findByType(Class<T> type) { String keyPrefix = type.getName().replaceAll("\\$", "/"); Set<T> result = new HashSet<>(); List<String> keys; try { keys = kvClient.getKeys(keyPrefix); } catch (ConsulException e) { return result; } if (keys != null) { Object obj; for (String key : keys) { // change bookmark back into actual key key = key.substring(key.lastIndexOf('/') + 1); obj = lookupByName(key.replaceAll("\\$", "/")); if (type.isInstance(obj)) { result.add(type.cast(obj)); } } } return result; }
@Override public <T> Map<String, T> findByTypeWithName(Class<T> type) { Map<String, T> result = new HashMap<>(); // encode $ signs as they occur in subclass types String keyPrefix = type.getName().replaceAll("\\$", "/"); kvClient = consul.keyValueClient(); List<String> keys; try { keys = kvClient.getKeys(keyPrefix); } catch (ConsulException e) { return result; } if (keys != null) { Object obj; for (String key : keys) { // change bookmark back into actual key key = key.substring(key.lastIndexOf('/') + 1); obj = lookupByName(key.replaceAll("\\$", "/")); if (type.isInstance(obj)) { result.put(key, type.cast(obj)); } } } return result; }
@InvokeOnHeader(ConsulKeyValueActions.GET_KEYS) protected void getKeys(Message message) throws Exception { setBodyAndResult( message, getClient().getKeys( getMandatoryHeader(message, ConsulConstants.CONSUL_KEY, getConfiguration().getKey(), String.class) ) ); }