final IAsyncResultHandler<ISignalReadStream<T>> handler) { try { Get get = new Get.Builder(getIndexName(), cacheKey).type("cacheEntry").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get);
new Get.Builder(INDEX, ID) .type(TYPE) .build());
final IAsyncResultHandler<ISignalReadStream<T>> handler) { try { Get get = new Get.Builder(getIndexName(), cacheKey).type("cacheEntry").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get);
Get get = new Get.Builder(getIndexName(), id).type("rateBucket").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); RateLimiterBucket bucket;
Get get = new Get.Builder(getIndexName(), id).type("sharedStateProperty").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); if (result.isSucceeded()) {
Get get = new Get.Builder(getIndexName(), id).type("sharedStateProperty").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); if (result.isSucceeded()) {
Get get = new Get.Builder(getIndexName(), id).type("rateBucket").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); RateLimiterBucket bucket;
/** * The validate function will take as arguments indexName, documentType, id, json and validate the document against the index. */ @Override public final void validateDocumentIndex(String indexName, String documentType, String id, String json) { LOGGER.info("Validating Elasticsearch document, indexName={}, documentType={}, id={}.", indexName, documentType, id); Get get = new Get.Builder(indexName, id).type(documentType).build(); JestResult jestResult = jestClientHelper.execute(get); // Retrieve the JSON string from the get response final String jsonStringFromIndex = jestResult.getSourceAsString(); // If the document does not exist in the index add the document to the index if (StringUtils.isEmpty(jsonStringFromIndex)) { LOGGER.warn("Document does not exist in the index, adding the document to the index."); Index index = new Index.Builder(json).index(indexName).type(documentType).id(id).build(); jestResult = jestClientHelper.execute(index); LOGGER.info("adding the document to the index is {}", jestResult.isSucceeded()); } // Else if the JSON does not match the JSON from the index update the index else if (!json.equals(jsonStringFromIndex)) { LOGGER.warn("Document does not match the document in the index, updating the document in the index."); Index index = new Index.Builder(json).index(indexName).type(documentType).id(id).build(); jestResult = jestClientHelper.execute(index); LOGGER.info("updating the document to the index is {}", jestResult.isSucceeded()); } }
/** * The validate function will take as arguments indexName, documentType, id, json and validate the document against the index. */ @Override public final void validateDocumentIndex(String indexName, String documentType, String id, String json) { LOGGER.info("Validating Elasticsearch document, indexName={}, documentType={}, id={}.", indexName, documentType, id); Get get = new Get.Builder(indexName, id).type(documentType).build(); JestResult jestResult = jestClientHelper.execute(get); // Retrieve the JSON string from the get response final String jsonStringFromIndex = jestResult.getSourceAsString(); // If the document does not exist in the index add the document to the index if (StringUtils.isEmpty(jsonStringFromIndex)) { LOGGER.warn("Document does not exist in the index, adding the document to the index."); Index index = new Index.Builder(json).index(indexName).type(documentType).id(id).build(); jestResult = jestClientHelper.execute(index); LOGGER.info("adding the document to the index is {}", jestResult.isSucceeded()); } // Else if the JSON does not match the JSON from the index update the index else if (!json.equals(jsonStringFromIndex)) { LOGGER.warn("Document does not match the document in the index, updating the document in the index."); Index index = new Index.Builder(json).index(indexName).type(documentType).id(id).build(); jestResult = jestClientHelper.execute(index); LOGGER.info("updating the document to the index is {}", jestResult.isSucceeded()); } }
/** * @see io.apiman.gateway.engine.components.ICacheStoreComponent#get(java.lang.String, java.lang.Class, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public <T> void get(String cacheKey, final Class<T> type, final IAsyncResultHandler<T> handler) { Get get = new Get.Builder(getIndexName(), cacheKey).type("cacheEntry").build(); //$NON-NLS-1$ try { JestResult result = getClient().execute(get); if (result.isSucceeded()) { ESCacheEntry cacheEntry = result.getSourceAsObject(ESCacheEntry.class); try { T rval = (T) mapper.reader(type).readValue(cacheEntry.getHead()); handler.handle(AsyncResultImpl.create(rval)); } catch (IOException e) { // TODO log this error. handler.handle(AsyncResultImpl.create((T) null)); } } else { handler.handle(AsyncResultImpl.create((T) null)); } } catch (Throwable e) { handler.handle(AsyncResultImpl.create(e, type)); } }
/** * Checks the ES store to see if the 'dataVersion' entry has been updated with a newer * version #. If it has, then we need to invalidate our cache. */ protected void checkCacheVersion() { // Be very aggressive in invalidating the cache. boolean invalidate = true; try { Get get = new Get.Builder(getDefaultIndexName(), "instance").type("dataVersion").build(); //$NON-NLS-1$ //$NON-NLS-2$ JestResult result = getClient().execute(get); if (result.isSucceeded()) { String latestDV = result.getJsonObject().get("_version").getAsString(); //$NON-NLS-1$ if (latestDV != null && dataVersion != null && latestDV.equals(dataVersion)) { invalidate = false; } else { dataVersion = latestDV; } } } catch (IOException e) { // TODO need to use the gateway logger to log this! e.printStackTrace(); } if (invalidate) { invalidateCache(); } }
/** * Ensures that the api referenced by the Contract at the head of * the iterator actually exists (is published). * @param contract * @param apiMap */ private void validateContract(final Contract contract) throws RegistrationException { final String id = getApiId(contract); try { Get get = new Get.Builder(getIndexName(), id).type("api").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); if (!result.isSucceeded()) { String apiId = contract.getApiId(); String orgId = contract.getApiOrgId(); throw new ApiNotFoundException(Messages.i18n.format("ESRegistry.ApiNotFoundInOrg", apiId, orgId)); //$NON-NLS-1$ } } catch (IOException e) { throw new RegistrationException(Messages.i18n.format("ESRegistry.ErrorValidatingClient"), e); //$NON-NLS-1$ } }
/** * Checks the ES store to see if the 'dataVersion' entry has been updated with a newer * version #. If it has, then we need to invalidate our cache. */ protected void checkCacheVersion() { // Be very aggressive in invalidating the cache. boolean invalidate = true; try { Get get = new Get.Builder(getDefaultIndexName(), "instance").type("dataVersion").build(); //$NON-NLS-1$ //$NON-NLS-2$ JestResult result = getClient().execute(get); if (result.isSucceeded()) { String latestDV = result.getJsonObject().get("_version").getAsString(); //$NON-NLS-1$ if (latestDV != null && dataVersion != null && latestDV.equals(dataVersion)) { invalidate = false; } else { dataVersion = latestDV; } } } catch (IOException e) { logger.warn("Elasticsearch is not available, using cache"); invalidate = false; } if (invalidate) { invalidateCache(); } }
/** * Ensures that the api referenced by the Contract at the head of * the iterator actually exists (is published). * @param contract * @param apiMap */ private void validateContract(final Contract contract) throws RegistrationException { final String id = getApiId(contract); try { Get get = new Get.Builder(getIndexName(), id).type("api").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); if (!result.isSucceeded()) { String apiId = contract.getApiId(); String orgId = contract.getApiOrgId(); throw new ApiNotFoundException(Messages.i18n.format("ESRegistry.ApiNotFoundInOrg", apiId, orgId)); //$NON-NLS-1$ } } catch (IOException e) { throw new RegistrationException(Messages.i18n.format("ESRegistry.ErrorValidatingClient"), e); //$NON-NLS-1$ } }
/** * @see io.fabric8.gateway.apiman.ServiceMappingStorage#get(java.lang.String) */ @Override public ServiceMapping get(String path) { String id = idFromPath(path); Get get = new Get.Builder(ESConstants.INDEX_NAME, id).type("f8_service_mapping").build(); //$NON-NLS-1$ try { JestResult result = getClient().execute(get); if (result.isSucceeded()) { return result.getSourceAsObject(ServiceMapping.class); } return null; } catch (Exception e) { // TODO log this error return null; } }
/** * @see io.apiman.gateway.engine.components.ICacheStoreComponent#get(java.lang.String, java.lang.Class, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public <T> void get(String cacheKey, final Class<T> type, final IAsyncResultHandler<T> handler) { Get get = new Get.Builder(getIndexName(), cacheKey).type("cacheEntry").build(); //$NON-NLS-1$ try { JestResult result = getClient().execute(get); if (result.isSucceeded()) { ESCacheEntry cacheEntry = result.getSourceAsObject(ESCacheEntry.class); try { T rval = (T) mapper.reader(type).readValue(cacheEntry.getHead()); handler.handle(AsyncResultImpl.create(rval)); } catch (IOException e) { // TODO log this error. handler.handle(AsyncResultImpl.create((T) null)); } } else { handler.handle(AsyncResultImpl.create((T) null)); } } catch (Throwable e) { handler.handle(AsyncResultImpl.create(e, type)); } }
/** * Gets an entity. Callers must unmarshal the resulting map. * @param type * @param id * @throws StorageException */ private Map<String, Object> getEntity(String type, String id) throws StorageException { try { JestResult response = esClient.execute(new Get.Builder(getIndexName(), id).type(type).build()); if (!response.isSucceeded()) { return null; } return response.getSourceAsObject(Map.class); } catch (Exception e) { throw new StorageException(e); } }
/** * Gets an entity. Callers must unmarshal the resulting map. * @param type * @param id * @throws StorageException */ private Map<String, Object> getEntity(String type, String id) throws StorageException { try { JestResult response = esClient.execute(new Get.Builder(getIndexName(), id).type(type).build()); if (!response.isSucceeded()) { return null; } return response.getSourceAsObject(Map.class); } catch (Exception e) { throw new StorageException(e); } }
/** * Gets the client synchronously. * @param id * @throws IOException */ protected Client getClient(String id) throws IOException { Get get = new Get.Builder(getIndexName(), id).type("client").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); if (result.isSucceeded()) { Client client = result.getSourceAsObject(Client.class); return client; } else { return null; } }
/** * Gets the api synchronously. * @param id * @throws IOException */ protected Api getApi(String id) throws IOException { Get get = new Get.Builder(getIndexName(), id).type("api").build(); //$NON-NLS-1$ JestResult result = getClient().execute(get); if (result.isSucceeded()) { Api api = result.getSourceAsObject(Api.class); return api; } else { return null; } }