From fec8137259053776fb6f56afe588ee607baedda0 Mon Sep 17 00:00:00 2001 From: Anatoliy_Papenko Date: Mon, 21 Sep 2015 18:44:21 +0300 Subject: [PATCH 1/2] [SOAPUI-0000] HotFix Proper JMS message charset using --- .../jms/HermesJmsRequestTransport.java | 21 ++++++++++--------- .../submit/transports/jms/JMSResponse.java | 5 +++-- .../submit/transports/jms/util/JMSUtils.java | 20 ++++++++++++++++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/HermesJmsRequestTransport.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/HermesJmsRequestTransport.java index a03e7d92e3..f55a9e0ba3 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/HermesJmsRequestTransport.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/HermesJmsRequestTransport.java @@ -324,13 +324,14 @@ private String applyFilters(SubmitContext submitContext, Request request) { private Message createBytesMessageFromText(SubmitContext submitContext, String requestContent, Session session) throws JMSException { BytesMessage bytesMessage = session.createBytesMessage(); - bytesMessage.writeBytes(requestContent.getBytes()); + final Request request = (Request) submitContext.getProperty(WSDL_REQUEST); + bytesMessage.writeBytes(JMSUtils.extractBytesFromString(requestContent, request)); return bytesMessage; } private Message createTextMessageFromAttachment(SubmitContext submitContext, Request request, Session session) { try { - String content = convertStreamToString(request.getAttachments()[0].getInputStream()); + String content = convertStreamToString(request.getAttachments()[0].getInputStream(), request); TextMessage textMessageSend = session.createTextMessage(); String messageBody = PropertyExpander.expandProperties(submitContext, content); textMessageSend.setText(messageBody); @@ -341,14 +342,14 @@ private Message createTextMessageFromAttachment(SubmitContext submitContext, Req return null; } - private String convertStreamToString(InputStream is) { - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + private String convertStreamToString(InputStream is, Request request) { + BufferedReader reader = new BufferedReader(new InputStreamReader(is, JMSUtils.resolveCharset(request))); StringBuilder sb = new StringBuilder(); - String line = null; + String line; try { while ((line = reader.readLine()) != null) { - sb.append(line + "\n"); + sb.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); @@ -454,10 +455,10 @@ public void removeRequestFilter(RequestFilter filter) { @Override public void insertRequestFilter(RequestFilter filter, RequestFilter refFilter) { - int ix = filters.indexOf( refFilter ); - if( ix == -1 ) - filters.add( filter ); + int ix = filters.indexOf(refFilter); + if (ix == -1) + filters.add(filter); else - filters.add( ix, filter ); + filters.add(ix, filter); } } diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/JMSResponse.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/JMSResponse.java index 704a723a7f..8ccc8d01f0 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/JMSResponse.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/JMSResponse.java @@ -31,6 +31,7 @@ import com.eviware.soapui.impl.wsdl.WsdlRequest; import com.eviware.soapui.impl.wsdl.submit.transports.http.SSLInfo; import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse; +import com.eviware.soapui.impl.wsdl.submit.transports.jms.util.JMSUtils; import com.eviware.soapui.model.iface.Attachment; import com.eviware.soapui.model.iface.Request; import com.eviware.soapui.support.types.StringToStringsMap; @@ -118,7 +119,7 @@ public String[] getPropertyNames() { public byte[] getRawRequestData() { if (messageSend != null) { - return messageSend.toString().getBytes(); + return JMSUtils.extractBytesFromString(messageSend.toString(), request); } else { return "".getBytes(); } @@ -126,7 +127,7 @@ public byte[] getRawRequestData() { public byte[] getRawResponseData() { if (messageReceive != null) { - return messageReceive.toString().getBytes(); + return JMSUtils.extractBytesFromString(messageReceive.toString(), request); } else { return "".getBytes(); } diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/util/JMSUtils.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/util/JMSUtils.java index c961c0396e..571f3767a6 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/util/JMSUtils.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/jms/util/JMSUtils.java @@ -16,6 +16,8 @@ package com.eviware.soapui.impl.wsdl.submit.transports.jms.util; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Enumeration; import javax.jms.BytesMessage; @@ -108,4 +110,22 @@ public static byte[] extractByteArrayFromMessage(BytesMessage message) throws JM message.readBytes(bytes); return bytes; } + + public static byte[] extractBytesFromString(String source, Request request) { + if (source == null || source.isEmpty()) { + return new byte[0]; + } + final Charset charset = resolveCharset(request); + return source.getBytes(charset); + } + + public static Charset resolveCharset(Request request) { + final Charset charset; + if (request == null || request.getEncoding() == null || !Charset.isSupported(request.getEncoding())) { + charset = StandardCharsets.UTF_8; + } else { + charset = Charset.forName(request.getEncoding()); + } + return charset; + } } From acee0b4bedc03337133b393c63ca68f168d6244a Mon Sep 17 00:00:00 2001 From: Anatoliy_Papenko Date: Mon, 21 Sep 2015 18:48:29 +0300 Subject: [PATCH 2/2] [SOAPUI-0000] Use UTF-8 charset constant instead of hard-coded string --- .../http/support/attachments/HttpRequestDataSource.java | 3 ++- .../attachments/HttpRequestMimeMessageRequestEntity.java | 3 ++- .../support/attachments/MimeMessageMockResponseEntity.java | 3 ++- .../http/support/attachments/MockResponseDataSource.java | 3 ++- .../http/support/attachments/RestRequestDataSource.java | 3 ++- .../attachments/RestRequestMimeMessageRequestEntity.java | 3 ++- .../http/support/attachments/WsdlRequestDataSource.java | 3 ++- .../attachments/WsdlRequestMimeMessageRequestEntity.java | 3 ++- .../http/support/attachments/XOPPartDataSource.java | 5 +++-- 9 files changed, 19 insertions(+), 10 deletions(-) diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestDataSource.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestDataSource.java index cbd7d9cbc1..2fff783ce4 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestDataSource.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestDataSource.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.activation.DataSource; @@ -45,7 +46,7 @@ public String getContentType() { } public InputStream getInputStream() throws IOException { - byte[] bytes = requestContent.getBytes("UTF-8"); + byte[] bytes = requestContent.getBytes(StandardCharsets.UTF_8); return new ByteArrayInputStream(bytes); } diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestMimeMessageRequestEntity.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestMimeMessageRequestEntity.java index eb775472f4..64a30cba63 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestMimeMessageRequestEntity.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/HttpRequestMimeMessageRequestEntity.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; @@ -77,7 +78,7 @@ public boolean isRepeatable() { public void writeTo(OutputStream arg0) throws IOException { try { - arg0.write("\r\n".getBytes()); + arg0.write("\r\n".getBytes(StandardCharsets.UTF_8)); ((MimeMultipart) message.getContent()).writeTo(arg0); } catch (Exception e) { SoapUI.logError(e); diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MimeMessageMockResponseEntity.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MimeMessageMockResponseEntity.java index ae9ae6c599..7e9607d0fe 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MimeMessageMockResponseEntity.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MimeMessageMockResponseEntity.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; @@ -94,7 +95,7 @@ public boolean isRepeatable() { public void writeTo(OutputStream arg0) throws IOException { try { - arg0.write("\r\n".getBytes()); + arg0.write("\r\n".getBytes(StandardCharsets.UTF_8)); ((MimeMultipart) message.getContent()).writeTo(arg0); } catch (Exception e) { SoapUI.logError(e); diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MockResponseDataSource.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MockResponseDataSource.java index 13dada8e6e..f93667f043 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MockResponseDataSource.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/MockResponseDataSource.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; /** * DataSource for an existing WsdlMockResponse @@ -59,7 +60,7 @@ public String getContentType() { } public InputStream getInputStream() throws IOException { - byte[] bytes = responseContent.getBytes("UTF-8"); + byte[] bytes = responseContent.getBytes(StandardCharsets.UTF_8); return new ByteArrayInputStream(bytes); } diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestDataSource.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestDataSource.java index 49dc24efda..1858ca7617 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestDataSource.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestDataSource.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.activation.DataSource; @@ -45,7 +46,7 @@ public String getContentType() { } public InputStream getInputStream() throws IOException { - byte[] bytes = requestContent.getBytes("UTF-8"); + byte[] bytes = requestContent.getBytes(StandardCharsets.UTF_8); return new ByteArrayInputStream(bytes); } diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestMimeMessageRequestEntity.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestMimeMessageRequestEntity.java index 182e2fff12..d1c57aa770 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestMimeMessageRequestEntity.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/RestRequestMimeMessageRequestEntity.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; @@ -77,7 +78,7 @@ public boolean isRepeatable() { public void writeTo(OutputStream arg0) throws IOException { try { - arg0.write("\r\n".getBytes()); + arg0.write("\r\n".getBytes(StandardCharsets.UTF_8)); ((MimeMultipart) message.getContent()).writeTo(arg0); } catch (Exception e) { SoapUI.logError(e); diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestDataSource.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestDataSource.java index fd031222ca..d5ea09b223 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestDataSource.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestDataSource.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.activation.DataSource; @@ -54,7 +55,7 @@ public String getContentType() { } public InputStream getInputStream() throws IOException { - byte[] bytes = requestContent.getBytes("UTF-8"); + byte[] bytes = requestContent.getBytes(StandardCharsets.UTF_8); return new ByteArrayInputStream(bytes); } diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestMimeMessageRequestEntity.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestMimeMessageRequestEntity.java index d6c81a0eb7..96b4549a40 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestMimeMessageRequestEntity.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/WsdlRequestMimeMessageRequestEntity.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; @@ -92,7 +93,7 @@ public boolean isRepeatable() { @Override public void writeTo(OutputStream arg0) throws IOException { try { - arg0.write("\r\n".getBytes()); + arg0.write("\r\n".getBytes(StandardCharsets.UTF_8)); ((MimeMultipart) message.getContent()).writeTo(arg0); } catch (Exception e) { SoapUI.logError(e); diff --git a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/XOPPartDataSource.java b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/XOPPartDataSource.java index 00a542ef96..b84edda771 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/XOPPartDataSource.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/XOPPartDataSource.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import javax.activation.DataSource; @@ -72,9 +73,9 @@ public InputStream getInputStream() throws IOException { if (SchemaUtils.isInstanceOf(schemaType, XmlHexBinary.type)) { return new ByteArrayInputStream(Hex.decodeHex(content.toCharArray())); } else if (SchemaUtils.isInstanceOf(schemaType, XmlBase64Binary.type)) { - return new ByteArrayInputStream(Base64.decodeBase64(content.getBytes())); + return new ByteArrayInputStream(Base64.decodeBase64(content.getBytes(StandardCharsets.UTF_8))); } else if (SchemaUtils.isAnyType(schemaType)) { - return new ByteArrayInputStream(content.getBytes()); + return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); } else { throw new IOException("Invalid type for XOPPartDataSource; " + schemaType.getName()); }