Apache Web Services Policy User's Guide:

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

Content

  1. Create a policy object
  2. Write a policy object
  3. Normalization
  4. Merge policy objects
  5. Intersect policy objects

Create a policy object

You can create a policy obejct using one of the following two methods

  1. Create a policy object by redaing a policy document
  2. Create a policy object programmatically

Create a policy object by reading a policy document

The following steps need to be taken to create a policy object from a policy document

  1. Create a PolicyReader - This is the class that creates a policy object using an InputStream.
  2. Feed the policy document to PolicyReader as an InputStream-This will return a policy object

1. How to create a PolicyReader

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);

2. Feed the policy document to PolicyReader as an InputStream.

This step will return a policy object.

				
String pathToPolicyFile = " /home/ ../Policy1.xml";

FileInputStream fis = new FileInputStream(pathToPolicyFile);

Policy policy = reader.readPolicy(fis);

Create a policy object programmatically.

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
				

Write a policy object

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 );

Normalization

The following code will achieve policy normalization

				
Policy policy1 = reader.readPolicy(...);

Policy normalized = (Policy) policy1.normalize();

Merge policy objects

The following code will merge two policy objects

				
Policy policy1 = reader.readPolicy(...);

Policy policy2 = reader.readPolicy(...);

Policy merged = (Policy) policy1.merge(policy2);

Intersect policy objects

The following code will intersect two policy objects

				
Policy policy1 = reader.readPolicy(..)

Policy policy2 = reader.readPolicy(..)

Policy intersected = (Policy) policy1.intersect(policy2);