use of org.jboss.resteasy.spi.LoggableFailure in project quickstart by wildfly.
the class ContactRESTService method createContact.
/**
* Creates a new contact from the values provided. Performs validation and will return a JAX-RS response with either 200 (ok)
* or with a map of fields, and related errors.
*
* @param Contact
* @return Response
*/
@SuppressWarnings("unused")
@POST
public Response createContact(Contact contact) {
log.info("createContact started. Contact = " + contact.getFirstName() + " " + contact.getLastName() + " " + contact.getEmail() + " " + contact.getPhoneNumber() + " " + contact.getBirthDate() + " " + contact.getId());
if (contact == null) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
Response.ResponseBuilder builder = null;
try {
// Go add the new Contact.
Contact created = service.create(contact);
// Construct a location of created contact.
URI location = null;
try {
UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
uriBuilder.path(Long.toString(created.getId()));
location = uriBuilder.build();
} catch (LoggableFailure lf) {
// IGNORED: UriInfo methods throw this if called outside the scope of request,
// that happens in ContactRegistrationTest.testRegister method.
}
// Create an CREATED Response and pass the URI location back in case it is needed.
builder = Response.created(location);
log.info("createContact completed. Contact = " + contact.getFirstName() + " " + contact.getLastName() + " " + contact.getEmail() + " " + contact.getPhoneNumber() + " " + contact.getBirthDate() + " " + contact.getId());
} catch (ConstraintViolationException ce) {
log.info("ConstraintViolationException - " + ce.toString());
// Handle bean validation issues
builder = createViolationResponse(ce.getConstraintViolations());
} catch (ValidationException e) {
log.info("ValidationException - " + e.toString());
// Handle the unique constrain violation
Map<String, String> responseObj = new HashMap<>();
responseObj.put("email", "That email is already used, please use a unique email");
builder = Response.status(Response.Status.CONFLICT).entity(responseObj);
} catch (Exception e) {
log.info("Exception - " + e.toString());
// Handle generic exceptions
Map<String, String> responseObj = new HashMap<>();
responseObj.put("error", e.getMessage());
builder = Response.status(Response.Status.BAD_REQUEST).entity(responseObj);
}
return builder.build();
}
Aggregations