I want to send a image over the SOAP service I connect with so that it sends the email to the customer with the attachment as image. I'm given the below interface to send:
<uploadedFile>
<Name>B2B51850001</Name>
<FileType>txt</FileType>
<Dfile>dGVzdCBmb3IgZW1haWwu</Dfile>
( base64 string)
</uploadedFile>
I'm creating QR byte image using zxing library and sending it to form the payload as below:
public EmailNotificationRequest sendEmailRequestQR(String name, LocalDate timestamp, String email, String mobile, byte[] base64image) {
FileUploader fileUploader = new FileUploader();
fileUploader.setName(name + "_QR");
fileUploader.setFileType(TXT);
InputStream inputStream = null;
if(base64image != null) {
inputStream = new ByteArrayInputStream(base64image);
}
InputStream finalInputStream = inputStream;
transformToIS(fileUploader, finalInputStream);
requestData.getUploadedFile().add(fileUploader);
emailNotificationRequest.setRequestData(requestData);
}
The conversion of byte to IS
private void transformToIS(FileUploader fileUploader, InputStream finalInputStream) {
fileUploader.setDfile(new DataHandler(new DataSource() {
@Override
public InputStream getInputStream() throws IOException {
return finalInputStream;
}
@Override
public OutputStream getOutputStream() throws IOException {
return null;
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public String getName() {
return "InputStreamDataSource";
}
}));
}
The byte[] image is getting converted like this:
public byte[] generateQRCodeImage(String content, int width, int height) throws WriterException, IOException {
logger.info("Inside QR Code Image Generator");
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(encode, "PNG", byteArrayOutputStream);
logger.info("Exiting Qr Code Image Generator");
return byteArrayOutputStream.toByteArray();
}
I'm seeing a string in the xml payload and when I see in the base64 to image converted I see the image. Following is being sent in the xml iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAABc0lEQVR4Xu2WW4oEIQxFBbcluHXBbQWce2LN9Dy/xvtnrLbRU2A0N7HK+svK94kPu+QS7JJ/kFFKXVH6qFH3wEOmnmB2RZ8MXGRUxqPP0eoe2MhcozXt9XnNRkZf+2idhCc0oTZz4CG58Lv90M45kja6JML6jxmIJktreiq7zLO1EAUrWp/Rs5WSyEACgczUY2LmLKSkB8idEx2vyJ0lEuAoSivaloeHjIxbVQD5W3urBqIiURQ4dSEnHokYiJYWqJyktly37A1kZrBU+VQs8IM5B5FCtDQXk37Ez0U6CqlKY3SCHx7CbfFkFmX8kwdnCWlFj9oHLpgIabX4bAhdgsri107PkrkrhBxB8bElYiFZIjjJTGYXQR6CwYHmZWgiWH5ypQq3QBwklyWDewhJlSainUnrnT7I5eoiyqe8YiV5eYP8fUS3IEKsxM9ISGDJffHKVw8OkkXaZo0lyRI4CAqRPlSRtH6jWnjIr3bJJdglp8kb4c6fyh/JBuUAAAAASUVORK5CYII=
However when the email is being sent the attachment is text file with mangled characters. Please note this is third party SOAP service with above interface.
Could someone point out if something is being wrongly sent? It doesnt make sense to feed the base64 string to the inputstream to me