提交 2e832622 编写于 作者: R Rossen Stoyanchev

Move spring-web-reactive classes to spring-core

上级 1022683d
......@@ -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") {
......
......@@ -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<MimeType> 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
......
......@@ -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).
......
......@@ -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<XMLEvent> events) {
return new ListBasedXMLEventReader(events);
}
/**
* Create a SAX {@link ContentHandler} that writes to the given StAX {@link XMLStreamWriter}.
* @param streamWriter the StAX stream writer
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
/**
* Default implementation of the type conversion system.
*/
package org.springframework.core.convert.support;
/*
* 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;
}
}
......@@ -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<Reso
}
private static Optional<File> getFile(Resource resource) {
if (ResourceUtils2.hasFile(resource)) {
if (ResourceUtils.hasFile(resource)) {
try {
return Optional.of(resource.getFile());
}
......
/*
* 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<MimeType> getMimeType(String filename) {
if (filename != null) {
String mimeType = fileTypeMap.getContentType(filename);
if (StringUtils.hasText(mimeType)) {
return Optional.of(parseMimeType(mimeType));
}
}
return Optional.empty();
}
}
/*
* 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<XMLEvent> events) {
return new ListBasedXMLEventReader(events);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册