protected void setReason(Fault fault) { fault.setReason(localize(Messages.ASYNCHRONOUS_TASK_FAILED)); } }
@Override public Response toResponse(MappingException exception) { log.error( "Mapping exception while processing \"{}\" request for path \"{}\"", request.getMethod(), uriInfo.getPath() ); log.error("Exception", exception); final Fault fault = new Fault(); fault.setReason("Operation Failed"); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(fault).build(); } }
@Override public Response toResponse(InvalidEnumValueException exception) { Fault fault = new Fault(); fault.setReason("Invalid Enum value"); fault.setDetail(exception.getMessage()); return Response.status(Response.Status.BAD_REQUEST).entity(fault).build(); }
private static Fault fault(String reason, String detail) { Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(detail); return fault; }
protected Fault fault(String reason, String detail) { Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(detail); return fault; }
private static Fault fault(String reason, String detail) { Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(detail); return fault; } }
private static Fault fault(String reason, String detail) { Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(detail); return fault; } }
/** * Checks if the given value is within the range given by the {@code min} and {@code max} parameters. If the value * is {@code null} it will do nothing. * * @param name the name of the attribute * @param value the value of the attribute * @param min the min value of the range * @param max the max value of the range */ public void validateRange(String name, Integer value, int min, int max) { if (value != null && (value < min || value > max)) { Fault fault = new Fault(); fault.setReason(localize(Messages.VALUE_OUT_OF_RANGE_REASON)); fault.setDetail(localize(Messages.VALUE_OUT_OF_RANGE_DETAIL_TEMPLATE, value.toString(), name, String.valueOf(min), String.valueOf(max))); throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(fault).build()); } }
@Override public Response toResponse(MalformedIdException exception) { log.error( "Malformed id detected while processing \"{}\" request for path \"{}\"", request.getMethod(), uriInfo.getPath() ); log.error("Exception", exception); final Fault fault = new Fault(); fault.setReason("Operation failed"); fault.setDetail(exception.getCause().getMessage()); return Response.status(Status.BAD_REQUEST).entity(fault).build(); } }
/** * Validate presence of required parameters. * * @param reason the fault reason * @param detail the fault detail * @param model the incoming representation * @param frameOffset the stack frame offset of the public resource method * @param missingMembers the stack frame offset of the public resource method * @param required the required field names * @return error Response if appropriate */ private static Response assertRequired(String reason, String detail, Object model, int frameOffset, String... required) { List<String> missing = doAssertRequired(reason, detail, model, frameOffset, required); Response response = null; if (!missing.isEmpty()) { StackTraceElement[] trace = new Throwable().getStackTrace(); Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(MessageFormat.format(detail, model.getClass().getSimpleName(), missing, trace[frameOffset + 1].getMethodName())); response = Response.status(INCOMPLETE_PARAMS_STATUS) .entity(fault) .build(); } return response; } /**
@Override public Response toResponse(ValidationException exception) { log.error( "Input validation failed while processing '{}' request for path '{}'.", request.getMethod(), uriInfo.getPath() ); log.error("Exception", exception); final Fault fault = new Fault(); fault.setReason("Input validation failed."); fault.setDetail(exception.getMessage()); return Response.status(Status.BAD_REQUEST).entity(fault).build(); } }
private void validateBrickNames(Action action) { List<GlusterBrick> bricks = action.getBricks().getGlusterBricks(); for (GlusterBrick brick : bricks) { if (brick.getName() == null || brick.getName().equals("")) { Fault fault = new Fault(); fault.setReason(localize(Messages.INCOMPLETE_PARAMS_REASON)); throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST) .entity(fault) .build()); } } }
private Response handleFailure(Action action, String message) { action.setStatus(CreationStatus.FAILED.value()); action.setFault(new Fault()); action.getFault().setReason(message); return Response.ok().entity(action).build(); }
@Override public Response toResponse(IOException exception) { // Check if the cause of the exception is an invalid value, and generate a specific error message: Throwable cause = exception.getCause(); if (cause instanceof InvalidValueException) { Fault fault = new Fault(); fault.setReason("Invalid value"); fault.setDetail(cause.getMessage()); return Response.status(Response.Status.BAD_REQUEST).entity(fault).build(); } // For any other kind of exception generate an error response with information describing the correct syntax for // the request: try { log.error( "IO exception while processing \"{}\" request for path \"{}\"", request.getMethod(), uriInfo.getPath() ); log.error("Exception", exception); UsageFinder finder = new UsageFinder(); return Response.status(Status.BAD_REQUEST).entity(finder.getUsageMessage(uriInfo, request)).build(); } catch(Exception error) { throw new WebApplicationException(error, Response.status(Status.INTERNAL_SERVER_ERROR).build()); } }
/** * A method handling GET requests with media type x-virt-viewer. * Returns a console representation usable by virt-viewer client (e.g. a .vv file) * * @return a console representation for virt-viewer (e.g. a .vv file) */ @GET @Produces(ApiMediaType.APPLICATION_X_VIRT_VIEWER) public Response generateDescriptor() { try { QueryReturnValue consoleDescriptorReturnValue = generateDescriptorResponse(); Response.ResponseBuilder builder; if (consoleDescriptorReturnValue.getSucceeded() && consoleDescriptorReturnValue.getReturnValue() != null) { builder = Response.ok(((String) consoleDescriptorReturnValue.getReturnValue()) .getBytes(StandardCharsets.UTF_8), ApiMediaType.APPLICATION_X_VIRT_VIEWER); } else { Fault fault = new Fault(); fault.setReason(consoleDescriptorReturnValue.getExceptionString()); builder = Response.status(Response.Status.CONFLICT).type(ApiMediaType.APPLICATION_XML).entity(fault); } return builder.build(); } catch (Exception ex) { return handleConfigureConsoleError(ex.getMessage()); } }
/** * Impose immutability constraints. * * @param <T> representation type * @param strict array of strictly immutable field names * @param incoming incoming representation * @param existing existing representation * @param reason the fault reason * @param detail the fault detail * @return error Response if appropriate */ public static <T extends BaseResource> Response imposeConstraints(String[] strict, T incoming, T existing, String reason, String detail) { for (String s: strict) { String field = capitalize(s); if (isSet(incoming, field) && different(incoming, existing, field)) { Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(MessageFormat.format(detail, s)); return Response.status(BROKEN_CONSTRAINT_STATUS) .entity(fault) .build(); } } return null; } }
public Fault getUsageMessage(UriInfo uriInfo, Request request) throws ClassNotFoundException, IOException { Fault fault = new Fault(); fault.setReason(RESPONSE); fault.setDetail("For correct usage, see: " + getUsageLink(uriInfo, request.getMethod())); return fault; }
@Override public Fault adapt(V3Fault from) { Fault to = new Fault(); if (from.isSetReason()) { to.setReason(from.getReason()); } if (from.isSetDetail()) { to.setDetail(from.getDetail()); } return to; } }
@Override public Response remove() { boolean destroy = ParametersHelper.getBooleanParameter(httpHeaders, uriInfo, DESTROY, true, false); get(); if (destroy) { StorageDomainParametersBase parameters = new StorageDomainParametersBase(guid); return performAction(ActionType.ForceRemoveStorageDomain, parameters); } else { String host = ParametersHelper.getParameter(httpHeaders, uriInfo, HOST); if (host == null) { Fault fault = new Fault(); fault.setReason("host parameter is missing"); throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(fault).build()); } boolean format = ParametersHelper.getBooleanParameter(httpHeaders, uriInfo, FORMAT, true, false); Guid hostId = getHostId(host); RemoveStorageDomainParameters parameters = new RemoveStorageDomainParameters(guid); parameters.setVdsId(hostId); parameters.setDoFormat(format); return performAction(ActionType.RemoveStorageDomain, parameters); } }
@Override public Response activate(Action action) { validateParameters(action, "bricks"); validateBrickNames(action); GlusterVolumeEntity volume = getEntity(GlusterVolumeEntity.class, QueryType.GetGlusterVolumeById, new IdQueryParameters(asGuid(getVolumeId())), ""); if (volume.getAsyncTask() != null && volume.getAsyncTask().getType() == GlusterTaskType.REMOVE_BRICK && volume.getAsyncTask().getStatus() == JobExecutionStatus.FINISHED) { return stopMigrate(action); } else { Fault fault = new Fault(); fault.setReason(localize(Messages.CANNOT_ACTIVATE_UNLESS_MIGRATION_COMPLETED)); throw new WebApplicationException(Response.status(Response.Status.CONFLICT) .entity(fault) .build()); } }