本文整理汇总了Java中javax.xml.soap.AttachmentPart.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java AttachmentPart.setContentType方法的具体用法?Java AttachmentPart.setContentType怎么用?Java AttachmentPart.setContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.soap.AttachmentPart
的用法示例。
在下文中一共展示了AttachmentPart.setContentType方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addSoapAttachement
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
void addSoapAttachement() {
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
AttachmentPart a = message.createAttachmentPart();
a.setContentType("binary/octet-stream");
message.addAttachmentPart(a);
} catch (SOAPException e) {
e.printStackTrace();
}
}
示例2: addRequestPayload
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
@Override
public boolean addRequestPayload(Payload [] payloads){
if (this.request == null)
return false;
for (int i = 0; i < payloads.length; i++){
if (payloads[i] != null){
AttachmentPart ap = this.request.createAttachmentPart();
if (ap != null){
// Create file datasource.
FileDataSource fileDS = new FileDataSource(new File(payloads[i].getFilePath()));
ap.setDataHandler(new DataHandler(fileDS));
ap.setContentType(payloads[i].getContentType());
ap.addMimeHeader("Content-Disposition", "attachment; filename=" + fileDS.getName());
this.request.addAttachmentPart(ap);
if (this.log != null){
this.log.info(fileDS.getName());
this.log.info("Adding Payload " + i + " " + payloads[i].getFilePath());
}
} else{
if (this.log != null){
this.log.error("Unable to create attachment part in SOAP request at :" + i);
}
}
}
}
this.setRequestDirty(true);
return true;
}
示例3: send
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
/**
* Send the web service request to Hermes2 requesting for sending
* a <code>AS2 message</code> loopback with a set of <code>payloads</code>.
*
* @param payloads
* The payload set acting as the attachment in <code>AS2 Message</code>.
* @return
* A String representing the ID of the message you request to send.
* @throws Exception
*
* @see hk.hku.cecid.corvus.test.Payload
*/
public String send(Payload [] payloads) throws Exception {
// Make a SOAP Connection and SOAP Message.
SOAPConnection soapConn = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage request = MessageFactory.newInstance().createMessage();
// Populate the SOAP Body by filling the required parameters.
/* This is the sample WSDL request for the sending AS2 message WS request.
*
* <as2_from> as2from </as2_from>
* <as2_to> as2to </as2_to>
* <type> type </type>
*/
SOAPBody soapBody = request.getSOAPBody();
soapBody.addChildElement(createElement("as2_from", nsPrefix, nsURI, this.as2From));
soapBody.addChildElement(createElement("as2_to" , nsPrefix, nsURI, this.as2To));
soapBody.addChildElement(createElement("type" , nsPrefix, nsURI, this.type));
// Add the payloads
for (int i=0; i < payloads.length; i++) {
AttachmentPart attachmentPart = request.createAttachmentPart();
FileDataSource fileDS = new FileDataSource(new File(payloads[i].getFilePath()));
attachmentPart.setDataHandler(new DataHandler(fileDS));
attachmentPart.setContentType(payloads[i].getContentType());
request.addAttachmentPart(attachmentPart);
}
// Send the request to Hermes and return the message Id to "sender" web services.
SOAPMessage response = soapConn.call(request, senderWSURL);
SOAPBody responseBody = response.getSOAPBody();
if (!responseBody.hasFault()){
SOAPElement messageIdElement = getFirstChild(responseBody, "message_id", nsURI);
return messageIdElement == null ? null : messageIdElement.getValue();
} else {
throw new SOAPException(responseBody.getFault().getFaultString());
}
}
示例4: testMultipleAttachments
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
@Validated @Test
public void testMultipleAttachments() throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage();
AttachmentPart a1 = msg.createAttachmentPart(new DataHandler("<some_xml/>", "text/xml"));
a1.setContentType("text/xml");
msg.addAttachmentPart(a1);
AttachmentPart a2 = msg.createAttachmentPart(new DataHandler("<some_xml/>", "text/xml"));
a2.setContentType("text/xml");
msg.addAttachmentPart(a2);
AttachmentPart a3 = msg.createAttachmentPart(new DataHandler("text", "text/plain"));
a3.setContentType("text/plain");
msg.addAttachmentPart(a3);
assertTrue(msg.countAttachments() == 3);
MimeHeaders mimeHeaders = new MimeHeaders();
mimeHeaders.addHeader("Content-Type", "text/xml");
int nAttachments = 0;
Iterator iterator = msg.getAttachments(mimeHeaders);
while (iterator.hasNext()) {
nAttachments++;
AttachmentPart ap = (AttachmentPart)iterator.next();
assertTrue(ap.equals(a1) || ap.equals(a2));
}
assertTrue(nAttachments == 2);
}
示例5: addAttachment
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
public static void addAttachment(SOAPMessage soapMessage,String contentId,String contentType,InputStream content,Map<String,String> headers) throws Exception {
AttachmentPart attachmentPart = soapMessage.createAttachmentPart();
//attachmentPart.setContentId(encodeContentId(contentId));
attachmentPart.setMimeHeader("Content-ID",encodeContentId(contentId));
// attachmentPart.setMimeHeader("Content-ID",contentId);
attachmentPart.setContentType(contentType);
attachmentPart.setRawContent(content,contentType);
for(Map.Entry<String,String> header : headers.entrySet()) {
attachmentPart.addMimeHeader(header.getKey(), header.getValue());
}
soapMessage.addAttachmentPart(attachmentPart);
soapMessage.saveChanges();
soapMessage.getMimeHeaders().setHeader("Content-Type",soapMessage.getMimeHeaders().getHeader("Content-Type")[0] + "; start=\"<[email protected]>\"");
}
示例6: send
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
/**
* Send the web service request to Hermes2 requesting for sending
* a <code>EbXML message</code> loopback with a set of <code>payloads</code>.
*
* @param payloads
* The payload set acting as the attachment in <code>EbXML Message</code>.
* @return
* A String representing the ID of the message you request to send.
* @throws Exception
*
* @see hk.hku.cecid.corvus.test.Payload
*/
public String send(Payload [] payloads) throws Exception {
// Make a SOAP Connection and SOAP Message.
SOAPConnection soapConn = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage request = MessageFactory.newInstance().createMessage();
// Populate the SOAP Body by filling the required parameters.
/* This is the sample WSDL request for the sending EbMS message WS request.
*
* <cpaId> ebmscpaid </cpaId>
* <service> http://localhost:8080/corvus/httpd/ebms/inbound <service>
* <action> action </action>
* <convId> convId </convId>
* <fromPartyId> fromPartyId </fromPartyId>
* <fromPartyType> fromPartyType </fromPartyType>
* <toPartyId> toPartyId </toPartyId>
* <toPartyType> toPartyType </toPartyType>
* <refToMessageId> </refToMessageId>
*/
SOAPBody soapBody = request.getSOAPBody();
soapBody.addChildElement(createElement("cpaId", nsPrefix, nsURI, cpaId));
soapBody.addChildElement(createElement("service", nsPrefix, nsURI, service));
soapBody.addChildElement(createElement("action", nsPrefix, nsURI, action));
soapBody.addChildElement(createElement("convId", nsPrefix, nsURI, conversationId));
soapBody.addChildElement(createElement("fromPartyId", nsPrefix, nsURI, fromPartyId));
soapBody.addChildElement(createElement("fromPartyType", nsPrefix, nsURI, fromPartyType));
soapBody.addChildElement(createElement("toPartyId", nsPrefix, nsURI, toPartyId));
soapBody.addChildElement(createElement("toPartyType", nsPrefix, nsURI, toPartyType));
soapBody.addChildElement(createElement("refToMessageId", nsPrefix, nsURI, refToMessageId));
// Add the payloads
for (int i=0; i < payloads.length; i++) {
AttachmentPart attachmentPart = request.createAttachmentPart();
FileDataSource fileDS = new FileDataSource(new File(payloads[i].getFilePath()));
attachmentPart.setDataHandler(new DataHandler(fileDS));
attachmentPart.setContentType(payloads[i].getContentType());
request.addAttachmentPart(attachmentPart);
}
// Send the request to Hermes and return the message Id to "sender" web services.
SOAPMessage response = soapConn.call(request, senderWSURL);
SOAPBody responseBody = response.getSOAPBody();
if (!responseBody.hasFault()){
SOAPElement messageIdElement = getFirstChild(responseBody, "message_id", nsURI);
return messageIdElement == null ? null : messageIdElement.getValue();
} else {
throw new SOAPException(responseBody.getFault().getFaultString());
}
}
示例7: toSAAJElement
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
/**
* Converts an OMNode into a SAAJ SOAPElement
*
* @param saajEle
* @param omNode
* @param saajSOAPMsg
* @throws SOAPException If conversion fails
*/
private void toSAAJElement(SOAPElement saajEle,
OMNode omNode,
javax.xml.soap.SOAPMessage saajSOAPMsg) throws SOAPException {
if (omNode instanceof OMText) {
return; // simply return since the text has already been added to saajEle
}
if (omNode instanceof OMElement) {
OMElement omEle = (OMElement)omNode;
for (Iterator childIter = omEle.getChildren(); childIter.hasNext();) {
OMNode omChildNode = (OMNode)childIter.next();
SOAPElement saajChildEle = null;
if (omChildNode instanceof OMText) {
// check whether the omtext refers to an attachment
final OMText omText = (OMText)omChildNode;
if (omText.isOptimized()) { // is this an attachment?
final DataHandler datahandler = (DataHandler)omText.getDataHandler();
AttachmentPart attachment = saajSOAPMsg.createAttachmentPart(datahandler);
final String id = IDGenerator.generateID();
attachment.setContentId("<" + id + ">");
attachment.setContentType(datahandler.getContentType());
saajSOAPMsg.addAttachmentPart(attachment);
SOAPElement xopInclude = saajEle.addChildElement(MTOMConstants.XOP_INCLUDE,
"xop", MTOMConstants.XOP_NAMESPACE_URI);
xopInclude.addAttribute(
saajSOAPMsg.getSOAPPart().getEnvelope().createName("href"),
"cid:" + id);
} else {
saajChildEle = saajEle.addTextNode(omText.getText());
}
} else if (omChildNode instanceof OMElement) {
OMElement omChildEle = (OMElement)omChildNode;
final QName omChildQName = omChildEle.getQName();
saajChildEle =
saajEle.addChildElement(omChildQName.getLocalPart(),
omChildQName.getPrefix(),
omChildQName.getNamespaceURI());
for (Iterator attribIter = omChildEle.getAllAttributes();
attribIter.hasNext();) {
OMAttribute attr = (OMAttribute)attribIter.next();
final QName attrQName = attr.getQName();
saajChildEle.addAttribute(saajSOAPMsg.getSOAPPart().getEnvelope().
createName(attrQName.getLocalPart(),
attrQName.getPrefix(),
attrQName.getNamespaceURI()),
attr.getAttributeValue());
}
}
// go down the tree adding child elements, till u reach a leaf(i.e. text element)
toSAAJElement(saajChildEle, omChildNode, saajSOAPMsg);
}
}
}
示例8: testSendReceiveMessageWithAttachment
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
@Validated @Test
public void testSendReceiveMessageWithAttachment() throws Exception {
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage request = mf.createMessage();
//create the SOAPPart
createSOAPPart(request);
//Attach a text/plain object with the SOAP request
String sampleMessage = "Sample Message: Hello World!";
AttachmentPart textAttach = request.createAttachmentPart(sampleMessage, "text/plain");
textAttach.addMimeHeader("Content-Transfer-Encoding", "binary");
textAttach.setContentId("[email protected]");
request.addAttachmentPart(textAttach);
//Attach a java.awt.Image object to the SOAP request
DataHandler imageDH = new DataHandler(TestUtils.getTestFileAsDataSource("axis2.jpg"));
AttachmentPart jpegAttach = request.createAttachmentPart(imageDH);
jpegAttach.addMimeHeader("Content-Transfer-Encoding", "binary");
jpegAttach.setContentId("[email protected]");
jpegAttach.setContentType("image/jpg");
request.addAttachmentPart(jpegAttach);
SOAPConnection sCon = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = sCon.call(request, getAddress());
assertEquals(2, response.countAttachments());
Iterator attachIter = response.getAttachments();
int i = 0;
while (attachIter.hasNext()) {
AttachmentPart attachment = (AttachmentPart)attachIter.next();
final Object content = attachment.getDataHandler().getContent();
if (content instanceof String) {
assertEquals(sampleMessage, (String)content);
} else if (content instanceof ByteArrayInputStream) {
ByteArrayInputStream bais = (ByteArrayInputStream)content;
byte[] b = new byte[15000];
final int lengthRead = bais.read(b);
FileOutputStream fos =
new FileOutputStream(new File(System.getProperty("basedir", ".") + "/" +
"target/test-resources/result" + (i++) + ".jpg"));
fos.write(b, 0, lengthRead);
fos.flush();
fos.close();
assertTrue(attachment.getContentType().equals("image/jpeg")
|| attachment.getContentType().equals("text/plain"));
}
}
sCon.close();
}
示例9: saveMsgWithAttachments
import javax.xml.soap.AttachmentPart; //导入方法依赖的package包/类
public int saveMsgWithAttachments(OutputStream os) throws Exception {
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
SOAPElement el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI));
SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX);
el2.addTextNode("field4value");
el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI));
el2 = el.addChildElement("bodyfield3a", NS_PREFIX);
el2.addTextNode("bodyvalue3a");
el2 = el.addChildElement("bodyfield3b", NS_PREFIX);
el2.addTextNode("bodyvalue3b");
el.addChildElement("datefield", NS_PREFIX);
// First Attachment
AttachmentPart ap = msg.createAttachmentPart();
final String testText = "some attachment text...";
ap.setContent(testText, "text/plain");
msg.addAttachmentPart(ap);
// Second attachment
DataHandler dh = new DataHandler(TestUtils.getTestFileAsDataSource("axis2.jpg"));
AttachmentPart ap2 = msg.createAttachmentPart(dh);
ap2.setContentType("image/jpg");
msg.addAttachmentPart(ap2);
msg.saveChanges(); // This is only required with Sun's SAAJ implementation
MimeHeaders headers = msg.getMimeHeaders();
assertTrue(headers != null);
String [] contentType = headers.getHeader("Content-Type");
assertTrue(contentType != null);
int i = 0;
for (Iterator iter = msg.getAttachments(); iter.hasNext();) {
AttachmentPart attachmentPart = (AttachmentPart)iter.next();
final Object content = attachmentPart.getDataHandler().getContent();
if (content instanceof String) {
assertEquals(testText, (String)content);
} else if (content instanceof FileInputStream) {
// try to write to a File and check whether it is ok
final FileInputStream fis = (FileInputStream)content;
byte[] b = new byte[15000];
final int lengthRead = fis.read(b);
FileOutputStream fos =
new FileOutputStream(new File(System.getProperty("basedir", ".") + "/" +
"target/test-resources/atts-op" + (i++) + ".jpg"));
fos.write(b, 0, lengthRead);
fos.flush();
fos.close();
}
}
msg.writeTo(os);
os.flush();
return msg.countAttachments();
}