Author: ay
Date: Thu Sep 22 14:07:41 2011
New Revision: 1174150
URL: http://svn.apache.org/viewvc?rev=1174150&view=rev
Log:
[CXF-3816] a policy provider/interceptor for asserting specified policy
assertions
Added:
cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProvider.java
(with props)
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProviderTest.java
(with props)
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy.xml
(with props)
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy2.xml
(with props)
Added:
cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProvider.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProvider.java?rev=1174150&view=auto
==============================================================================
---
cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProvider.java
(added)
+++
cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProvider.java
Thu Sep 22 14:07:41 2011
@@ -0,0 +1,85 @@
+/**
+ * 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.
+ */
+
+package org.apache.cxf.ws.policy;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+
+/**
+ * This policy interceptor provider can be used to implicitly handle unknown
policy assertions.
+ */
+public class IgnorablePolicyInterceptorProvider extends
AbstractPolicyInterceptorProvider {
+ private static final Logger LOG =
LogUtils.getL7dLogger(IgnorablePolicyInterceptorProvider.class);
+
+ private IgnorableAssertionsInterceptor interceptor = new
IgnorableAssertionsInterceptor();
+
+ /**
+ * @param type
+ */
+ public IgnorablePolicyInterceptorProvider(QName type) {
+ this(Collections.singletonList(type));
+ }
+
+ /**
+ * @param at
+ */
+ public IgnorablePolicyInterceptorProvider(Collection<QName> at) {
+ super(at);
+
+ getInInterceptors().add(interceptor);
+ getOutInterceptors().add(interceptor);
+ getInFaultInterceptors().add(interceptor);
+ getOutFaultInterceptors().add(interceptor);
+ }
+
+ private class IgnorableAssertionsInterceptor <T extends Message> extends
AbstractPhaseInterceptor<T> {
+
+ public IgnorableAssertionsInterceptor() {
+ // somewhat irrelevant
+ super(Phase.POST_LOGICAL);
+ }
+
+ public void handleMessage(Message message) throws Fault {
+ AssertionInfoMap aim = message.get(AssertionInfoMap.class);
+ for (QName an : getAssertionTypes()) {
+ Collection<AssertionInfo> ais = aim.getAssertionInfo(an);
+ if (LOG.isLoggable(Level.INFO)) {
+ LOG.info("Asserting for " + an);
+ }
+ if (null != ais) {
+ for (AssertionInfo ai : ais) {
+ ai.setAsserted(true);
+ }
+ }
+ }
+ }
+ }
+
+}
Propchange:
cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProvider.java
------------------------------------------------------------------------------
svn:executable = *
Added:
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProviderTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProviderTest.java?rev=1174150&view=auto
==============================================================================
---
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProviderTest.java
(added)
+++
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProviderTest.java
Thu Sep 22 14:07:41 2011
@@ -0,0 +1,184 @@
+/**
+ * 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.
+ */
+
+package org.apache.cxf.ws.policy;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import junit.framework.Assert;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.interceptor.Interceptor;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
+import org.apache.neethi.Assertion;
+
+import org.junit.Test;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+/**
+ *
+ */
+public class IgnorablePolicyInterceptorProviderTest extends Assert {
+ private static final QName ONEWAY_QNAME = new
QName("http://tempuri.org/policy", "OneWay");
+ private static final QName DUPLEX_QNAME = new
QName("http://tempuri.org/policy", "Duplex");
+
+ @Test
+ public void testProvider() {
+ Bus bus = null;
+ try {
+ bus = new
SpringBusFactory().createBus("/org/apache/cxf/ws/policy/ignorable-policy.xml",
false);
+
+ PolicyInterceptorProviderRegistry pipreg =
+ bus.getExtension(PolicyInterceptorProviderRegistry.class);
+
+ assertNotNull(pipreg);
+
+ PolicyInterceptorProvider pip = pipreg.get(ONEWAY_QNAME);
+
+ assertNotNull(pip);
+
+ PolicyInterceptorProvider pip2 = pipreg.get(DUPLEX_QNAME);
+
+ assertEquals(pip, pip2);
+
+ } finally {
+ if (null != bus) {
+ bus.shutdown(true);
+ BusFactory.setDefaultBus(null);
+ }
+ }
+ }
+
+ @Test
+ public void testInterceptorAssertion() {
+ Bus bus = null;
+ try {
+ bus = new
SpringBusFactory().createBus("/org/apache/cxf/ws/policy/ignorable-policy.xml",
false);
+
+ PolicyInterceptorProviderRegistry pipreg =
+ bus.getExtension(PolicyInterceptorProviderRegistry.class);
+
+ assertNotNull(pipreg);
+
+ PolicyInterceptorProvider pip = pipreg.get(ONEWAY_QNAME);
+
+ assertNotNull(pip);
+
+ List<Interceptor<Message>> list;
+ list = CastUtils.cast(pip.getOutInterceptors());
+ verifyAssertion(list);
+
+ list = CastUtils.cast(pip.getInInterceptors());
+ verifyAssertion(list);
+
+ list = CastUtils.cast(pip.getOutFaultInterceptors());
+ verifyAssertion(list);
+
+ list = CastUtils.cast(pip.getInFaultInterceptors());
+ verifyAssertion(list);
+
+ } finally {
+ if (null != bus) {
+ bus.shutdown(true);
+ BusFactory.setDefaultBus(null);
+ }
+ }
+ }
+
+
+ private void verifyAssertion(List<Interceptor<Message>> list) {
+ Message message = new MessageImpl();
+ AssertionInfoMap aim = createTestAssertions();
+ message.put(AssertionInfoMap.class, aim);
+ try {
+ aim.check();
+ fail("not yet asserted");
+ } catch (PolicyException e) {
+ // ok
+ }
+ for (Interceptor<Message> p : list) {
+ p.handleMessage(message);
+ }
+
+ aim.check();
+ }
+
+ @Test
+ public void testTwoBuses() {
+ ClassPathXmlApplicationContext context = null;
+ Bus cxf1 = null;
+ Bus cxf2 = null;
+ try {
+ context = new
ClassPathXmlApplicationContext("/org/apache/cxf/ws/policy/ignorable-policy2.xml");
+ cxf1 = (Bus)context.getBean("cxf1");
+ assertNotNull(cxf1);
+
+ cxf2 = (Bus)context.getBean("cxf2");
+ assertNotNull(cxf2);
+
+ PolicyInterceptorProviderRegistry pipreg1 =
+ cxf1.getExtension(PolicyInterceptorProviderRegistry.class);
+ assertNotNull(pipreg1);
+
+ PolicyInterceptorProviderRegistry pipreg2 =
+ cxf2.getExtension(PolicyInterceptorProviderRegistry.class);
+ assertNotNull(pipreg2);
+
+ PolicyInterceptorProvider pip1 = pipreg1.get(ONEWAY_QNAME);
+
+ assertNotNull(pip1);
+
+ PolicyInterceptorProvider pip2 = pipreg2.get(ONEWAY_QNAME);
+
+ assertEquals(pip1, pip2);
+
+ } finally {
+ if (null != cxf1) {
+ cxf1.shutdown(true);
+ BusFactory.setDefaultBus(null);
+ }
+ }
+ }
+
+
+ private AssertionInfoMap createTestAssertions() {
+ AssertionInfoMap aim = new
AssertionInfoMap(CastUtils.cast(Collections.EMPTY_LIST,
+
PolicyAssertion.class));
+ Assertion a = new PrimitiveAssertion(ONEWAY_QNAME);
+ Assertion b = new PrimitiveAssertion(DUPLEX_QNAME);
+
+ AssertionInfo ai = new AssertionInfo(a);
+ AssertionInfo bi = new AssertionInfo(b);
+
+ aim.put(ONEWAY_QNAME, Collections.singleton(ai));
+ aim.put(DUPLEX_QNAME, Collections.singleton(bi));
+
+ return aim;
+ }
+}
Propchange:
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/IgnorablePolicyInterceptorProviderTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy.xml
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy.xml?rev=1174150&view=auto
==============================================================================
---
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy.xml
(added)
+++
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy.xml
Thu Sep 22 14:07:41 2011
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:cxf="http://cxf.apache.org/core"
+ xmlns:wsrm-mgmt="http://cxf.apache.org/ws/rm/manager"
+ xmlns:wsrm-policy="http://schemas.xmlsoap.org/ws/2005/02/rm/policy"
+ xmlns:http="http://cxf.apache.org/transports/http/configuration"
+ xsi:schemaLocation="
+http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
+http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schema/transports/http.xsd
+http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <import resource="classpath:META-INF/cxf/cxf.xml" />
+
+ <cxf:bus>
+ <cxf:outInterceptors>
+ <bean id="logOutbound"
class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
+ </cxf:outInterceptors>
+ </cxf:bus>
+
+ <bean class="org.apache.cxf.ws.policy.IgnorablePolicyInterceptorProvider">
+ <constructor-arg>
+ <!-- the list of assertion types that can be ignored -->
+ <list>
+ <bean class="javax.xml.namespace.QName">
+ <constructor-arg value="http://tempuri.org/policy"/>
+ <constructor-arg value="Duplex"/>
+ </bean>
+ <bean class="javax.xml.namespace.QName">
+ <constructor-arg value="http://tempuri.org/policy"/>
+ <constructor-arg value="OneWay"/>
+ </bean>
+ </list>
+ </constructor-arg>
+ </bean>
+
+</beans>
\ No newline at end of file
Propchange:
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy.xml
------------------------------------------------------------------------------
svn:executable = *
Added:
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy2.xml
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy2.xml?rev=1174150&view=auto
==============================================================================
---
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy2.xml
(added)
+++
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy2.xml
Thu Sep 22 14:07:41 2011
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:cxf="http://cxf.apache.org/core"
+ xmlns:wsrm-mgmt="http://cxf.apache.org/ws/rm/manager"
+ xmlns:wsrm-policy="http://schemas.xmlsoap.org/ws/2005/02/rm/policy"
+ xmlns:http="http://cxf.apache.org/transports/http/configuration"
+ xsi:schemaLocation="
+http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
+http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schema/transports/http.xsd
+http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <import resource="classpath:META-INF/cxf/cxf.xml" />
+
+
+ <!-- use bean ids if you want to load multiple external attachment
providers -->
+
+
+ <cxf:bus bus="cxf1">
+ <cxf:outInterceptors>
+ <bean id="logOutbound"
class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
+ </cxf:outInterceptors>
+ </cxf:bus>
+
+ <cxf:bus bus="cxf2">
+ <cxf:inInterceptors>
+ <bean id="logInbound"
class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
+ </cxf:inInterceptors>
+ </cxf:bus>
+
+ <bean class="org.apache.cxf.ws.policy.IgnorablePolicyInterceptorProvider">
+ <constructor-arg>
+ <!-- the list of assertion types that can be ignored -->
+ <list>
+ <bean class="javax.xml.namespace.QName">
+ <constructor-arg value="http://tempuri.org/policy"/>
+ <constructor-arg value="Duplex"/>
+ </bean>
+ <bean class="javax.xml.namespace.QName">
+ <constructor-arg value="http://tempuri.org/policy"/>
+ <constructor-arg value="OneWay"/>
+ </bean>
+ </list>
+ </constructor-arg>
+ </bean>
+
+</beans>
\ No newline at end of file
Propchange:
cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/ignorable-policy2.xml
------------------------------------------------------------------------------
svn:executable = *