/** * Makes the responseBuilder CORS compatible, assumes default methods. * * @param responseBuilder the modified response builder * @return response builder */ public static Response.ResponseBuilder makeCORS(Response.ResponseBuilder responseBuilder) { return makeCORS(responseBuilder, ""); }
/** * Creates a response using the given object. * * @param object the object to respond with * @return the response */ private static Response createResponse(Object object, AlluxioConfiguration alluxioConf, @Nullable Map<String, Object> headers) { if (object instanceof Void) { return Response.ok().build(); } if (object instanceof String) { // Need to explicitly encode the string as JSON because Jackson will not do it automatically. ObjectMapper mapper = new ObjectMapper(); try { return Response.ok(mapper.writeValueAsString(object)).build(); } catch (JsonProcessingException e) { return createErrorResponse(e, alluxioConf); } } Response.ResponseBuilder rb = Response.ok(object); if (headers != null) { headers.forEach(rb::header); } if (alluxioConf.getBoolean(PropertyKey.WEBUI_CORS_ENABLED)) { return makeCORS(rb).build(); } return rb.build(); }
/** * Creates an error response using the given exception. * * @param e the exception to be converted into {@link ErrorResponse} and encoded into json * @return the response */ private static Response createErrorResponse(Exception e, AlluxioConfiguration alluxioConf) { AlluxioStatusException se = AlluxioStatusException.fromThrowable(e); ErrorResponse response = new ErrorResponse(se.getStatus(), se.getMessage()); Response.ResponseBuilder rb = Response.serverError().entity(response); if (alluxioConf.getBoolean(PropertyKey.WEBUI_CORS_ENABLED)) { return makeCORS(rb).build(); } return rb.build(); }