From 5ca2c56cf0841b8fadfa3c60471419bb5cb105ad Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 17 Sep 2018 14:39:54 +0200 Subject: [PATCH] Polishing --- .../expression/spel/ast/StringLiteral.java | 4 +- .../spel/standard/SpelCompiler.java | 14 +-- .../ServerSentEventHttpMessageWriter.java | 4 +- .../context/request/ServletWebRequest.java | 8 +- .../adapter/DefaultServerWebExchange.java | 6 +- ...ServerSentEventHttpMessageReaderTests.java | 15 ++-- ...ServerSentEventHttpMessageWriterTests.java | 9 +- .../DefaultHandlerExceptionResolver.java | 9 +- .../web/servlet/tags/UrlTag.java | 89 ++++++++++--------- .../socket/sockjs/client/SockJsUrlInfo.java | 2 +- 10 files changed, 79 insertions(+), 81 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/StringLiteral.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/StringLiteral.java index 930bc4738d..35d6cb568f 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/StringLiteral.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/StringLiteral.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 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. @@ -33,7 +33,7 @@ public class StringLiteral extends Literal { public StringLiteral(String payload, int pos, String value) { - super(payload,pos); + super(payload, pos); value = value.substring(1, value.length() - 1); this.value = new TypedValue(value.replaceAll("''", "'").replaceAll("\"\"", "\"")); this.exitTypeDescriptor = "Ljava/lang/String"; diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java b/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java index 56589819fa..70ce806561 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/standard/SpelCompiler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -132,9 +132,9 @@ public class SpelCompiler implements Opcodes { @Nullable private Class createExpressionClass(SpelNodeImpl expressionToCompile) { // Create class outline 'spel/ExNNN extends org.springframework.expression.spel.CompiledExpression' - String clazzName = "spel/Ex" + getNextSuffix(); + String className = "spel/Ex" + getNextSuffix(); ClassWriter cw = new ExpressionClassWriter(); - cw.visit(V1_5, ACC_PUBLIC, clazzName, null, "org/springframework/expression/spel/CompiledExpression", null); + cw.visit(V1_5, ACC_PUBLIC, className, null, "org/springframework/expression/spel/CompiledExpression", null); // Create default constructor MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); @@ -152,7 +152,7 @@ public class SpelCompiler implements Opcodes { new String[ ]{"org/springframework/expression/EvaluationException"}); mv.visitCode(); - CodeFlow cf = new CodeFlow(clazzName, cw); + CodeFlow cf = new CodeFlow(className, cw); // Ask the expression AST to generate the body of the method try { @@ -181,7 +181,7 @@ public class SpelCompiler implements Opcodes { byte[] data = cw.toByteArray(); // TODO need to make this conditionally occur based on a debug flag // dump(expressionToCompile.toStringAST(), clazzName, data); - return loadClass(clazzName.replaceAll("/", "."), data); + return loadClass(className.replaceAll("/", "."), data); } /** @@ -256,12 +256,12 @@ public class SpelCompiler implements Opcodes { } int getClassesDefinedCount() { - return classesDefinedCount; + return this.classesDefinedCount; } public Class defineClass(String name, byte[] bytes) { Class clazz = super.defineClass(name, bytes, 0, bytes.length); - classesDefinedCount++; + this.classesDefinedCount++; return clazz; } } diff --git a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java index 7645c9f097..5eb1a81865 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java @@ -199,8 +199,8 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter httpEncoder = (HttpMessageEncoder) this.encoder; - return httpEncoder.getEncodeHints(actualType, elementType, mediaType, request, response); + HttpMessageEncoder encoder = (HttpMessageEncoder) this.encoder; + return encoder.getEncodeHints(actualType, elementType, mediaType, request, response); } return Collections.emptyMap(); } diff --git a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java index 4c13fae123..94f823a452 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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 + * 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, @@ -63,13 +63,13 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ private static final List SAFE_METHODS = Arrays.asList("GET", "HEAD"); /** - * Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match" + * Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match". * @see Section 2.3 of RFC 7232 */ private static final Pattern ETAG_HEADER_VALUE_PATTERN = Pattern.compile("\\*|\\s*((W\\/)?(\"[^\"]*\"))\\s*,?"); /** - * Date formats as specified in the HTTP RFC + * Date formats as specified in the HTTP RFC. * @see Section 7.1.1.1 of RFC 7231 */ private static final String[] DATE_FORMATS = new String[] { diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java index c4a136ed28..8849763bad 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java @@ -310,10 +310,10 @@ public class DefaultServerWebExchange implements ServerWebExchange { } // We will perform this validation... etag = padEtagIfNecessary(etag); - for (String clientETag : ifNoneMatch) { + for (String clientEtag : ifNoneMatch) { // Compare weak/strong ETags as per https://tools.ietf.org/html/rfc7232#section-2.3 - if (StringUtils.hasLength(clientETag) && - clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", ""))) { + if (StringUtils.hasLength(clientEtag) && + clientEtag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", ""))) { this.notModified = true; break; } diff --git a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java index 0b7f2987f3..221b148004 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -32,6 +32,8 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import static org.junit.Assert.*; /** + * Unit tests for {@link ServerSentEventHttpMessageReader}. + * * @author Sebastien Deleuze */ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAllocatingTestCase { @@ -42,24 +44,21 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll @Test public void cantRead() { - assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), - new MediaType("foo", "bar"))); + assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), new MediaType("foo", "bar"))); assertFalse(messageReader.canRead(ResolvableType.forClass(Object.class), null)); } @Test public void canRead() { - assertTrue(messageReader.canRead(ResolvableType.forClass(Object.class), - new MediaType("text", "event-stream"))); - assertTrue(messageReader.canRead(ResolvableType.forClass(ServerSentEvent.class), - new MediaType("foo", "bar"))); + assertTrue(messageReader.canRead(ResolvableType.forClass(Object.class), new MediaType("text", "event-stream"))); + assertTrue(messageReader.canRead(ResolvableType.forClass(ServerSentEvent.class), new MediaType("foo", "bar"))); } @Test public void readServerSentEvents() { MockServerHttpRequest request = MockServerHttpRequest.post("/").body( "id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n\n" + - "id:c43\nevent:bar\nretry:456\ndata:baz\n\n"); + "id:c43\nevent:bar\nretry:456\ndata:baz\n\n"); Flux events = this.messageReader .read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class), diff --git a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java index f7f1195ccd..6e39291365 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java @@ -41,6 +41,7 @@ import static org.springframework.core.ResolvableType.*; /** * Unit tests for {@link ServerSentEventHttpMessageWriter}. + * * @author Sebastien Deleuze * @author Rossen Stoyanchev */ @@ -48,14 +49,12 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll private static final Map HINTS = Collections.emptyMap(); - private ServerSentEventHttpMessageWriter messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder()); @Test public void canWrite() { - assertTrue(this.messageWriter.canWrite(forClass(Object.class), null)); assertFalse(this.messageWriter.canWrite(forClass(Object.class), new MediaType("foo", "bar"))); @@ -69,7 +68,6 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll @Test public void writeServerSentEvent() { - ServerSentEvent event = ServerSentEvent.builder().data("bar").id("c42").event("foo") .comment("bla\nbla bla\nbla bla bla").retry(Duration.ofMillis(123L)).build(); @@ -134,7 +132,6 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll @Test // SPR-14899 public void writePojoWithPrettyPrint() { - ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build(); this.messageWriter = new ServerSentEventHttpMessageWriter(new Jackson2JsonEncoder(mapper)); @@ -172,8 +169,8 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll testWrite(source, MediaType.TEXT_EVENT_STREAM, response, clazz); } - private void testWrite(Publisher source, MediaType mediaType, MockServerHttpResponse response, - Class clazz) { + private void testWrite( + Publisher source, MediaType mediaType, MockServerHttpResponse response, Class clazz) { this.messageWriter.write(source, forClass(clazz), mediaType, response, HINTS) .block(Duration.ofMillis(5000)); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java index b9b6bd9ecf..b6fa2d823e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java @@ -88,7 +88,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; * * *

MissingServletRequestParameterException

- *

500 (SC_INTERNAL_SERVER_ERROR)

+ *

400 (SC_BAD_REQUEST)

* * *

ServletRequestBindingException

@@ -364,7 +364,8 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes /** * Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion cannot occur. *

The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. - * Alternatively, a fallback view could be chosen, or the TypeMismatchException could be rethrown as-is. + * Alternatively, a fallback view could be chosen, or the ConversionNotSupportedException could be + * rethrown as-is. * @param ex the ConversionNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response @@ -401,7 +402,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes * Handle the case where a {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} * cannot read from a HTTP request. *

The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. - * Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could be + * Alternatively, a fallback view could be chosen, or the HttpMessageNotReadableException could be * rethrown as-is. * @param ex the HttpMessageNotReadableException to be handled * @param request current HTTP request @@ -422,7 +423,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes * {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} * cannot write to a HTTP request. *

The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. - * Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could + * Alternatively, a fallback view could be chosen, or the HttpMessageNotWritableException could * be rethrown as-is. * @param ex the HttpMessageNotWritableException to be handled * @param request current HTTP request diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java index 43afa94b27..0a3240b861 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/UrlTag.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -38,7 +38,7 @@ import org.springframework.web.util.TagUtils; import org.springframework.web.util.UriUtils; /** - * The {@code } tag creates URLs. Modeled after the JSTL c:url tag with + * The {@code } tag creates URLs. Modeled after the JSTL {@code c:url} tag with * backwards compatibility in mind. * *

Enhancements to the JSTL functionality include: @@ -76,56 +76,56 @@ import org.springframework.web.util.UriUtils; * Attribute Summary * * - * Attribute - * Required? - * Runtime Expression? - * Description + * Attribute + * Required? + * Runtime Expression? + * Description * * * - * - * value

- * true

- * true

+ * + * value + * true + * true * The URL to build. This value can include template {placeholders} that are * replaced with the URL encoded value of the named parameter. Parameters - * must be defined using the param tag inside the body of this tag.

+ * must be defined using the param tag inside the body of this tag. * - * - * context

- * false

- * true

+ * + * context + * false + * true * Specifies a remote application context path. - * The default is the current application context path.

+ * The default is the current application context path. * - * - * var

- * false

- * true

+ * + * var + * false + * true * The name of the variable to export the URL value to. - * If not specified the URL is written as output.

+ * If not specified the URL is written as output. * - * - * scope

- * false

- * true

+ * + * scope + * false + * true * The scope for the var. 'application', 'session', 'request' and 'page' * scopes are supported. Defaults to page scope. This attribute has no - * effect unless the var attribute is also defined.

+ * effect unless the var attribute is also defined. * - * - * htmlEscape

- * false

- * true

+ * + * htmlEscape + * false + * true * Set HTML escaping for this tag, as a boolean value. Overrides the - * default HTML escaping setting for the current page.

+ * default HTML escaping setting for the current page. * - * - * javaScriptEscape

- * false

- * true

+ * + * javaScriptEscape + * false + * true * Set JavaScript escaping for this tag, as a boolean value. - * Default is false.

+ * Default is false. * * * @@ -166,7 +166,7 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { /** - * Sets the value of the URL + * Sets the value of the URL. */ public void setValue(String value) { if (value.contains(URL_TYPE_ABSOLUTE)) { @@ -245,7 +245,7 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { if (this.var == null) { // print the url to the writer try { - pageContext.getOut().print(url); + this.pageContext.getOut().print(url); } catch (IOException ex) { throw new JspException(ex); @@ -253,7 +253,7 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { } else { // store the url as a variable - pageContext.setAttribute(var, url, scope); + this.pageContext.setAttribute(this.var, url, this.scope); } return EVAL_PAGE; } @@ -265,8 +265,8 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { */ String createUrl() throws JspException { Assert.state(this.value != null, "No value set"); - HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); - HttpServletResponse response = (HttpServletResponse) pageContext.getResponse(); + HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest(); + HttpServletResponse response = (HttpServletResponse) this.pageContext.getResponse(); StringBuilder url = new StringBuilder(); if (this.type == UrlType.CONTEXT_RELATIVE) { @@ -317,7 +317,7 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { protected String createQueryString(List params, Set usedParams, boolean includeQueryStringDelimiter) throws JspException { - String encoding = pageContext.getResponse().getCharacterEncoding(); + String encoding = this.pageContext.getResponse().getCharacterEncoding(); StringBuilder qs = new StringBuilder(); for (Param param : params) { if (!usedParams.contains(param.getName()) && StringUtils.hasLength(param.getName())) { @@ -354,14 +354,15 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware { protected String replaceUriTemplateParams(String uri, List params, Set usedParams) throws JspException { - String encoding = pageContext.getResponse().getCharacterEncoding(); + String encoding = this.pageContext.getResponse().getCharacterEncoding(); for (Param param : params) { String template = URL_TEMPLATE_DELIMITER_PREFIX + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX; if (uri.contains(template)) { usedParams.add(param.getName()); String value = param.getValue(); try { - uri = uri.replace(template, (value != null ? UriUtils.encodePath(value, encoding) : "")); + uri = uri.replace(template, + (value != null ? UriUtils.encodePath(value, encoding) : "")); } catch (UnsupportedCharsetException ex) { throw new JspException(ex); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java index e01abaabf1..986624815f 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java @@ -68,7 +68,7 @@ public class SockJsUrlInfo { public String getSessionId() { if (this.sessionId == null) { - this.sessionId = getUuid().toString().replace("-",""); + this.sessionId = getUuid().toString().replace("-", ""); } return this.sessionId; }