@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; } }
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); } }
private void throwError(CouchResponse response, String message) { String msg = format("%s: %s", message, response.getErrorReason()); throw ApiException.internalServerError(msg); }
protected final void throwError(CouchResponse response, String message) { if (response.isNotFound()) { throw ApiNotFoundException.notFound(message); } else if (response.isConflict()) { throw ApiConflictException.conflict(message); } else { message = format("%s: %s", message, response.getErrorReason()).trim(); throw ApiException.internalServerError(message); } }
private void createNotifyDatabase(DomainProfileEntity domainProfile) { CouchDatabase couchDatabase = notificationDb(domainProfile); WriteResponse createResponse = couchDatabase.createDatabase(); if (createResponse.isError()) { String msg = String.format("Error creating notify couch database [%s] - %s", domainProfile.getNotificationDbName(), createResponse.getErrorReason()); throw ApiException.internalServerError(msg); } // Create the designs String[] designNames = new String[]{"Entity", "Notification", "Task", "Summary"}; for (String designName : designNames) { String designPath = String.format("/couch/%s-design.json", designName); InputStream designStream = getClass().getResourceAsStream(designPath); if (designStream == null) { String msg = String.format("Unable to find couch design file at: %s", designPath); throw ApiException.internalServerError(msg); } try { String designContent = IoUtils.toString(designStream); WriteResponse response = couchDatabase.put().design(designName, designContent).execute(); if (response.isError()) { String msg = String.format("Error creating views %s - %s", response.getHttpStatus(), response.getErrorReason()); throw ApiException.internalServerError(msg); } } catch (IOException ex) { String msg = "Error reading design file: " + designPath; throw ApiException.internalServerError(ex, msg); } } }
public static DrivingDirections getDrivingDirections(LatLng locationA, LatLng locationB) throws IOException { // https://developers.google.com/maps/documentation/directions/ ExceptionUtils.assertNotNull(locationA, "locationA"); ExceptionUtils.assertNotNull(locationB, "locationB"); String url = "http://maps.googleapis.com/maps/api/directions/json"; url += String.format("?origin=%s,%s", locationA.getLatitude(), locationA.getLongitude()); url += String.format("&destination=%s,%s", locationB.getLatitude(), locationB.getLongitude()); url += "&sensor=false"; URLConnection connection = new URL(url).openConnection(); String json = IoUtils.toString(connection.getInputStream()); DrivingDirections directions = objectMapper.readValue(json, DrivingDirections.class); if (directions.getStatus().isNotOK()) { String msg = String.format("Unable to compute driving directions: %s (%s)", directions.getStatus(), directions.getStatus().getDescription()); throw ApiException.internalServerError(msg); } return directions; }
private void processDomain(NotificationDomain notificationDomain) { String domainName = notificationDomain.getDomainName(); log.trace("Processing all tasks for domain {}.", domainName); try { // Find all pending tasks for this domain. QueryResult<TaskEntity> pendingTasks = notificationDomain.query(new TaskQuery().setTaskStatus(TaskStatus.PENDING)); if (pendingTasks.isNotEmpty()) { // Map our tasks by provider. Map<String, List<TaskEntity>> tasksMappedByProvider = mapTasksByProvider(pendingTasks); processTasksByProvider(notificationDomain, tasksMappedByProvider); } } catch (Exception e) { throw ApiException.internalServerError(format("Exception processing the domain %s", domainName)); } }
@Override protected UserDetails retrieveUser(String userName, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { try { if (ProviderUtils.isGoogleAuthentication(authentication) == false) { throw new UsernameNotFoundException("Not Goolge Authentication"); } String code = authentication.getCredentials().toString(); GoogleAuthentication googleAuth = getAuthResponse(code, clientId, clientSecret); UserInfo userInfo = getUserInfo(googleAuth); CurrentUserSource source = store.getCurrentUserSourceByEmail(userInfo.getEmail()); if (source == null) { throw new BadCredentialsException(CurrentUser.INVALID_USER_NAME_OR_PASSWORD); } return new CurrentUser(source); } catch (IOException e) { throw ApiException.internalServerError("Exception during Google-Authentication", e); } }
public static GoogleAuthentication getAuthResponse(String code, String clientId, String clientSecret) throws IOException { Client client = ClientBuilder.newBuilder().build(); Form form = new Form(); form.param("code", code); form.param("client_id", clientId); form.param("client_secret", clientSecret); form.param("grant_type", "authorization_code"); form.param("redirect_uri", "postmessage"); UriBuilder uriBuilder = UriBuilder.fromUri("https://accounts.google.com/o/oauth2/token"); Response jerseyResponse = client.target(uriBuilder) .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE)); int status = jerseyResponse.getStatus(); String json = jerseyResponse.readEntity(String.class); TiogaJacksonObjectMapper objectMapper = new TiogaJacksonObjectMapper(); GoogleAuthentication googleAuth = objectMapper.readValue(json, GoogleAuthentication.class); // If there was an error in the token info, abort. if (StringUtils.isNotBlank(googleAuth.getError())) { String msg = String.format("Authentication Error: %s", googleAuth.getError()); throw ApiException.internalServerError(msg); } return googleAuth; }
throw ApiException.internalServerError(msg);
throw ApiException.internalServerError(msg); throw ApiException.internalServerError(msg); throw ApiException.internalServerError("Unexpected exception reading couch document.", ex);
if (createResponse.isError()) { String msg = String.format("Error creating master database (%s) - %s", localMasterDatabase.getDatabaseName(), createResponse.getErrorReason()); throw ApiException.internalServerError(msg); if (designStream == null) { String msg = String.format("Unable to find couch design file at: %s", designPath); throw ApiException.internalServerError(msg); throw ApiException.internalServerError(msg); throw ApiException.internalServerError(ex, msg);
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) {
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 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 static ApiException fromCode(HttpStatusCode httpStatusCode, Throwable ex, String...traits) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(ex, traits); case UNAUTHORIZED: return unauthorized(ex, traits); case FORBIDDEN: return forbidden(ex, traits); case NOT_FOUND: return notFound(ex, traits); case CONFLICT: return conflict(ex, traits); case PRECONDITION_FAILED: return preconditionFailed(ex, traits); case IM_A_TEAPOT: return imATeapot(ex, traits); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(ex, traits); case NOT_IMPLEMENTED: return notImplemented(ex, traits); case SERVICE_UNAVAILABLE: return serviceUnavailable(ex, traits); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, ex, traits); } } public static ApiException fromCode(int code, String message, Throwable ex, String...traits) {
public static ApiException fromCode(HttpStatusCode httpStatusCode, FineMessage richMessage) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(richMessage); case UNAUTHORIZED: return unauthorized(richMessage); case FORBIDDEN: return forbidden(richMessage); case NOT_FOUND: return notFound(richMessage); case CONFLICT: return conflict(richMessage); case PRECONDITION_FAILED: return preconditionFailed(richMessage); case IM_A_TEAPOT: return imATeapot(richMessage); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(richMessage); case NOT_IMPLEMENTED: return notImplemented(richMessage); case SERVICE_UNAVAILABLE: return serviceUnavailable(richMessage); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, richMessage); } } public static ApiException fromCode(int code, FineMessage richMessage, Throwable ex) {
public static ApiException fromCode(HttpStatusCode httpStatusCode, FineMessage richMessage, Throwable ex) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(richMessage, ex); case UNAUTHORIZED: return unauthorized(richMessage, ex); case FORBIDDEN: return forbidden(richMessage, ex); case NOT_FOUND: return notFound(richMessage, ex); case CONFLICT: return conflict(richMessage, ex); case PRECONDITION_FAILED: return preconditionFailed(richMessage, ex); case IM_A_TEAPOT: return imATeapot(richMessage, ex); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(richMessage, ex); case NOT_IMPLEMENTED: return notImplemented(richMessage, ex); case SERVICE_UNAVAILABLE: return serviceUnavailable(richMessage, ex); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, richMessage, ex); } } public static ApiException fromCode(int code, FineMessageSet messageSet) {
public static ApiException fromCode(HttpStatusCode httpStatusCode, String message, String...traits) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(message, traits); case UNAUTHORIZED: return unauthorized(message, traits); case FORBIDDEN: return forbidden(message, traits); case NOT_FOUND: return notFound(message, traits); case CONFLICT: return conflict(message, traits); case PRECONDITION_FAILED: return preconditionFailed(message, traits); case IM_A_TEAPOT: return imATeapot(message, traits); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(message, traits); case NOT_IMPLEMENTED: return notImplemented(message, traits); case SERVICE_UNAVAILABLE: return serviceUnavailable(message, traits); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, message, traits); } } public static ApiException fromCode(int code, Throwable ex, String...traits) {
public static ApiException fromCode(HttpStatusCode httpStatusCode, FineMessageSet messageSet, Throwable ex) { switch (httpStatusCode) { // The 400 series case BAD_REQUEST: return badRequest(messageSet, ex); case UNAUTHORIZED: return unauthorized(messageSet, ex); case FORBIDDEN: return forbidden(messageSet, ex); case NOT_FOUND: return notFound(messageSet, ex); case CONFLICT: return conflict(messageSet, ex); case PRECONDITION_FAILED: return preconditionFailed(messageSet, ex); case IM_A_TEAPOT: return imATeapot(messageSet, ex); // The 500 series case INTERNAL_SERVER_ERROR: return internalServerError(messageSet, ex); case NOT_IMPLEMENTED: return notImplemented(messageSet, ex); case SERVICE_UNAVAILABLE: return serviceUnavailable(messageSet, ex); // The 100, 200 & 300 series default: return new ApiException(httpStatusCode, messageSet, ex); } } private static HttpStatusCode toHttpStatusCode(int code) {