@Override public Map<String, Object> getTableTemplate(String apiKey, String table) { checkNotNull(table, "table"); try { URI uri = _dataStore.clone() .segment("_table", table) .build(); return _client.resource(uri) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .get(new TypeReference<Map<String,Object>>(){}); } catch (EmoClientException e) { throw convertException(e); } }
@Override public void deleteStashTime(String id, String dataCenter) { checkNotNull(id, "id"); checkNotNull(dataCenter, "dataCenter"); try { URI uri = _compactionControlSource.clone() .segment("_compcontrol", "stash-time", id) .queryParam("dataCenter", dataCenter) .build(); _client.resource(uri) .type(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, _apiKey) .delete(); } catch (UniformInterfaceException e) { throw convertException(e); } }
protected void doAcknowledge(String apiKey, String queue, Collection<String> messageIds) { checkNotNull(queue, "queue"); checkNotNull(messageIds, "messageIds"); try { URI uri = _queueService.clone() .segment(queue, "ack") .queryParam("partitioned", _partitionSafe) .build(); _client.resource(uri) .type(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .post(messageIds); } catch (EmoClientException e) { throw convertException(e); } }
@Override public void delete(String apiKey, String table, String blobId) { checkNotNull(table, "table"); checkNotNull(blobId, "blobId"); try { _client.resource(toUri(table, blobId)) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .delete(); } catch (EmoClientException e) { throw convertException(e); } }
@Override public Map<StashTimeKey, StashRunTimeInfo> getAllStashTimes() { try { URI uri = _compactionControlSource.clone() .segment("_compcontrol", "stash-time") .build(); return _client.resource(uri) .type(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, _apiKey) .get(Map.class); } catch (UniformInterfaceException e) { throw convertException(e); } }
@Override public void setTableAttributes(String apiKey, String table, Map<String, String> attributes, Audit audit) { checkNotNull(table, "table"); checkNotNull(attributes, "attributes"); checkNotNull(audit, "audit"); URI uri = _blobStore.clone() .segment("_table", table, "attributes") .queryParam("audit", RisonHelper.asORison(audit)) .build(); try { _client.resource(uri) .type(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .put(attributes); } catch (EmoClientException e) { throw convertException(e); } }
@Override public CreateEmoApiKeyResponse createApiKey(String apiKey, CreateEmoApiKeyRequest request) throws EmoApiKeyNotFoundException { checkNotNull(request, "request"); checkArgument(!Strings.isNullOrEmpty(request.getOwner()), "Non-empty owner is required"); try { URI uri = _uac.clone() .segment("api-key") .build(); EmoResource resource = _client.resource(uri); for (Map.Entry<String, String> customQueryParam : request.getCustomRequestParameters().entries()) { resource = resource.queryParam(customQueryParam.getKey(), customQueryParam.getValue()); } return resource .type(APPLICATION_X_CREATE_API_KEY_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .post(CreateEmoApiKeyResponse.class, JsonHelper.asJson(request)); } catch (EmoClientException e) { throw convertException(e); } }
protected void doUnclaimAll(String apiKey, String queue) { checkNotNull(queue, "queue"); try { URI uri = _queueService.clone() .segment(queue, "unclaimall") .queryParam("partitioned", _partitionSafe) .build(); _client.resource(uri) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .post(); } catch (EmoClientException e) { throw convertException(e); } }
@Override public boolean getTableExists(String apiKey, String table) { checkNotNull(table, "table"); URI uri = _blobStore.clone() .segment("_table", table) .build(); EmoResponse response = _client.resource(uri) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .head(); if (response.getStatus() == Response.Status.OK.getStatusCode()) { return true; } else if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode() && UnknownTableException.class.getName().equals(response.getFirstHeader("X-BV-Exception"))) { return false; } else { throw convertException(new EmoClientException(response)); } }
@Override public void updateApiKey(String apiKey, UpdateEmoApiKeyRequest request) throws EmoApiKeyNotFoundException { checkNotNull(request, "request"); String id = checkNotNull(request.getId(), "id"); checkArgument(!request.isOwnerPresent() || !Strings.isNullOrEmpty(request.getOwner()), "Non-empty owner is required"); try { URI uri = _uac.clone() .segment("api-key") .segment(id) .build(); _client.resource(uri) .type(APPLICATION_X_UPDATE_API_KEY_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .put(JsonHelper.asJson(request)); } catch (EmoClientException e) { throw convertException(e); } }
@Override public void dropTable(String apiKey, String table, Audit audit) throws UnknownTableException { checkNotNull(table, "table"); checkNotNull(audit, "audit"); URI uri = _dataStore.clone() .segment("_table", table) .build(); EmoResponse response = _client.resource(uri) .queryParam("audit", RisonHelper.asORison(audit)) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .delete(EmoResponse.class); if (response.getStatus() != Response.Status.OK.getStatusCode()) { throw convertException(new EmoClientException(response)); } }
@Override public PollResult poll(String apiKey, @PartitionKey String subscription, Duration claimTtl, int limit) { checkNotNull(subscription, "subscription"); checkNotNull(claimTtl, "claimTtl"); URI uri = getPollUriBuilder(subscription, claimTtl, limit).build(); EmoResponse response = _client.resource(uri) .queryParam("includeTags", "true") .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .get(EmoResponse.class); if (response.getStatus() != Response.Status.OK.getStatusCode()) { throw convertException(new EmoClientException(response)); } Iterator<Event> events = response.getEntity(new TypeReference<Iterator<Event>>() {}); boolean moreEvents; String databusEmpty = response.getFirstHeader(POLL_DATABUS_EMPTY_HEADER); if (databusEmpty != null) { // Use the header value from the server to determine if the databus subscription is empty moreEvents = !Boolean.parseBoolean(databusEmpty); } else { // Must be polling an older version of Emo which did not include this header. Infer whether the queue // is empty based on whether any results were returned. moreEvents = events.hasNext(); } return new PollResult(events, limit, moreEvents); }
@Override public void deleteApiKey(String apiKey, String id) throws EmoApiKeyNotFoundException { checkNotNull(id, "id"); try { URI uri = _uac.clone() .segment("api-key") .segment(id) .build(); _client.resource(uri) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .delete(); } catch (EmoClientException e) { throw convertException(e); } }
@Override public Subscription getSubscription(String apiKey, String subscription) throws UnknownSubscriptionException { checkNotNull(subscription, "subscription"); try { URI uri = _databus.clone() .segment(subscription) .build(); return _client.resource(uri) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .get(Subscription.class); } catch (EmoClientException e) { throw convertException(e); } }
@Override public BlobMetadata getMetadata(String apiKey, String table, String blobId) throws BlobNotFoundException { checkNotNull(table, "table"); checkNotNull(blobId, "blobId"); try { EmoResponse response = _client.resource(toUri(table, blobId)) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .head(); if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode() && BlobNotFoundException.class.getName().equals(response.getFirstHeader("X-BV-Exception"))) { throw new BlobNotFoundException(blobId, new EmoClientException(response)); } else if (response.getStatus() != Response.Status.OK.getStatusCode()) { throw new EmoClientException(response); } return parseMetadataHeaders(blobId, response); } catch (EmoClientException e) { throw convertException(e); } }
@Override public void setTableAttributes(String apiKey, String table, Map<String, String> attributes, Audit audit) { checkNotNull(table, "table"); checkNotNull(attributes, "attributes"); checkNotNull(audit, "audit"); URI uri = _blobStore.clone() .segment("_table", table, "attributes") .queryParam("audit", RisonHelper.asORison(audit)) .build(); try { _client.resource(uri) .type(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .put(attributes); } catch (EmoClientException e) { throw convertException(e); } }
@Override public String migrateApiKey(String apiKey, MigrateEmoApiKeyRequest request) { checkNotNull(request, "request"); String id = checkNotNull(request.getId(), "id"); try { URI uri = _uac.clone() .segment("api-key") .segment(id) .segment("migrate") .build(); EmoResource resource = _client.resource(uri); for (Map.Entry<String, String> customQueryParam : request.getCustomRequestParameters().entries()) { resource = resource.queryParam(customQueryParam.getKey(), customQueryParam.getValue()); } CreateEmoApiKeyResponse response = resource .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .post(CreateEmoApiKeyResponse.class, null); return response.getKey(); } catch (EmoClientException e) { throw convertException(e); } }
@Override public Map<StashTimeKey, StashRunTimeInfo> getStashTimesForPlacement(String placement) { checkNotNull(placement, "placement"); try { URI uri = _compactionControlSource.clone() .segment("_compcontrol", "stash-time") .queryParam("placement", placement) .build(); return _client.resource(uri) .type(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, _apiKey) .get(Map.class); } catch (UniformInterfaceException e) { throw convertException(e); } }