<commit_before>package org.fedorahosted.flies.rest.service;

import java.io.InputStream;
import java.io.Serializable;

import javax.ws.rs.WebApplicationException;
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.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.jboss.resteasy.spi.NoLogWebApplicationException;
import org.jboss.seam.resteasy.SeamResteasyProviderFactory;

public class RestUtils {

	/**
	 * 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 static <T extends Serializable> void validateEntity(T entity) {
		@SuppressWarnings("rawtypes")
		ClassValidator<T> validator = new ClassValidator(entity.getClass());
		if(validator.hasValidationRules() ) {
			InvalidValue[] invalidValues = validator.getInvalidValues(entity);
			if(invalidValues.length != 0) {
				StringBuilder message = new StringBuilder();
				message.append("Request body contains invalid values:\n");
				for(InvalidValue invalidValue : invalidValues) {
					message.append(invalidValue.getPropertyPath());
					message.append(">");
					message.append(invalidValue.getPropertyName());
					message.append(": ");
					message.append(invalidValue.getMessage());
					message.append("\n");
				}
				throw new NoLogWebApplicationException (
						Response.status(Status.BAD_REQUEST).entity(message.toString()).build());
			}
		}
	}
	
	public  static <T extends Serializable> 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) {
			throw new NoLogWebApplicationException(
					Response.status(Status.BAD_REQUEST).entity("Unable to read request body").build());
		}

		validateEntity(entity);
		
		return entity;
	}
	
	
}<commit_msg>provide error details in REST bad content results<commit_after>package org.fedorahosted.flies.rest.service;

import java.io.InputStream;
import java.io.Serializable;

import javax.ws.rs.WebApplicationException;
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.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.jboss.resteasy.spi.NoLogWebApplicationException;
import org.jboss.seam.resteasy.SeamResteasyProviderFactory;

public class RestUtils {

	/**
	 * 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 static <T extends Serializable> void validateEntity(T entity) {
		@SuppressWarnings("rawtypes")
		ClassValidator<T> validator = new ClassValidator(entity.getClass());
		if(validator.hasValidationRules() ) {
			InvalidValue[] invalidValues = validator.getInvalidValues(entity);
			if(invalidValues.length != 0) {
				StringBuilder message = new StringBuilder();
				message.append("Request body contains invalid values:\n");
				for(InvalidValue invalidValue : invalidValues) {
					message.append(invalidValue.getPropertyPath());
					message.append(">");
					message.append(invalidValue.getPropertyName());
					message.append(": ");
					message.append(invalidValue.getMessage());
					message.append("\n");
				}
				throw new NoLogWebApplicationException (
						Response.status(Status.BAD_REQUEST).entity(message.toString()).build());
			}
		}
	}
	
	public  static <T extends Serializable> 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) {
			throw new NoLogWebApplicationException(
					Response.status(Status.BAD_REQUEST).entity("Unable to read request body: " + e.getMessage()).build());
		}

		validateEntity(entity);
		
		return entity;
	}
	
	
}
{'fraction_non_alphanumeric': 0.1065952336966562, 'fraction_numerical': 0.0003694808793644929, 'mean_word_length': 4.834051724137931, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6891425', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1497, 'n_words': 338}