package com.proofpoint.platform.sample;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import javax.validation.ConstraintViolation;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.List;

@Provider
public class ValidationExceptionMapper
        implements ExceptionMapper<ValidationException>
{
    @Override
    public Response toResponse(ValidationException e)
    {
        List<String> messages = Lists.newArrayList(Iterables.transform(e.getViolations(), asString()));

        return Response.status(Response.Status.BAD_REQUEST)
                .type(MediaType.APPLICATION_JSON_TYPE)
                .entity(messages)
                .build();
    }

    private Function<ConstraintViolation<?>, String> asString()
    {
        return new Function<ConstraintViolation<?>, String>()
        {
            @Override
            public String apply(ConstraintViolation<?> input)
            {
                return input.getPropertyPath().toString() + " " + input.getMessage();
            }
        };
    }

}

@@ -63,105 +63,29 @@
 mon.
-base.Function;%0Aimport com.google.common.collect.Iterables;%0Aimport com.google.common.collect.
+collect.Immutable
 List
-s
 ;%0A%0Ai
@@ -273,31 +273,8 @@
 der;
-%0Aimport java.util.List;
 %0A%0A@P
@@ -464,103 +464,261 @@
     
-List%3CString%3E messages = Lists.newArrayList(Iterables.transform(e.getViolations(), asString()));
+ImmutableList.Builder%3CString%3E messages = new ImmutableList.Builder%3CString%3E();%0A        for (ConstraintViolation%3C?%3E violation : e.getViolations()) %7B%0A            messages.add(violation.getPropertyPath().toString() + %22 %22 + violation.getMessage());%0A        %7D
 %0A%0A  
@@ -866,34 +866,16 @@
 ages
-)%0A                
 .build()
 ;%0A  
@@ -874,373 +874,41 @@
 ld()
-;
+)
 %0A    
-%7D%0A%0A
     
-private Function%3CConstraintViolation%3C?%3E, String%3E asString()%0A    %7B%0A        return new Function%3CConstraintViolation%3C?%3E, String%3E()%0A        %7B%0A            @Override%0A            public String apply(ConstraintViolation%3C?%3E input)%0A            %7B%0A                return input.getPropertyPath().toString() + %22 %22 + input.getMessage();%0A            %7D%0A        %7D
+        .build()
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -904,11 +904,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

simplify construction of messages