From 7bc8e5199eb82fe28caebb11c0606b468bc15034 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 29 Sep 2014 23:50:23 +0200 Subject: [PATCH] Polishing --- .../DefaultMessageListenerContainer.java | 18 +-- .../converter/FormHttpMessageConverter.java | 118 ++++++++++-------- 2 files changed, 72 insertions(+), 64 deletions(-) diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java b/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java index 01c04dbdf9..1215a924c7 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java @@ -43,7 +43,7 @@ import org.springframework.util.backoff.FixedBackOff; * Message listener container variant that uses plain JMS client APIs, specifically * a loop of {@code MessageConsumer.receive()} calls that also allow for * transactional reception of messages (registering them with XA transactions). - * Designed to work in a native JMS environment as well as in a J2EE environment, + * Designed to work in a native JMS environment as well as in a Java EE environment, * with only minimal differences in configuration. * *

This is a simple but nevertheless powerful form of message listener container. @@ -60,7 +60,7 @@ import org.springframework.util.backoff.FixedBackOff; * abstraction. By default, the specified number of invoker tasks will be created * on startup, according to the {@link #setConcurrentConsumers "concurrentConsumers"} * setting. Specify an alternative {@code TaskExecutor} to integrate with an existing - * thread pool facility (such as a J2EE server's), for example using a + * thread pool facility (such as a Java EE server's), for example using a * {@link org.springframework.scheduling.commonj.WorkManagerTaskExecutor CommonJ WorkManager}. * With a native JMS setup, each of those listener threads is going to use a * cached JMS {@code Session} and {@code MessageConsumer} (only refreshed in case @@ -71,11 +71,11 @@ import org.springframework.util.backoff.FixedBackOff; * {@link org.springframework.transaction.PlatformTransactionManager} into the * {@link #setTransactionManager "transactionManager"} property. This will usually * be a {@link org.springframework.transaction.jta.JtaTransactionManager} in a - * J2EE environment, in combination with a JTA-aware JMS {@code ConnectionFactory} - * obtained from JNDI (check your J2EE server's documentation). Note that this + * Java EE environment, in combination with a JTA-aware JMS {@code ConnectionFactory} + * obtained from JNDI (check your Java EE server's documentation). Note that this * listener container will automatically reobtain all JMS handles for each transaction * in case an external transaction manager is specified, for compatibility with - * all J2EE servers (in particular JBoss). This non-caching behavior can be + * all Java EE servers (in particular JBoss). This non-caching behavior can be * overridden through the {@link #setCacheLevel "cacheLevel"} / * {@link #setCacheLevelName "cacheLevelName"} property, enforcing caching of * the {@code Connection} (or also {@code Session} and {@code MessageConsumer}) @@ -209,7 +209,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe * of concurrent consumers. *

Specify an alternative {@code TaskExecutor} for integration with an existing * thread pool. Note that this really only adds value if the threads are - * managed in a specific fashion, for example within a J2EE environment. + * managed in a specific fashion, for example within a Java EE environment. * A plain thread pool does not add much value, as this listener container * will occupy a number of threads for its entire lifetime. * @see #setConcurrentConsumers @@ -262,11 +262,11 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe *

Default is {@link #CACHE_NONE} if an external transaction manager has been specified * (to reobtain all resources freshly within the scope of the external transaction), * and {@link #CACHE_CONSUMER} otherwise (operating with local JMS resources). - *

Some JavaEE servers only register their JMS resources with an ongoing XA + *

Some Java EE servers only register their JMS resources with an ongoing XA * transaction in case of a freshly obtained JMS {@code Connection} and {@code Session}, * which is why this listener container by default does not cache any of those. - * However, depending on how smart your JavaEE server is with respect to the caching - * of transactional resource, consider switching this setting to at least + * However, depending on the rules of your server with respect to the caching + * of transactional resources, consider switching this setting to at least * {@link #CACHE_CONNECTION} or {@link #CACHE_SESSION} even in conjunction with an * external transaction manager. * @see #CACHE_NONE diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index 2e1c6ae42b..987817bbc7 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -46,23 +46,24 @@ import org.springframework.util.StringUtils; * Implementation of {@link HttpMessageConverter} to read and write 'normal' HTML * forms and also to write (but not read) multipart data (e.g. file uploads). * - *

In other words this converter can read and write the + *

In other words, this converter can read and write the * {@code "application/x-www-form-urlencoded"} media type as * {@link MultiValueMap MultiValueMap<String, String>} and it can also * write (but not read) the {@code "multipart/form-data"} media type as * {@link MultiValueMap MultiValueMap<String, Object>}. * - *

When writing multipart data this converter uses other + *

When writing multipart data, this converter uses other * {@link HttpMessageConverter HttpMessageConverters} to write the respective - * MIME parts. By default basic converters are registered (for {@code Strings} + * MIME parts. By default, basic converters are registered (for {@code Strings} * and {@code Resources}). These can be overridden through the - * {@link #setPartConverters(java.util.List) partConverters} property. + * {@link #setPartConverters partConverters} property. * - *

For example the following snippet shows how to submit an HTML form: + *

For example, the following snippet shows how to submit an HTML form: *

- * RestTemplate template = new RestTemplate(); // FormHttpMessageConverter is configured by default
+ * RestTemplate template = new RestTemplate();  // FormHttpMessageConverter is configured by default
  * MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
- * form.add("field 1", "value 1"); form.add("field 2", "value 2");
+ * form.add("field 1", "value 1");
+ * form.add("field 2", "value 2");
  * form.add("field 2", "value 3");
  * template.postForLocation("http://example.com/myForm", form);
  * 
@@ -75,7 +76,8 @@ import org.springframework.util.StringUtils; * template.postForLocation("http://example.com/myFileUpload", parts); * * - *

Some methods in this class were inspired by {@code org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}. + *

Some methods in this class were inspired by + * {@code org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}. * * @author Arjen Poutsma * @author Rossen Stoyanchev @@ -85,12 +87,11 @@ import org.springframework.util.StringUtils; public class FormHttpMessageConverter implements HttpMessageConverter> { private static final byte[] BOUNDARY_CHARS = - new byte[]{'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', + new byte[] {'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; - private final Random random = new Random(); private Charset charset = Charset.forName("UTF-8"); @@ -100,6 +101,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter> partConverters = new ArrayList>(); + private final Random random = new Random(); + public FormHttpMessageConverter() { this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); @@ -157,7 +160,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter partConverter) { @@ -236,6 +239,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter map, MediaType contentType) { if (contentType != null) { return MediaType.MULTIPART_FORM_DATA.equals(contentType); @@ -285,17 +289,15 @@ public class FormHttpMessageConverter implements HttpMessageConverter parts, HttpOutputMessage outputMessage) - throws IOException { - + private void writeMultipart(MultiValueMap parts, HttpOutputMessage outputMessage) throws IOException { byte[] boundary = generateMultipartBoundary(); - Map parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII")); + MediaType contentType = new MediaType(MediaType.MULTIPART_FORM_DATA, parameters); outputMessage.getHeaders().setContentType(contentType); writeParts(outputMessage.getBody(), parts, boundary); - writeEnd(boundary, outputMessage.getBody()); + writeEnd(outputMessage.getBody(), boundary); } private void writeParts(OutputStream os, MultiValueMap parts, byte[] boundary) throws IOException { @@ -303,38 +305,21 @@ public class FormHttpMessageConverter implements HttpMessageConverter entity = getEntity(part); - writePart(name, entity, os); + writeBoundary(os, boundary); + writePart(name, getHttpEntity(part), os); writeNewLine(os); } } } } - private void writeBoundary(byte[] boundary, OutputStream os) throws IOException { - os.write('-'); - os.write('-'); - os.write(boundary); - writeNewLine(os); - } - - private HttpEntity getEntity(Object part) { - if (part instanceof HttpEntity) { - return (HttpEntity) part; - } - else { - return new HttpEntity(part); - } - } - @SuppressWarnings("unchecked") private void writePart(String name, HttpEntity partEntity, OutputStream os) throws IOException { Object partBody = partEntity.getBody(); Class partType = partBody.getClass(); HttpHeaders partHeaders = partEntity.getHeaders(); MediaType partContentType = partHeaders.getContentType(); - for (HttpMessageConverter messageConverter : partConverters) { + for (HttpMessageConverter messageConverter : this.partConverters) { if (messageConverter.canWrite(partType, partContentType)) { HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os); multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody)); @@ -345,24 +330,10 @@ public class FormHttpMessageConverter implements HttpMessageConverter getHttpEntity(Object part) { + if (part instanceof HttpEntity) { + return (HttpEntity) part; + } + else { + return new HttpEntity(part); + } + } + /** * Return the filename of the given multipart part. This value will be used for the * {@code Content-Disposition} header. @@ -400,11 +386,33 @@ public class FormHttpMessageConverter implements HttpMessageConverter