Skip to content

Commit

Permalink
Allow using SOAPEncoder constructor taking builder (#1841)
Browse files Browse the repository at this point in the history
* Allow to use the SOAPEncoder constructor taking a builder to enable changing the protocol when overwriting the class

Change the SOAPEncoder constructor which takes a builder from private to protected

Fixes #1839

* Update SOAPEncoder.java

Co-authored-by: Marvin Froeder <velo@users.noreply.github.com>
  • Loading branch information
Nicklas2751 and velo committed Nov 18, 2022
1 parent a8f889a commit 3a39bc6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion soap/src/main/java/feign/soap/SOAPEncoder.java
Expand Up @@ -93,7 +93,7 @@ public class SOAPEncoder implements Encoder {
private final JAXBContextFactory jaxbContextFactory;
private final String soapProtocol;

private SOAPEncoder(Builder builder) {
public SOAPEncoder(Builder builder) {
this.jaxbContextFactory = builder.jaxbContextFactory;
this.writeXmlDeclaration = builder.writeXmlDeclaration;
this.charsetEncoding = builder.charsetEncoding;
Expand Down
62 changes: 62 additions & 0 deletions soap/src/test/java/feign/soap/SOAPCodecTest.java
Expand Up @@ -22,11 +22,17 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -383,6 +389,62 @@ public void notFoundDecodesToNull() throws Exception {
.decode(response, byte[].class)).isEmpty();
}

@Test
public void changeSoapProtocolAndSetHeader() {
Encoder encoder =
new ChangedProtocolAndHeaderSOAPEncoder(new JAXBContextFactory.Builder().build());

GetPrice mock = new GetPrice();
mock.item = new Item();
mock.item.value = "Apples";

RequestTemplate template = new RequestTemplate();
encoder.encode(mock, GetPrice.class, template);

String soapEnvelop = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<env:Header>" +
(System.getProperty("java.version").startsWith("1.8")
? "<wss:Security xmlns:wss=\"http://schemas.xmlsoap.org/ws/2002/12/secext\">"
: "<wss:Security xmlns=\"http://schemas.xmlsoap.org/ws/2002/12/secext\" xmlns:wss=\"http://schemas.xmlsoap.org/ws/2002/12/secext\">")
+
"<wss:UsernameToken>" +
"<wss:Username>test</wss:Username>" +
"<wss:Password>test</wss:Password>" +
"</wss:UsernameToken>" +
"</wss:Security>" +
"</env:Header>" +
"<env:Body>" +
"<GetPrice>" +
"<Item>Apples</Item>" +
"</GetPrice>" +
"</env:Body>" +
"</env:Envelope>";
assertThat(template).hasBody(soapEnvelop);
}

static class ChangedProtocolAndHeaderSOAPEncoder extends SOAPEncoder {

public ChangedProtocolAndHeaderSOAPEncoder(JAXBContextFactory jaxbContextFactory) {
super(new SOAPEncoder.Builder()
.withSOAPProtocol("SOAP 1.2 Protocol")
.withJAXBContextFactory(jaxbContextFactory));
}

@Override
protected SOAPMessage modifySOAPMessage(SOAPMessage soapMessage) throws SOAPException {
SOAPFactory soapFactory = SOAPFactory.newInstance();
String uri = "http://schemas.xmlsoap.org/ws/2002/12/secext";
String prefix = "wss";
SOAPElement security = soapFactory.createElement("Security", prefix, uri);
SOAPElement usernameToken = soapFactory.createElement("UsernameToken", prefix, uri);
usernameToken.addChildElement("Username", prefix, uri).setValue("test");
usernameToken.addChildElement("Password", prefix, uri).setValue("test");
security.addChildElement(usernameToken);
soapMessage.getSOAPHeader().addChildElement(security);
return soapMessage;
}
}

@XmlRootElement(name = "GetPrice")
@XmlAccessorType(XmlAccessType.FIELD)
Expand Down

0 comments on commit 3a39bc6

Please sign in to comment.