001/*002 * Copyright 2002-2020 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 at007 *008 * https://www.apache.org/licenses/LICENSE-2.0009 *010 * Unless required by applicable law or agreed to in writing, software011 * 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 and014 * limitations under the License.015 */016017package org.springframework.messaging.converter;018019import java.io.ByteArrayInputStream;020import java.io.ByteArrayOutputStream;021import java.io.StringReader;022import java.io.StringWriter;023import java.io.Writer;024import java.util.Arrays;025import javax.xml.transform.Result;026import javax.xml.transform.Source;027import javax.xml.transform.stream.StreamResult;028import javax.xml.transform.stream.StreamSource;029030import org.springframework.beans.TypeMismatchException;031import org.springframework.messaging.Message;032import org.springframework.messaging.MessageHeaders;033import org.springframework.oxm.Marshaller;034import org.springframework.oxm.Unmarshaller;035import org.springframework.util.Assert;036import org.springframework.util.MimeType;037038/**039 * Implementation of {@link MessageConverter} that can read and write XML using Spring's040 * {@link Marshaller} and {@link Unmarshaller} abstractions.041 *042 * <p>This converter requires a {@code Marshaller} and {@code Unmarshaller} before it can043 * be used. These can be injected by the {@linkplain #MarshallingMessageConverter(Marshaller)044 * constructor} or {@linkplain #setMarshaller(Marshaller) bean properties}.045 *046 * @author Arjen Poutsma047 * @since 4.2048 * @see Marshaller049 * @see Unmarshaller050 */051public class MarshallingMessageConverter extends AbstractMessageConverter {052053 private Marshaller marshaller;054055 private Unmarshaller unmarshaller;056057058 /**059 * Default construct allowing for {@link #setMarshaller(Marshaller)} and/or060 * {@link #setUnmarshaller(Unmarshaller)} to be invoked separately.061 */062 public MarshallingMessageConverter() {063 this(new MimeType("application", "xml"), new MimeType("text", "xml"),064 new MimeType("application", "*+xml"));065 }066067 /**068 * Constructor with a given list of MIME types to support.069 * @param supportedMimeTypes the MIME types070 */071 public MarshallingMessageConverter(MimeType... supportedMimeTypes) {072 super(Arrays.asList(supportedMimeTypes));073 }074075 /**076 * Constructor with {@link Marshaller}. If the given {@link Marshaller} also077