MCPcopy Index your code
hub / github.com/OpenFeign/feign / SOAPEncoder

Class SOAPEncoder

soap-jakarta/src/main/java/feign/soap/SOAPEncoder.java:81–223  ·  view source on GitHub ↗

Encodes requests using SOAPMessage and JAXB for the body part. Basic example with Feign.Builder: public interface MyApi { @RequestLine("POST /getObject") @Headers({ "SOAPAction: getObject", "Content-Type: text/xml" }) MyJaxbObjectResponse getObject(MyJ

Source from the content-addressed store, hash-verified

79 * contexts.
80 */
81public class SOAPEncoder implements Encoder {
82
83 private static final String DEFAULT_SOAP_PROTOCOL = SOAPConstants.SOAP_1_1_PROTOCOL;
84
85 private final boolean writeXmlDeclaration;
86 private final boolean formattedOutput;
87 private final Charset charsetEncoding;
88 private final JAXBContextFactory jaxbContextFactory;
89 private final String soapProtocol;
90
91 public SOAPEncoder(Builder builder) {
92 this.jaxbContextFactory = builder.jaxbContextFactory;
93 this.writeXmlDeclaration = builder.writeXmlDeclaration;
94 this.charsetEncoding = builder.charsetEncoding;
95 this.soapProtocol = builder.soapProtocol;
96 this.formattedOutput = builder.formattedOutput;
97 }
98
99 public SOAPEncoder(JAXBContextFactory jaxbContextFactory) {
100 this.jaxbContextFactory = jaxbContextFactory;
101 this.writeXmlDeclaration = true;
102 this.formattedOutput = false;
103 this.charsetEncoding = StandardCharsets.UTF_8;
104 this.soapProtocol = DEFAULT_SOAP_PROTOCOL;
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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected