11/**
2- * Copyright 2012-2020 The Feign Authors
2+ * Copyright 2012-2021 The Feign Authors
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55 * in compliance with the License. You may obtain a copy of the License at
4545 * <p>
4646 * Basic example with with Feign.Builder:
4747 * </p>
48- *
48+ *
4949 * <pre>
50- *
50+ *
5151 * public interface MyApi {
52- *
52+ *
5353 * @RequestLine("POST /getObject")
5454 * @Headers({
5555 * "SOAPAction: getObject",
5656 * "Content-Type: text/xml"
5757 * })
5858 * MyJaxbObjectResponse getObject(MyJaxbObjectRequest request);
59- *
59+ *
6060 * }
61- *
61+ *
6262 * ...
63- *
63+ *
6464 * JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
6565 * .withMarshallerJAXBEncoding("UTF-8")
6666 * .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd")
6969 * api = Feign.builder()
7070 * .encoder(new SOAPEncoder(jaxbFactory))
7171 * .target(MyApi.class, "http://api");
72- *
72+ *
7373 * ...
7474 *
7575 * try {
7878 * log.info(faultException.getFault().getFaultString());
7979 * }
8080 * </pre>
81- *
81+ *
8282 * <p>
8383 * The JAXBContextFactory should be reused across requests as it caches the created JAXB contexts.
8484 * </p>
@@ -124,6 +124,9 @@ public void encode(Object object, Type bodyType, RequestTemplate template) {
124124 Boolean .toString (writeXmlDeclaration ));
125125 soapMessage .setProperty (SOAPMessage .CHARACTER_SET_ENCODING , charsetEncoding .displayName ());
126126 soapMessage .getSOAPBody ().addDocument (document );
127+
128+ soapMessage = modifySOAPMessage (soapMessage );
129+
127130 ByteArrayOutputStream bos = new ByteArrayOutputStream ();
128131 if (formattedOutput ) {
129132 Transformer t = TransformerFactory .newInstance ().newTransformer ();
@@ -140,6 +143,30 @@ public void encode(Object object, Type bodyType, RequestTemplate template) {
140143 }
141144 }
142145
146+ /**
147+ * Override this in order to modify the SOAP message object before it's finally encoded. <br>
148+ * This might be useful to add SOAP Headers, which are not supported by this SOAPEncoder directly.
149+ * <br>
150+ * This is an example of how to add a security header: <code>
151+ * protected SOAPMessage modifySOAPMessage(SOAPMessage soapMessage) throws SOAPException {
152+ * SOAPFactory soapFactory = SOAPFactory.newInstance();
153+ * String uri = "http://schemas.xmlsoap.org/ws/2002/12/secext";
154+ * String prefix = "wss";
155+ * SOAPElement security = soapFactory.createElement("Security", prefix, uri);
156+ * SOAPElement usernameToken = soapFactory.createElement("UsernameToken", prefix, uri);
157+ * usernameToken.addChildElement("Username", prefix, uri).setValue("test");
158+ * usernameToken.addChildElement("Password", prefix, uri).setValue("test");
159+ * security.addChildElement(usernameToken);
160+ * soapMessage.getSOAPHeader().addChildElement(security);
161+ * return soapMessage;
162+ * }
163+ * </code>
164+ */
165+ protected SOAPMessage modifySOAPMessage (SOAPMessage soapMessage ) throws SOAPException {
166+ // Intentionally blank
167+ return soapMessage ;
168+ }
169+
143170 /**
144171 * Creates instances of {@link SOAPEncoder}.
145172 */
@@ -177,9 +204,9 @@ public Builder withCharsetEncoding(Charset charsetEncoding) {
177204
178205 /**
179206 * The protocol used to create message factory. Default is "SOAP 1.1 Protocol".
180- *
207+ *
181208 * @param soapProtocol a string constant representing the MessageFactory protocol.
182- *
209+ *
183210 * @see SOAPConstants#SOAP_1_1_PROTOCOL
184211 * @see SOAPConstants#SOAP_1_2_PROTOCOL
185212 * @see SOAPConstants#DYNAMIC_SOAP_PROTOCOL
0 commit comments