Polishing

This commit is contained in:
Juergen Hoeller
2020-10-07 14:36:38 +02:00
parent 6d137b56c1
commit f24714f852
12 changed files with 107 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -151,21 +151,6 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
}
/**
* Returns the default content type for the payload. Called when
* {@link #toMessage(Object, MessageHeaders)} is invoked without message headers or
* without a content type header.
* <p>By default, this returns the first element of the {@link #getSupportedMimeTypes()
* supportedMimeTypes}, if any. Can be overridden in sub-classes.
* @param payload the payload being converted to message
* @return the content type, or {@code null} if not known
*/
@Nullable
protected MimeType getDefaultContentType(Object payload) {
List<MimeType> mimeTypes = getSupportedMimeTypes();
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
}
@Override
@Nullable
public final Object fromMessage(Message<?> message, Class<?> targetClass) {
@@ -181,10 +166,6 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
return convertFromInternal(message, targetClass, conversionHint);
}
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
}
@Override
@Nullable
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
@@ -224,6 +205,11 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
return builder.build();
}
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
}
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
return (supports(payload.getClass()) && supportsMimeType(headers));
}
@@ -249,6 +235,22 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
return (headers != null && this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
}
/**
* Return the default content type for the payload. Called when
* {@link #toMessage(Object, MessageHeaders)} is invoked without
* message headers or without a content type header.
* <p>By default, this returns the first element of the
* {@link #getSupportedMimeTypes() supportedMimeTypes}, if any.
* Can be overridden in subclasses.
* @param payload the payload being converted to a message
* @return the content type, or {@code null} if not known
*/
@Nullable
protected MimeType getDefaultContentType(Object payload) {
List<MimeType> mimeTypes = getSupportedMimeTypes();
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
}
/**
* Whether the given class is supported by this converter.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -684,8 +684,10 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
if (conn != null) {
conn.send(HEARTBEAT).addCallback(
new ListenableFutureCallback<Void>() {
@Override
public void onSuccess(@Nullable Void result) {
}
@Override
public void onFailure(Throwable ex) {
handleFailure(ex);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -53,14 +53,14 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
/**
* A protected constructor to create new headers.
* Protected constructor to create a new instance.
*/
protected NativeMessageHeaderAccessor() {
this((Map<String, List<String>>) null);
}
/**
* A protected constructor to create new headers.
* Protected constructor to create an instance with the given native headers.
* @param nativeHeaders native headers to create the message with (may be {@code null})
*/
protected NativeMessageHeaderAccessor(@Nullable Map<String, List<String>> nativeHeaders) {
@@ -70,7 +70,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
}
/**
* A protected constructor accepting the headers of an existing message to copy.
* Protected constructor that copies headers from another message.
*/
protected NativeMessageHeaderAccessor(@Nullable Message<?> message) {
super(message);
@@ -85,6 +85,10 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
}
}
/**
* Subclasses can use this method to access the "native" headers sub-map.
*/
@SuppressWarnings("unchecked")
@Nullable
protected Map<String, List<String>> getNativeHeaders() {
@@ -92,7 +96,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
}
/**
* Return a copy of the native header values or an empty map.
* Return a copy of the native headers sub-map, or an empty map.
*/
public Map<String, List<String>> toNativeHeaderMap() {
Map<String, List<String>> map = getNativeHeaders();
@@ -114,6 +118,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
/**
* Whether the native header map contains the give header name.
* @param headerName the name of the header
*/
public boolean containsNativeHeader(String headerName) {
Map<String, List<String>> map = getNativeHeaders();
@@ -121,8 +126,9 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
}
/**
* Return all values for the specified native header.
* or {@code null} if none.
* Return all values for the specified native header, if present.
* @param headerName the name of the header
* @return the associated values, or {@code null} if none
*/
@Nullable
public List<String> getNativeHeader(String headerName) {
@@ -131,15 +137,16 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
}
/**
* Return the first value for the specified native header,
* or {@code null} if none.
* Return the first value for the specified native header, if present.
* @param headerName the name of the header
* @return the associated value, or {@code null} if none
*/
@Nullable
public String getFirstNativeHeader(String headerName) {
Map<String, List<String>> map = getNativeHeaders();
if (map != null) {
List<String> values = map.get(headerName);
if (values != null) {
if (!CollectionUtils.isEmpty(values)) {
return values.get(0);
}
}
@@ -148,6 +155,8 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
/**
* Set the specified native header value replacing existing values.
* <p>In order for this to work, the accessor must be {@link #isMutable()
* mutable}. See {@link MessageHeaderAccessor} for details.
*/
public void setNativeHeader(String name, @Nullable String value) {
Assert.state(isMutable(), "Already immutable");
@@ -173,6 +182,10 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
/**
* Add the specified native header value to existing values.
* <p>In order for this to work, the accessor must be {@link #isMutable()
* mutable}. See {@link MessageHeaderAccessor} for details.
* @param name the name of the header
* @param value the header value to set
*/
public void addNativeHeader(String name, @Nullable String value) {
Assert.state(isMutable(), "Already immutable");
@@ -189,6 +202,10 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
setModified(true);
}
/**
* Add the specified native headers to existing values.
* @param headers the headers to set
*/
public void addNativeHeaders(@Nullable MultiValueMap<String, String> headers) {
if (headers == null) {
return;
@@ -196,23 +213,38 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
headers.forEach((key, values) -> values.forEach(value -> addNativeHeader(key, value)));
}
/**
* Remove the specified native header value replacing existing values.
* <p>In order for this to work, the accessor must be {@link #isMutable()
* mutable}. See {@link MessageHeaderAccessor} for details.
* @param headerName the name of the header
* @return the associated values, or {@code null} if the header was not present
*/
@Nullable
public List<String> removeNativeHeader(String name) {
public List<String> removeNativeHeader(String headerName) {
Assert.state(isMutable(), "Already immutable");
Map<String, List<String>> nativeHeaders = getNativeHeaders();
if (nativeHeaders == null) {
if (CollectionUtils.isEmpty(nativeHeaders)) {
return null;
}
return nativeHeaders.remove(name);
return nativeHeaders.remove(headerName);
}
/**
* Return the first value for the specified native header,
* or {@code null} if none.
* @param headerName the name of the header
* @param headers the headers map to introspect
* @return the associated value, or {@code null} if none
*/
@SuppressWarnings("unchecked")
@Nullable
public static String getFirstNativeHeader(String headerName, Map<String, Object> headers) {
Map<String, List<String>> map = (Map<String, List<String>>) headers.get(NATIVE_HEADERS);
if (map != null) {
List<String> values = map.get(headerName);
if (values != null) {
if (!CollectionUtils.isEmpty(values)) {
return values.get(0);
}
}