View Javadoc
1   /*
2    * Copyright 2019 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.bremersee.http.codec.xml;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.function.BiConsumer;
24  import javax.xml.XMLConstants;
25  import javax.xml.bind.JAXBElement;
26  import javax.xml.bind.JAXBException;
27  import javax.xml.bind.UnmarshalException;
28  import javax.xml.bind.Unmarshaller;
29  import javax.xml.bind.annotation.XmlRootElement;
30  import javax.xml.bind.annotation.XmlSchema;
31  import javax.xml.bind.annotation.XmlType;
32  import javax.xml.namespace.QName;
33  import javax.xml.stream.XMLEventReader;
34  import javax.xml.stream.XMLInputFactory;
35  import javax.xml.stream.XMLStreamException;
36  import javax.xml.stream.events.XMLEvent;
37  import org.bremersee.xml.JaxbContextBuilder;
38  import org.reactivestreams.Publisher;
39  import org.springframework.core.ResolvableType;
40  import org.springframework.core.codec.AbstractDecoder;
41  import org.springframework.core.codec.CodecException;
42  import org.springframework.core.codec.DecodingException;
43  import org.springframework.core.codec.Hints;
44  import org.springframework.core.io.buffer.DataBuffer;
45  import org.springframework.core.io.buffer.DataBufferLimitException;
46  import org.springframework.core.io.buffer.DataBufferUtils;
47  import org.springframework.core.log.LogFormatUtils;
48  import org.springframework.http.MediaType;
49  import org.springframework.http.codec.xml.XmlEventDecoder;
50  import org.springframework.lang.NonNull;
51  import org.springframework.lang.Nullable;
52  import org.springframework.util.Assert;
53  import org.springframework.util.ClassUtils;
54  import org.springframework.util.MimeType;
55  import org.springframework.util.MimeTypeUtils;
56  import org.springframework.util.xml.StaxUtils;
57  import org.springframework.validation.annotation.Validated;
58  import reactor.core.Exceptions;
59  import reactor.core.publisher.Flux;
60  import reactor.core.publisher.Mono;
61  import reactor.core.publisher.SynchronousSink;
62  
63  /**
64   * Decode from a bytes stream containing XML elements to a stream of {@code Object}s (POJOs).
65   *
66   * <p>The decoding parts are taken from {@link org.springframework.http.codec.xml.Jaxb2XmlDecoder}.
67   *
68   * @author Sebastien Deleuze
69   * @author Arjen Poutsma
70   * @author Christian Bremer
71   */
72  @Validated
73  public class ReactiveJaxbDecoder extends AbstractDecoder<Object> {
74  
75    /**
76     * The default value for JAXB annotations.
77     *
78     * @see XmlRootElement#name()
79     * @see XmlRootElement#namespace()
80     * @see XmlType#name()
81     * @see XmlType#namespace()
82     */
83    private static final String JAXB_DEFAULT_ANNOTATION_VALUE = "##default";
84  
85    private static final XMLInputFactory inputFactory = StaxUtils.createDefensiveInputFactory();
86  
87  
88    private final XmlEventDecoder xmlEventDecoder = new XmlEventDecoder();
89  
90    private final JaxbContextBuilder jaxbContextBuilder;
91  
92    private int maxInMemorySize = 256 * 1024;
93  
94    /**
95     * Instantiates a new reactive jaxb decoder.
96     *
97     * @param jaxbContextBuilder the jaxb context builder
98     */
99    public ReactiveJaxbDecoder(final JaxbContextBuilder jaxbContextBuilder) {
100     super(MimeTypeUtils.APPLICATION_XML, MimeTypeUtils.TEXT_XML,
101         new MediaType("application", "*+xml"));
102     this.jaxbContextBuilder = jaxbContextBuilder != null
103         ? jaxbContextBuilder
104         : JaxbContextBuilder.builder()
105             .withCanUnmarshal(JaxbContextBuilder.CAN_UNMARSHAL_ALL);
106   }
107 
108   /**
109    * Set the max number of bytes that can be buffered by this decoder. This is either the size of the entire input when
110    * decoding as a whole, or when using async parsing with Aalto XML, it is the size of one top-level XML tree. When the
111    * limit is exceeded, {@link DataBufferLimitException} is raised.
112    *
113    * <p>By default this is set to 256K.
114    *
115    * @param byteCount the max number of bytes to buffer, or -1 for unlimited
116    */
117   public void setMaxInMemorySize(int byteCount) {
118     this.maxInMemorySize = byteCount;
119     this.xmlEventDecoder.setMaxInMemorySize(byteCount);
120   }
121 
122   /**
123    * Return the {@link #setMaxInMemorySize configured} byte count limit.
124    *
125    * @return the max in memory size
126    */
127   public int getMaxInMemorySize() {
128     return this.maxInMemorySize;
129   }
130 
131 
132   @Override
133   public boolean canDecode(@NonNull ResolvableType elementType, @Nullable MimeType mimeType) {
134     if (super.canDecode(elementType, mimeType)) {
135       final Class<?> outputClass = elementType.getRawClass();
136       return jaxbContextBuilder.canUnmarshal(outputClass);
137     } else {
138       return false;
139     }
140   }
141 
142   @NonNull
143   @Override
144   public Flux<Object> decode(@NonNull Publisher<DataBuffer> inputStream, ResolvableType elementType,
145       @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
146 
147     Flux<XMLEvent> xmlEventFlux = this.xmlEventDecoder.decode(
148         inputStream, ResolvableType.forClass(XMLEvent.class), mimeType, hints);
149 
150     Class<?> outputClass = elementType.toClass();
151     QName typeName = toQName(outputClass);
152     Flux<List<XMLEvent>> splitEvents = split(xmlEventFlux, typeName);
153 
154     return splitEvents.map(events -> {
155       Object value = unmarshal(events, outputClass);
156       LogFormatUtils.traceDebug(logger, traceOn -> {
157         String formatted = LogFormatUtils.formatValue(value, !traceOn);
158         return Hints.getLogPrefix(hints) + "Decoded [" + formatted + "]";
159       });
160       return value;
161     });
162   }
163 
164   @NonNull
165   @Override
166   @SuppressWarnings({"rawtypes", "unchecked", "cast"})
167   // XMLEventReader is Iterator<Object> on JDK 9
168   public Object decode(DataBuffer dataBuffer, ResolvableType targetType,
169       @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingException {
170 
171     try {
172       Iterator eventReader = inputFactory.createXMLEventReader(dataBuffer.asInputStream());
173       List<XMLEvent> events = new ArrayList<>();
174       eventReader.forEachRemaining(event -> events.add((XMLEvent) event));
175       return unmarshal(events, targetType.toClass());
176     } catch (XMLStreamException ex) {
177       throw Exceptions.propagate(ex);
178     } finally {
179       DataBufferUtils.release(dataBuffer);
180     }
181   }
182 
183   @NonNull
184   @Override
185   public Mono<Object> decodeToMono(@NonNull Publisher<DataBuffer> input, @NonNull ResolvableType elementType,
186       @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
187 
188     //noinspection NullableInLambdaInTransform
189     return DataBufferUtils.join(input, this.maxInMemorySize)
190         .map(dataBuffer -> decode(dataBuffer, elementType, mimeType, hints));
191   }
192 
193   private Object unmarshal(List<XMLEvent> events, Class<?> outputClass) {
194     try {
195       Unmarshaller unmarshaller = jaxbContextBuilder.buildUnmarshaller(outputClass);
196       XMLEventReader eventReader = StaxUtils.createXMLEventReader(events);
197       if (outputClass.isAnnotationPresent(XmlRootElement.class)) {
198         return unmarshaller.unmarshal(eventReader);
199       } else {
200         JAXBElement<?> jaxbElement = unmarshaller.unmarshal(eventReader, outputClass);
201         return jaxbElement.getValue();
202       }
203     } catch (UnmarshalException ex) {
204       throw new DecodingException("Could not unmarshal XML to " + outputClass, ex);
205     } catch (JAXBException ex) {
206       throw new CodecException("Invalid JAXB configuration", ex);
207     }
208   }
209 
210   /**
211    * Returns the qualified name for the given class, according to the mapping rules in the JAXB specification.
212    *
213    * @param outputClass the output class
214    * @return the q name
215    */
216   QName toQName(Class<?> outputClass) {
217     String localPart;
218     String namespaceUri;
219 
220     if (outputClass.isAnnotationPresent(XmlRootElement.class)) {
221       XmlRootElement annotation = outputClass.getAnnotation(XmlRootElement.class);
222       localPart = annotation.name();
223       namespaceUri = annotation.namespace();
224     } else if (outputClass.isAnnotationPresent(XmlType.class)) {
225       XmlType annotation = outputClass.getAnnotation(XmlType.class);
226       localPart = annotation.name();
227       namespaceUri = annotation.namespace();
228     } else {
229       throw new IllegalArgumentException("Output class [" + outputClass.getName()
230           + "] is neither annotated with @XmlRootElement nor @XmlType");
231     }
232 
233     if (JAXB_DEFAULT_ANNOTATION_VALUE.equals(localPart)) {
234       localPart = ClassUtils.getShortNameAsProperty(outputClass);
235     }
236     if (JAXB_DEFAULT_ANNOTATION_VALUE.equals(namespaceUri)) {
237       Package outputClassPackage = outputClass.getPackage();
238       if (outputClassPackage != null && outputClassPackage.isAnnotationPresent(XmlSchema.class)) {
239         XmlSchema annotation = outputClassPackage.getAnnotation(XmlSchema.class);
240         namespaceUri = annotation.namespace();
241       } else {
242         namespaceUri = XMLConstants.NULL_NS_URI;
243       }
244     }
245     return new QName(namespaceUri, localPart);
246   }
247 
248   /**
249    * Split a flux of {@link XMLEvent XMLEvents} into a flux of XMLEvent lists, one list for each branch of the tree that
250    * starts with the given qualified name. That is, given the XMLEvents shown {@linkplain XmlEventDecoder here}, and the
251    * {@code desiredName} "{@code child}", this method returns a flux of two lists, each of which containing the events
252    * of a particular branch of the tree that starts with "{@code child}".
253    * <ol>
254    * <li>The first list, dealing with the first branch of the tree:
255    * <ol>
256    * <li>{@link javax.xml.stream.events.StartElement} {@code child}</li>
257    * <li>{@link javax.xml.stream.events.Characters} {@code foo}</li>
258    * <li>{@link javax.xml.stream.events.EndElement} {@code child}</li>
259    * </ol>
260    * <li>The second list, dealing with the second branch of the tree:
261    * <ol>
262    * <li>{@link javax.xml.stream.events.StartElement} {@code child}</li>
263    * <li>{@link javax.xml.stream.events.Characters} {@code bar}</li>
264    * <li>{@link javax.xml.stream.events.EndElement} {@code child}</li>
265    * </ol>
266    * </li>
267    * </ol>
268    *
269    * @param xmlEventFlux the xml event as flux
270    * @param desiredName the desired name
271    * @return the list of xml events as flux
272    */
273   Flux<List<XMLEvent>> split(Flux<XMLEvent> xmlEventFlux, QName desiredName) {
274     return xmlEventFlux.handle(new SplitHandler(desiredName));
275   }
276 
277 
278   private static class SplitHandler implements
279       BiConsumer<XMLEvent, SynchronousSink<List<XMLEvent>>> {
280 
281     private final QName desiredName;
282 
283     private List<XMLEvent> events;
284 
285     private int elementDepth = 0;
286 
287     private int barrier = Integer.MAX_VALUE;
288 
289     /**
290      * Instantiates a new split handler.
291      *
292      * @param desiredName the desired name
293      */
294     public SplitHandler(QName desiredName) {
295       this.desiredName = desiredName;
296     }
297 
298     @Override
299     public void accept(XMLEvent event, SynchronousSink<List<XMLEvent>> sink) {
300       if (event.isStartElement()) {
301         if (this.barrier == Integer.MAX_VALUE) {
302           QName startElementName = event.asStartElement().getName();
303           if (this.desiredName.equals(startElementName)) {
304             this.events = new ArrayList<>();
305             this.barrier = this.elementDepth;
306           }
307         }
308         this.elementDepth++;
309       }
310       if (this.elementDepth > this.barrier) {
311         Assert.state(this.events != null, "No XMLEvent List");
312         this.events.add(event);
313       }
314       if (event.isEndElement()) {
315         this.elementDepth--;
316         if (this.elementDepth == this.barrier) {
317           this.barrier = Integer.MAX_VALUE;
318           Assert.state(this.events != null, "No XMLEvent List");
319           sink.next(this.events);
320         }
321       }
322     }
323   }
324 
325 }