private EmoApiKey getApiKeyFromResponse(EmoResponse response) { if (response.getStatus() == Response.Status.OK.getStatusCode()) { return response.getEntity(EmoApiKey.class); } else if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) { return null; } throw convertException(new EmoClientException(response)); }
protected RuntimeException convertException(EmoClientException e) { EmoResponse response = e.getResponse(); String exceptionType = response.getFirstHeader("X-BV-Exception"); if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode() && IllegalArgumentException.class.getName().equals(exceptionType)) { return new IllegalArgumentException(response.getEntity(String.class), e); } else if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode() && UnknownMoveException.class.getName().equals(exceptionType)) { return response.getEntity(UnknownMoveException.class); } else if (response.getStatus() == Response.Status.FORBIDDEN.getStatusCode() && UnauthorizedException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnauthorizedException.class).initCause(e); } else { return (RuntimeException) new UnauthorizedException().initCause(e); } } else if (response.getStatus() == Response.Status.SERVICE_UNAVAILABLE.getStatusCode() && ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e); } } return e; }
return new IllegalArgumentException(response.getEntity(String.class), e); return (RuntimeException) response.getEntity(UnknownSubscriptionException.class).initCause(e); } else { return (RuntimeException) new UnknownSubscriptionException().initCause(e); return response.getEntity(UnknownMoveException.class); } else if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode() && UnknownReplayException.class.getName().equals(exceptionType)) { return response.getEntity(UnknownReplayException.class); } else if (response.getStatus() == Response.Status.FORBIDDEN.getStatusCode() && UnauthorizedSubscriptionException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnauthorizedSubscriptionException.class).initCause(e); } else { return (RuntimeException) new UnauthorizedSubscriptionException().initCause(e); UnauthorizedException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnauthorizedException.class).initCause(e); } else { return (RuntimeException) new UnauthorizedException().initCause(e); ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
return new IllegalArgumentException(response.getEntity(String.class), e); } else if (JsonStreamProcessingException.class.getName().equals(exceptionType)) { return new JsonStreamProcessingException(response.getEntity(String.class)); } else if (DeltaSizeLimitException.class.getName().equals(exceptionType)) { return response.getEntity(DeltaSizeLimitException.class); } else if (AuditSizeLimitException.class.getName().equals(exceptionType)) { return response.getEntity(AuditSizeLimitException.class); TableExistsException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(TableExistsException.class).initCause(e); } else { return (RuntimeException) new TableExistsException().initCause(e); UnknownTableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownTableException.class).initCause(e); } else { return (RuntimeException) new UnknownTableException().initCause(e); UnknownPlacementException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownPlacementException.class).initCause(e); } else { return (RuntimeException) new UnknownPlacementException().initCause(e); StashNotAvailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(StashNotAvailableException.class).initCause(e);
return new IllegalArgumentException(response.getEntity(String.class), e); } else if (JsonStreamProcessingException.class.getName().equals(exceptionType)) { return new JsonStreamProcessingException(response.getEntity(String.class)); } else if (DeltaSizeLimitException.class.getName().equals(exceptionType)) { return response.getEntity(DeltaSizeLimitException.class); } else if (AuditSizeLimitException.class.getName().equals(exceptionType)) { return response.getEntity(AuditSizeLimitException.class); TableExistsException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(TableExistsException.class).initCause(e); } else { return (RuntimeException) new TableExistsException().initCause(e); UnknownTableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownTableException.class).initCause(e); } else { return (RuntimeException) new UnknownTableException().initCause(e); UnknownPlacementException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownPlacementException.class).initCause(e); } else { return (RuntimeException) new UnknownPlacementException().initCause(e); StashNotAvailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(StashNotAvailableException.class).initCause(e);
return new IllegalArgumentException(response.getEntity(String.class), e); return (RuntimeException) response.getEntity(TableExistsException.class).initCause(e); } else { return (RuntimeException) new TableExistsException().initCause(e); UnknownTableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownTableException.class).initCause(e); } else { return (RuntimeException) new UnknownTableException().initCause(e); BlobNotFoundException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(BlobNotFoundException.class).initCause(e); } else { return (RuntimeException) new BlobNotFoundException().initCause(e); UnknownPlacementException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownPlacementException.class).initCause(e); } else { return (RuntimeException) new UnknownPlacementException().initCause(e); RangeNotSatisfiableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(RangeNotSatisfiableException.class).initCause(e); } else { return (RuntimeException) new RangeNotSatisfiableException(null, -1, -1).initCause(e);
return new IllegalArgumentException(response.getEntity(String.class), e); return (RuntimeException) response.getEntity(TableExistsException.class).initCause(e); } else { return (RuntimeException) new TableExistsException().initCause(e); UnknownTableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownTableException.class).initCause(e); } else { return (RuntimeException) new UnknownTableException().initCause(e); BlobNotFoundException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(BlobNotFoundException.class).initCause(e); } else { return (RuntimeException) new BlobNotFoundException().initCause(e); UnknownPlacementException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnknownPlacementException.class).initCause(e); } else { return (RuntimeException) new UnknownPlacementException().initCause(e); RangeNotSatisfiableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(RangeNotSatisfiableException.class).initCause(e); } else { return (RuntimeException) new RangeNotSatisfiableException(null, -1, -1).initCause(e);
if (InvalidEmoPermissionException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(InvalidEmoPermissionException.class).initCause(e); } else { return (RuntimeException) new InvalidEmoPermissionException().initCause(e); return new IllegalArgumentException(response.getEntity(String.class), e); } else if (JsonStreamProcessingException.class.getName().equals(exceptionType)) { return new JsonStreamProcessingException(response.getEntity(String.class)); if (EmoApiKeyExistsException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(EmoApiKeyExistsException.class).initCause(e); } else { return (RuntimeException) new EmoApiKeyExistsException().initCause(e); return (RuntimeException) response.getEntity(EmoRoleExistsException.class).initCause(e); } else { return (RuntimeException) new EmoRoleExistsException().initCause(e); if (EmoApiKeyNotFoundException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(EmoApiKeyNotFoundException.class).initCause(e); } else { return (RuntimeException) new EmoApiKeyNotFoundException().initCause(e); return (RuntimeException) response.getEntity(EmoRoleNotFoundException.class).initCause(e); } else { return (RuntimeException) new EmoRoleNotFoundException().initCause(e);
@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 EmoRole getRole(String apiKey, EmoRoleKey roleKey) { checkNotNull(roleKey, "roleKey"); URI uri = _uac.clone() .segment("role") .segment(roleKey.getGroup()) .segment(roleKey.getId()) .build(); EmoResponse response = _client.resource(uri) .accept(MediaType.APPLICATION_JSON_TYPE) .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey) .get(EmoResponse.class); if (response.getStatus() == Response.Status.OK.getStatusCode()) { return response.getEntity(EmoRole.class); } else if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) { return null; } throw convertException(new EmoClientException(response)); }