MarshallingHttpMessageConverter.java 5.0 KB
Newer Older
1
/*
2
 * Copyright 2002-2010 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18 19 20 21 22
package org.springframework.http.converter.xml;

import java.io.IOException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;

23
import org.springframework.beans.TypeMismatchException;
24
import org.springframework.http.HttpHeaders;
25 26
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
27
import org.springframework.oxm.Marshaller;
28
import org.springframework.oxm.MarshallingFailureException;
29
import org.springframework.oxm.Unmarshaller;
30
import org.springframework.oxm.UnmarshallingFailureException;
31 32 33
import org.springframework.util.Assert;

/**
J
Juergen Hoeller 已提交
34 35
 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
 * that can read and write XML using Spring's {@link Marshaller} and {@link Unmarshaller} abstractions.
36
 *
J
Juergen Hoeller 已提交
37 38 39
 * <p>This converter requires a {@code Marshaller} and {@code Unmarshaller} before it can be used.
 * These can be injected by the {@linkplain #MarshallingHttpMessageConverter(Marshaller) constructor}
 * or {@linkplain #setMarshaller(Marshaller) bean properties}.
40
 *
J
Juergen Hoeller 已提交
41 42
 * <p>By default, this converter supports {@code text/xml} and {@code application/xml}. This can be
 * overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
43 44 45 46
 *
 * @author Arjen Poutsma
 * @since 3.0
 */
47
public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConverter<Object> {
48 49 50 51 52

	private Marshaller marshaller;

	private Unmarshaller unmarshaller;

53

54
	/**
J
Juergen Hoeller 已提交
55 56 57
	 * Construct a new {@code MarshallingHttpMessageConverter} with no {@link Marshaller} or
	 * {@link Unmarshaller} set. The Marshaller and Unmarshaller must be set after construction
	 * by invoking {@link #setMarshaller(Marshaller)} and {@link #setUnmarshaller(Unmarshaller)} .
58 59 60 61 62
	 */
	public MarshallingHttpMessageConverter() {
	}

	/**
J
Juergen Hoeller 已提交
63 64 65 66
	 * Construct a new {@code MarshallingMessageConverter} with the given {@link Marshaller} set.
	 * <p>If the given {@link Marshaller} also implements the {@link Unmarshaller} interface,
	 * it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.
	 * <p>Note that all {@code Marshaller} implementations in Spring also implement the
67 68 69 70
	 * {@code Unmarshaller} interface, so that you can safely use this constructor.
	 * @param marshaller object used as marshaller and unmarshaller
	 */
	public MarshallingHttpMessageConverter(Marshaller marshaller) {
J
Juergen Hoeller 已提交
71
		Assert.notNull(marshaller, "Marshaller must not be null");
72 73
		this.marshaller = marshaller;
		if (marshaller instanceof Unmarshaller) {
74 75 76 77 78
			this.unmarshaller = (Unmarshaller) marshaller;
		}
	}

	/**
J
Juergen Hoeller 已提交
79 80
	 * Construct a new <code>MarshallingMessageConverter</code> with the given
	 * {@code Marshaller} and {@code Unmarshaller}.
81 82 83 84
	 * @param marshaller the Marshaller to use
	 * @param unmarshaller the Unmarshaller to use
	 */
	public MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
J
Juergen Hoeller 已提交
85 86
		Assert.notNull(marshaller, "Marshaller must not be null");
		Assert.notNull(unmarshaller, "Unmarshaller must not be null");
87 88 89 90
		this.marshaller = marshaller;
		this.unmarshaller = unmarshaller;
	}

J
Juergen Hoeller 已提交
91

92 93 94
	/**
	 * Set the {@link Marshaller} to be used by this message converter.
	 */
95 96 97 98
	public void setMarshaller(Marshaller marshaller) {
		this.marshaller = marshaller;
	}

99 100 101
	/**
	 * Set the {@link Unmarshaller} to be used by this message converter.
	 */
102 103 104 105
	public void setUnmarshaller(Unmarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}

106

107
	@Override
108
	public boolean supports(Class<?> clazz) {
109
		return this.unmarshaller.supports(clazz);
110 111 112
	}

	@Override
113
	protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
114
		Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
115
		try {
116 117 118 119 120
			Object result = this.unmarshaller.unmarshal(source);
			if (!clazz.isInstance(result)) {
				throw new TypeMismatchException(result, clazz);
			}
			return result;
121 122 123 124
		}
		catch (UnmarshallingFailureException ex) {
			throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex);
		}
125 126 127 128
	}

	@Override
	protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
129
		Assert.notNull(this.marshaller, "Property 'marshaller' is required");
130
		try {
131
			this.marshaller.marshal(o, result);
132 133 134 135
		}
		catch (MarshallingFailureException ex) {
			throw new HttpMessageNotWritableException("Could not write [" + o + "]", ex);
		}
136
	}
137

138
}