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

SOAPEncoder: Add support to modify soap message manually #1503

Merged
merged 8 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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/SOAPDecoder.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down
49 changes: 38 additions & 11 deletions soap/src/main/java/feign/soap/SOAPEncoder.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -45,22 +45,22 @@
* <p>
* Basic example with with Feign.Builder:
* </p>
*
*
* <pre>
*
*
* public interface MyApi {
*
*
* &#64;RequestLine("POST /getObject")
* &#64;Headers({
* "SOAPAction: getObject",
* "Content-Type: text/xml"
* })
* MyJaxbObjectResponse getObject(MyJaxbObjectRequest request);
*
*
* }
*
*
* ...
*
*
* JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
* .withMarshallerJAXBEncoding("UTF-8")
* .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd")
Expand All @@ -69,7 +69,7 @@
* api = Feign.builder()
* .encoder(new SOAPEncoder(jaxbFactory))
* .target(MyApi.class, "http://api");
*
*
* ...
*
* try {
Expand All @@ -78,7 +78,7 @@
* log.info(faultException.getFault().getFaultString());
* }
* </pre>
*
*
* <p>
* The JAXBContextFactory should be reused across requests as it caches the created JAXB contexts.
* </p>
Expand Down Expand Up @@ -124,6 +124,9 @@ public void encode(Object object, Type bodyType, RequestTemplate template) {
Boolean.toString(writeXmlDeclaration));
soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charsetEncoding.displayName());
soapMessage.getSOAPBody().addDocument(document);

soapMessage = modifySOAPMessage(soapMessage);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (formattedOutput) {
Transformer t = TransformerFactory.newInstance().newTransformer();
Expand All @@ -140,6 +143,30 @@ public void encode(Object object, Type bodyType, RequestTemplate template) {
}
}

/**
* Override this in order to modify the SOAP message object before it's finally encoded. <br>
* This might be useful to add SOAP Headers, which are not supported by this SOAPEncoder directly.
* <br>
* This is an example of how to add a security header: <code>
* 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;
* }
* </code>
*/
protected SOAPMessage modifySOAPMessage(SOAPMessage soapMessage) throws SOAPException {
// Intentionally blank
return soapMessage;
}

/**
* Creates instances of {@link SOAPEncoder}.
*/
Expand Down Expand Up @@ -177,9 +204,9 @@ public Builder withCharsetEncoding(Charset charsetEncoding) {

/**
* The protocol used to create message factory. Default is "SOAP 1.1 Protocol".
*
*
* @param soapProtocol a string constant representing the MessageFactory protocol.
*
*
* @see SOAPConstants#SOAP_1_1_PROTOCOL
* @see SOAPConstants#SOAP_1_2_PROTOCOL
* @see SOAPConstants#DYNAMIC_SOAP_PROTOCOL
Expand Down
2 changes: 1 addition & 1 deletion soap/src/main/java/feign/soap/SOAPErrorDecoder.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion soap/src/test/java/feign/soap/SOAPCodecTest.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion soap/src/test/java/feign/soap/SOAPFaultDecoderTest.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion soap/src/test/java/feign/soap/package-info.java
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down