Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using SOAPEncoder constructor taking builder #1841

Merged
merged 4 commits into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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