private static Get prepareGet(Map<String, Object> indexInformation) { final String index = (String) indexInformation.get(DataReader.INDEX_NAME_ELEMENT); final String id = (String) indexInformation.get(DataReader.INDEX_ID_ELEMENT); final Get.Builder getBuilder = new Get.Builder(index, id); if (indexInformation.containsKey(DataReader.INDEX_TYPE_ELEMENT)) { getBuilder.type((String) indexInformation.get(DataReader.INDEX_TYPE_ELEMENT)); } return getBuilder.build(); }
@Override public boolean isValidDocumentIndex(String indexName, String documentType, String id, String json) { 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(); // Return true if the json from the index is not null or empty and the json from the index matches the object from the database return StringUtils.isNotEmpty(jsonStringFromIndex) && jsonStringFromIndex.equals(json); }
@Override public boolean isValidDocumentIndex(String indexName, String documentType, String id, String json) { 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(); // Return true if the json from the index is not null or empty and the json from the index matches the object from the database return StringUtils.isNotEmpty(jsonStringFromIndex) && jsonStringFromIndex.equals(json); }
/** * @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(); } }
/** * @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; } }
/** * 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.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 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; } }
/** * 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; } }
public ResultMessage get(String messageId, String index) throws DocumentNotFoundException, IOException { final Get get = new Get.Builder(index, messageId).type(IndexMapping.TYPE_MESSAGE).build(); final DocumentResult result = client.execute(get); if (!result.isSucceeded()) { throw new DocumentNotFoundException(index, messageId); } @SuppressWarnings("unchecked") final Map<String, Object> message = (Map<String, Object>) result.getSourceAsObject(Map.class, false); return ResultMessage.parseFromSource(result.getId(), result.getIndex(), message); }
@Override public Movie get(String id) { Get get = new Get.Builder(INDEX, id).type(TYPE).build(); try { JestResult result = client.execute(get); Movie movie = result.getSourceAsObject(Movie.class); return movie; } catch (IOException e) { log.error("get异常", e); return null; } }
public <T> T queryForObject(String indexName, GetQuery query, Class<T> clazz, JestGetResultMapper mapper) { ElasticsearchPersistentEntity<Object> persistentEntity = getPersistentEntityFor(clazz); String index = indexName == null ? persistentEntity.getIndexName() : indexName; Get.Builder build = new Get.Builder(index, query.getId()).type(persistentEntity.getIndexType()); DocumentResult result = execute(build.build(), true); return mapper.mapResult(result, clazz); }
public ResultMessage get(String messageId, String index) throws DocumentNotFoundException, IOException { final Get get = new Get.Builder(index, messageId).type(IndexMapping.TYPE_MESSAGE).build(); final DocumentResult result = client.execute(get); if (!result.isSucceeded()) { throw new DocumentNotFoundException(index, messageId); } @SuppressWarnings("unchecked") final Map<String, Object> message = (Map<String, Object>) result.getSourceAsObject(Map.class, false); return ResultMessage.parseFromSource(result.getId(), result.getIndex(), message); }