import javax.xml.bind.*; import javax.xml.soap.*; public class Demo { public static void main(String[] args) throws Exception { MessageFactory mf = MessageFactory.newInstance(); SOAPMessage message = mf.createMessage(); SOAPBody body = message.getSOAPBody(); Foo foo = new Foo(); foo.setBar("Hello World"); JAXBContext jc = JAXBContext.newInstance(Foo.class); Marshaller marshaller = jc.createMarshaller(); marshaller.marshal(foo, body); message.saveChanges(); message.writeTo(System.out); } }
@Override public boolean handleFault(SOAPMessageContext context) { if (!shouldExecute(context)) { return true; } final Span span = tracingPlugin.getTracer().scopeManager().active().span(); Tags.ERROR.set(span, Boolean.TRUE); try { final SOAPFault fault = context.getMessage().getSOAPBody().getFault(); span.setTag("soap.fault.reason", fault.getFaultString()); span.setTag("soap.fault.code", fault.getFaultCode()); } catch (SOAPException e) { logger.warn("Exception while trying to access SOAP fault (this exception was suppressed)", e); } return true; }
String example = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body><ns2:farm xmlns:ns2=\"http://adamish.com/example/farm\"><horse height=\"123\" name=\"glue factory\"/></ns2:farm></soapenv:Body></soapenv:Envelope>"; SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(example.getBytes())); Unmarshaller unmarshaller = JAXBContext.newInstance(Farm.class).createUnmarshaller(); Farm farm = (Farm)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
Farm farm = new Farm(); farm.getHorse().add(new Horse()); farm.getHorse().get(0).setName("glue factory"); farm.getHorse().get(0).setHeight(BigInteger.valueOf(123)); Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Marshaller marshaller = JAXBContext.newInstance(Farm.class).createMarshaller(); marshaller.marshal(farm, document); SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); soapMessage.getSOAPBody().addDocument(document); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); soapMessage.writeTo(outputStream); String output = new String(outputStream.toByteArray());
String example = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body><ns2:farm xmlns:ns2=\"http://adamish.com/example/farm\"><horse height=\"123\" name=\"glue factory\"/></ns2:farm></soapenv:Body></soapenv:Envelope>"; SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(example.getBytes())); Unmarshaller unmarshaller = JAXBContext.newInstance(Farm.class).createUnmarshaller(); Farm farm = (Farm)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
URL endpoint = new URL("http://www.webservicex.net/whois.asmx"); SOAPMessage message = connection.call(sm, endpoint); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new SoapNamespaceContext()); Node resultNode = (Node) xpath.evaluate("//m:GetWhoISResult", message.getSOAPBody(), XPathConstants.NODE); ... do something with resultNode...
public static boolean isFaultMessage(final SOAPMessage msg) { try { return msg.getSOAPBody().getFault() != null; } catch (final Exception ignore) { } return false; }
/** * Given a {@link SOAPMessage}, get the content as a {@link Document} * * @param soapMessage * @return * @throws SOAPException */ public static Document getSOAPData(SOAPMessage soapMessage) throws SOAPException { return soapMessage.getSOAPBody().extractContentAsDocument(); }
public static Document documentFromSoapBody(SOAPMessage soapMessage) throws ParserConfigurationException, SOAPException, TransformerException { SOAPBodyElement content = getFirstElementChild(soapMessage.getSOAPBody()); if (content == null) { throw new SOAPException("Unable to find any content in SOAPBody"); } Document document = dbf.newDocumentBuilder().newDocument(); Node node = document.importNode(content, true); document.appendChild(node); return document; }
public static String addSoapBodyUuid(SOAPMessageContext smc) throws SOAPException { String id = "_" + UUID.randomUUID().toString(); smc.getMessage().getSOAPBody().addAttribute( new QName(WSU_NAMESPACE, WSU_ID_LOCAL_NAME, WSU_PREFIX), id); return id; }
private String decodeResponse(SOAPMessage response) throws SOAPException { NodeList nodelist = response.getSOAPBody().getElementsByTagNameNS(sayHi.getNamespaceURI(), "responseType"); if (nodelist.getLength() == 1) { Node node = nodelist.item(0).getFirstChild(); if (node != null) { return node.getNodeValue(); } } return null; } }
public static SOAPBody getBody(SOAPMessage m) throws SOAPException { try { return m.getSOAPBody(); } catch (UnsupportedOperationException ex) { return m.getSOAPPart().getEnvelope().getBody(); } catch (IllegalArgumentException ex) { //java9 return null; } } public static void setFaultCode(SOAPFault f, QName code) throws SOAPException {
public static SOAPBody getBody(SOAPMessage m) throws SOAPException { try { return m.getSOAPBody(); } catch (UnsupportedOperationException ex) { return m.getSOAPPart().getEnvelope().getBody(); } catch (IllegalArgumentException ex) { //java9 return null; } } public static void setFaultCode(SOAPFault f, QName code) throws SOAPException {
SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); sm.getSOAPPart().getEnvelope().setPrefix("soap"); sm.getSOAPPart().getEnvelope().removeNamespaceDeclaration("env"); sm.getSOAPHeader().setPrefix("soap"); sm.getSOAPBody().setPrefix("soap");
//Build the SOAP Fault Response MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); SOAPMessage response = factory.createMessage(); SOAPFault fault = response.getSOAPBody().addFault(); fault.setFaultString(myCustomErrorString); fault.addDetail().addChildElement("exception").addTextNode(myCustomErrorId);
@Override protected void runTest() throws Throwable { SOAPBody body = newMessageFactory().createMessage().getSOAPBody(); SOAPElement child = body.addChildElement((SOAPElement)body.getOwnerDocument().createElementNS("urn:test", "p:test")); assertThat(child).isInstanceOf(SOAPBodyElement.class); } }
public HWDOMSourcePayloadProvider() { try { factory = MessageFactory.newInstance(); InputStream is = getClass().getResourceAsStream("resources/sayHiRpcLiteralResp.xml"); sayHiResponse = factory.createMessage(null, is).getSOAPBody().extractContentAsDocument(); is.close(); is = getClass().getResourceAsStream("resources/GreetMeRpcLiteralResp.xml"); greetMeResponse = factory.createMessage(null, is).getSOAPBody().extractContentAsDocument(); is.close(); } catch (Exception ex) { ex.printStackTrace(); } }
public SOAPMessage invoke(SOAPMessage request) { try { MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage msg = messageFactory.createMessage(); msg.getSOAPBody().addFault(new QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"), "Foo"); return msg; } catch (Exception e) { e.printStackTrace(); } return null; }
public void handleResponse(Response<SOAPMessage> response) { try { asyncHandlerInvokedCount++; SOAPMessage reply = response.get(); replyBuffer = reply.getSOAPBody().getTextContent(); } catch (Exception e) { // } }
private SOAPMessage encodeRequest(MessageFactory factory, String value) throws SOAPException { SOAPMessage request = factory.createMessage(); SOAPEnvelope envelope = request.getSOAPPart().getEnvelope(); request.setProperty("soapaction", ""); if (value != null) { request.getSOAPBody().addBodyElement(envelope.createName(value, "ns1", sayHi.getNamespaceURI())); } return request; }