View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.http.converter.support;
18  
19  import javax.xml.transform.Source;
20  
21  import org.springframework.http.converter.FormHttpMessageConverter;
22  import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
23  import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
24  import org.springframework.http.converter.xml.SourceHttpMessageConverter;
25  import org.springframework.util.ClassUtils;
26  
27  /**
28   * Extension of {@link org.springframework.http.converter.FormHttpMessageConverter},
29   * adding support for XML and JSON-based parts.
30   *
31   * @author Rossen Stoyanchev
32   * @since 3.2
33   */
34  public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConverter {
35  
36  	private static final boolean jaxb2Present =
37  			ClassUtils.isPresent("javax.xml.bind.Binder", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
38  
39  	private static final boolean jackson2Present =
40  			ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", AllEncompassingFormHttpMessageConverter.class.getClassLoader()) &&
41  					ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
42  
43  
44  	public AllEncompassingFormHttpMessageConverter() {
45  		addPartConverter(new SourceHttpMessageConverter<Source>());
46  		if (jaxb2Present) {
47  			addPartConverter(new Jaxb2RootElementHttpMessageConverter());
48  		}
49  		if (jackson2Present) {
50  			addPartConverter(new MappingJackson2HttpMessageConverter());
51  		}
52  	}
53  
54  }