001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.method.annotation; 018 019import java.util.Collections; 020import java.util.List; 021 022import org.springframework.util.ObjectUtils; 023import org.springframework.web.bind.WebDataBinder; 024import org.springframework.web.bind.annotation.InitBinder; 025import org.springframework.web.bind.support.DefaultDataBinderFactory; 026import org.springframework.web.bind.support.WebBindingInitializer; 027import org.springframework.web.context.request.NativeWebRequest; 028import org.springframework.web.method.HandlerMethod; 029import org.springframework.web.method.support.InvocableHandlerMethod; 030 031/** 032 * Adds initialization to a WebDataBinder via {@code @InitBinder} methods. 033 * 034 * @author Rossen Stoyanchev 035 * @since 3.1 036 */ 037public class InitBinderDataBinderFactory extends DefaultDataBinderFactory { 038 039 private final List<InvocableHandlerMethod> binderMethods; 040 041 042 /** 043 * Create a new InitBinderDataBinderFactory instance. 044 * @param binderMethods {@code @InitBinder} methods 045 * @param initializer for global data binder initialization 046 */ 047 public InitBinderDataBinderFactory(List<InvocableHandlerMethod> binderMethods, WebBindingInitializer initializer) { 048 super(initializer); 049 this.binderMethods = (binderMethods != null ? binderMethods : Collections.<InvocableHandlerMethod>emptyList()); 050 } 051 052 053 /** 054 * Initialize a WebDataBinder with {@code @InitBinder} methods. 055 * <p>If the {@code @InitBinder} annotation specifies attributes names, 056 * it is invoked only if the names include the target object name. 057 * @throws Exception if one of the invoked @{@link InitBinder} methods fails 058 * @see #isBinderMethodApplicable 059 */ 060 @Override 061 public void initBinder(WebDataBinder dataBinder, NativeWebRequest request) throws Exception { 062 for (InvocableHandlerMethod binderMethod : this.binderMethods) { 063 if (isBinderMethodApplicable(binderMethod, dataBinder)) { 064 Object returnValue = binderMethod.invokeForRequest(request, null, dataBinder); 065 if (returnValue != null) { 066 throw new IllegalStateException( 067 "@InitBinder methods must not return a value (should be void): " + binderMethod); 068 } 069 } 070 } 071 } 072 073 /** 074 * Determine whether the given {@code @InitBinder} method should be used 075 * to initialize the given {@link WebDataBinder} instance. By default we 076 * check the specified attribute names in the annotation value, if any. 077 */ 078 protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder dataBinder) { 079 InitBinder ann = initBinderMethod.getMethodAnnotation(InitBinder.class); 080 String[] names = ann.value(); 081 return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName())); 082 } 083 084}