You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2012/10/11 22:03:32 UTC

svn commit: r1397272 - in /cxf/trunk: rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/ systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/ systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/policy/server/

Author: dkulp
Date: Thu Oct 11 20:03:31 2012
New Revision: 1397272

URL: http://svn.apache.org/viewvc?rev=1397272&view=rev
Log:
[CXF-4547] Attach configuration provided policies into the wsdl
Patch from Jason Pell applied

Modified:
    cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/ServiceModelPolicyUpdater.java
    cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyServiceTest.java
    cxf/trunk/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/policy/server/javafirstserver.xml

Modified: cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/ServiceModelPolicyUpdater.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/ServiceModelPolicyUpdater.java?rev=1397272&r1=1397271&r2=1397272&view=diff
==============================================================================
--- cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/ServiceModelPolicyUpdater.java (original)
+++ cxf/trunk/rt/ws/policy/src/main/java/org/apache/cxf/ws/policy/ServiceModelPolicyUpdater.java Thu Oct 11 20:03:31 2012
@@ -44,16 +44,20 @@
 package org.apache.cxf.ws.policy;
 
 import java.util.Collection;
+
 import javax.wsdl.extensions.UnknownExtensibilityElement;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
 import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.service.model.BindingMessageInfo;
 import org.apache.cxf.service.model.BindingOperationInfo;
+import org.apache.cxf.service.model.DescriptionInfo;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.service.model.Extensible;
 import org.apache.cxf.staxutils.W3CDOMStreamWriter;
@@ -71,14 +75,24 @@ public class ServiceModelPolicyUpdater {
     public void addPolicyAttachments(Collection<PolicyAttachment> attachments) {
         for (PolicyAttachment pa : attachments) {
             boolean policyUsed = false;
-            String policyId = pa.getPolicy().getId();
-
-            // Add wsp:PolicyReference to wsdl:binding/wsdl:operation
+            
             for (BindingOperationInfo boi : ei.getBinding().getOperations()) {
+                BindingMessageInfo inputMessage = boi.getInput();
+                BindingMessageInfo outputMessage = boi.getOutput();
+                
                 if (pa.appliesTo(boi)) {
-                    addPolicyRef(boi, policyId);
+                    // Add wsp:PolicyReference to wsdl:binding/wsdl:operation
+                    addPolicyRef(boi, pa.getPolicy());
                     // Add it to wsdl:portType/wsdl:operation too
-                    addPolicyRef(ei.getInterface().getOperation(boi.getName()), policyId);
+                    // FIXME - since the appliesTo is for BindingOperationInfo, I think its dodgy
+                    // that the policy ref should also be associated with the port type
+                    addPolicyRef(ei.getInterface().getOperation(boi.getName()), pa.getPolicy());
+                    policyUsed = true;
+                } else if (pa.appliesTo(inputMessage)) {
+                    addPolicyRef(inputMessage, pa.getPolicy());
+                    policyUsed = true;
+                } else if (pa.appliesTo(outputMessage)) {
+                    addPolicyRef(outputMessage, pa.getPolicy());
                     policyUsed = true;
                 }
             }
@@ -90,14 +104,14 @@ public class ServiceModelPolicyUpdater {
         }
     }
 
-    private void addPolicyRef(Extensible ext, String policyId) {
+    private void addPolicyRef(Extensible ext, Policy p) {
         Document doc = DOMUtils.createDocument();
-        Element el = doc.createElementNS(Constants.URI_POLICY_13_NS, Constants.ELEM_POLICY_REF);
+        Element el = doc.createElementNS(p.getNamespace(), Constants.ELEM_POLICY_REF);
         el.setPrefix(Constants.ATTR_WSP);
-        el.setAttribute(Constants.ATTR_URI, "#" + policyId);
+        el.setAttribute(Constants.ATTR_URI, "#" + p.getId());
 
         UnknownExtensibilityElement uee = new UnknownExtensibilityElement();
-        uee.setElementType(new QName(Constants.URI_POLICY_13_NS, Constants.ELEM_POLICY_REF));
+        uee.setElementType(new QName(p.getNamespace(), Constants.ELEM_POLICY_REF));
         uee.setElement(el);
         uee.setRequired(true);
 
@@ -109,15 +123,26 @@ public class ServiceModelPolicyUpdater {
             W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
             p.serialize(writer);
             Element policyEl = writer.getDocument().getDocumentElement();
-
+            
+            // FIXME - overwrite the Id to include the namespace prefix, for some reason neethi does not do this!
+            Attr idAttr = policyEl.getAttributeNode(Constants.ATTR_ID);
+            if (null != idAttr) {
+                idAttr.setPrefix(Constants.ATTR_WSU);
+            }
+            
             // Remove xmlns:xmlns attribute which Xerces chokes on
             policyEl.removeAttribute("xmlns:xmlns");
 
             UnknownExtensibilityElement uee = new UnknownExtensibilityElement();
-            uee.setElementType(new QName(Constants.URI_POLICY_13_NS, Constants.ELEM_POLICY));
+            uee.setElementType(new QName(p.getNamespace(), Constants.ELEM_POLICY));
             uee.setElement(policyEl);
 
-            ei.getService().addExtensor(uee);
+            if (ei.getService().getDescription() == null) {
+                DescriptionInfo description = new DescriptionInfo();
+                description.setName(ei.getService().getName());
+                ei.getService().setDescription(description);
+            }
+            ei.getService().getDescription().addExtensor(uee);
         } catch (XMLStreamException ex) {
             throw new RuntimeException("Could not serialize policy", ex);
         } catch (ParserConfigurationException e) {

Modified: cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyServiceTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyServiceTest.java?rev=1397272&r1=1397271&r2=1397272&view=diff
==============================================================================
--- cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyServiceTest.java (original)
+++ cxf/trunk/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/policy/JavaFirstPolicyServiceTest.java Thu Oct 11 20:03:31 2012
@@ -29,6 +29,7 @@ import org.w3c.dom.Element;
 
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.service.model.MessageInfo.Type;
 import org.apache.cxf.systest.ws.common.SecurityTestUtil;
 import org.apache.cxf.systest.ws.policy.server.JavaFirstPolicyServer;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -58,13 +59,46 @@ public class JavaFirstPolicyServiceTest 
     }
 
     @org.junit.Test
-    public void testJavaFirstWsdl() throws Exception {
-        HttpURLConnection connection = getHttpConnection("http://localhost:" + PORT2
-                                                         + "/JavaFirstPolicyService?wsdl");
-        InputStream is = connection.getInputStream();
-        String wsdlContents = IOUtils.toString(is);
+    public void testJavaFirstAttachmentWsdl() throws Exception {
+        Document doc = loadWsdl("JavaFirstAttachmentPolicyService");
 
-        Document doc = DOMUtils.readXml(new StringReader(wsdlContents));
+        Element binding = DOMUtils.getFirstChildWithName(doc.getDocumentElement(), WSDL_NAMESPACE, "binding");
+        assertNotNull(binding);
+        
+        List<Element> operationMessages = DOMUtils.getChildrenWithName(binding, WSDL_NAMESPACE, "operation");
+        assertEquals(4, operationMessages.size());
+        
+        Element doOperationLevelPolicy = getOperationElement("doOperationLevelPolicy", operationMessages);
+        assertEquals("#UsernameToken", 
+                     getOperationPolicyReferenceId(doOperationLevelPolicy, Constants.URI_POLICY_13_NS));
+        
+        Element doInputMessagePolicy = getOperationElement("doInputMessagePolicy", operationMessages);
+        assertEquals("#UsernameToken", 
+                     getMessagePolicyReferenceId(doInputMessagePolicy, Type.INPUT, Constants.URI_POLICY_13_NS));
+        assertNull(getMessagePolicyReferenceId(
+                                               doInputMessagePolicy, Type.OUTPUT, Constants.URI_POLICY_13_NS));
+        
+        Element doOutputMessagePolicy = getOperationElement("doOutputMessagePolicy", operationMessages);
+        assertEquals("#UsernameToken", 
+                     getMessagePolicyReferenceId(doOutputMessagePolicy, Type.OUTPUT, Constants.URI_POLICY_13_NS));
+        assertNull(getMessagePolicyReferenceId(
+                                               doOutputMessagePolicy, Type.INPUT, Constants.URI_POLICY_13_NS));
+        
+        Element doNoPolicy = getOperationElement("doNoPolicy", operationMessages);
+        assertNull(getMessagePolicyReferenceId(doNoPolicy, Type.INPUT, Constants.URI_POLICY_13_NS));
+        assertNull(getMessagePolicyReferenceId(doNoPolicy, Type.OUTPUT, Constants.URI_POLICY_13_NS));
+        
+        // ensure that the policies are attached to the wsdl:definitions
+        List<Element> policyMessages = DOMUtils.getChildrenWithName(doc.getDocumentElement(), 
+                                                                    Constants.URI_POLICY_13_NS, "Policy");
+        assertEquals(1, policyMessages.size());
+        
+        assertEquals("UsernameToken", getPolicyId(policyMessages.get(0)));
+    }
+    
+    @org.junit.Test
+    public void testJavaFirstWsdl() throws Exception {
+        Document doc = loadWsdl("JavaFirstPolicyService");
 
         Element portType = DOMUtils.getFirstChildWithName(doc.getDocumentElement(), WSDL_NAMESPACE, "portType");
         assertNotNull(portType);
@@ -72,16 +106,20 @@ public class JavaFirstPolicyServiceTest 
         List<Element> operationMessages = DOMUtils.getChildrenWithName(portType, WSDL_NAMESPACE, "operation");
         assertEquals(5, operationMessages.size());
         
-        Element operationOne = getOperationMessage("doOperationOne", operationMessages);
-        assertEquals("#InternalTransportAndUsernamePolicy", getPolicyReferenceId(operationOne));
-        Element operationTwo = getOperationMessage("doOperationTwo", operationMessages);
-        assertEquals("#TransportAndUsernamePolicy", getPolicyReferenceId(operationTwo));
-        Element operationThree = getOperationMessage("doOperationThree", operationMessages);
-        assertEquals("#InternalTransportAndUsernamePolicy", getPolicyReferenceId(operationThree));
-        Element operationFour = getOperationMessage("doOperationFour", operationMessages);
-        assertEquals("#TransportAndUsernamePolicy", getPolicyReferenceId(operationFour));
-        Element operationPing = getOperationMessage("doPing", operationMessages);
-        assertNull(getPolicyReferenceId(operationPing));
+        Element operationOne = getOperationElement("doOperationOne", operationMessages);
+        assertEquals("#InternalTransportAndUsernamePolicy", 
+                     getMessagePolicyReferenceId(operationOne, Type.INPUT, Constants.URI_POLICY_NS));
+        Element operationTwo = getOperationElement("doOperationTwo", operationMessages);
+        assertEquals("#TransportAndUsernamePolicy", 
+                     getMessagePolicyReferenceId(operationTwo, Type.INPUT, Constants.URI_POLICY_NS));
+        Element operationThree = getOperationElement("doOperationThree", operationMessages);
+        assertEquals("#InternalTransportAndUsernamePolicy", 
+                     getMessagePolicyReferenceId(operationThree, Type.INPUT, Constants.URI_POLICY_NS));
+        Element operationFour = getOperationElement("doOperationFour", operationMessages);
+        assertEquals("#TransportAndUsernamePolicy", 
+                     getMessagePolicyReferenceId(operationFour, Type.INPUT, Constants.URI_POLICY_NS));
+        Element operationPing = getOperationElement("doPing", operationMessages);
+        assertNull(getMessagePolicyReferenceId(operationPing, Type.INPUT, Constants.URI_POLICY_NS));
         
         List<Element> policyMessages = DOMUtils.getChildrenWithName(doc.getDocumentElement(), 
                                                                     Constants.URI_POLICY_NS, "Policy");
@@ -91,13 +129,22 @@ public class JavaFirstPolicyServiceTest 
         assertEquals("TransportAndUsernamePolicy", getPolicyId(policyMessages.get(0)));
         assertEquals("InternalTransportAndUsernamePolicy", getPolicyId(policyMessages.get(1)));
     }
+
+    private Document loadWsdl(String serviceName) throws Exception {
+        HttpURLConnection connection = getHttpConnection("http://localhost:" + PORT2
+                                                         + "/" + serviceName + "?wsdl");
+        InputStream is = connection.getInputStream();
+        String wsdlContents = IOUtils.toString(is);
+
+        return DOMUtils.readXml(new StringReader(wsdlContents));
+    }
     
     private String getPolicyId(Element element) {
         return element.getAttributeNS(PolicyConstants.WSU_NAMESPACE_URI,
                                      PolicyConstants.WSU_ID_ATTR_NAME);
     }
     
-    private Element getOperationMessage(String operationName, List<Element> operationMessages) {
+    private Element getOperationElement(String operationName, List<Element> operationMessages) {
         Element operationElement = null;
         for (Element operation : operationMessages) {
             if (operationName.equals(operation.getAttribute("name"))) {
@@ -109,10 +156,21 @@ public class JavaFirstPolicyServiceTest 
         return operationElement;
     }
     
-    private String getPolicyReferenceId(Element operationMessage) {
-        Element inputMessage = DOMUtils.getFirstChildWithName(operationMessage, WSDL_NAMESPACE, "input");
-        assertNotNull(inputMessage);
-        Element policyReference = DOMUtils.getFirstChildWithName(inputMessage, Constants.URI_POLICY_NS, 
+    private String getMessagePolicyReferenceId(Element operationElement, Type type, String policyNamespace) {
+        Element messageElement = DOMUtils.getFirstChildWithName(operationElement, WSDL_NAMESPACE, 
+                                                              type.name().toLowerCase());
+        assertNotNull(messageElement);
+        Element policyReference = DOMUtils.getFirstChildWithName(messageElement, policyNamespace, 
+                                                                 "PolicyReference");
+        if (policyReference != null) {
+            return policyReference.getAttribute("URI");
+        } else {
+            return null;
+        }
+    }
+    
+    private String getOperationPolicyReferenceId(Element operationElement, String policyNamespace) {
+        Element policyReference = DOMUtils.getFirstChildWithName(operationElement, policyNamespace, 
                                                                  "PolicyReference");
         if (policyReference != null) {
             return policyReference.getAttribute("URI");

Modified: cxf/trunk/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/policy/server/javafirstserver.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/policy/server/javafirstserver.xml?rev=1397272&r1=1397271&r2=1397272&view=diff
==============================================================================
--- cxf/trunk/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/policy/server/javafirstserver.xml (original)
+++ cxf/trunk/systests/ws-security/src/test/resources/org/apache/cxf/systest/ws/policy/server/javafirstserver.xml Thu Oct 11 20:03:31 2012
@@ -44,9 +44,18 @@
         </cxf:features>
     </cxf:bus>
     
+    <p:externalAttachment location="classpath:java_first_policies/AttachmentPolicy.xml"/>
+    
+    <!--  register a policy attachment appliesto checker! -->
+	<bean id="uridomainexpressionbuilder" 
+	    class="org.apache.cxf.systest.ws.policy.URIDomainExpressionBuilder" />
+	
     <bean id="org.apache.cxf.systest.ws.policy.JavaFirstPolicyService"
         class="org.apache.cxf.systest.ws.policy.JavaFirstPolicyServiceImpl" />
     
+    <bean id="org.apache.cxf.systest.ws.policy.JavaFirstAttachmentPolicyService"
+        class="org.apache.cxf.systest.ws.policy.JavaFirstAttachmentPolicyServiceImpl" />
+    
     <jaxws:endpoint 
        id="JavaFirstPolicyService"
        address="http://localhost:${testutil.ports.JavaFirstPolicyServer.2}/JavaFirstPolicyService" 
@@ -55,7 +64,22 @@
        <jaxws:properties>
           <entry key="ws-security.callback-handler"
                 value="org.apache.cxf.systest.ws.wssec10.client.UTPasswordCallback" />
-       </jaxws:properties> 
+       </jaxws:properties>
+    </jaxws:endpoint>
+    
+    <jaxws:endpoint 
+       id="JavaFirstAttachmentPolicyService"
+       address="http://localhost:${testutil.ports.JavaFirstPolicyServer.2}/JavaFirstAttachmentPolicyService" 
+       implementor="#org.apache.cxf.systest.ws.policy.JavaFirstAttachmentPolicyService">
+        
+       <jaxws:properties>
+          <entry key="ws-security.callback-handler"
+                value="org.apache.cxf.systest.ws.wssec10.client.UTPasswordCallback" />
+       </jaxws:properties>
+       
+       <jaxws:features>
+			<p:policies />
+	   </jaxws:features>
     </jaxws:endpoint> 
     
     <wsp:Policy wsu:Id="InternalTransportAndUsernamePolicy"