Search in sources :

Example 1 with LoggableFailure

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();
}
Also used : Response(javax.ws.rs.core.Response) LoggableFailure(org.jboss.resteasy.spi.LoggableFailure) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) ConstraintViolationException(javax.validation.ConstraintViolationException) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST)

Aggregations

URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 ValidationException (javax.validation.ValidationException)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 LoggableFailure (org.jboss.resteasy.spi.LoggableFailure)1