Browse Source

Polishing

pull/1186/head
Juergen Hoeller 9 years ago
parent
commit
c64f39943f
  1. 5
      spring-beans/src/main/java/org/springframework/beans/factory/config/YamlMapFactoryBean.java
  2. 99
      spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/BufferingStompDecoder.java
  3. 5
      spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java

5
spring-beans/src/main/java/org/springframework/beans/factory/config/YamlMapFactoryBean.java

@ -108,15 +108,14 @@ public class YamlMapFactoryBean extends YamlProcessor implements FactoryBean<Map
/** /**
* Template method that subclasses may override to construct the object * Template method that subclasses may override to construct the object
* returned by this factory. The default implementation returns the * returned by this factory.
* merged Map instance.
* <p>Invoked lazily the first time {@link #getObject()} is invoked in * <p>Invoked lazily the first time {@link #getObject()} is invoked in
* case of a shared singleton; else, on each {@link #getObject()} call. * case of a shared singleton; else, on each {@link #getObject()} call.
* <p>The default implementation returns the merged {@code Map} instance.
* @return the object returned by this factory * @return the object returned by this factory
* @see #process(java.util.Map, MatchCallback) * @see #process(java.util.Map, MatchCallback)
*/ */
protected Map<String, Object> createMap() { protected Map<String, Object> createMap() {
Map<String, Object> result = new LinkedHashMap<>(); Map<String, Object> result = new LinkedHashMap<>();
process((properties, map) -> merge(result, map)); process((properties, map) -> merge(result, map));
return result; return result;

99
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/BufferingStompDecoder.java

@ -16,7 +16,6 @@
package org.springframework.messaging.simp.stomp; package org.springframework.messaging.simp.stomp;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -28,7 +27,6 @@ import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
/** /**
* An extension of {@link org.springframework.messaging.simp.stomp.StompDecoder} * An extension of {@link org.springframework.messaging.simp.stomp.StompDecoder}
* that buffers content remaining in the input ByteBuffer after the parent * that buffers content remaining in the input ByteBuffer after the parent
@ -45,6 +43,7 @@ import org.springframework.util.MultiValueMap;
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0.3 * @since 4.0.3
* @see StompDecoder
*/ */
public class BufferingStompDecoder { public class BufferingStompDecoder {
@ -52,84 +51,63 @@ public class BufferingStompDecoder {
private final int bufferSizeLimit; private final int bufferSizeLimit;
private final Queue<ByteBuffer> chunks = new LinkedBlockingQueue<>(); private final Queue<ByteBuffer> chunks = new LinkedBlockingQueue<ByteBuffer>();
private volatile Integer expectedContentLength; private volatile Integer expectedContentLength;
/**
* Create a new {@code BufferingStompDecoder} wrapping the given {@code StompDecoder}.
* @param stompDecoder the target decoder to wrap
* @param bufferSizeLimit the buffer size limit
*/
public BufferingStompDecoder(StompDecoder stompDecoder, int bufferSizeLimit) { public BufferingStompDecoder(StompDecoder stompDecoder, int bufferSizeLimit) {
Assert.notNull(stompDecoder, "'stompDecoder' is required"); Assert.notNull(stompDecoder, "StompDecoder is required");
Assert.isTrue(bufferSizeLimit > 0, "Buffer size must be greater than 0"); Assert.isTrue(bufferSizeLimit > 0, "Buffer size limit must be greater than 0");
this.stompDecoder = stompDecoder; this.stompDecoder = stompDecoder;
this.bufferSizeLimit = bufferSizeLimit; this.bufferSizeLimit = bufferSizeLimit;
} }
/** /**
* Return the wrapped * Return the wrapped {@link StompDecoder}.
* {@link org.springframework.messaging.simp.stomp.StompDecoder}.
*/ */
public StompDecoder getStompDecoder() { public final StompDecoder getStompDecoder() {
return this.stompDecoder; return this.stompDecoder;
} }
/** /**
* Return the configured buffer size limit. * Return the configured buffer size limit.
*/ */
public int getBufferSizeLimit() { public final int getBufferSizeLimit() {
return this.bufferSizeLimit; return this.bufferSizeLimit;
} }
/**
* Calculate the current buffer size.
*/
public int getBufferSize() {
int size = 0;
for (ByteBuffer buffer : this.chunks) {
size = size + buffer.remaining();
}
return size;
}
/**
* Get the expected content length of the currently buffered, incomplete STOMP frame.
*/
public Integer getExpectedContentLength() {
return this.expectedContentLength;
}
/** /**
* Decodes one or more STOMP frames from the given {@code ByteBuffer} into a * Decodes one or more STOMP frames from the given {@code ByteBuffer} into a
* list of {@link Message}s. * list of {@link Message}s.
*
* <p>If there was enough data to parse a "content-length" header, then the * <p>If there was enough data to parse a "content-length" header, then the
* value is used to determine how much more data is needed before a new * value is used to determine how much more data is needed before a new
* attempt to decode is made. * attempt to decode is made.
*
* <p>If there was not enough data to parse the "content-length", or if there * <p>If there was not enough data to parse the "content-length", or if there
* is "content-length" header, every subsequent call to decode attempts to * is "content-length" header, every subsequent call to decode attempts to
* parse again with all available data. Therefore the presence of a "content-length" * parse again with all available data. Therefore the presence of a "content-length"
* header helps to optimize the decoding of large messages. * header helps to optimize the decoding of large messages.
*
* @param newBuffer a buffer containing new data to decode * @param newBuffer a buffer containing new data to decode
*
* @return decoded messages or an empty list * @return decoded messages or an empty list
* @throws StompConversionException raised in case of decoding issues * @throws StompConversionException raised in case of decoding issues
*/ */
public List<Message<byte[]>> decode(ByteBuffer newBuffer) { public List<Message<byte[]>> decode(ByteBuffer newBuffer) {
this.chunks.add(newBuffer); this.chunks.add(newBuffer);
checkBufferLimits(); checkBufferLimits();
if (getExpectedContentLength() != null && getBufferSize() < this.expectedContentLength) { if (this.expectedContentLength != null && getBufferSize() < this.expectedContentLength) {
return Collections.<Message<byte[]>>emptyList(); return Collections.emptyList();
} }
ByteBuffer bufferToDecode = assembleChunksAndReset(); ByteBuffer bufferToDecode = assembleChunksAndReset();
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
List<Message<byte[]>> messages = this.stompDecoder.decode(bufferToDecode, headers); List<Message<byte[]>> messages = this.stompDecoder.decode(bufferToDecode, headers);
if (bufferToDecode.hasRemaining()) { if (bufferToDecode.hasRemaining()) {
@ -140,21 +118,6 @@ public class BufferingStompDecoder {
return messages; return messages;
} }
private void checkBufferLimits() {
if (getExpectedContentLength() != null) {
if (getExpectedContentLength() > getBufferSizeLimit()) {
throw new StompConversionException(
"The 'content-length' header " + getExpectedContentLength() +
" exceeds the configured message buffer size limit " + getBufferSizeLimit());
}
}
if (getBufferSize() > getBufferSizeLimit()) {
throw new StompConversionException("The configured stomp frame buffer size limit of " +
getBufferSizeLimit() + " bytes has been exceeded");
}
}
private ByteBuffer assembleChunksAndReset() { private ByteBuffer assembleChunksAndReset() {
ByteBuffer result; ByteBuffer result;
if (this.chunks.size() == 1) { if (this.chunks.size() == 1) {
@ -172,4 +135,36 @@ public class BufferingStompDecoder {
return result; return result;
} }
private void checkBufferLimits() {
if (this.expectedContentLength != null) {
if (this.expectedContentLength > this.bufferSizeLimit) {
throw new StompConversionException(
"STOMP 'content-length' header value " + this.expectedContentLength +
" exceeds configured buffer size limit " + this.bufferSizeLimit);
}
}
if (getBufferSize() > this.bufferSizeLimit) {
throw new StompConversionException("The configured STOMP buffer size limit of " +
this.bufferSizeLimit + " bytes has been exceeded");
}
}
/**
* Calculate the current buffer size.
*/
public int getBufferSize() {
int size = 0;
for (ByteBuffer buffer : this.chunks) {
size = size + buffer.remaining();
}
return size;
}
/**
* Get the expected content length of the currently buffered, incomplete STOMP frame.
*/
public Integer getExpectedContentLength() {
return this.expectedContentLength;
}
} }

5
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,8 +40,9 @@ import org.springframework.util.Assert;
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
* @see StompDecoder
*/ */
public final class StompEncoder { public class StompEncoder {
private static final byte LF = '\n'; private static final byte LF = '\n';

Loading…
Cancel
Save