001/*
002 * Copyright 2002-2017 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.bind.support;
018
019import org.springframework.lang.Nullable;
020import org.springframework.web.bind.WebDataBinder;
021import org.springframework.web.context.request.NativeWebRequest;
022
023/**
024 * Create a {@link WebRequestDataBinder} instance and initialize it with a
025 * {@link WebBindingInitializer}.
026 *
027 * @author Rossen Stoyanchev
028 * @since 3.1
029 */
030public class DefaultDataBinderFactory implements WebDataBinderFactory {
031
032        @Nullable
033        private final WebBindingInitializer initializer;
034
035
036        /**
037         * Create a new {@code DefaultDataBinderFactory} instance.
038         * @param initializer for global data binder initialization
039         * (or {@code null} if none)
040         */
041        public DefaultDataBinderFactory(@Nullable WebBindingInitializer initializer) {
042                this.initializer = initializer;
043        }
044
045
046        /**
047         * Create a new {@link WebDataBinder} for the given target object and
048         * initialize it through a {@link WebBindingInitializer}.
049         * @throws Exception in case of invalid state or arguments
050         */
051        @Override
052        @SuppressWarnings("deprecation")
053        public final WebDataBinder createBinder(
054                        NativeWebRequest webRequest, @Nullable Object target, String objectName) throws Exception {
055
056                WebDataBinder dataBinder = createBinderInstance(target, objectName, webRequest);
057                if (this.initializer != null) {
058                        this.initializer.initBinder(dataBinder, webRequest);
059                }
060                initBinder(dataBinder, webRequest);
061                return dataBinder;
062        }
063
064        /**
065         * Extension point to create the WebDataBinder instance.
066         * By default this is {@code WebRequestDataBinder}.
067         * @param target the binding target or {@code null} for type conversion only
068         * @param objectName the binding target object name
069         * @param webRequest the current request
070         * @throws Exception in case of invalid state or arguments
071         */
072        protected WebDataBinder createBinderInstance(
073                        @Nullable Object target, String objectName, NativeWebRequest webRequest) throws Exception {
074
075                return new WebRequestDataBinder(target, objectName);
076        }
077
078        /**
079         * Extension point to further initialize the created data binder instance
080         * (e.g. with {@code @InitBinder} methods) after "global" initialization
081         * via {@link WebBindingInitializer}.
082         * @param dataBinder the data binder instance to customize
083         * @param webRequest the current request
084         * @throws Exception if initialization fails
085         */
086        protected void initBinder(WebDataBinder dataBinder, NativeWebRequest webRequest)
087                        throws Exception {
088
089        }
090
091}