本文整理汇总了Java中javax.xml.soap.AttachmentPart类的典型用法代码示例。如果您正苦于以下问题:Java AttachmentPart类的具体用法?Java AttachmentPart怎么用?Java AttachmentPart使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AttachmentPart类属于javax.xml.soap包,在下文中一共展示了AttachmentPart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
public void test() throws Exception {
File file = new File("message.xml");
file.deleteOnExit();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = createMessage(mf);
// Save the soap message to file
try (FileOutputStream sentFile = new FileOutputStream(file)) {
msg.writeTo(sentFile);
}
// See if we get the image object back
try (FileInputStream fin = new FileInputStream(file)) {
SOAPMessage newMsg = mf.createMessage(msg.getMimeHeaders(), fin);
newMsg.writeTo(new ByteArrayOutputStream());
Iterator<?> i = newMsg.getAttachments();
while (i.hasNext()) {
AttachmentPart att = (AttachmentPart) i.next();
Object obj = att.getContent();
if (!(obj instanceof StreamSource)) {
fail("Got incorrect attachment type [" + obj.getClass() + "], " +
"expected [javax.xml.transform.stream.StreamSource]");
}
}
}
}
示例2: createMessage
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
private SOAPMessage createMessage(MessageFactory mf) throws SOAPException, IOException {
SOAPMessage msg = mf.createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
Name name = envelope.createName("hello", "ex", "http://example.com");
envelope.getBody().addChildElement(name).addTextNode("THERE!");
String s = "<root><hello>THERE!</hello></root>";
AttachmentPart ap = msg.createAttachmentPart(
new StreamSource(new ByteArrayInputStream(s.getBytes())),
"text/xml"
);
msg.addAttachmentPart(ap);
msg.saveChanges();
return msg;
}
示例3: getAttachmentsInfo
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
/**
* Returns a string containing info about all the SOAP attachments.
*
* @param soapMessage SOAP message
* @return String containing attachments info
*/
public static String getAttachmentsInfo(SOAPMessage soapMessage) {
try {
int numOfAttachments = soapMessage.countAttachments();
Iterator attachments = soapMessage.getAttachments();
StringBuilder buf = new StringBuilder("Number of attachments: ");
buf.append(numOfAttachments);
int count = 1;
while (attachments.hasNext()) {
AttachmentPart attachment = (AttachmentPart) attachments.next();
buf.append(" | #").append(count);
buf.append(" | Content Location: ").append(attachment.getContentLocation());
buf.append(" | Content Id: ").append(attachment.getContentId());
buf.append(" | Content Size: ").append(attachment.getSize());
buf.append(" | Content Type: ").append(attachment.getContentType());
count++;
}
return buf.toString();
} catch (SOAPException e) {
LOGGER.error(e.getMessage(), e);
return "";
}
}
示例4: handleInbound
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
@Override
protected boolean handleInbound(SOAPMessageContext msgContext)
{
SOAPMessage soapMessage = msgContext.getMessage();
Iterator<?> it = soapMessage.getAttachments();
while(it.hasNext())
{
try
{
AttachmentPart attachment = (AttachmentPart)it.next();
System.out.println("Recv " + attachment.getContentType() + " attachment:");
System.out.println("'"+attachment.getContent()+"'");
return true;
}
catch (SOAPException e)
{
throw new RuntimeException("Failed to access attachment data");
}
}
throw new IllegalStateException("Missing attachment on the client side");
}
示例5: parseAll
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
public void parseAll() throws MessagingException {
if (parsed) {
return;
}
if (soapPart == null) {
readSOAPPart();
}
List<MIMEPart> prts = mm.getAttachments();
for(MIMEPart part : prts) {
if (part != soapPart) {
AttachmentPart attach = new AttachmentPartImpl(part);
this.addBodyPart(new MimeBodyPart(part));
}
}
parsed = true;
}
示例6: getStringAttachment
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
/**
* Searches the attachment with the given content id and returns its string
* contents. If there's no attachment with the given content id or its value
* is not a string, null is returned.
*
* @param contentId content id of the attachment
* @param attachments list of attachments to be searched
* @return string value of the attachment or null
*/
public static String getStringAttachment(String contentId, Iterator attachments) {
if (attachments == null) {
return null;
}
try {
while (attachments.hasNext()) {
AttachmentPart att = (AttachmentPart) attachments.next();
if (att.getContentId().equals(contentId)) {
return new Scanner(att.getRawContent(), CHARSET).useDelimiter("\\A").next();
}
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
示例7: getResponsePayloads
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
/**
* Get the payload from the SOAP response. This should be called
* during {@link #onResponse()}.
*
* @return A set of payload in SOAP message.
*
* @throws SOAPException
* When unable to extract the payload in the SOAP Response.
* @throws IOException
* When unable to open the input stream for the payload.
*/
public Payload[] getResponsePayloads() throws SOAPException, IOException{
if (this.response == null)
return null;
int index = 0;
Iterator itr = this.response.getAttachments();
Payload[] payloads = new Payload[this.response.countAttachments()];
while(itr.hasNext()){
AttachmentPart ap = (AttachmentPart) itr.next();
payloads[index] = new Payload(
ap.getDataHandler().getInputStream(),
ap.getContentType());
index++;
}
return payloads;
}
示例8: testSendMultipartSoapMessage
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
@Test
@RunAsClient
public void testSendMultipartSoapMessage() throws Exception {
final MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
final SOAPMessage msg = msgFactory.createMessage();
final SOAPBodyElement bodyElement = msg.getSOAPBody().addBodyElement(
new QName("urn:ledegen:soap-attachment:1.0", "echoImage"));
bodyElement.addTextNode("cid:" + IN_IMG_NAME);
final AttachmentPart ap = msg.createAttachmentPart();
ap.setDataHandler(getResource("saaj/jbws3857/" + IN_IMG_NAME));
ap.setContentId(IN_IMG_NAME);
msg.addAttachmentPart(ap);
final SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
final SOAPConnection connection = conFactory.createConnection();
final SOAPMessage response = connection.call(msg, new URL("http://" + baseURL.getHost()+ ":" + baseURL.getPort() + "/" + PROJECT_NAME + "/testServlet"));
final String contentTypeWeHaveSent = getBodyElementTextValue(response);
assertContentTypeStarts("multipart/related", contentTypeWeHaveSent);
}
示例9: serializeRequest
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
@Override
protected void serializeRequest(ServiceRequest request, SOAPElement soapRequest, SOAPEnvelope envelope) throws SOAPException {
// Get TestServiceRequest object
TestServiceRequest testServiceRequest = (TestServiceRequest) request.getRequestData();
// Add responseBodySize element
SOAPElement responseBodySize = soapRequest.addChildElement(envelope.createName("responseBodySize"));
responseBodySize.addTextNode(testServiceRequest.getResponseBodySize());
// Add responseAttachmentSize element
SOAPElement responseAttachmentSize = soapRequest.addChildElement(envelope.createName("responseAttachmentSize"));
responseAttachmentSize.addTextNode(testServiceRequest.getResponseAttachmentSize());
// Add request payload
SOAPElement payload = soapRequest.addChildElement(envelope.createName("payload"));
payload.addTextNode(testServiceRequest.getRequestPayload());
// Add request attachment
if (testServiceRequest.getRequestAttachment() != null && !testServiceRequest.getRequestAttachment().isEmpty()) {
String attachment = "<attachmentData>" + testServiceRequest.getRequestAttachment() + "</attachmentData>";
AttachmentPart attachPart = request.getSoapMessage().createAttachmentPart(attachment, "text/xml");
attachPart.setContentId("attachment_id");
request.getSoapMessage().addAttachmentPart(attachPart);
}
}
示例10: getOperationFromSOAPAttachment
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
private String getOperationFromSOAPAttachment(Iterator<AttachmentPart> it)
throws SOAPException {
AttachmentPart opAttachment = null;
while (it.hasNext()) {
opAttachment = it.next();
if ("<operation>".equals(opAttachment.getContentId())) {
System.out.println("Found operation in SOAP attachment");
return opAttachment.getContent().toString();
}
}
return null;
}
示例11: sendMessage
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
public SOAPMessage sendMessage() throws Exception {
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = conFactory.createConnection();
MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage msg = msgFactory.createMessage();
SOAPBodyElement bodyElement = msg.getSOAPBody().addBodyElement(new QName("urn:switchyard-quickstart:soap-attachment:1.0", "echoImage"));
bodyElement.addTextNode("cid:switchyard.png");
// CXF does not set content-type.
msg.getMimeHeaders().addHeader("Content-Type", "multipart/related; type=\"text/xml\"; start=\"<[email protected]>\"");
msg.getSOAPPart().setContentId("<[email protected]>");
AttachmentPart ap = msg.createAttachmentPart();
ap.setDataHandler(new DataHandler(new StreamDataSource()));
ap.setContentId("<switchyard.png>");
msg.addAttachmentPart(ap);
return connection.call(msg, new URL(SWITCHYARD_WEB_SERVICE));
}
示例12: sendMessage
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
public SOAPMessage sendMessage() throws Exception {
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = conFactory.createConnection();
MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage msg = msgFactory.createMessage();
SOAPBodyElement bodyElement = msg.getSOAPBody().addBodyElement(new QName("urn:switchyard-quickstart:soap-attachment:1.0", "echoImage"));
bodyElement.addTextNode("cid:switchyard.png");
msg.getSOAPPart().setContentId("<[email protected]>");
AttachmentPart ap = msg.createAttachmentPart();
ap.setDataHandler(new DataHandler(new StreamDataSource()));
ap.setContentId("<switchyard.png>");
msg.addAttachmentPart(ap);
msg.saveChanges();
// SWITCHYARD-2818/CXF-6665 - CXF does not set content-type properly.
String contentType = msg.getMimeHeaders().getHeader("Content-Type")[0];
contentType += "; start=\"<[email protected]>\"; start-info=\"application/soap+xml\"; action=\"urn:switchyard-quickstart:soap-attachment:1.0:echoImage\"";
msg.getMimeHeaders().setHeader("Content-Type", contentType);
return connection.call(msg, new URL(SWITCHYARD_WEB_SERVICE));
}
示例13: sendMessage
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
public static SOAPMessage sendMessage(String switchyard_web_service) throws Exception {
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = conFactory.createConnection();
MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage msg = msgFactory.createMessage();
SOAPBodyElement bodyElement = msg.getSOAPBody().addBodyElement(new QName("urn:switchyard-quickstart:soap-attachment:1.0", "echoImage"));
bodyElement.addTextNode("cid:switchyard.png");
AttachmentPart ap = msg.createAttachmentPart();
URL imageUrl = Classes.getResource("switchyard.png");
ap.setDataHandler(new DataHandler(new URLDataSource(imageUrl)));
ap.setContentId("switchyard.png");
msg.addAttachmentPart(ap);
msg.saveChanges();
// SWITCHYARD-2818/CXF-6665 - CXF does not set content-type properly.
String contentType = msg.getMimeHeaders().getHeader("Content-Type")[0];
contentType += "; start=\"<[email protected]>\"; start-info=\"application/soap+xml\"; action=\"urn:switchyard-quickstart:soap-attachment:1.0:echoImage\"";
msg.getMimeHeaders().setHeader("Content-Type", contentType);
return connection.call(msg, new URL(switchyard_web_service));
}
示例14: cacheSOAPMessageInfo
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
/**
* Updates information about the SOAPMessage so that
* we can determine later if it has changed
* @param sm SOAPMessage
*/
private void cacheSOAPMessageInfo(SOAPMessage sm) {
cachedSoapPart = null;
cachedSoapEnvelope = null;
cachedAttachmentParts.clear();
try {
cachedSoapPart = sm.getSOAPPart();
if (cachedSoapPart != null) {
cachedSoapEnvelope = cachedSoapPart.getEnvelope();
}
if (sm.countAttachments() > 0) {
Iterator it = sm.getAttachments();
while (it != null && it.hasNext()) {
AttachmentPart ap = (AttachmentPart) it.next();
cachedAttachmentParts.add(ap);
}
}
} catch (Throwable t) {
if (log.isDebugEnabled()) {
log.debug("Ignoring ", t);
}
}
}
示例15: invoke
import javax.xml.soap.AttachmentPart; //导入依赖的package包/类
/**
* Very simple operation.
* If there are no attachments, an exception is thrown.
* Otherwise the message is echoed.
*/
public SOAPMessage invoke(SOAPMessage soapMessage) {
System.out.println(">> SoapMessageCheckMTOMProvider: Request received.");
int numAttachments = soapMessage.countAttachments();
if (numAttachments == 0) {
System.out.println(">> SoapMessageCheckMTOMProvider: No Attachments.");
throw new WebServiceException("No Attachments are detected");
}
SOAPMessage response = null;
try {
MessageFactory factory = MessageFactory.newInstance();
response = factory.createMessage();
SOAPPart soapPart = response.getSOAPPart();
SOAPBody soapBody = soapPart.getEnvelope().getBody();
soapBody.addChildElement((SOAPBodyElement)
soapMessage.
getSOAPBody().getChildElements().next());
response.addAttachmentPart((AttachmentPart) soapMessage.getAttachments().next());
} catch (SOAPException e) {
throw new WebServiceException(e);
}
System.out.println(">> SoapMessageCheckMTOMProvider: Returning.");
return response; // echo back the same message
}