use of javax.xml.soap.SOAPPart in project cxf by apache.
the class W3CDOMStreamReaderTest method testReader.
@Test
public void testReader() throws Exception {
ByteArrayInputStream is = new ByteArrayInputStream("<Test xmlns=\"http://example.org/types\"><argument>foobar</argument></Test>".getBytes());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage();
SOAPPart part = msg.getSOAPPart();
Document doc = docBuilder.parse(is);
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(part.getEnvelope());
XMLStreamReader reader = StaxUtils.createXMLStreamReader(new DOMSource(doc));
StaxUtils.copy(reader, writer);
assertTrue(StaxUtils.toString(writer.getDocument()).endsWith(RESULT));
}
use of javax.xml.soap.SOAPPart in project cxf by apache.
the class SoapFaultSerializerTest method testCXF4181.
@Test
public void testCXF4181() throws Exception {
// Try WITH SAAJ
SoapMessage m = new SoapMessage(new MessageImpl());
m.put(Message.HTTP_REQUEST_METHOD, "POST");
m.setVersion(Soap12.getInstance());
XMLStreamReader reader = StaxUtils.createXMLStreamReader(this.getClass().getResourceAsStream("cxf4181.xml"));
m.setContent(XMLStreamReader.class, reader);
new SAAJPreInInterceptor().handleMessage(m);
new ReadHeadersInterceptor(null).handleMessage(m);
new StartBodyInterceptor().handleMessage(m);
new SAAJInInterceptor().handleMessage(m);
new Soap12FaultInInterceptor().handleMessage(m);
Node nd = m.getContent(Node.class);
SOAPPart part = (SOAPPart) nd;
assertEquals("S", part.getEnvelope().getPrefix());
assertEquals("S2", part.getEnvelope().getHeader().getPrefix());
assertEquals("S3", part.getEnvelope().getBody().getPrefix());
SOAPFault fault = part.getEnvelope().getBody().getFault();
assertEquals("S", fault.getPrefix());
assertEquals("Authentication Failure", fault.getFaultString());
SoapFault fault2 = (SoapFault) m.getContent(Exception.class);
assertNotNull(fault2);
assertEquals(Soap12.getInstance().getSender(), fault2.getFaultCode());
assertEquals(new QName("http://schemas.xmlsoap.org/ws/2005/02/trust", "FailedAuthentication"), fault2.getSubCode());
Element el = part.getEnvelope().getBody();
nd = el.getFirstChild();
int count = 0;
while (nd != null) {
if (nd instanceof Element) {
count++;
}
nd = nd.getNextSibling();
}
assertEquals(1, count);
// Try WITHOUT SAAJ
m = new SoapMessage(new MessageImpl());
m.setVersion(Soap12.getInstance());
reader = StaxUtils.createXMLStreamReader(this.getClass().getResourceAsStream("cxf4181.xml"));
m.setContent(XMLStreamReader.class, reader);
m.put(Message.HTTP_REQUEST_METHOD, "POST");
new ReadHeadersInterceptor(null).handleMessage(m);
new StartBodyInterceptor().handleMessage(m);
new Soap12FaultInInterceptor().handleMessage(m);
// nd = m.getContent(Node.class);
fault2 = (SoapFault) m.getContent(Exception.class);
assertNotNull(fault2);
assertEquals(Soap12.getInstance().getSender(), fault2.getFaultCode());
assertEquals(new QName("http://schemas.xmlsoap.org/ws/2005/02/trust", "FailedAuthentication"), fault2.getSubCode());
}
use of javax.xml.soap.SOAPPart in project cxf by apache.
the class SAAJInInterceptor method handleMessage.
@SuppressWarnings("unchecked")
public void handleMessage(SoapMessage message) throws Fault {
if (isGET(message)) {
return;
}
Boolean bodySet = (Boolean) message.get(BODY_FILLED_IN);
if (Boolean.TRUE.equals(bodySet)) {
return;
}
message.put(BODY_FILLED_IN, Boolean.TRUE);
try {
SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
if (soapMessage == null) {
MessageFactory factory = preInterceptor.getFactory(message);
soapMessage = factory.createMessage();
message.setContent(SOAPMessage.class, soapMessage);
}
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
if (xmlReader == null) {
return;
}
final SOAPPart part = soapMessage.getSOAPPart();
Document node = (Document) message.getContent(Node.class);
if (node != part && node != null) {
StaxUtils.copy(node, new SAAJStreamWriter(part));
} else {
SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope();
if (node == null) {
adjustPrefixes(env, (String) message.get(ReadHeadersInterceptor.ENVELOPE_PREFIX), (String) message.get(ReadHeadersInterceptor.BODY_PREFIX));
}
List<XMLEvent> events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.ENVELOPE_EVENTS);
applyEvents(events, env);
SOAPBody body = soapMessage.getSOAPBody();
events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.BODY_EVENTS);
applyEvents(events, body);
}
message.setContent(Node.class, soapMessage.getSOAPPart());
Collection<Attachment> atts = message.getAttachments();
if (atts != null) {
for (Attachment a : atts) {
if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
try {
((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(message);
} catch (IOException e) {
throw new Fault(e);
}
}
AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
Iterator<String> i = a.getHeaderNames();
while (i != null && i.hasNext()) {
String h = i.next();
String val = a.getHeader(h);
ap.addMimeHeader(h, val);
}
if (StringUtils.isEmpty(ap.getContentId())) {
ap.setContentId(a.getId());
}
soapMessage.addAttachmentPart(ap);
}
}
// replace header element if necessary
if (message.hasHeaders()) {
replaceHeaders(soapMessage, message);
}
if (soapMessage.getSOAPPart().getEnvelope().getHeader() == null) {
soapMessage.getSOAPPart().getEnvelope().addHeader();
}
// If we have an xmlReader that already is counting the attributes and such
// then we don't want to rely on the system level defaults in StaxUtils.copy
// CXF-6173
boolean secureReader = StaxUtils.isSecureReader(xmlReader, message);
StaxUtils.copy(xmlReader, new SAAJStreamWriter(soapMessage.getSOAPPart(), soapMessage.getSOAPPart().getEnvelope().getBody()), true, !secureReader);
DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart().getEnvelope().getBody());
xmlReader = StaxUtils.createXMLStreamReader(bodySource);
xmlReader.nextTag();
// move past body tag
xmlReader.nextTag();
message.setContent(XMLStreamReader.class, xmlReader);
} catch (SOAPException soape) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), soape, message.getVersion().getSender());
} catch (XMLStreamException e) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), e, message.getVersion().getSender());
}
}
use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.
the class XMLABaseComponent method discover.
/**
* discover
*
* @param request
* @param discoverUrl
* @param restrictions
* @param properties
* @param rh
* @throws XMLAException
*/
private void discover(final String request, final URL discoverUrl, final Map restrictions, final Map properties, final Rowhandler rh) throws XMLAException {
try {
SOAPConnection connection = scf.createConnection();
SOAPMessage msg = mf.createMessage();
MimeHeaders mh = msg.getMimeHeaders();
// $NON-NLS-1$ //$NON-NLS-2$
mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Discover\"");
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
// $NON-NLS-1$//$NON-NLS-2$
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
// $NON-NLS-1$ //$NON-NLS-2$
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
SOAPBody body = envelope.getBody();
// $NON-NLS-1$//$NON-NLS-2$
Name nDiscover = envelope.createName("Discover", "", XMLABaseComponent.XMLA_URI);
SOAPElement eDiscover = body.addChildElement(nDiscover);
eDiscover.setEncodingStyle(XMLABaseComponent.ENCODING_STYLE);
// $NON-NLS-1$//$NON-NLS-2$
Name nPara = envelope.createName("RequestType", "", XMLABaseComponent.XMLA_URI);
SOAPElement eRequestType = eDiscover.addChildElement(nPara);
eRequestType.addTextNode(request);
// add the parameters
if (restrictions != null) {
addParameterList(envelope, eDiscover, "Restrictions", "RestrictionList", // $NON-NLS-1$ //$NON-NLS-2$
restrictions);
}
// $NON-NLS-1$//$NON-NLS-2$
addParameterList(envelope, eDiscover, "Properties", "PropertyList", properties);
msg.saveChanges();
debug(// $NON-NLS-1$
Messages.getInstance().getString("XMLABaseComponent.DEBUG_0006_DISCOVER_REQUEST") + request);
logSoapMsg(msg);
// run the call
SOAPMessage reply = connection.call(msg, discoverUrl);
debug(// $NON-NLS-1$
Messages.getInstance().getString("XMLABaseComponent.DEBUG_0007_DISCOVER_RESPONSE") + request);
logSoapMsg(reply);
errorCheck(reply);
SOAPElement eRoot = findDiscoverRoot(reply);
// $NON-NLS-1$ //$NON-NLS-2$
Name nRow = envelope.createName("row", "", XMLABaseComponent.ROWS_URI);
Iterator itRow = eRoot.getChildElements(nRow);
while (itRow.hasNext()) {
// RowLoop
SOAPElement eRow = (SOAPElement) itRow.next();
rh.handleRow(eRow, envelope);
}
// RowLoop
connection.close();
} catch (UnsupportedOperationException e) {
throw new XMLAException(e);
} catch (SOAPException e) {
throw new XMLAException(e);
}
}
use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.
the class XMLABaseComponent method soapFault.
/**
* check SOAP reply for Error, return fault Code
*
* @param reply the message to check
* @param aReturn ArrayList containing faultcode,faultstring,faultactor
*/
private boolean soapFault(final SOAPMessage reply, final String[] faults) throws SOAPException {
SOAPPart sp = reply.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody body = envelope.getBody();
if (!body.hasFault()) {
return false;
}
SOAPFault fault = body.getFault();
faults[0] = fault.getFaultCode();
faults[1] = fault.getFaultString();
faults[2] = fault.getFaultActor();
// probably not neccessary with Microsoft;
Detail detail = fault.getDetail();
if (detail == null) {
return true;
}
// $NON-NLS-1$
String detailMsg = "";
Iterator it = detail.getDetailEntries();
for (; it.hasNext(); ) {
DetailEntry det = (DetailEntry) it.next();
Iterator ita = det.getAllAttributes();
for (boolean cont = false; ita.hasNext(); cont = true) {
Name name = (Name) ita.next();
if (cont) {
// $NON-NLS-1$
detailMsg += "; ";
}
detailMsg += name.getLocalName();
// $NON-NLS-1$
detailMsg += " = ";
detailMsg += det.getAttributeValue(name);
}
}
faults[3] = detailMsg;
return true;
}
Aggregations