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 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.messaging.converter;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.StringReader;
022import java.io.StringWriter;
023import java.io.Writer;
024
025import javax.xml.transform.Result;
026import javax.xml.transform.Source;
027import javax.xml.transform.stream.StreamResult;
028import javax.xml.transform.stream.StreamSource;
029
030import org.springframework.beans.TypeMismatchException;
031import org.springframework.lang.Nullable;
032import org.springframework.messaging.Message;
033import org.springframework.messaging.MessageHeaders;
034import org.springframework.oxm.Marshaller;
035import org.springframework.oxm.Unmarshaller;
036import org.springframework.util.Assert;
037import org.springframework.util.MimeType;
038
039/**
040 * Implementation of {@link MessageConverter} that can read and write XML using Spring's
041 * {@link Marshaller} and {@link Unmarshaller} abstractions.
042 *
043 * <p>This converter requires a {@code Marshaller} and {@code Unmarshaller} before it can
044 * be used. These can be injected by the {@linkplain #MarshallingMessageConverter(Marshaller)
045 * constructor} or {@linkplain #setMarshaller(Marshaller) bean properties}.
046 *
047 * @author Arjen Poutsma
048 * @since 4.2
049 * @see Marshaller
050 * @see Unmarshaller
051 */
052public class MarshallingMessageConverter extends AbstractMessageConverter {
053
054        @Nullable
055        private Marshaller marshaller;
056
057        @Nullable
058        private Unmarshaller unmarshaller;
059
060
061        /**
062         * Default construct allowing for {@link #setMarshaller(Marshaller)} and/or
063         * {@link #setUnmarshaller(Unmarshaller)} to be invoked separately.
064         */
065        public MarshallingMessageConverter() {
066                this(new MimeType("application", "xml"), new MimeType("text", "xml"),
067                                new MimeType("application", "*+xml"));
068        }
069
070        /**
071         * Constructor with a given list of MIME types to support.
072         * @param supportedMimeTypes the MIME types
073         */
074        public MarshallingMessageConverter(MimeType... supportedMimeTypes) {
075                super(supportedMimeTypes);
076        }
077
078        /**
079         * Constructor with {@link Marshaller}. If the given {@link Marshaller} also
080         * implements {@link Unmarshaller}, it is also used for unmarshalling.
081         * <p>Note that all {@code Marshaller} implementations in Spring also implement
082         * {@code Unmarshaller} so that you can safely use this constructor.
083         * @param marshaller object used as marshaller and unmarshaller
084         */
085        public MarshallingMessageConverter(Marshaller marshaller) {
086                this();
087                Assert.notNull(marshaller, "Marshaller must not be null");
088                this.marshaller = marshaller;
089                if (marshaller instanceof Unmarshaller) {
090                        this.unmarshaller = (Unmarshaller) marshaller;
091                }
092        }
093
094
095        /**
096         * Set the {@link Marshaller} to be used by this message converter.
097         */
098        public void setMarshaller(@Nullable Marshaller marshaller) {
099                this.marshaller = marshaller;
100        }
101
102        /**
103         * Return the configured Marshaller.
104         */
105        @Nullable
106        public Marshaller getMarshaller() {
107                return this.marshaller;
108        }
109
110        /**
111         * Set the {@link Unmarshaller} to be used by this message converter.
112         */
113        public void setUnmarshaller(@Nullable Unmarshaller unmarshaller) {
114                this.unmarshaller = unmarshaller;
115        }
116
117        /**
118         * Return the configured unmarshaller.
119         */
120        @Nullable
121        public Unmarshaller getUnmarshaller() {
122                return this.unmarshaller;
123        }
124
125
126        @Override
127        protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
128                return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null &&
129                                this.unmarshaller.supports(targetClass));
130        }
131
132        @Override
133        protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
134                return (supportsMimeType(headers) && this.marshaller != null &&
135                                this.marshaller.supports(payload.getClass()));
136        }
137
138        @Override
139        protected boolean supports(Class<?> clazz) {
140                // should not be called, since we override canConvertFrom/canConvertTo instead
141                throw new UnsupportedOperationException();
142        }
143
144        @Override
145        @Nullable
146        protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
147                Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
148                try {
149                        Source source = getSource(message.getPayload());
150                        Object result = this.unmarshaller.unmarshal(source);
151                        if (!targetClass.isInstance(result)) {
152                                throw new TypeMismatchException(result, targetClass);
153                        }
154                        return result;
155                }
156                catch (Exception ex) {
157                        throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
158                }
159        }
160
161        private Source getSource(Object payload) {
162                if (payload instanceof byte[]) {
163                        return new StreamSource(new ByteArrayInputStream((byte[]) payload));
164                }
165                else {
166                        return new StreamSource(new StringReader(payload.toString()));
167                }
168        }
169
170        @Override
171        @Nullable
172        protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers,
173                        @Nullable Object conversionHint) {
174
175                Assert.notNull(this.marshaller, "Property 'marshaller' is required");
176                try {
177                        if (byte[].class == getSerializedPayloadClass()) {
178                                ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
179                                Result result = new StreamResult(out);
180                                this.marshaller.marshal(payload, result);
181                                payload = out.toByteArray();
182                        }
183                        else {
184                                Writer writer = new StringWriter(1024);
185                                Result result = new StreamResult(writer);
186                                this.marshaller.marshal(payload, result);
187                                payload = writer.toString();
188                        }
189                }
190                catch (Throwable ex) {
191                        throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
192                }
193                return payload;
194        }
195
196}