This document will instruct you how to use WS-Policy model, taking you step by step through the process using a simple sample.
Send your feedback to: commons-dev@ws.apache.org (Prefix the subject with [Policy])
To subscribe to developer mailing list see here
You can create a policy obejct using one of the following two methods
The following steps need to be taken to create a policy object from a policy document
PolicyReader is the class that creates a policy object using an InputStream.
PolicyReader reader = PolicyFactory.getPolicyReader(PolicyFactory.OM_POLICY_READER); // or PolicyReader reader = PolicyFactory.getPolicyReader(PolicyFactory.DOM_POLICY_READER);
This step will return a policy object.
String pathToPolicyFile = " /home/ ../Policy1.xml"; FileInputStream fis = new FileInputStream(pathToPolicyFile); Policy policy = reader.readPolicy(fis);
Lets consider the sample policy below.
<wsp:Policy> <wsp:ExactlyOne> // Corresponds to XorCompositeAssertion <wsp:All> // Corresponds to AndCompositeAssertion <ns1:TestAssertion> TestAssertion1 </ns:TestAssertion> </wsp:All> </wsp:ExactlyOne> </wsp:Policy>
Now lets create programmatically the policy object which represents the above policy
Policy p = new Policy(); // will give you the following element <wsp:Policy> </wsp:Policy> XorCompositeAssertion x1 = new XorCompositeAssertion(); p.addTerm(x1); //will give you XorCompositeAssertion as seen below <wsp:Policy> <wsp:ExactlyOne>....</wsp:ExactlyOne> </wsp:Policy> AndCompositeAssertion a1 = new AndCompositeAssertion(); x1.addTerm(a1);// will give you AndCompositeAssertion as seen below <wsp:Policy> <wsp:ExactlyOne> <wsp:All>...</wsp:All> </wsp:ExactlyOne> </wsp:Policy> QName name = new QName("Test", "http://tests.org/tests", "ns1"); PrimitiveAssertion prim1 = new PrimitiveAssertion(name); prim1.setStrValue("MyTestAssertion"); a1.addTerm(prim1); //will give you the complete policy
The following code will achieve this task
String pathToOutFile = "/home/... /Policy2.xml"; FileOutputStream fos = new FileOutputStream(pathToOutFile); PolicyWriter writer = PolicyFactory.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER); writer.writePolicy(policy, fos );
The following code will achieve policy normalization
Policy policy1 = reader.readPolicy(...); Policy normalized = (Policy) policy1.normalize();
The following code will merge two policy objects
Policy policy1 = reader.readPolicy(...); Policy policy2 = reader.readPolicy(...); Policy merged = (Policy) policy1.merge(policy2);
The following code will intersect two policy objects
Policy policy1 = reader.readPolicy(..) Policy policy2 = reader.readPolicy(..) Policy intersected = (Policy) policy1.intersect(policy2);