001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.tide.spring;
022
023import java.util.List;
024import java.util.Locale;
025import java.util.Map;
026
027import javax.servlet.ServletContext;
028
029import org.granite.context.GraniteContext;
030import org.granite.messaging.service.ExceptionConverter;
031import org.granite.messaging.service.ServiceException;
032import org.granite.messaging.webapp.HttpGraniteContext;
033import org.springframework.context.ApplicationContext;
034import org.springframework.validation.BindingResult;
035import org.springframework.validation.Errors;
036import org.springframework.validation.FieldError;
037import org.springframework.validation.ObjectError;
038import org.springframework.web.context.support.WebApplicationContextUtils;
039
040
041public class SpringValidationExceptionConverter implements ExceptionConverter {
042    
043    public static final String VALIDATION_FAILED = "Validation.Failed";
044    
045
046    public boolean accepts(Throwable t, Throwable finalException) {
047        return t.getClass().equals(SpringValidationException.class);
048    }
049
050    public ServiceException convert(Throwable t, String detail, Map<String, Object> extendedData) {
051        Errors errors = ((SpringValidationException)t).getErrors();
052        extendedData.put("invalidValues", convertErrors(errors));
053        
054        ServiceException se = new ServiceException(VALIDATION_FAILED, t.getMessage(), detail, t);
055        se.getExtendedData().putAll(extendedData);
056        return se;
057    }
058
059    
060    public static org.granite.tide.validators.InvalidValue[] convertErrors(Errors errors) {
061        Object bean = null;
062        GraniteContext context = GraniteContext.getCurrentInstance();
063        ServletContext sc = ((HttpGraniteContext)context).getServletContext();
064        ApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(sc);
065        
066        BindingResult bindingResult = null;
067        if (errors instanceof BindingResult) {
068                bindingResult = (BindingResult)errors;
069                bean = bindingResult.getTarget();
070        }
071        
072        org.granite.tide.validators.InvalidValue[] converted = new org.granite.tide.validators.InvalidValue[errors.getErrorCount()];
073        List<? extends ObjectError> allErrors = errors.getAllErrors();
074        int i = 0;
075        for (ObjectError error : allErrors) {
076                if (error instanceof FieldError) {
077                        FieldError ferror = (FieldError)error;
078                        converted[i++] = new org.granite.tide.validators.InvalidValue(bean != null ? bean : ferror.getObjectName(), 
079                                        ferror.getField(),
080                                        ferror.getRejectedValue(),
081                                        springContext.getMessage(ferror, Locale.FRENCH));                                       
082                }
083                else {
084                        converted[i++] = new org.granite.tide.validators.InvalidValue(bean != null ? bean : error.getObjectName(), 
085                                        null,
086                                        null,
087                                        springContext.getMessage(error, Locale.FRENCH));                                        
088                }
089        }
090        return converted;
091    }
092}