MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("example", serverURI); SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example"); SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example"); soapBodyElem2.addTextNode("123"); MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", serverURI + "VerifyEmail"); soapMessage.saveChanges(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); Source sourceContent = soapResponse.getSOAPPart().getContent(); System.out.print("\nResponse SOAP Message = "); StreamResult result = new StreamResult(System.out);
private SOAPMessage getSoapMessageFromString(String xml) throws SOAPException, IOException { MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8")))); return message; }
@Test public void testDoNotOverriderSoapAction() throws Exception { MimeHeaders mimeHeaders = new MimeHeaders(); String testSoapAction = "fooAction"; mimeHeaders.setHeader(TransportConstants.HEADER_SOAP_ACTION, testSoapAction); String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"></soapenv:Envelope>"; SOAPMessage message = MessageFactory.newInstance() .createMessage(mimeHeaders, new ByteArrayInputStream(soap.getBytes())); SaajSoapMessage soapMessage = new SaajSoapMessage(message); DefaultSoapHeaderMapper headerMapper = new DefaultSoapHeaderMapper(); headerMapper.fromHeadersToRequest(new MessageHeaders(null), soapMessage); assertEquals(testSoapAction, soapMessage.getSoapAction()); }
private static Collection<String> parseSoapResponseForUrls(byte[] data) throws SOAPException, IOException { //System.out.println(new String(data)); final Collection<String> urls = new ArrayList<>(); MessageFactory factory = MessageFactory.newInstance(DeviceDiscovery.WS_DISCOVERY_SOAP_VERSION); final MimeHeaders headers = new MimeHeaders(); headers.addHeader("Content-type", DeviceDiscovery.WS_DISCOVERY_CONTENT_TYPE); SOAPMessage message = factory.createMessage(headers, new ByteArrayInputStream(data)); SOAPBody body = message.getSOAPBody(); for (Node node : getNodeMatching(body, ".*:XAddrs")) { if (node.getTextContent().length() > 0) { urls.addAll(Arrays.asList(node.getTextContent().split(" "))); } } return urls; } }
public static SOAPMessage buildSOAPMessage(final String soapAction, final String soapPartAsString) { SOAPMessage soapMessage = null; try { soapMessage = SOAPUtil.getMessageFactory().createMessage(); final MimeHeaders hd = soapMessage.getMimeHeaders(); hd.addHeader("SOAPAction", soapAction); final SOAPPart soapPart = soapMessage.getSOAPPart(); soapPart.setContent(new StreamSource(new ByteArrayInputStream(soapPartAsString.getBytes()))); soapMessage.saveChanges(); } catch (final Exception e) { throw new RuntimeException("Exception while building a SOAPMessage", e); } return soapMessage; }
MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage( new MimeHeaders(), new ByteArrayInputStream(xmlInput.getBytes(Charset .forName("UTF-8")))); SOAPBody body = message.getSOAPBody(); NodeList returnList = body.getElementsByTagName("ObtenerIndicadoresEconomicosXML");
" </soapenv:Body>\n" + "</soapenv:Envelope>"; MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to createMessage..."); SOAPMessage newMsg = messageFactory.createMessage( new MimeHeaders(), new ByteArrayInputStream(soapIn.getBytes(Charset.forName("UTF-8")))); System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to get new envelope..."); SOAPEnvelope newEnv = newMsg.getSOAPPart().getEnvelope(); //fails here System.out.println("InneoquestSoapHandler.createNewSoapResponse: about to get old envelope..."); newMsg.writeTo(System.out); System.out.println("**"); System.out.println("Envolope " + newEnv);
final SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL) .createMessage(new MimeHeaders(), new ByteArrayInputStream(yourSoapMessageWithFault.getBytes())); if (msg != null) { SOAPEnvelope env = msg.getSOAPPart().getEnvelope(); SOAPBody body = env.getBody(); if (body != null && body.hasFault()) { final SOAPFault fault = body.getFault(); final DetailEntry entry = (DetailEntry)fault.getDetail().getDetailEntries().next(); //now UNMarshall this entry to your custom exception class.
byte[] encoded = Files.readAllBytes(Paths.get("C:\\Users\\timofb\\Documents\\test.txt")); String soapXml = new String(encoded, StandardCharsets.UTF_8); SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); java.net.URL endpoint = new URL("http://" + ip + "/cs-repository/services/xds-iti41"); SOAPConnection connection = soapConnectionFactory.createConnection(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(encoded)); AttachmentPart attachment = message.createAttachmentPart(); attachment.setContent("sm_content", "text/plain"); attachment.setContentId("1.9f910338bf0cac0e783bfdec7e53be9237684caa8f8f4e6d@apache.org"); message.addAttachmentPart(attachment); SOAPMessage response = connection.call(message, endpoint); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.writeTo(out); String strMsg = new String(out.toByteArray()); return strMsg;
@Override public void writeTo(SOAPMessage message, OutputStream outputStream) throws SOAPException, IOException { if (message.saveRequired()) { message.saveChanges(); } if (outputStream instanceof TransportOutputStream) { TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream; // some SAAJ implementations (Axis 1) do not have a Content-Type header by default MimeHeaders headers = message.getMimeHeaders(); if (ObjectUtils.isEmpty(headers.getHeader(TransportConstants.HEADER_CONTENT_TYPE))) { SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); if (envelope.getElementQName().getNamespaceURI() .equals(SoapVersion.SOAP_11.getEnvelopeNamespaceUri())) { headers.addHeader(TransportConstants.HEADER_CONTENT_TYPE, SoapVersion.SOAP_11.getContentType()); } else { headers.addHeader(TransportConstants.HEADER_CONTENT_TYPE, SoapVersion.SOAP_12.getContentType()); } if (message.saveRequired()) { message.saveChanges(); } } for (Iterator<?> iterator = headers.getAllHeaders(); iterator.hasNext();) { MimeHeader mimeHeader = (MimeHeader) iterator.next(); transportOutputStream.addHeader(mimeHeader.getName(), mimeHeader.getValue()); } } message.writeTo(outputStream); }
SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.removeAttribute("xmlns:SOAP-ENV"); envelope.setPrefix("soap"); envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); MimeHeaders mimeheaders = soapMessage.getMimeHeaders(); mimeheaders.addHeader("SOAPAction", "SendRequest"); SOAPHeader header = soapMessage.getSOAPHeader(); header.detachNode();
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=\"<root.message@cxf.apache.org>\""); msg.getSOAPPart().setContentId("<root.message@cxf.apache.org>"); 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)); }
QName code = faultCode; try { MimeHeaders headers = new MimeHeaders(); Map<String, List<String>> httpHeaders; List<String> values = httpHeaders.get(key); for (String value : values) { headers.addHeader(key, value); soapMessage = msgFactory.createMessage(headers, inCtx.getInputStream()); soapMessage.getSOAPPart().getEnvelope(); } catch (SOAPException se) { LOG.log(Level.SEVERE, "SOAP_PARSING_FAILURE_MSG", se);
try { inputStream = checkForUtf8ByteOrderMark(inputStream); SOAPMessage saajMessage = messageFactory.createMessage(mimeHeaders, inputStream); saajMessage.getSOAPPart().getEnvelope(); postProcess(saajMessage); return new SaajSoapMessage(saajMessage, langAttributeOnSoap11FaultString, messageFactory); .arrayToCommaDelimitedString(mimeHeaders.getHeader(TransportConstants.HEADER_CONTENT_TYPE)); if (contentType.contains("startinfo")) { contentType = contentType.replace("startinfo", "start-info"); mimeHeaders.setHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType); try { SOAPMessage saajMessage = messageFactory.createMessage(mimeHeaders, inputStream); postProcess(saajMessage); return new SaajSoapMessage(saajMessage,
private static SOAPMessage createSOAPRequest(FileInputStream requestFile, String serverURI, String serviceName) throws Exception { MessageFactory messageFactory = MessageFactory.newInstance(); MimeHeaders headers = new MimeHeaders(); SOAPMessage soapMessage = messageFactory.createMessage(headers, requestFile); requestFile.close(); String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes()); headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", serverURI + serviceName); headers.addHeader("Authorization", "Basic " + authorization); soapMessage.saveChanges(); return soapMessage; }
public static MimeHeaders copy(MimeHeaders headers) { MimeHeaders newHeaders = new MimeHeaders(); Iterator eachHeader = headers.getAllHeaders(); while (eachHeader.hasNext()) { MimeHeader currentHeader = (MimeHeader) eachHeader.next(); newHeaders.addHeader( currentHeader.getName(), currentHeader.getValue()); } return newHeaders; } }
private SOAPMessage parse(InputStream request) throws IOException { SOAPMessage message = null; try { MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); message = messageFactory.createMessage(new MimeHeaders(), request); } catch (SOAPException e) { logger.warn("parse() failed", e); } return message; }
@Override protected void runTest() throws Throwable { MimeHeaders mimeHeaders = new MimeHeaders(); mimeHeaders.addHeader("Content-Type", spec.getContentType()); InputStream in = SOAPSampleSet.MUST_UNDERSTAND.getMessage(spec).getInputStream(); try { SOAPMessage message = newMessageFactory().createMessage(mimeHeaders, in); SOAPHeader header = message.getSOAPHeader(); Iterator it = header.examineMustUnderstandHeaderElements(null); assertTrue(it.hasNext()); assertTrue(it.next() instanceof SOAPHeaderElement); assertFalse(it.hasNext()); } finally { in.close(); } } }
ByteArrayInputStream in = ...; MessageFactory mf = MessageFactory.newInstance(); SOAPMessage message = mf.createMessage(new MimeHeaders(), in); System.out.println(message.getSOAPBody().getElementsByTagNameNS("http://tempuri.org", "MyOperation"));
/** * * */ public static void setToHttpServletResponse(MimeHeaders headers, HttpServletResponse response){ Iterator<?> i = headers.getAllHeaders(); while(i.hasNext()){ MimeHeader h = (MimeHeader)i.next(); response.addHeader(h.getName(), h.getValue()); } }