Skip to content

Use StandardCharsets.UTF_8 instead of a string UTF-8 value. #1335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;

import javax.xml.transform.*;
Expand Down Expand Up @@ -619,7 +620,7 @@ private void sendRequest(WebServiceConnection connection, WebServiceMessage requ
if (sentMessageTracingLogger.isTraceEnabled()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
request.writeTo(os);
sentMessageTracingLogger.trace("Sent request [" + os.toString("UTF-8") + "]");
sentMessageTracingLogger.trace("Sent request [" + os.toString(StandardCharsets.UTF_8) + "]");
} else if (sentMessageTracingLogger.isDebugEnabled()) {
sentMessageTracingLogger.debug("Sent request [" + request + "]");
}
Expand Down Expand Up @@ -672,8 +673,8 @@ private void logResponse(MessageContext messageContext) throws IOException {
messageContext.getRequest().writeTo(requestStream);
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
messageContext.getResponse().writeTo(responseStream);
receivedMessageTracingLogger.trace("Received response [" + responseStream.toString("UTF-8") + "] for request ["
+ requestStream.toString("UTF-8") + "]");
receivedMessageTracingLogger.trace("Received response [" + responseStream.toString(StandardCharsets.UTF_8) + "] for request ["
+ requestStream.toString(StandardCharsets.UTF_8) + "]");
} else if (receivedMessageTracingLogger.isDebugEnabled()) {
receivedMessageTracingLogger.debug("Received response [" + messageContext.getResponse() + "] for request ["
+ messageContext.getRequest() + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -189,7 +190,7 @@ public void receive(MessageContext messageContext) throws Exception {
private String getMessageContent(WebServiceMessage message) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
message.writeTo(bos);
return bos.toString("UTF-8");
return bos.toString(StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -71,7 +72,7 @@ public abstract class AbstractWebServiceMessageTestCase {
private String getExpectedString() throws IOException {

StringWriter expectedWriter = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(payload.getInputStream(), "UTF-8"), expectedWriter);
FileCopyUtils.copy(new InputStreamReader(payload.getInputStream(), StandardCharsets.UTF_8), expectedWriter);
return expectedWriter.toString();
}

Expand Down Expand Up @@ -124,7 +125,7 @@ public void testEventReaderPayload() throws Exception {
@Test
public void testReaderPayload() throws Exception {

Reader reader = new InputStreamReader(payload.getInputStream(), "UTF-8");
Reader reader = new InputStreamReader(payload.getInputStream(), StandardCharsets.UTF_8);
StreamSource streamSource = new StreamSource(reader, payload.getURL().toString());
transformer.transform(streamSource, webServiceMessage.getPayloadResult());
StringWriter resultWriter = new StringWriter();
Expand Down Expand Up @@ -158,7 +159,7 @@ public void testStreamPayload() throws Exception {
ByteArrayOutputStream expectedStream = new ByteArrayOutputStream();
FileCopyUtils.copy(payload.getInputStream(), expectedStream);

assertThat(resultStream.toString("UTF-8")).and(expectedStream.toString("UTF-8")).ignoreWhitespace().areIdentical();
assertThat(resultStream.toString(StandardCharsets.UTF_8)).and(expectedStream.toString(StandardCharsets.UTF_8)).ignoreWhitespace().areIdentical();

validateMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.xmlunit.assertj.XmlAssert.*;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -83,6 +84,6 @@ public void testWriteTo() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
message.writeTo(os);

assertThat(os.toString("UTF-8")).and(content).ignoreWhitespace().areIdentical();
assertThat(os.toString(StandardCharsets.UTF_8)).and(content).ignoreWhitespace().areIdentical();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void testStaxSourceStreamReader() throws Exception {
@Test
public void testStreamSourceInputStream() throws Exception {

InputStream is = new ByteArrayInputStream(REQUEST.getBytes("UTF-8"));
InputStream is = new ByteArrayInputStream(REQUEST.getBytes(StandardCharsets.UTF_8));
testSource(new StreamSource(is));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;

Expand All @@ -49,7 +50,7 @@ public void properties() throws IOException {
SoapMessage soapMessage = (SoapMessage) messageFactory.createWebServiceMessage();
ByteArrayOutputStream os = new ByteArrayOutputStream();
soapMessage.writeTo(os);
String result = os.toString("UTF-8");
String result = os.toString(StandardCharsets.UTF_8);

assertThat(result).startsWith("<?xml version=\"1.0\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void testWriteToTransportOutputStream() throws Exception {
String soapAction = "http://springframework.org/spring-ws/Action";
soapMessage.setSoapAction(soapAction);
soapMessage.writeTo(tos);
String result = bos.toString("UTF-8");
String result = bos.toString(StandardCharsets.UTF_8);

XmlAssert.assertThat(result)
.and("<" + getNS() + ":Envelope xmlns:" + getNS() + "='http://schemas.xmlsoap.org/soap/envelope/'>"
Expand All @@ -88,7 +89,7 @@ public void testWriteToTransportOutputStream() throws Exception {
@Override
public void testWriteToTransportResponseAttachment() throws Exception {

InputStreamSource inputStreamSource = new ByteArrayResource("contents".getBytes("UTF-8"));
InputStreamSource inputStreamSource = new ByteArrayResource("contents".getBytes(StandardCharsets.UTF_8));
soapMessage.addAttachment("contentId", inputStreamSource, "text/plain");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MockTransportOutputStream tos = new MockTransportOutputStream(bos);
Expand Down Expand Up @@ -140,7 +141,7 @@ public void testSetLiveDocument() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
soapMessage.writeTo(bos);

String result = bos.toString("UTF-8");
String result = bos.toString(StandardCharsets.UTF_8);

XmlAssert.assertThat(result)
.and("<" + getNS() + ":Envelope xmlns:" + getNS() + "='http://schemas.xmlsoap.org/soap/envelope/'>"
Expand Down Expand Up @@ -169,7 +170,7 @@ public void testSetOtherDocument() throws Exception {
bos = new ByteArrayOutputStream();
soapMessage.writeTo(bos);

String result = bos.toString("UTF-8");
String result = bos.toString(StandardCharsets.UTF_8);

XmlAssert.assertThat(result)
.and("<" + getNS() + ":Envelope xmlns:" + getNS() + "='http://schemas.xmlsoap.org/soap/envelope/'>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void testWriteToTransportOutputStream() throws Exception {
String soapAction = "http://springframework.org/spring-ws/Action";
soapMessage.setSoapAction(soapAction);
soapMessage.writeTo(tos);
String result = bos.toString("UTF-8");
String result = bos.toString(StandardCharsets.UTF_8);

XmlAssert.assertThat(result)
.and("<" + getNS() + ":Envelope xmlns:" + getNS() + "='http://www.w3.org/2003/05/soap-envelope'>" + getHeader()
Expand All @@ -89,7 +90,7 @@ public void testWriteToTransportOutputStream() throws Exception {
@Override
public void testWriteToTransportResponseAttachment() throws Exception {

InputStreamSource inputStreamSource = new ByteArrayResource("contents".getBytes("UTF-8"));
InputStreamSource inputStreamSource = new ByteArrayResource("contents".getBytes(StandardCharsets.UTF_8));
soapMessage.addAttachment("contentId", inputStreamSource, "text/plain");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MockTransportOutputStream tos = new MockTransportOutputStream(bos);
Expand Down Expand Up @@ -141,7 +142,7 @@ public void testSetLiveDocument() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
soapMessage.writeTo(bos);

String result = bos.toString("UTF-8");
String result = bos.toString(StandardCharsets.UTF_8);

XmlAssert.assertThat(result)
.and("<" + getNS() + ":Envelope xmlns:" + getNS() + "='http://www.w3.org/2003/05/soap-envelope'>" + getHeader()
Expand Down Expand Up @@ -170,7 +171,7 @@ public void testSetOtherDocument() throws Exception {
bos = new ByteArrayOutputStream();
soapMessage.writeTo(bos);

String result = bos.toString("UTF-8");
String result = bos.toString(StandardCharsets.UTF_8);

XmlAssert.assertThat(result)
.and("<" + getNS() + ":Envelope xmlns:" + getNS() + "='http://www.w3.org/2003/05/soap-envelope'>" + getHeader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse
assertThat(httpServletRequest.getHeader(REQUEST_HEADER_NAME)).isEqualTo(REQUEST_HEADER_VALUE);

String receivedRequest = new String(FileCopyUtils.copyToByteArray(httpServletRequest.getInputStream()),
"UTF-8");
StandardCharsets.UTF_8);

XmlAssert.assertThat(receivedRequest).and(SOAP_REQUEST).ignoreWhitespace().areIdentical();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.xml.soap.SOAPMessage;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void setUp() throws Exception {
@Test
public void receive() throws Exception {

byte[] bytes = SOAP_CONTENT.getBytes("UTF-8");
byte[] bytes = SOAP_CONTENT.getBytes(StandardCharsets.UTF_8);
httpServletRequest.addHeader("Content-Type", "text/xml");
httpServletRequest.addHeader("Content-Length", Integer.toString(bytes.length));
httpServletRequest.addHeader(HEADER_NAME, HEADER_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Properties;

Expand Down Expand Up @@ -140,7 +141,7 @@ public void testPreserveCustomHeaders() throws Exception {
SoapMessage message = loadSoap11Message("customHeader-soap.xml");
MessageContext messageContext = new DefaultMessageContext(message, getSoap11MessageFactory());
message.writeTo(os);
String document = os.toString("UTF-8");
String document = os.toString(StandardCharsets.UTF_8);

assertXpathEvaluatesTo("Header 1 does not exist", "test1", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header1",
document);
Expand All @@ -153,7 +154,7 @@ public void testPreserveCustomHeaders() throws Exception {

os = new ByteArrayOutputStream();
message.writeTo(os);
document = os.toString("UTF-8");
document = os.toString(StandardCharsets.UTF_8);

assertXpathEvaluatesTo("Header 1 does not exist", "test1", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header1",
document);
Expand All @@ -162,7 +163,7 @@ public void testPreserveCustomHeaders() throws Exception {

os = new ByteArrayOutputStream();
message.writeTo(os);
document = os.toString("UTF-8");
document = os.toString(StandardCharsets.UTF_8);

assertXpathEvaluatesTo("Header 1 does not exist", "test1", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header1",
document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import javax.xml.namespace.QName;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void withPayloadSource() throws Exception {
public void withPayloadResource() throws Exception {

String payload = "<payload xmlns='http://springframework.org'/>";
ResponseCreator responseCreator = ResponseCreators.withPayload(new ByteArrayResource(payload.getBytes("UTF-8")));
ResponseCreator responseCreator = ResponseCreators.withPayload(new ByteArrayResource(payload.getBytes(StandardCharsets.UTF_8)));

WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);

Expand Down Expand Up @@ -101,7 +102,7 @@ public void withSoapEnvelopeResource() throws Exception {
xmlBuilder.append("</soap:Envelope>");
String envelope = xmlBuilder.toString();
ResponseCreator responseCreator = ResponseCreators
.withSoapEnvelope(new ByteArrayResource(envelope.getBytes("UTF-8")));
.withSoapEnvelope(new ByteArrayResource(envelope.getBytes(StandardCharsets.UTF_8)));
WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory);

XmlAssert.assertThat(getSoapEnvelopeAsString((SoapMessage) response)).and(envelope).ignoreWhitespace().areSimilar();
Expand Down