SAAJ replaces xop:Include link with inline attachment - java

We prepare multipart SOAP request using SAAJ implementation provided by WAS 8.5.5 and it looks to be correct until we don't add XOP link referencing attachment being sent as a part:
<xop:Include href="cid:my_attachment_id" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
Once such link is added into envelope, it's replaced by SAAJ with base64-encoded content of attachment. As a result, attachment is sent twice, in binary and base64 forms.
Any idea how such unexpected inlining could be prevented?
UPD trying to followup the comment
We fill element named "data" of type base64Binary and then check the request with wireshark. Say we have 2 cases:
1) Non-XOP link like below is sent as is, confirmed by wireshark
<data><link href="cid:my_attachment_id"/></data>
2) XOP link formed initially as
<data><xop:Include href="cid:my_attachment_id"
xmlns:xop="http://www.w3.org/2004/08/xop/include"/></data>
according to wireshark is translated into
<data>PGh0dHA6Ly9mb28uY29tPg0KW2Zvb10oaHR0cDov....</data>
The content of "data" element here is exactly base64-encoded attachment identified by the given content-id (my_attachment_id).

Related

adding raw text to a SOAP header

In Java, I need to add a SOAP header containing authentication data for a client-side SOAP request to a 3rd party web service that I have no control over. I have set up a SOAPHandler, and am monitoring the actual code sent to the server-side to see what is going on. The authentication is not working.
What I precisely need is:
<soap12:Header>
<AuthenticationHeader xmlns="http://abc.xyz.com/">
<UserName>uname</UserName>
<Password>pwd</Password>
</AuthenticationHeader>
</soap12:Header>
I can create the AuthenticationHeader element and add it with the SOAPHeader.addHeaderElement method without issue, but I can't get the username and password content added properly. What I can get from using the various commands to set the text for a SOAPElement (setValue, setTextContent, addTextNode), and then using SOAPHeaderElement.addChildElement is
<soap12:Header>
<AuthenticationHeader xmlns="http://abc.xyz.com/">
<UserName xmlns="">uname</UserName>
<Password xmlns="">pwd</Password>
</AuthenticationHeader>
</soap12:Header>
The server side can't cope with the extra xmlns="" that all the commands I have tried seem to add to the UserName and Password tag names.
If I try to construct a string and add that text directly to the AutheticationHeader element, the < and > characters are escaped. I've also tried CDATA tags...that prevents escaping of the < and > characters, but the server still doesn't like it.
I found a solution constructing a Document to hold the raw text (which prevents escaping the characters), and then adding that, but unfortunately, it seems that Document can only be added to the SOAP body, not the SOAP header.
Does anyone have any suggestions how to overcome this? The server people are programming in .NET, not Java, so they aren't able to help.
I am getting the proper response back from the server, with all off the correct SOAP output tags, but the error message contained in the tag is "Can't Authenticate".
Thanks!
Following code will generate the correct Authentication Header that you would like to generate.
SOAPHeader header = request.getSOAPHeader();
Document doc = header.getOwnerDocument();
Element el1 = doc.createElementNS("http://abc.xyz.com/","UserName");
el1.setTextContent("MyName");
Element el2 = doc.createElementNS("http://abc.xyz.com/","Password");
el2.setTextContent("pass******");
Element el0 = doc.createElementNS("http://abc.xyz.com/", "AuthenticationHeader");
el0.appendChild(el1);
el0.appendChild(el2);
header.appendChild(el0);

JavaMail content-transfer-encoding issue

I have some Java code which sends out an email with code somewhat like the following: Actually i got Mimemessage from Httprequest param and in that mimemessage i'm going to append some content to existing body.
If Mimemessage is of Multipart content-type , i'm not facing any issue while sending message.
If the message is of text/plain and text/html content-type, the content-transfer encoding which i set didn't applied to body.
Based on this docs
Q: Even though JavaMail does all the encoding and decoding for me, I need to manually control the encoding for some body parts. A: In the rare case that you need to control the encoding, there are several ways to override JavaMail's default behavior. A simple approach is as follows. After creating the entire message, call msg.saveChanges() and then use something like mbp.setHeader("Content-Transfer-Encoding", "base64") to force base64 encoding for the given body part.
Another approach is to subclass MimeBodyPart and override the updateHeaders method so that it first calls super.updateHeaders() and then sets the Content-Transfer-Encoding header as above.
Applied above also. But it doesn't works for me.
InputStream ins = request.getInputStream();
MimeMessage msg = new MimeMessage(session,ins);
msg.setContent("some non-Ascii content","text/plain; charset="UTF-8"");
//Tried setheader before saveChanges() method, also doesn't work for me
//msg.setHeader("Content-Transfer-Encoding","base64");
msg.saveChanges();
//Now tried based on above docs after saveChanges method, also doesn't work
msg.setHeader("Content-Transfer-Encoding","base64");
please help to solve this.
You duplicated most of this question in your other post, and I answered part of it there.
You would probably be better off sending the content for the mail message in the http request, then creating a new message on the server based on that content, instead of trying to send a complete MIME message to the server that you then edit.

GAE: Incoming emails can not preserve format

I've set up my GAE/Java project to receiving emails and it works pretty fine excepting it can not preserve the incoming mail's format(e.g. bold, italic, font size, text color, bulleted list...), and the content type of incoming mails are always "text/plain", as a result from the end user's view the mail content huddled and unreadable.
For example I send a formatted mail from Gmail, when I receiving the mail in GAE all formats is tripped off and leaves a bulk of plain text.
Is there any way I can get incoming mail type as HTML so the format would be preserved?
While sending the mail through server. Set the body content type text/html.
.
.
.
htmlPart = new MimeBodyPart();
htmlPart.setContent("<b>html content</b>", "text/html");
This should work for you..
Looks like a duplicate of this question and answer
Moreover, I am copying a few excerpts from Google App Engine Documentation here which says:
The message contains a subject, a plaintext body, and an optional HTML body.
It can also contain file attachments, as well as a limited set of headers.
And I am guessing the content type should be text/html

How to convert a zip file into an octet stream

I am working on SOAP web services. I have been given a third party WSDL URL for which I need to generate client code. I need to pass a zip file which contains a bundle of XML, for which the passing parameter should be type octet stream. I don't know how to pass an argument as an octet stream. How can I do this?
A SOAP Web-service needs to return the result in a format defined by SOAP - basically in XML. Octet streams are a different mechanism of file/data transfer, so that's not what you need.
What you need to use is "SOAP with Attachments" or "MTOM (Message Transmission Optimization Mechanism)", where you ZIP file will be attached to the SOAP message in some way. If you're using Java with Axis 2, This article in the Axis documentation describes what you what you need to do.
Remember that your webservice clients will also need to understand MTOM or SwA. So pick the approach that is easily supported by your clients.

Setting multiple content encodings in MimeMessage

When using javax.mail.*, I'm trying to send a message with the content encoded in both text/plain and text/html. How can I add both encodings to the MimeMessage?
Does setText override the previous text set? ie: if I do setText("", "text/plain") then setText("", "text/html"), will the secord call override the message text previously set or will they both be present in the message?
Q: How do I send mail with both plain text as well as HTML text so that each mail reader can choose the format appropriate for it?
A: You'll want to send a MIME multipart/alternative message. You construct such a message essentially the same way you construct a multipart/mixed message, using a MimeMultipart object constructed using new MimeMultipart("alternative"). You then insert the text/plain body part as the first part in the multpart and insert the text/html body part as the second part in the multipart. You'll need to construct the plain and html parts yourself to have appropriate content. See RFC2046 for details of the structure of such a message.
http://www.oracle.com/technetwork/java/faq-135477.html#sendmpa

Categories

Resources