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

CXF-8977: add support for MTOM Serialization Policy Assertion 1.1 #1685

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class MTOMAssertionBuilder implements AssertionBuilder<Element> {
private static final QName[] KNOWN_ELEMENTS
= {MetadataConstants.MTOM_ASSERTION_QNAME};
= {MetadataConstants.MTOM_ASSERTION_QNAME, MetadataConstants.MTOM11_ASSERTION_QNAME};

public Assertion build(Element elem, AssertionBuilderFactory f) {
String localName = elem.getLocalName();
Expand All @@ -47,6 +47,8 @@ public Assertion build(Element elem, AssertionBuilderFactory f) {

if (MetadataConstants.MTOM_ASSERTION_QNAME.equals(qn)) {
return new PrimitiveAssertion(MetadataConstants.MTOM_ASSERTION_QNAME, optional);
} else if (MetadataConstants.MTOM11_ASSERTION_QNAME.equals(qn)) {
return new PrimitiveAssertion(MetadataConstants.MTOM11_ASSERTION_QNAME, optional);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.Collection;

import javax.xml.namespace.QName;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageUtils;
Expand All @@ -39,7 +41,14 @@ public void handleMessage(Message message) throws Fault {

// extract Assertion information
if (aim != null) {
Collection<AssertionInfo> ais = aim.get(MetadataConstants.MTOM_ASSERTION_QNAME);
assertAssertion(message, aim, MetadataConstants.MTOM_ASSERTION_QNAME);
assertAssertion(message, aim, MetadataConstants.MTOM11_ASSERTION_QNAME);
}
}

private static void assertAssertion(Message message, AssertionInfoMap aim, QName type) {
Collection<AssertionInfo> ais = aim.get(type);
if (ais != null) {
for (AssertionInfo ai : ais) {
if (MessageUtils.isRequestor(message)) {
//just turn on MTOM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MTOMPolicyInterceptorProvider extends AbstractPolicyInterceptorProv
static {
Collection<QName> types = new ArrayList<>();
types.add(MetadataConstants.MTOM_ASSERTION_QNAME);
types.add(MetadataConstants.MTOM11_ASSERTION_QNAME);
ASSERTION_TYPES = types;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public final class MetadataConstants {
public static final QName MTOM_ASSERTION_QNAME =
new QName("http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization",
"OptimizedMimeSerialization");
public static final QName MTOM11_ASSERTION_QNAME =
new QName("http://www.w3.org/2007/08/soap12-mtom-policy", "MTOM");


private MetadataConstants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
public class MtomPolicyTest extends AbstractBusClientServerTestBase {
public static final String PORT = TestUtil.getPortNumber(MtomPolicyTest.class);
public static final String PORT2 = TestUtil.getPortNumber(MtomPolicyTest.class, 2);
public static final String PORT3 = TestUtil.getPortNumber(MtomPolicyTest.class, 3);
public static final String PORT4 = TestUtil.getPortNumber(MtomPolicyTest.class, 4);

static TestUtilities testUtilities = new TestUtilities(MtomPolicyTest.class);

Expand All @@ -72,7 +74,7 @@ public static void createTheBus() throws Exception {
@Test
public void testRequiredMtom() throws Exception {
String address = "http://localhost:" + PORT + "/EchoService";
setupServer(true, address);
setupServer("mtom-policy.xml", address);

sendMtomMessage(address);

Expand All @@ -88,7 +90,7 @@ public void testRequiredMtom() throws Exception {
@Test
public void testOptionalMtom() throws Exception {
String address = "http://localhost:" + PORT2 + "/EchoService";
setupServer(false, address);
setupServer("mtom-policy-optional.xml", address);

sendMtomMessage(address);

Expand All @@ -97,7 +99,34 @@ public void testOptionalMtom() throws Exception {
testUtilities.assertNoFault(res);
}

public void setupServer(boolean mtomRequired, String address) throws Exception {
@Test
public void testRequiredMtom11() throws Exception {
String address = "http://localhost:" + PORT3 + "/EchoService";
setupServer("mtom11-policy.xml", address);

sendMtomMessage(address);

Node res = testUtilities.invoke(address, "http://schemas.xmlsoap.org/soap/http", "nonmtom.xml");

NodeList list = testUtilities.assertValid("//faultstring", res);
String text = list.item(0).getTextContent();
assertTrue(text.contains("These policy alternatives can not be satisfied: "));
assertTrue(text.contains("{http://www.w3.org/2007/08/soap12-mtom-policy}MTOM"));
}

@Test
public void testOptionalMtom11() throws Exception {
String address = "http://localhost:" + PORT4 + "/EchoService";
setupServer("mtom11-policy-optional.xml", address);

sendMtomMessage(address);

Node res = testUtilities.invoke(address, "http://schemas.xmlsoap.org/soap/http", "nonmtom.xml");

testUtilities.assertNoFault(res);
}

public void setupServer(String policyResource, String address) throws Exception {
getStaticBus().getExtension(PolicyEngine.class).setAlternativeSelector(
new FirstAlternativeSelector());
JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
Expand All @@ -107,15 +136,9 @@ public void setupServer(boolean mtomRequired, String address) throws Exception {

WSPolicyFeature policyFeature = new WSPolicyFeature();
List<Element> policyElements = new ArrayList<>();
if (mtomRequired) {
policyElements.add(StaxUtils.read(
getClass().getResourceAsStream("mtom-policy.xml"))
.getDocumentElement());
} else {
policyElements.add(StaxUtils.read(
getClass().getResourceAsStream("mtom-policy-optional.xml"))
.getDocumentElement());
}
policyElements.add(StaxUtils.read(
getClass().getResourceAsStream(policyResource))
.getDocumentElement());
policyFeature.setPolicyElements(policyElements);

sf.getFeatures().add(policyFeature);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<wsp:Policy xmlns:wsp="http://www.w3.org/2006/07/ws-policy" xmlns:mtom="http://www.w3.org/2007/08/soap12-mtom-policy">
<mtom:MTOM wsp:Optional="true"/>
</wsp:Policy>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<wsp:Policy xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:mtom="http://www.w3.org/2007/08/soap12-mtom-policy">
<mtom:MTOM/>
</wsp:Policy>
Loading