Skip to content

Commit b36bb50

Browse files
Laess3rvelo
andauthored
SOAPEncoder: Add support to modify soap message manually (OpenFeign#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 52d9815 commit b36bb50

6 files changed

Lines changed: 43 additions & 16 deletions

File tree

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
* 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

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

Lines changed: 38 additions & 11 deletions
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
* 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
@@ -45,22 +45,22 @@
4545
* <p>
4646
* Basic example with with Feign.Builder:
4747
* </p>
48-
*
48+
*
4949
* <pre>
50-
*
50+
*
5151
* public interface MyApi {
52-
*
52+
*
5353
* &#64;RequestLine("POST /getObject")
5454
* &#64;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")
@@ -69,7 +69,7 @@
6969
* api = Feign.builder()
7070
* .encoder(new SOAPEncoder(jaxbFactory))
7171
* .target(MyApi.class, "http://api");
72-
*
72+
*
7373
* ...
7474
*
7575
* try {
@@ -78,7 +78,7 @@
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

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
* 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

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
* 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

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
* 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

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
* 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

0 commit comments

Comments
 (0)