public static ApiException fromCode(HttpStatusCode httpStatusCode) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(); case UNAUTHORIZED: return unauthorized(); case FORBIDDEN: return forbidden(); case NOT_FOUND: return notFound(); case CONFLICT: return conflict(); case PRECONDITION_FAILED: return preconditionFailed(); case IM_A_TEAPOT: return imATeapot(); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(); case NOT_IMPLEMENTED: return notImplemented(); case SERVICE_UNAVAILABLE: return serviceUnavailable(); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode); } } public static ApiException fromCode(int code, String message, String...traits) {
public void checkStatus() throws Exception { Response response = client.target(apiPath + "/v2/status") .request(MediaType.APPLICATION_JSON_TYPE) .get(); HttpStatusCode statusCode = HttpStatusCode.findByCode(response.getStatus()); if (statusCode != HttpStatusCode.OK) { throw ApiException.fromCode(statusCode, "Get of tasks FAILED: " + response.getStatusInfo()); } String json = response.readEntity(String.class); JOptionPane.showMessageDialog(null, json, "Status: " + statusCode, JOptionPane.INFORMATION_MESSAGE); }
public static ApiException fromCode(int code, Throwable ex, String...traits) { return fromCode(toHttpStatusCode(code), ex, traits); } public static ApiException fromCode(HttpStatusCode httpStatusCode, Throwable ex, String...traits) {
public void validatePasswords(String password, String confirmed) { if (StringUtils.isBlank(password) || password.equals(confirmed) == false) { throw ApiException.badRequest("The two passwords do not match."); } }
@Override // TODO - why is this not getContext()? public ExecutionContext context() { ExecutionContext context = threadLocal.get(); if (context == null) { throw ApiException.internalServerError("There is no current execution context for this thread."); } else { return context; } }
if (createResponse.isError()) { String msg = String.format("Error creating request couch database [%s] - %s", domainProfile.getRequestDbName(), createResponse.getErrorReason()); throw ApiException.internalServerError(msg); if (designStream == null) { String msg = String.format("Unable to find design file at: %s", designPath); throw ApiException.internalServerError(msg); if (response.isError()) { String msg = String.format("Error creating views %s - %s", response.getHttpStatus(), response.getErrorReason()); throw ApiException.internalServerError(msg); throw ApiException.internalServerError(msg); throw ApiException.fromCode(statusCode, "Error adding user: " + response.readEntity(String.class));
private void throwIfNotFound(GetEntityResponse response, String message) { if (response.isEmpty() || response.isNotFound()) { throw ApiException.notFound(message); } }
public ExceptionInfo newExceptionInfo() { return new ExceptionInfo(ApiException.forbidden("I'm sorry, I cannot let you do that.", new RuntimeException("I tripped when I was running with scissors"))); } }
@POST @Path("/update") public Response updateAccount(@FormParam("firstName") String firstName, @FormParam("lastName") String lastName, @FormParam("emailAddress") String newEmailAddress) throws Exception { String oldEmailAddress = account.getEmailAddress(); if (EqualsUtils.objectsNotEqual(oldEmailAddress, newEmailAddress)) { // They are changing the emails address. if (execContext.getAccountStore().getByEmail(newEmailAddress) != null) { String msg = String.format("The email address %s is already in use.", newEmailAddress); throw ApiException.conflict(msg); } } UpdateAccountAction action = new UpdateAccountAction(firstName, lastName, newEmailAddress); account.apply(action); execContext.getAccountStore().update(account); if (EqualsUtils.objectsNotEqual(oldEmailAddress, newEmailAddress)) { // The email address has changed - we will need to update the session execContext.getSessionStore().newSession(newEmailAddress); } execContext.setLastMessage("You account details have been updated."); return Response.seeOther(new URI("manage/account")).build(); }
public void validatePassword(String password) { if (EqualsUtils.objectsNotEqual(this.password, password)) { throw ApiException.badRequest(INVALID_USER_NAME_OR_PASSWORD); } } public void validatePasswords(String password, String confirmed) {
private void sendEmail(URL templateUrl, String emailAddress, String subject, String message) { try { String template = IoUtils.toString(templateUrl); String content = template.replace("${message-content}", message); // AwsUtils.sendEmail(subject, content, "Munchie Monster <support@munchiemonster.com>", new MunMonEmailAddress(emailAddress)); } catch (Exception e) { String msg = String.format("Exception sending email to %s", emailAddress); throw ApiException.internalServerError(msg, e); } }
public static ApiException fromCode(int code) { return fromCode(toHttpStatusCode(code)); } public static ApiException fromCode(HttpStatusCode httpStatusCode) {
private void throwIfNotFound(GetAttachmentResponse response, String message) { if (response.isEmpty() || response.isNotFound()) { throw ApiException.notFound(message); } } }
public static ApiException fromCode(HttpStatusCode httpStatusCode, FineMessageSet messageSet) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(messageSet); case UNAUTHORIZED: return unauthorized(messageSet); case FORBIDDEN: return forbidden(messageSet); case NOT_FOUND: return notFound(messageSet); case CONFLICT: return conflict(messageSet); case PRECONDITION_FAILED: return preconditionFailed(messageSet); case IM_A_TEAPOT: return imATeapot(messageSet); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(messageSet); case NOT_IMPLEMENTED: return notImplemented(); case SERVICE_UNAVAILABLE: return serviceUnavailable(messageSet); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, messageSet); } } public static ApiException fromCode(int code, FineMessageSet messageSet, Throwable ex) {
public ApiException toBadRequestException() { String msg = StringUtils.toDelineatedString("\n", this); return ApiException.badRequest(msg); } }
public void startProcessor() { Response response = client.target(apiPath + "/v2/admin/system/task-processor/actions/execute") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity("", MediaType.WILDCARD_TYPE)); HttpStatusCode statusCode = HttpStatusCode.findByCode(response.getStatus()); if (statusCode != HttpStatusCode.NO_CONTENT) { throw ApiException.fromCode(statusCode, "Start of task processor failed: " + response.getStatusInfo()); } } }
private void throwError(CouchResponse response, String message) { String msg = format("%s: %s", message, response.getErrorReason()); throw ApiException.internalServerError(msg); }
public static ApiException fromCode(int code, String message, Throwable ex, String...traits) { return fromCode(toHttpStatusCode(code), message, ex, traits); } public static ApiException fromCode(HttpStatusCode httpStatusCode, String message, Throwable ex, String...traits) {
@GET @Path("/q/{pushRequestId}") public Response resolveCallback(@PathParam("pushRequestId") String pushRequestId) throws URISyntaxException { PushRequest request = execContext.getPushRequestStore().getByPushRequestId(pushRequestId); if (request == null) { throw ApiException.notFound("API request not found for " + pushRequestId); } Domain domain = execContext.getDomainStore().getByDocumentId(request.getDomainId()); if (domain == null) { throw ApiException.notFound("Domain not found for " + request.getDomainId()); } if (LqNotificationPush.PUSH_TYPE.equals(request.getPushType())) { String path = String.format("manage/domain/%s/notifications/%s", domain.getDomainKey(), pushRequestId); return Response.seeOther(new URI(path)).build(); } return Response.seeOther(getUriInfo().getBaseUriBuilder().build()).build(); }
public static ApiException fromCode(HttpStatusCode httpStatusCode, String message, Throwable ex, String...traits) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(message, ex, traits); case UNAUTHORIZED: return unauthorized(message, ex, traits); case FORBIDDEN: return forbidden(message, ex, traits); case NOT_FOUND: return notFound(message, ex, traits); case CONFLICT: return conflict(message, ex, traits); case PRECONDITION_FAILED: return preconditionFailed(message, ex, traits); case IM_A_TEAPOT: return imATeapot(message, ex, traits); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(message, ex, traits); case NOT_IMPLEMENTED: return notImplemented(message, ex, traits); case SERVICE_UNAVAILABLE: return serviceUnavailable(message, ex, traits); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, message, ex, traits); } } public static ApiException fromCode(int code, FineMessage richMessage) {