diff --git a/build.gradle b/build.gradle index b90592ea917..c5b45f002c0 100644 --- a/build.gradle +++ b/build.gradle @@ -363,6 +363,10 @@ project("spring-core") { compile("commons-logging:commons-logging:1.2") optional("org.aspectj:aspectjweaver:${aspectjVersion}") optional("net.sf.jopt-simple:jopt-simple:5.0.2") + optional("org.reactivestreams:reactive-streams:1.0.0") + optional("io.projectreactor:reactor-core:${reactorCoreVersion}") + optional "io.reactivex:rxjava:${rxJavaVersion}" + optional("io.netty:netty-buffer:${nettyVersion}") testCompile("org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}") testCompile("xmlunit:xmlunit:${xmlunitVersion}") testCompile("com.fasterxml.woodstox:woodstox-core:5.0.2") { diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/AbstractDecoder.java b/spring-core/src/main/java/org/springframework/core/codec/AbstractDecoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/AbstractDecoder.java rename to spring-core/src/main/java/org/springframework/core/codec/AbstractDecoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/AbstractEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/AbstractEncoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/AbstractEncoder.java rename to spring-core/src/main/java/org/springframework/core/codec/AbstractEncoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/AbstractSingleValueEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/AbstractSingleValueEncoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/AbstractSingleValueEncoder.java rename to spring-core/src/main/java/org/springframework/core/codec/AbstractSingleValueEncoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/ByteBufferDecoder.java b/spring-core/src/main/java/org/springframework/core/codec/ByteBufferDecoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/ByteBufferDecoder.java rename to spring-core/src/main/java/org/springframework/core/codec/ByteBufferDecoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java rename to spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/CodecException.java b/spring-core/src/main/java/org/springframework/core/codec/CodecException.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/CodecException.java rename to spring-core/src/main/java/org/springframework/core/codec/CodecException.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/Decoder.java b/spring-core/src/main/java/org/springframework/core/codec/Decoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/Decoder.java rename to spring-core/src/main/java/org/springframework/core/codec/Decoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/Encoder.java b/spring-core/src/main/java/org/springframework/core/codec/Encoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/Encoder.java rename to spring-core/src/main/java/org/springframework/core/codec/Encoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/ResourceDecoder.java b/spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/ResourceDecoder.java rename to spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/ResourceEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/ResourceEncoder.java rename to spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/StringDecoder.java b/spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/StringDecoder.java rename to spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/StringEncoder.java b/spring-core/src/main/java/org/springframework/core/codec/StringEncoder.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/StringEncoder.java rename to spring-core/src/main/java/org/springframework/core/codec/StringEncoder.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/codec/package-info.java b/spring-core/src/main/java/org/springframework/core/codec/package-info.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/codec/package-info.java rename to spring-core/src/main/java/org/springframework/core/codec/package-info.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/convert/support/MonoToCompletableFutureConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/MonoToCompletableFutureConverter.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/convert/support/MonoToCompletableFutureConverter.java rename to spring-core/src/main/java/org/springframework/core/convert/support/MonoToCompletableFutureConverter.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/convert/support/ReactorToRxJava1Converter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ReactorToRxJava1Converter.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/convert/support/ReactorToRxJava1Converter.java rename to spring-core/src/main/java/org/springframework/core/convert/support/ReactorToRxJava1Converter.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBuffer.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/FlushingDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/FlushingDataBuffer.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/FlushingDataBuffer.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/FlushingDataBuffer.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/support/DataBufferUtils.java b/spring-core/src/main/java/org/springframework/core/io/buffer/support/DataBufferUtils.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/core/io/buffer/support/DataBufferUtils.java rename to spring-core/src/main/java/org/springframework/core/io/buffer/support/DataBufferUtils.java diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index 522cfbd41b1..d5594d7569c 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -16,6 +16,8 @@ package org.springframework.util; +import java.io.IOException; +import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; @@ -26,8 +28,13 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Random; +import javax.activation.FileTypeMap; +import javax.activation.MimetypesFileTypeMap; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; import org.springframework.util.MimeType.SpecificityComparator; /** @@ -49,6 +56,11 @@ public abstract class MimeTypeUtils { private static Charset US_ASCII = Charset.forName("US-ASCII"); + private static final FileTypeMap fileTypeMap; + + static { + fileTypeMap = initFileTypeMap(); + } /** * Public constant mime type that includes all media ranges (i.e. "*/*"). @@ -208,6 +220,31 @@ public abstract class MimeTypeUtils { TEXT_XML = MimeType.valueOf(TEXT_XML_VALUE); } + private static FileTypeMap initFileTypeMap() { + // See if we can find the extended mime.types from the context-support module... + Resource mappingLocation = new ClassPathResource("org/springframework/mail/javamail/mime.types"); + if (mappingLocation.exists()) { + InputStream inputStream = null; + try { + inputStream = mappingLocation.getInputStream(); + return new MimetypesFileTypeMap(inputStream); + } + catch (IOException ex) { + // ignore + } + finally { + if (inputStream != null) { + try { + inputStream.close(); + } + catch (IOException ex) { + // ignore + } + } + } + } + return FileTypeMap.getDefaultFileTypeMap(); + } /** * Parse the given String into a single {@code MimeType}. @@ -285,6 +322,22 @@ public abstract class MimeTypeUtils { return result; } + /** + * Returns the {@code MimeType} of the given file name, using the Java Activation + * Framework. + * @param filename the filename whose mime type is to be found + * @return the mime type, if any + */ + public static Optional getMimeType(String filename) { + if (filename != null) { + String mimeType = fileTypeMap.getContentType(filename); + if (StringUtils.hasText(mimeType)) { + return Optional.of(parseMimeType(mimeType)); + } + } + return Optional.empty(); + } + /** * Return a string representation of the given list of {@code MimeType} objects. * @param mimeTypes the string to parse diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index 3ea92707e13..d02da53582f 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -18,12 +18,18 @@ package org.springframework.util; import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.DescriptiveResource; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; + /** * Utility methods for resolving resource locations to files in the * file system. Mainly for internal use within the framework. @@ -296,6 +302,32 @@ public abstract class ResourceUtils { url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION)); } + /** + * Indicates whether the given resource has a file, so that {@link + * Resource#getFile()} + * can be called without an {@link java.io.IOException}. + * @param resource the resource to check + * @return {@code true} if the given resource has a file; {@code false} otherwise + * @since 5.0 + */ + public static boolean hasFile(Resource resource) { + Assert.notNull(resource, "'resource' must not be null"); + + // the following Resource implementations do not support getURI/getFile + if (resource instanceof ByteArrayResource || + resource instanceof DescriptiveResource || + resource instanceof InputStreamResource) { + return false; + } + try { + URI resourceUri = resource.getURI(); + return URL_PROTOCOL_FILE.equals(resourceUri.getScheme()); + } + catch (IOException ignored) { + } + return false; + } + /** * Extract the URL for the actual jar file from the given URL * (which may point to a resource in a jar file or to a jar file itself). diff --git a/spring-web-reactive/src/main/java/org/springframework/util/xml/AbstractXMLEventReader.java b/spring-core/src/main/java/org/springframework/util/xml/AbstractXMLEventReader.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/util/xml/AbstractXMLEventReader.java rename to spring-core/src/main/java/org/springframework/util/xml/AbstractXMLEventReader.java diff --git a/spring-web-reactive/src/main/java/org/springframework/util/xml/ListBasedXMLEventReader.java b/spring-core/src/main/java/org/springframework/util/xml/ListBasedXMLEventReader.java similarity index 100% rename from spring-web-reactive/src/main/java/org/springframework/util/xml/ListBasedXMLEventReader.java rename to spring-core/src/main/java/org/springframework/util/xml/ListBasedXMLEventReader.java diff --git a/spring-core/src/main/java/org/springframework/util/xml/StaxUtils.java b/spring-core/src/main/java/org/springframework/util/xml/StaxUtils.java index eb98be07013..5227f6e8b68 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/StaxUtils.java +++ b/spring-core/src/main/java/org/springframework/util/xml/StaxUtils.java @@ -16,12 +16,14 @@ package org.springframework.util.xml; +import java.util.List; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; +import javax.xml.stream.events.XMLEvent; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.stax.StAXResult; @@ -211,6 +213,16 @@ public abstract class StaxUtils { } } + /** + * Create a {@link XMLEventReader} from the given list of {@link XMLEvent}. + * @param events the list of {@link XMLEvent}s. + * @return an {@code XMLEventReader} that reads from the given events + * @since 5.0 + */ + public static XMLEventReader createXMLEventReader(List events) { + return new ListBasedXMLEventReader(events); + } + /** * Create a SAX {@link ContentHandler} that writes to the given StAX {@link XMLStreamWriter}. * @param streamWriter the StAX stream writer diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/ByteBufferDecoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/ByteBufferDecoderTests.java similarity index 88% rename from spring-web-reactive/src/test/java/org/springframework/core/codec/ByteBufferDecoderTests.java rename to spring-core/src/test/java/org/springframework/core/codec/ByteBufferDecoderTests.java index 8162200f347..18017342a7f 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/ByteBufferDecoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/ByteBufferDecoderTests.java @@ -26,7 +26,7 @@ import reactor.core.test.TestSubscriber; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.MediaType; +import org.springframework.util.MimeTypeUtils; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -40,12 +40,9 @@ public class ByteBufferDecoderTests extends AbstractDataBufferAllocatingTestCase @Test public void canDecode() { - assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), - MediaType.TEXT_PLAIN)); - assertFalse(this.decoder - .canDecode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN)); - assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), - MediaType.APPLICATION_JSON)); + assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.TEXT_PLAIN)); + assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/ByteBufferEncoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/ByteBufferEncoderTests.java similarity index 85% rename from spring-web-reactive/src/test/java/org/springframework/core/codec/ByteBufferEncoderTests.java rename to spring-core/src/test/java/org/springframework/core/codec/ByteBufferEncoderTests.java index 157724d3b17..656fe6977b6 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/ByteBufferEncoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/ByteBufferEncoderTests.java @@ -28,9 +28,11 @@ import reactor.core.test.TestSubscriber; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.MediaType; +import org.springframework.util.MimeTypeUtils; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Sebastien Deleuze @@ -46,12 +48,9 @@ public class ByteBufferEncoderTests extends AbstractDataBufferAllocatingTestCase @Test public void canEncode() { - assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), - MediaType.TEXT_PLAIN)); - assertFalse(this.encoder - .canEncode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN)); - assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), - MediaType.APPLICATION_JSON)); + assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.TEXT_PLAIN)); + assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java similarity index 76% rename from spring-web-reactive/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java rename to spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java index 15db097214a..8f6e6d3ef4c 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java @@ -28,10 +28,12 @@ import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.MediaType; +import org.springframework.util.MimeTypeUtils; import org.springframework.util.StreamUtils; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Arjen Poutsma @@ -42,17 +44,14 @@ public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase { @Test public void canDecode() throws Exception { - assertTrue( - this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class), - MediaType.TEXT_PLAIN)); - assertTrue( - this.decoder.canDecode(ResolvableType.forClass(ByteArrayResource.class), - MediaType.TEXT_PLAIN)); - assertTrue(this.decoder.canDecode(ResolvableType.forClass(Resource.class), - MediaType.TEXT_PLAIN)); - assertTrue( - this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class), - MediaType.APPLICATION_JSON)); + assertTrue(this.decoder.canDecode( + ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.decoder.canDecode( + ResolvableType.forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.decoder.canDecode( + ResolvableType.forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.decoder.canDecode( + ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/ResourceEncoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/ResourceEncoderTests.java similarity index 78% rename from spring-web-reactive/src/test/java/org/springframework/core/codec/ResourceEncoderTests.java rename to spring-core/src/test/java/org/springframework/core/codec/ResourceEncoderTests.java index b27f987a2a5..24510019967 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/ResourceEncoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/ResourceEncoderTests.java @@ -29,7 +29,7 @@ import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.MediaType; +import org.springframework.util.MimeTypeUtils; import static org.junit.Assert.assertTrue; @@ -42,17 +42,14 @@ public class ResourceEncoderTests extends AbstractDataBufferAllocatingTestCase { @Test public void canEncode() throws Exception { - assertTrue( - this.encoder.canEncode(ResolvableType.forClass(InputStreamResource.class), - MediaType.TEXT_PLAIN)); - assertTrue( - this.encoder.canEncode(ResolvableType.forClass(ByteArrayResource.class), - MediaType.TEXT_PLAIN)); - assertTrue(this.encoder.canEncode(ResolvableType.forClass(Resource.class), - MediaType.TEXT_PLAIN)); - assertTrue( - this.encoder.canEncode(ResolvableType.forClass(InputStreamResource.class), - MediaType.APPLICATION_JSON)); + assertTrue(this.encoder.canEncode( + ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.encoder.canEncode( + ResolvableType.forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.encoder.canEncode( + ResolvableType.forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.encoder.canEncode( + ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/StringDecoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/StringDecoderTests.java similarity index 93% rename from spring-web-reactive/src/test/java/org/springframework/core/codec/StringDecoderTests.java rename to spring-core/src/test/java/org/springframework/core/codec/StringDecoderTests.java index 43492ae6a9e..d807b80b2b1 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/StringDecoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/StringDecoderTests.java @@ -24,7 +24,7 @@ import reactor.core.test.TestSubscriber; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.MediaType; +import org.springframework.util.MimeTypeUtils; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -41,11 +41,11 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase { @Test public void canDecode() { - assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN)); - assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_HTML)); - assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MediaType.APPLICATION_JSON)); - assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN)); - assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class), MediaType.APPLICATION_JSON)); + assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); + assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_HTML)); + assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON)); + assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); + assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class), MimeTypeUtils.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/core/codec/StringEncoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/StringEncoderTests.java similarity index 86% rename from spring-web-reactive/src/test/java/org/springframework/core/codec/StringEncoderTests.java rename to spring-core/src/test/java/org/springframework/core/codec/StringEncoderTests.java index 2a4e60f18c2..1f62c66248e 100644 --- a/spring-web-reactive/src/test/java/org/springframework/core/codec/StringEncoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/StringEncoderTests.java @@ -28,7 +28,7 @@ import reactor.core.test.TestSubscriber; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; import org.springframework.core.io.buffer.support.DataBufferUtils; -import org.springframework.http.MediaType; +import org.springframework.util.MimeTypeUtils; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -48,12 +48,9 @@ public class StringEncoderTests extends AbstractDataBufferAllocatingTestCase { @Test public void canWrite() { - assertTrue(this.encoder - .canEncode(ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN)); - assertFalse(this.encoder - .canEncode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN)); - assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), - MediaType.APPLICATION_JSON)); + assertTrue(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); + assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN)); + assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON)); } @Test diff --git a/spring-web-reactive/src/test/java/org/springframework/core/convert/support/MonoToCompletableFutureConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/MonoToCompletableFutureConverterTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/convert/support/MonoToCompletableFutureConverterTests.java rename to spring-core/src/test/java/org/springframework/core/convert/support/MonoToCompletableFutureConverterTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/convert/support/ReactorToRxJava1ConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/ReactorToRxJava1ConverterTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/convert/support/ReactorToRxJava1ConverterTests.java rename to spring-core/src/test/java/org/springframework/core/convert/support/ReactorToRxJava1ConverterTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/io/buffer/AbstractDataBufferAllocatingTestCase.java b/spring-core/src/test/java/org/springframework/core/io/buffer/AbstractDataBufferAllocatingTestCase.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/io/buffer/AbstractDataBufferAllocatingTestCase.java rename to spring-core/src/test/java/org/springframework/core/io/buffer/AbstractDataBufferAllocatingTestCase.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java rename to spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/io/buffer/PooledDataBufferTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/PooledDataBufferTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/io/buffer/PooledDataBufferTests.java rename to spring-core/src/test/java/org/springframework/core/io/buffer/PooledDataBufferTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtils.java b/spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtils.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtils.java rename to spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtils.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtilsTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtilsTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtilsTests.java rename to spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferTestUtilsTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/core/io/buffer/support/DataBufferUtilsTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferUtilsTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/core/io/buffer/support/DataBufferUtilsTests.java rename to spring-core/src/test/java/org/springframework/core/io/buffer/support/DataBufferUtilsTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/util/xml/ListBasedXMLEventReaderTests.java b/spring-core/src/test/java/org/springframework/util/xml/ListBasedXMLEventReaderTests.java similarity index 100% rename from spring-web-reactive/src/test/java/org/springframework/util/xml/ListBasedXMLEventReaderTests.java rename to spring-core/src/test/java/org/springframework/util/xml/ListBasedXMLEventReaderTests.java diff --git a/spring-web-reactive/src/test/resources/org/springframework/core/io/buffer/support/DataBufferUtilsTests.txt b/spring-core/src/test/resources/org/springframework/core/io/buffer/support/DataBufferUtilsTests.txt similarity index 100% rename from spring-web-reactive/src/test/resources/org/springframework/core/io/buffer/support/DataBufferUtilsTests.txt rename to spring-core/src/test/resources/org/springframework/core/io/buffer/support/DataBufferUtilsTests.txt diff --git a/spring-web-reactive/src/main/java/org/springframework/core/convert/support/package-info.java b/spring-web-reactive/src/main/java/org/springframework/core/convert/support/package-info.java deleted file mode 100644 index 2a23abd9f00..00000000000 --- a/spring-web-reactive/src/main/java/org/springframework/core/convert/support/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Default implementation of the type conversion system. - */ -package org.springframework.core.convert.support; diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/support/ResourceUtils2.java b/spring-web-reactive/src/main/java/org/springframework/core/io/support/ResourceUtils2.java deleted file mode 100644 index 94a3af3f14f..00000000000 --- a/spring-web-reactive/src/main/java/org/springframework/core/io/support/ResourceUtils2.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2002-2016 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. - * 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. - */ - -package org.springframework.core.io.support; - -import java.io.IOException; -import java.net.URI; - -import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.DescriptiveResource; -import org.springframework.core.io.InputStreamResource; -import org.springframework.core.io.Resource; -import org.springframework.util.Assert; -import org.springframework.util.ResourceUtils; - -/** - * @author Arjen Poutsma - */ -public abstract class ResourceUtils2 { - - /** - * Indicates whether the given resource has a file, so that {@link - * Resource#getFile()} - * can be called without an {@link java.io.IOException}. - * @param resource the resource to check - * @return {@code true} if the given resource has a file; {@code false} otherwise - */ - // TODO: refactor into Resource.hasFile() method - public static boolean hasFile(Resource resource) { - Assert.notNull(resource, "'resource' must not be null"); - - // the following Resource implementations do not support getURI/getFile - if (resource instanceof ByteArrayResource || - resource instanceof DescriptiveResource || - resource instanceof InputStreamResource) { - return false; - } - try { - URI resourceUri = resource.getURI(); - return ResourceUtils.URL_PROTOCOL_FILE.equals(resourceUri.getScheme()); - } - catch (IOException ignored) { - } - return false; - } -} diff --git a/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/ResourceHttpMessageConverter.java b/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/ResourceHttpMessageConverter.java index d98e7c1a357..0e228073fc4 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/ResourceHttpMessageConverter.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/ResourceHttpMessageConverter.java @@ -29,13 +29,13 @@ import org.springframework.core.codec.ResourceDecoder; import org.springframework.core.codec.ResourceEncoder; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; -import org.springframework.core.io.support.ResourceUtils2; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.http.support.MediaTypeUtils; import org.springframework.util.MimeTypeUtils2; +import org.springframework.util.ResourceUtils; /** * Implementation of {@link HttpMessageConverter} that can read and write @@ -116,7 +116,7 @@ public class ResourceHttpMessageConverter extends CodecHttpMessageConverter getFile(Resource resource) { - if (ResourceUtils2.hasFile(resource)) { + if (ResourceUtils.hasFile(resource)) { try { return Optional.of(resource.getFile()); } diff --git a/spring-web-reactive/src/main/java/org/springframework/util/MimeTypeUtils2.java b/spring-web-reactive/src/main/java/org/springframework/util/MimeTypeUtils2.java deleted file mode 100644 index 6ef2f9fb6cb..00000000000 --- a/spring-web-reactive/src/main/java/org/springframework/util/MimeTypeUtils2.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2002-2016 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. - * 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. - */ - -package org.springframework.util; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Optional; -import javax.activation.FileTypeMap; -import javax.activation.MimetypesFileTypeMap; - -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; - -/** - * TODO: merge into {@link MimeTypeUtils}, and use wherever we still have a runtime check - * to see if JAF is available (i.e. jafPresent). Since JAF has been included in the JDK - * since 1.6, we don't - * need that check anymore. (i.e. {@link org.springframework.http.converter.ResourceHttpMessageConverter} - * @author Arjen Poutsma - */ -public abstract class MimeTypeUtils2 extends MimeTypeUtils { - - private static final FileTypeMap fileTypeMap; - - static { - fileTypeMap = loadFileTypeMapFromContextSupportModule(); - } - - private static FileTypeMap loadFileTypeMapFromContextSupportModule() { - // See if we can find the extended mime.types from the context-support module... - Resource mappingLocation = - new ClassPathResource("org/springframework/mail/javamail/mime.types"); - if (mappingLocation.exists()) { - InputStream inputStream = null; - try { - inputStream = mappingLocation.getInputStream(); - return new MimetypesFileTypeMap(inputStream); - } - catch (IOException ex) { - // ignore - } - finally { - if (inputStream != null) { - try { - inputStream.close(); - } - catch (IOException ex) { - // ignore - } - } - } - } - return FileTypeMap.getDefaultFileTypeMap(); - } - - /** - * Returns the {@code MimeType} of the given file name, using the Java Activation - * Framework. - * @param filename the filename whose mime type is to be found - * @return the mime type, if any - */ - public static Optional getMimeType(String filename) { - if (filename != null) { - String mimeType = fileTypeMap.getContentType(filename); - if (StringUtils.hasText(mimeType)) { - return Optional.of(parseMimeType(mimeType)); - } - } - return Optional.empty(); - } - - -} diff --git a/spring-web-reactive/src/main/java/org/springframework/util/xml/StaxUtils2.java b/spring-web-reactive/src/main/java/org/springframework/util/xml/StaxUtils2.java deleted file mode 100644 index 7d4c2c3f8f0..00000000000 --- a/spring-web-reactive/src/main/java/org/springframework/util/xml/StaxUtils2.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2002-2016 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. - * 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. - */ - -package org.springframework.util.xml; - -import java.util.List; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.events.XMLEvent; - -/** - * TODO: to be merged with {@link StaxUtils}. - * @author Arjen Poutsma - */ -public abstract class StaxUtils2 { - - /** - * Create a {@link XMLEventReader} from the given list of {@link XMLEvent}. - * @param events the list of {@link XMLEvent}s. - * @return an {@code XMLEventReader} that reads from the given events - */ - public static XMLEventReader createXMLEventReader(List events) { - return new ListBasedXMLEventReader(events); - } - -}