INT-3626: More Sonar Fixes

JIRA: https://jira.spring.io/browse/INT-3626

Remove Direct Array Usages.

With the increased use of java configuration and DSL, it
is no longer safe to directly use array arguments.

(Except in simple wrapper objects).

Avoid (or mark //NOSONAR) catch Throwable.

Remove unused field.
This commit is contained in:
Gary Russell
2015-02-11 10:34:29 -05:00
parent bc509da062
commit 632740d2cf
27 changed files with 225 additions and 102 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -50,7 +50,8 @@ public class UploadedMultipartFile implements MultipartFile {
private final String originalFilename;
public UploadedMultipartFile(File file, long size, String contentType, String formParameterName, String originalFilename) {
public UploadedMultipartFile(File file, long size, String contentType, String formParameterName,
String originalFilename) {
Assert.notNull(file, "file must not be null");
Assert.hasText(contentType, "contentType is required");
Assert.hasText(formParameterName, "formParameterName is required");
@@ -63,12 +64,13 @@ public class UploadedMultipartFile implements MultipartFile {
this.originalFilename = originalFilename;
}
public UploadedMultipartFile(byte[] bytes, String contentType, String formParameterName, String originalFilename) {
public UploadedMultipartFile(byte[] bytes, String contentType, String formParameterName,//NOSONAR - direct storage
String originalFilename) {
Assert.notNull(bytes, "bytes must not be null");
Assert.hasText(contentType, "contentType is required");
Assert.hasText(formParameterName, "formParameterName is required");
Assert.hasText(originalFilename, "originalFilename is required");
this.bytes = bytes;
this.bytes = bytes;//NOSONAR - direct storage
this.size = bytes.length;
this.file = null;
this.contentType = contentType;
@@ -77,21 +79,25 @@ public class UploadedMultipartFile implements MultipartFile {
}
@Override
public String getName() {
return this.formParameterName;
}
@Override
public byte[] getBytes() throws IOException {
if (this.bytes != null) {
return this.bytes;
return this.bytes;//NOSONAR - direct access
}
return FileCopyUtils.copyToByteArray(this.file);
}
@Override
public String getContentType() {
return this.contentType;
}
@Override
public InputStream getInputStream() throws IOException {
if (this.bytes != null) {
return new ByteArrayInputStream(this.bytes);
@@ -99,18 +105,22 @@ public class UploadedMultipartFile implements MultipartFile {
return new BufferedInputStream(new FileInputStream(this.file));
}
@Override
public String getOriginalFilename() {
return this.originalFilename;
}
@Override
public long getSize() {
return this.size;
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
if (this.bytes != null) {
FileCopyUtils.copy(this.bytes, dest);

View File

@@ -293,6 +293,10 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
private volatile String userDefinedHeaderPrefix = "X-";
private volatile boolean isDefaultOutboundMapper;
private volatile boolean isDefaultInboundMapper;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
@@ -306,8 +310,15 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
* {@link DefaultHttpHeaderMapper#setUserDefinedHeaderPrefix(String)}. The default is 'X-'.
* @param outboundHeaderNames The outbound header names.
*/
public void setOutboundHeaderNames(String[] outboundHeaderNames) {
this.outboundHeaderNames = (outboundHeaderNames != null) ? outboundHeaderNames : new String[0];
public void setOutboundHeaderNames(String[] outboundHeaderNames) {//NOSONAR - false positive
if (HTTP_REQUEST_HEADER_NAMES == outboundHeaderNames) {
this.isDefaultOutboundMapper = true;
}
else if (HTTP_RESPONSE_HEADER_NAMES == outboundHeaderNames) {
this.isDefaultInboundMapper = true;
}
this.outboundHeaderNames = outboundHeaderNames != null ?
Arrays.copyOf(outboundHeaderNames, outboundHeaderNames.length) : new String[0];
this.outboundHeaderNamesLower = new String[this.outboundHeaderNames.length];
for (int i = 0; i < this.outboundHeaderNames.length; i++) {
if (HTTP_REQUEST_HEADER_NAME_PATTERN.equals(this.outboundHeaderNames[i])
@@ -333,8 +344,9 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
* {@link DefaultHttpHeaderMapper#setUserDefinedHeaderPrefix(String)}. The default is 'X-'.
* @param inboundHeaderNames The inbound header names.
*/
public void setInboundHeaderNames(String[] inboundHeaderNames) {
this.inboundHeaderNames = (inboundHeaderNames != null) ? inboundHeaderNames : new String[0];
public void setInboundHeaderNames(String[] inboundHeaderNames) {//NOSONAR - false positive
this.inboundHeaderNames = inboundHeaderNames != null ?
Arrays.copyOf(inboundHeaderNames, inboundHeaderNames.length) : new String[0];
this.inboundHeaderNamesLower = new String[this.inboundHeaderNames.length];
for (int i = 0; i < this.inboundHeaderNames.length; i++) {
if (HTTP_REQUEST_HEADER_NAME_PATTERN.equals(this.inboundHeaderNames[i])
@@ -355,7 +367,8 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
public void setExcludedOutboundStandardRequestHeaderNames(String[] excludedOutboundStandardRequestHeaderNames) {
Assert.notNull(excludedOutboundStandardRequestHeaderNames,
"'excludedOutboundStandardRequestHeaderNames' must not be null");
this.excludedOutboundStandardRequestHeaderNames = excludedOutboundStandardRequestHeaderNames;
this.excludedOutboundStandardRequestHeaderNames = Arrays.copyOf(excludedOutboundStandardRequestHeaderNames,
excludedOutboundStandardRequestHeaderNames.length);
}
/**
@@ -366,7 +379,8 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
public void setExcludedInboundStandardResponseHeaderNames(String[] excludedInboundStandardResponseHeaderNames) {
Assert.notNull(excludedInboundStandardResponseHeaderNames,
"'excludedInboundStandardResponseHeaderNames' must not be null");
this.excludedInboundStandardResponseHeaderNames = excludedInboundStandardResponseHeaderNames;
this.excludedInboundStandardResponseHeaderNames = Arrays.copyOf(excludedInboundStandardResponseHeaderNames,
excludedInboundStandardResponseHeaderNames.length);
}
/**
@@ -476,34 +490,34 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
}
private boolean shouldMapOutboundHeader(String headerName) {
String[] outboundHeaderNames = this.outboundHeaderNamesLower;
String[] outboundHeaderNamesLower = this.outboundHeaderNamesLower;
if (this.outboundHeaderNames == HTTP_RESPONSE_HEADER_NAMES) { // a default inbound mapper
if (this.isDefaultInboundMapper) {
/*
* When using the default response header name list, suppress the
* mapping of exclusions for specific headers.
*/
if (this.containsElementIgnoreCase(this.excludedInboundStandardResponseHeaderNames, headerName)) {
if (logger.isDebugEnabled()) {
logger.debug(MessageFormat.format("headerName=[{0}] WILL NOT be mapped", headerName));
logger.debug(MessageFormat.format("headerName=[{0}] WILL NOT be mapped (excluded)", headerName));
}
return false;
}
}
else if (this.outboundHeaderNames == HTTP_REQUEST_HEADER_NAMES) { // a default outbound mapper
outboundHeaderNames = this.outboundHeaderNamesLowerWithContentType;
else if (this.isDefaultOutboundMapper) {
outboundHeaderNamesLower = this.outboundHeaderNamesLowerWithContentType;
/*
* When using the default request header name list, suppress the
* mapping of exclusions for specific headers.
*/
if (this.containsElementIgnoreCase(this.excludedOutboundStandardRequestHeaderNames, headerName)) {
if (logger.isDebugEnabled()) {
logger.debug(MessageFormat.format("headerName=[{0}] WILL NOT be mapped", headerName));
logger.debug(MessageFormat.format("headerName=[{0}] WILL NOT be mapped (excluded)", headerName));
}
return false;
}
}
return this.shouldMapHeader(headerName, outboundHeaderNames);
return this.shouldMapHeader(headerName, outboundHeaderNamesLower);
}
private boolean shouldMapInboundHeader(String headerName) {