Package org.zanata.rest.service

Source Code of org.zanata.rest.service.RestUtils

package org.zanata.rest.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.MessageBodyReader;

import org.jboss.resteasy.spi.NoLogWebApplicationException;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.resteasy.SeamResteasyProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Name("restUtils")
public class RestUtils {
    private static final Logger log = LoggerFactory.getLogger(RestUtils.class);

    @In
    Validator validator;

    /**
     * Validate Hibernate Validator based constraints.
     *
     * If validation fails a WebApplicationException with status BAD_REQUEST is
     * thrown, with a message describing the validation errors.
     *
     * @param <T>
     *            class of entity to validate
     * @param entity
     *            Hibernate-validator annotated entity
     */
    @SuppressWarnings("unchecked")
    public <T> void validateEntity(T entity) {
        validator.getConstraintsForClass(entity.getClass());
        Set<ConstraintViolation<T>> violations = validator.validate(entity);
        if (!violations.isEmpty()) {
            StringBuilder message = new StringBuilder();
            message.append("Request body contains invalid values:\n");
            for (ConstraintViolation<T> violation : violations) {
                message.append(violation.getPropertyPath());
                message.append(": ");
                message.append(violation.getMessage());
                message.append("\n");
            }
            log.debug("Bad Request: {}", message);
            throw new NoLogWebApplicationException(Response
                    .status(Status.BAD_REQUEST).entity(message.toString())
                    .build());
        }
    }

    public <T> T unmarshall(Class<T> entityClass, InputStream is,
            MediaType requestContentType,
            MultivaluedMap<String, String> requestHeaders) {
        MessageBodyReader<T> reader =
                SeamResteasyProviderFactory.getInstance().getMessageBodyReader(
                        entityClass, entityClass, entityClass.getAnnotations(),
                        requestContentType);
        if (reader == null) {
            throw new RuntimeException(
                    "Unable to find MessageBodyReader for content type "
                            + requestContentType);
        }
        T entity;
        try {
            entity =
                    reader.readFrom(entityClass, entityClass,
                            entityClass.getAnnotations(), requestContentType,
                            requestHeaders, is);
        } catch (Exception e) {
            log.debug("Bad Request: Unable to read request body:", e);
            throw new NoLogWebApplicationException(Response
                    .status(Status.BAD_REQUEST)
                    .entity("Unable to read request body: " + e.getMessage())
                    .build());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
            }
        }

        validateEntity(entity);

        return entity;
    }

}
TOP

Related Classes of org.zanata.rest.service.RestUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.