Browse Source

Polishing

(cherry picked from commit 6fef8b9)
pull/545/head
Juergen Hoeller 12 years ago
parent
commit
4d3aa59028
  1. 17
      spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java
  2. 13
      spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverter.java
  3. 34
      spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java
  4. 9
      spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java

17
spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

@ -64,6 +64,7 @@ import javax.xml.transform.Result; @@ -64,6 +64,7 @@ import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
@ -82,7 +83,6 @@ import org.springframework.beans.factory.InitializingBean; @@ -82,7 +83,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.JdkVersion;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.oxm.GenericMarshaller;
import org.springframework.oxm.GenericUnmarshaller;
import org.springframework.oxm.MarshallingFailureException;
@ -102,7 +102,7 @@ import org.springframework.util.StringUtils; @@ -102,7 +102,7 @@ import org.springframework.util.StringUtils;
import org.springframework.util.xml.StaxUtils;
/**
* Implementation of the {@code Marshaller} interface for JAXB 2.0.
* Implementation of the {@code GenericMarshaller} interface for JAXB 2.0+.
*
* <p>The typical usage will be to set either the "contextPath" or the "classesToBeBound"
* property on this bean, possibly customize the marshaller and unmarshaller by setting
@ -110,6 +110,7 @@ import org.springframework.util.xml.StaxUtils; @@ -110,6 +110,7 @@ import org.springframework.util.xml.StaxUtils;
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @since 3.0
* @see #setContextPath(String)
* @see #setClassesToBeBound(Class[])
@ -169,8 +170,6 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -169,8 +170,6 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
private ClassLoader beanClassLoader;
private ResourceLoader resourceLoader;
private final Object jaxbContextMonitor = new Object();
private volatile JAXBContext jaxbContext;
@ -299,7 +298,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -299,7 +298,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
* Specify the {@code XmlAdapter}s to be registered with the JAXB {@code Marshaller}
* and {@code Unmarshaller}
*/
public void setAdapters(XmlAdapter<?, ?>[] adapters) {
public void setAdapters(XmlAdapter<?, ?>... adapters) {
this.adapters = adapters;
}
@ -313,7 +312,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -313,7 +312,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
/**
* Set the schema resources to use for validation.
*/
public void setSchemas(Resource[] schemaResources) {
public void setSchemas(Resource... schemaResources) {
this.schemaResources = schemaResources;
}
@ -887,7 +886,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -887,7 +886,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
private final MimeContainer mimeContainer;
private Jaxb2AttachmentMarshaller(MimeContainer mimeContainer) {
public Jaxb2AttachmentMarshaller(MimeContainer mimeContainer) {
this.mimeContainer = mimeContainer;
}
@ -941,7 +940,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -941,7 +940,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
private final MimeContainer mimeContainer;
private Jaxb2AttachmentUnmarshaller(MimeContainer mimeContainer) {
public Jaxb2AttachmentUnmarshaller(MimeContainer mimeContainer) {
this.mimeContainer = mimeContainer;
}
@ -991,7 +990,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -991,7 +990,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
private final int length;
private ByteArrayDataSource(String contentType, byte[] data, int offset, int length) {
public ByteArrayDataSource(String contentType, byte[] data, int offset, int length) {
this.contentType = contentType;
this.data = data;
this.offset = offset;

13
spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,6 @@ import java.util.LinkedHashSet; @@ -26,7 +26,6 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
@ -54,6 +53,7 @@ import org.springframework.http.converter.HttpMessageNotReadableException; @@ -54,6 +53,7 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
* does not support writing.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 3.2
*/
public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
@ -61,6 +61,7 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection> @@ -61,6 +61,7 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
private final XMLInputFactory inputFactory = createXmlInputFactory();
/**
* Always returns {@code false} since Jaxb2CollectionHttpMessageConverter
* required generic type information in order to read a Collection.
@ -121,6 +122,7 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection> @@ -121,6 +122,7 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
throw new UnsupportedOperationException();
}
@SuppressWarnings("unchecked")
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
@ -162,7 +164,6 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection> @@ -162,7 +164,6 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
/**
* Create a Collection of the given type, with the given initial capacity
* (if supported by the Collection type).
*
* @param collectionClass the type of Collection to instantiate
* @return the created Collection instance
*/
@ -220,14 +221,12 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection> @@ -220,14 +221,12 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
/**
* Create a {@code XMLInputFactory} that this converter will use to create {@link
* javax.xml.stream.XMLStreamReader} and {@link javax.xml.stream.XMLEventReader} objects.
* <p/> Can be overridden in subclasses, adding further initialization of the factory.
* <p>Can be overridden in subclasses, adding further initialization of the factory.
* The resulting factory is cached, so this method will only be called once.
*
* @return the created factory
*/
protected XMLInputFactory createXmlInputFactory() {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
return inputFactory;
}

34
spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,6 +32,12 @@ import javax.xml.transform.Source; @@ -32,6 +32,12 @@ import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@ -39,25 +45,22 @@ import org.springframework.http.converter.HttpMessageConversionException; @@ -39,25 +45,22 @@ import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.ClassUtils;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that can read
* and write XML using JAXB2.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
* that can read and write XML using JAXB2.
*
* <p>This converter can read classes annotated with {@link XmlRootElement} and {@link XmlType}, and write classes
* annotated with with {@link XmlRootElement}, or subclasses thereof.
* <p>This converter can read classes annotated with {@link XmlRootElement} and {@link XmlType},
* and write classes annotated with with {@link XmlRootElement}, or subclasses thereof.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 3.0
*/
public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessageConverter<Object> {
private boolean processExternalEntities = false;
private boolean processExternalEntities = false;
/**
@ -68,6 +71,9 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa @@ -68,6 +71,9 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
this.processExternalEntities = processExternalEntities;
}
/**
* Returns the configured value for whether XML external entities are allowed.
*/
public boolean isProcessExternalEntities() {
return this.processExternalEntities;
}
@ -81,7 +87,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa @@ -81,7 +87,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType);
return (AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType));
}
@Override
@ -99,7 +105,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa @@ -99,7 +105,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
return unmarshaller.unmarshal(source);
}
else {
JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz);
JAXBElement<?> jaxbElement = unmarshaller.unmarshal(source, clazz);
return jaxbElement.getValue();
}
}
@ -138,7 +144,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa @@ -138,7 +144,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
try {
Class clazz = ClassUtils.getUserClass(o);
Class<?> clazz = ClassUtils.getUserClass(o);
Marshaller marshaller = createMarshaller(clazz);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);

9
spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -57,6 +57,7 @@ import org.springframework.util.StreamUtils; @@ -57,6 +57,7 @@ import org.springframework.util.StreamUtils;
* that can read and write {@link Source} objects.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 3.0
*/
public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMessageConverter<T> {
@ -94,12 +95,13 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe @@ -94,12 +95,13 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
}
/**
* @return the configured value for whether XML external entities are allowed.
* Returns the configured value for whether XML external entities are allowed.
*/
public boolean isProcessExternalEntities() {
return this.processExternalEntities;
}
@Override
public boolean supports(Class<?> clazz) {
return SUPPORTED_CLASSES.contains(clazz);
@ -150,8 +152,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe @@ -150,8 +152,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
private SAXSource readSAXSource(InputStream body) throws IOException {
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature(
"http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
reader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
byte[] bytes = StreamUtils.copyToByteArray(body);
if (!isProcessExternalEntities()) {
reader.setEntityResolver(NO_OP_ENTITY_RESOLVER);

Loading…
Cancel
Save