use of javax.xml.soap.SOAPPart in project cxf by apache.
the class TestMtomProviderImpl method invoke.
public SOAPMessage invoke(final SOAPMessage request) {
try {
System.out.println("=== Received client request ===");
// create the SOAPMessage
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPBodyElement testResponse = body.addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
name.setTextContent("return detail + call detail");
SOAPElement attachinfo = testResponse.addChildElement("attachinfo", null, "http://cxf.apache.org/mime/types");
SOAPElement include = attachinfo.addChildElement("Include", "xop", "http://www.w3.org/2004/08/xop/include");
int fileSize = 0;
try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
for (int i = pre.read(); i != -1; i = pre.read()) {
fileSize++;
}
}
int count = 50;
byte[] data = new byte[fileSize * count];
for (int x = 0; x < count; x++) {
this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
}
DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
// create the image attachment
AttachmentPart attachment = message.createAttachmentPart(dh);
attachment.setContentId("mtom_xop.wsdl");
message.addAttachmentPart(attachment);
System.out.println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());
// add the reference to the image attachment
include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());
return message;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of javax.xml.soap.SOAPPart in project cxf by apache.
the class AbstractSecurityTest method getSoapMessageForDom.
protected SoapMessage getSoapMessageForDom(Document doc, String protocol) throws Exception {
SOAPMessage saajMsg = MessageFactory.newInstance(protocol).createMessage();
SOAPPart part = saajMsg.getSOAPPart();
SAAJStreamWriter writer = new SAAJStreamWriter(part);
StaxUtils.copy(doc, writer);
saajMsg.saveChanges();
MessageImpl message = new MessageImpl();
SoapMessage msg = new SoapMessage(message);
Exchange ex = new ExchangeImpl();
ex.setInMessage(msg);
msg.setContent(SOAPMessage.class, saajMsg);
return msg;
}
use of javax.xml.soap.SOAPPart in project cxf by apache.
the class SAAJOutInterceptor method handleMessage.
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage saaj = message.getContent(SOAPMessage.class);
try {
if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) {
// creating an empty SOAPHeader at this point in the
// pre-existing SOAPMessage avoids the <soap:body> and
// <soap:header> appearing in reverse order when the envolope
// is written to the wire
//
saaj.getSOAPPart().getEnvelope().addHeader();
}
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender());
}
if (saaj == null) {
SoapVersion version = message.getVersion();
try {
MessageFactory factory = getFactory(message);
SOAPMessage soapMessage = factory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
XMLStreamWriter origWriter = (XMLStreamWriter) message.get(ORIGINAL_XML_WRITER);
if (origWriter == null) {
origWriter = message.getContent(XMLStreamWriter.class);
}
message.put(ORIGINAL_XML_WRITER, origWriter);
W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart);
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.setContent(SOAPMessage.class, soapMessage);
message.setContent(Node.class, soapMessage.getSOAPPart());
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender());
}
} else if (!message.containsKey(ORIGINAL_XML_WRITER)) {
// as the SOAPMessage already has everything in place, we do not need XMLStreamWriter to write
// anything for us, so we just set XMLStreamWriter's output to a dummy output stream.
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
message.put(ORIGINAL_XML_WRITER, origWriter);
XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() {
public void write(int b) throws IOException {
}
public void write(byte[] b, int off, int len) throws IOException {
}
});
message.setContent(XMLStreamWriter.class, dummyWriter);
}
// Add a final interceptor to write the message
message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);
}
use of javax.xml.soap.SOAPPart in project cxf by apache.
the class ParseBodyTest method testReadSOAPFault.
@Test
public void testReadSOAPFault() throws Exception {
InputStream inStream = getClass().getResourceAsStream("soap12-fault.xml");
Document doc = StaxUtils.read(inStream);
SoapMessage msg = new SoapMessage(new MessageImpl());
Exchange ex = new ExchangeImpl();
ex.setInMessage(msg);
SOAPMessage saajMsg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
SOAPPart part = saajMsg.getSOAPPart();
SAAJStreamWriter writer = new SAAJStreamWriter(part);
StaxUtils.copy(doc, writer);
// Source s = new StaxSource(StaxUtils.createXMLStreamReader(doc));
// part.setContent(s);
saajMsg.saveChanges();
msg.setContent(SOAPMessage.class, saajMsg);
doc = part;
// System.out.println("OUTPUT: " + StaxUtils.toString(doc));
byte[] docbytes = getMessageBytes(doc);
// System.out.println("OUTPUT: " + new String(docbytes));
XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new NullResolver());
StaxUtils.read(db, reader, false);
}
use of javax.xml.soap.SOAPPart in project cxf by apache.
the class TestMustUnderstandHandler method handleMessage.
public boolean handleMessage(SOAPMessageContext ctx) {
boolean continueProcessing = true;
try {
Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
boolean outbound = (Boolean) b;
SOAPMessage msg = ctx.getMessage();
if (isServerSideHandler()) {
if (outbound) {
QName qname = new QName("http://cxf.apache.org/mu", "MU");
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
if (header == null) {
header = envelope.addHeader();
}
SOAPHeaderElement headerElement = header.addHeaderElement(envelope.createName("MU", "ns1", qname.getNamespaceURI()));
// QName soapMustUnderstand = new QName("http://schemas.xmlsoap.org/soap/envelope/",
// "mustUnderstand");
Name name = SOAPFactory.newInstance().createName("mustUnderstand", "soap", "http://schemas.xmlsoap.org/soap/envelope/");
headerElement.addAttribute(name, "1");
} else {
getHandlerInfoList(ctx).add(getHandlerId());
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
return continueProcessing;
}
Aggregations