Skip to content

Commit 7bc8e51

Browse files
committed
Polishing
1 parent 49cf30e commit 7bc8e51

File tree

2 files changed

+72
-64
lines changed

2 files changed

+72
-64
lines changed

spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* Message listener container variant that uses plain JMS client APIs, specifically
4444
* a loop of {@code MessageConsumer.receive()} calls that also allow for
4545
* transactional reception of messages (registering them with XA transactions).
46-
* Designed to work in a native JMS environment as well as in a J2EE environment,
46+
* Designed to work in a native JMS environment as well as in a Java EE environment,
4747
* with only minimal differences in configuration.
4848
*
4949
* <p>This is a simple but nevertheless powerful form of message listener container.
@@ -60,7 +60,7 @@
6060
* abstraction. By default, the specified number of invoker tasks will be created
6161
* on startup, according to the {@link #setConcurrentConsumers "concurrentConsumers"}
6262
* setting. Specify an alternative {@code TaskExecutor} to integrate with an existing
63-
* thread pool facility (such as a J2EE server's), for example using a
63+
* thread pool facility (such as a Java EE server's), for example using a
6464
* {@link org.springframework.scheduling.commonj.WorkManagerTaskExecutor CommonJ WorkManager}.
6565
* With a native JMS setup, each of those listener threads is going to use a
6666
* cached JMS {@code Session} and {@code MessageConsumer} (only refreshed in case
@@ -71,11 +71,11 @@
7171
* {@link org.springframework.transaction.PlatformTransactionManager} into the
7272
* {@link #setTransactionManager "transactionManager"} property. This will usually
7373
* be a {@link org.springframework.transaction.jta.JtaTransactionManager} in a
74-
* J2EE environment, in combination with a JTA-aware JMS {@code ConnectionFactory}
75-
* obtained from JNDI (check your J2EE server's documentation). Note that this
74+
* Java EE environment, in combination with a JTA-aware JMS {@code ConnectionFactory}
75+
* obtained from JNDI (check your Java EE server's documentation). Note that this
7676
* listener container will automatically reobtain all JMS handles for each transaction
7777
* in case an external transaction manager is specified, for compatibility with
78-
* all J2EE servers (in particular JBoss). This non-caching behavior can be
78+
* all Java EE servers (in particular JBoss). This non-caching behavior can be
7979
* overridden through the {@link #setCacheLevel "cacheLevel"} /
8080
* {@link #setCacheLevelName "cacheLevelName"} property, enforcing caching of
8181
* the {@code Connection} (or also {@code Session} and {@code MessageConsumer})
@@ -209,7 +209,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
209209
* of concurrent consumers.
210210
* <p>Specify an alternative {@code TaskExecutor} for integration with an existing
211211
* thread pool. Note that this really only adds value if the threads are
212-
* managed in a specific fashion, for example within a J2EE environment.
212+
* managed in a specific fashion, for example within a Java EE environment.
213213
* A plain thread pool does not add much value, as this listener container
214214
* will occupy a number of threads for its entire lifetime.
215215
* @see #setConcurrentConsumers
@@ -262,11 +262,11 @@ public void setCacheLevelName(String constantName) throws IllegalArgumentExcepti
262262
* <p>Default is {@link #CACHE_NONE} if an external transaction manager has been specified
263263
* (to reobtain all resources freshly within the scope of the external transaction),
264264
* and {@link #CACHE_CONSUMER} otherwise (operating with local JMS resources).
265-
* <p>Some JavaEE servers only register their JMS resources with an ongoing XA
265+
* <p>Some Java EE servers only register their JMS resources with an ongoing XA
266266
* transaction in case of a freshly obtained JMS {@code Connection} and {@code Session},
267267
* which is why this listener container by default does not cache any of those.
268-
* However, depending on how smart your JavaEE server is with respect to the caching
269-
* of transactional resource, consider switching this setting to at least
268+
* However, depending on the rules of your server with respect to the caching
269+
* of transactional resources, consider switching this setting to at least
270270
* {@link #CACHE_CONNECTION} or {@link #CACHE_SESSION} even in conjunction with an
271271
* external transaction manager.
272272
* @see #CACHE_NONE

spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,24 @@
4646
* Implementation of {@link HttpMessageConverter} to read and write 'normal' HTML
4747
* forms and also to write (but not read) multipart data (e.g. file uploads).
4848
*
49-
* <p>In other words this converter can read and write the
49+
* <p>In other words, this converter can read and write the
5050
* {@code "application/x-www-form-urlencoded"} media type as
5151
* {@link MultiValueMap MultiValueMap&lt;String, String&gt;} and it can also
5252
* write (but not read) the {@code "multipart/form-data"} media type as
5353
* {@link MultiValueMap MultiValueMap&lt;String, Object&gt;}.
5454
*
55-
* <p>When writing multipart data this converter uses other
55+
* <p>When writing multipart data, this converter uses other
5656
* {@link HttpMessageConverter HttpMessageConverters} to write the respective
57-
* MIME parts. By default basic converters are registered (for {@code Strings}
57+
* MIME parts. By default, basic converters are registered (for {@code Strings}
5858
* and {@code Resources}). These can be overridden through the
59-
* {@link #setPartConverters(java.util.List) partConverters} property.
59+
* {@link #setPartConverters partConverters} property.
6060
*
61-
* <p>For example the following snippet shows how to submit an HTML form:
61+
* <p>For example, the following snippet shows how to submit an HTML form:
6262
* <pre class="code">
63-
* RestTemplate template = new RestTemplate(); // FormHttpMessageConverter is configured by default
63+
* RestTemplate template = new RestTemplate(); // FormHttpMessageConverter is configured by default
6464
* MultiValueMap&lt;String, String&gt; form = new LinkedMultiValueMap&lt;String, String&gt;();
65-
* form.add("field 1", "value 1"); form.add("field 2", "value 2");
65+
* form.add("field 1", "value 1");
66+
* form.add("field 2", "value 2");
6667
* form.add("field 2", "value 3");
6768
* template.postForLocation("http://example.com/myForm", form);
6869
* </pre>
@@ -75,7 +76,8 @@
7576
* template.postForLocation("http://example.com/myFileUpload", parts);
7677
* </pre>
7778
*
78-
* <p>Some methods in this class were inspired by {@code org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}.
79+
* <p>Some methods in this class were inspired by
80+
* {@code org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}.
7981
*
8082
* @author Arjen Poutsma
8183
* @author Rossen Stoyanchev
@@ -85,12 +87,11 @@
8587
public class FormHttpMessageConverter implements HttpMessageConverter<MultiValueMap<String, ?>> {
8688

8789
private static final byte[] BOUNDARY_CHARS =
88-
new byte[]{'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
90+
new byte[] {'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
8991
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
9092
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
9193
'V', 'W', 'X', 'Y', 'Z'};
9294

93-
private final Random random = new Random();
9495

9596
private Charset charset = Charset.forName("UTF-8");
9697

@@ -100,6 +101,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
100101

101102
private List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
102103

104+
private final Random random = new Random();
105+
103106

104107
public FormHttpMessageConverter() {
105108
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
@@ -157,7 +160,7 @@ public void setPartConverters(List<HttpMessageConverter<?>> partConverters) {
157160
}
158161

159162
/**
160-
* Add a message body converter. Such a converters is used to convert objects
163+
* Add a message body converter. Such a converter is used to convert objects
161164
* to MIME parts.
162165
*/
163166
public void addPartConverter(HttpMessageConverter<?> partConverter) {
@@ -236,6 +239,7 @@ public void write(MultiValueMap<String, ?> map, MediaType contentType, HttpOutpu
236239
}
237240
}
238241

242+
239243
private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType) {
240244
if (contentType != null) {
241245
return MediaType.MULTIPART_FORM_DATA.equals(contentType);
@@ -285,56 +289,37 @@ private void writeForm(MultiValueMap<String, String> form, MediaType contentType
285289
StreamUtils.copy(bytes, outputMessage.getBody());
286290
}
287291

288-
private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
289-
throws IOException {
290-
292+
private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage) throws IOException {
291293
byte[] boundary = generateMultipartBoundary();
292-
293294
Map<String, String> parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII"));
295+
294296
MediaType contentType = new MediaType(MediaType.MULTIPART_FORM_DATA, parameters);
295297
outputMessage.getHeaders().setContentType(contentType);
296298

297299
writeParts(outputMessage.getBody(), parts, boundary);
298-
writeEnd(boundary, outputMessage.getBody());
300+
writeEnd(outputMessage.getBody(), boundary);
299301
}
300302

301303
private void writeParts(OutputStream os, MultiValueMap<String, Object> parts, byte[] boundary) throws IOException {
302304
for (Map.Entry<String, List<Object>> entry : parts.entrySet()) {
303305
String name = entry.getKey();
304306
for (Object part : entry.getValue()) {
305307
if (part != null) {
306-
writeBoundary(boundary, os);
307-
HttpEntity<?> entity = getEntity(part);
308-
writePart(name, entity, os);
308+
writeBoundary(os, boundary);
309+
writePart(name, getHttpEntity(part), os);
309310
writeNewLine(os);
310311
}
311312
}
312313
}
313314
}
314315

315-
private void writeBoundary(byte[] boundary, OutputStream os) throws IOException {
316-
os.write('-');
317-
os.write('-');
318-
os.write(boundary);
319-
writeNewLine(os);
320-
}
321-
322-
private HttpEntity<?> getEntity(Object part) {
323-
if (part instanceof HttpEntity) {
324-
return (HttpEntity<?>) part;
325-
}
326-
else {
327-
return new HttpEntity<Object>(part);
328-
}
329-
}
330-
331316
@SuppressWarnings("unchecked")
332317
private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException {
333318
Object partBody = partEntity.getBody();
334319
Class<?> partType = partBody.getClass();
335320
HttpHeaders partHeaders = partEntity.getHeaders();
336321
MediaType partContentType = partHeaders.getContentType();
337-
for (HttpMessageConverter<?> messageConverter : partConverters) {
322+
for (HttpMessageConverter<?> messageConverter : this.partConverters) {
338323
if (messageConverter.canWrite(partType, partContentType)) {
339324
HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os);
340325
multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
@@ -345,38 +330,39 @@ private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) t
345330
return;
346331
}
347332
}
348-
throw new HttpMessageNotWritableException(
349-
"Could not write request: no suitable HttpMessageConverter found for request type [" +
350-
partType.getName() + "]");
333+
throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " +
334+
"found for request type [" + partType.getName() + "]");
351335
}
352336

353-
private void writeEnd(byte[] boundary, OutputStream os) throws IOException {
354-
os.write('-');
355-
os.write('-');
356-
os.write(boundary);
357-
os.write('-');
358-
os.write('-');
359-
writeNewLine(os);
360-
}
361-
362-
private void writeNewLine(OutputStream os) throws IOException {
363-
os.write('\r');
364-
os.write('\n');
365-
}
366337

367338
/**
368339
* Generate a multipart boundary.
369340
* <p>The default implementation returns a random boundary.
370341
* Can be overridden in subclasses.
371342
*/
372343
protected byte[] generateMultipartBoundary() {
373-
byte[] boundary = new byte[random.nextInt(11) + 30];
344+
byte[] boundary = new byte[this.random.nextInt(11) + 30];
374345
for (int i = 0; i < boundary.length; i++) {
375-
boundary[i] = BOUNDARY_CHARS[random.nextInt(BOUNDARY_CHARS.length)];
346+
boundary[i] = BOUNDARY_CHARS[this.random.nextInt(BOUNDARY_CHARS.length)];
376347
}
377348
return boundary;
378349
}
379350

351+
/**
352+
* Return an {@link HttpEntity} for the given part Object.
353+
* @param part the part to return an {@link HttpEntity} for
354+
* @return the part Object itself it is an {@link HttpEntity},
355+
* or a newly built {@link HttpEntity} wrapper for that part
356+
*/
357+
protected HttpEntity<?> getHttpEntity(Object part) {
358+
if (part instanceof HttpEntity) {
359+
return (HttpEntity<?>) part;
360+
}
361+
else {
362+
return new HttpEntity<Object>(part);
363+
}
364+
}
365+
380366
/**
381367
* Return the filename of the given multipart part. This value will be used for the
382368
* {@code Content-Disposition} header.
@@ -400,11 +386,33 @@ protected String getFilename(Object part) {
400386
}
401387

402388

389+
private void writeBoundary(OutputStream os, byte[] boundary) throws IOException {
390+
os.write('-');
391+
os.write('-');
392+
os.write(boundary);
393+
writeNewLine(os);
394+
}
395+
396+
private static void writeEnd(OutputStream os, byte[] boundary) throws IOException {
397+
os.write('-');
398+
os.write('-');
399+
os.write(boundary);
400+
os.write('-');
401+
os.write('-');
402+
writeNewLine(os);
403+
}
404+
405+
private static void writeNewLine(OutputStream os) throws IOException {
406+
os.write('\r');
407+
os.write('\n');
408+
}
409+
410+
403411
/**
404412
* Implementation of {@link org.springframework.http.HttpOutputMessage} used
405413
* to write a MIME multipart.
406414
*/
407-
private class MultipartHttpOutputMessage implements HttpOutputMessage {
415+
private static class MultipartHttpOutputMessage implements HttpOutputMessage {
408416

409417
private final OutputStream outputStream;
410418

0 commit comments

Comments
 (0)