Skip to content

Commit 76b45e0

Browse files
Laess3rvelo
andauthored
SOAPEncoder: Add support to modify soap message manually (#1503)
* Add support to modify soap message manually This small extension adds the possibility to manually set soap headers. An example is given in the javadoc. * Increment copyright year * Fix code formatting * Increment copyright year * Increment copyright year * Fix formatting * Improve implementation Co-authored-by: Marvin Froeder <velo@users.noreply.github.com>
1 parent 1e09ea2 commit 76b45e0

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

soap/src/main/java/feign/soap/SOAPDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2020 The Feign Authors
2+
* Copyright 2012-2021 The Feign Authors
33
*
44
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at

soap/src/main/java/feign/soap/SOAPEncoder.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2020 The Feign Authors
2+
* Copyright 2012-2021 The Feign Authors
33
*
44
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -121,6 +121,9 @@ public void encode(Object object, Type bodyType, RequestTemplate template) {
121121
SOAPMessage.WRITE_XML_DECLARATION, Boolean.toString(writeXmlDeclaration));
122122
soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charsetEncoding.displayName());
123123
soapMessage.getSOAPBody().addDocument(document);
124+
125+
soapMessage = modifySOAPMessage(soapMessage);
126+
124127
ByteArrayOutputStream bos = new ByteArrayOutputStream();
125128
if (formattedOutput) {
126129
Transformer t = TransformerFactory.newInstance().newTransformer();
@@ -141,6 +144,30 @@ public void encode(Object object, Type bodyType, RequestTemplate template) {
141144
}
142145
}
143146

147+
/**
148+
* Override this in order to modify the SOAP message object before it's finally encoded. <br>
149+
* This might be useful to add SOAP Headers, which are not supported by this SOAPEncoder directly.
150+
* <br>
151+
* This is an example of how to add a security header: <code>
152+
* protected SOAPMessage modifySOAPMessage(SOAPMessage soapMessage) throws SOAPException {
153+
* SOAPFactory soapFactory = SOAPFactory.newInstance();
154+
* String uri = "http://schemas.xmlsoap.org/ws/2002/12/secext";
155+
* String prefix = "wss";
156+
* SOAPElement security = soapFactory.createElement("Security", prefix, uri);
157+
* SOAPElement usernameToken = soapFactory.createElement("UsernameToken", prefix, uri);
158+
* usernameToken.addChildElement("Username", prefix, uri).setValue("test");
159+
* usernameToken.addChildElement("Password", prefix, uri).setValue("test");
160+
* security.addChildElement(usernameToken);
161+
* soapMessage.getSOAPHeader().addChildElement(security);
162+
* return soapMessage;
163+
* }
164+
* </code>
165+
*/
166+
protected SOAPMessage modifySOAPMessage(SOAPMessage soapMessage) throws SOAPException {
167+
// Intentionally blank
168+
return soapMessage;
169+
}
170+
144171
/** Creates instances of {@link SOAPEncoder}. */
145172
public static class Builder {
146173

soap/src/main/java/feign/soap/SOAPErrorDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2020 The Feign Authors
2+
* Copyright 2012-2021 The Feign Authors
33
*
44
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at

soap/src/test/java/feign/soap/SOAPCodecTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2020 The Feign Authors
2+
* Copyright 2012-2021 The Feign Authors
33
*
44
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at

soap/src/test/java/feign/soap/SOAPFaultDecoderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2020 The Feign Authors
2+
* Copyright 2012-2021 The Feign Authors
33
*
44
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at

soap/src/test/java/feign/soap/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2020 The Feign Authors
2+
* Copyright 2012-2021 The Feign Authors
33
*
44
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at

0 commit comments

Comments
 (0)