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.http.converter.support;
018
019import org.springframework.http.converter.FormHttpMessageConverter;
020import org.springframework.http.converter.json.GsonHttpMessageConverter;
021import org.springframework.http.converter.json.JsonbHttpMessageConverter;
022import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
023import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
024import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
025import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
026import org.springframework.http.converter.xml.SourceHttpMessageConverter;
027import org.springframework.util.ClassUtils;
028
029/**
030 * Extension of {@link org.springframework.http.converter.FormHttpMessageConverter},
031 * adding support for XML and JSON-based parts.
032 *
033 * @author Rossen Stoyanchev
034 * @author Juergen Hoeller
035 * @since 3.2
036 */
037public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConverter {
038
039        private static final boolean jaxb2Present;
040
041        private static final boolean jackson2Present;
042
043        private static final boolean jackson2XmlPresent;
044
045        private static final boolean jackson2SmilePresent;
046
047        private static final boolean gsonPresent;
048
049        private static final boolean jsonbPresent;
050
051        static {
052                ClassLoader classLoader = AllEncompassingFormHttpMessageConverter.class.getClassLoader();
053                jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", classLoader);
054                jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
055                                                ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
056                jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
057                jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
058                gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);
059                jsonbPresent = ClassUtils.isPresent("javax.json.bind.Jsonb", classLoader);
060        }
061
062
063        public AllEncompassingFormHttpMessageConverter() {
064                try {
065                        addPartConverter(new SourceHttpMessageConverter<>());
066                }
067                catch (Error err) {
068                        // Ignore when no TransformerFactory implementation is available
069                }
070
071                if (jaxb2Present && !jackson2XmlPresent) {
072                        addPartConverter(new Jaxb2RootElementHttpMessageConverter());
073                }
074
075                if (jackson2Present) {
076                        addPartConverter(new MappingJackson2HttpMessageConverter());
077                }
078                else if (gsonPresent) {
079                        addPartConverter(new GsonHttpMessageConverter());
080                }
081                else if (jsonbPresent) {
082                        addPartConverter(new JsonbHttpMessageConverter());
083                }
084
085                if (jackson2XmlPresent) {
086                        addPartConverter(new MappingJackson2XmlHttpMessageConverter());
087                }
088
089                if (jackson2SmilePresent) {
090                        addPartConverter(new MappingJackson2SmileHttpMessageConverter());
091                }
092        }
093
094}