| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void encode(Object object, Type bodyType, RequestTemplate template) { |
| 109 | if (!(bodyType instanceof Class)) { |
| 110 | throw new UnsupportedOperationException( |
| 111 | "SOAP only supports encoding raw types. Found " + bodyType); |
| 112 | } |
| 113 | try { |
| 114 | Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); |
| 115 | Marshaller marshaller = jaxbContextFactory.createMarshaller((Class<?>) bodyType); |
| 116 | marshaller.marshal(object, document); |
| 117 | SOAPMessage soapMessage = MessageFactory.newInstance(soapProtocol).createMessage(); |
| 118 | soapMessage.setProperty( |
| 119 | SOAPMessage.WRITE_XML_DECLARATION, Boolean.toString(writeXmlDeclaration)); |
| 120 | soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charsetEncoding.displayName()); |
| 121 | soapMessage.getSOAPBody().addDocument(document); |
| 122 | |
| 123 | soapMessage = modifySOAPMessage(soapMessage); |
| 124 | |
| 125 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 126 | if (formattedOutput) { |
| 127 | Transformer t = TransformerFactory.newInstance().newTransformer(); |
| 128 | t.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 129 | t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); |
| 130 | t.transform(new DOMSource(soapMessage.getSOAPPart()), new StreamResult(bos)); |
| 131 | } else { |
| 132 | soapMessage.writeTo(bos); |
| 133 | } |
| 134 | template.body(bos.toString()); |
| 135 | } catch (SOAPException |
| 136 | | JAXBException |
| 137 | | ParserConfigurationException |
| 138 | | IOException |
| 139 | | TransformerFactoryConfigurationError |
| 140 | | TransformerException e) { |
| 141 | throw new EncodeException(e.toString(), e); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Override this in order to modify the SOAP message object before it's finally encoded. <br> |