Quick Start (SwaRef): Applying a policy to a PDF document using the web service API

The following Java web service code example applies a policy named Allow Copy to a PDF document named Loan.pdf. The policy set to which the policy is added is named Global Policy Set. The policy-protected document is saved as a PDF file named PolicyProtectedLoanDoc.pdf. (See Applying Policies to PDF Documents.)

/** 
    * Ensure that you create Java proxy files that consume 
    * the Rights Management services  WSDL. You can use JAX-WS to create 
    * the proxy Java files.   
    *    
    * For information, see "Invoking LiveCycle using SwaRef" in Programming with LiveCycle.   
    */ 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.xml.ws.BindingProvider; 
import com.adobe.idp.services.*; 
 
import java.io.*; 
     
public class ApplyPolicySwaRef { 
    public static void main(String[] args) { 
          try{ 
                // Setting configurations to retrieve the Web service 
                String url = "http://hiro-xp:8080/soap/services/RightsManagementService?blob=swaRef"; 
                String username = "administrator"; 
                String password = "password"; 
                 
                // Create the RightsManagementServiceService object 
                RightsManagementServiceService rightsManagementService = new RightsManagementServiceService(); 
                RightsManagementService rightsManagementClient = rightsManagementService.getRightsManagementService(); 
                 
                //Set connection properties required to invoke LiveCycle 
                ((BindingProvider) rightsManagementClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
                ((BindingProvider) rightsManagementClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); 
                ((BindingProvider) rightsManagementClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); 
                 
                //Reference a PDF document to which a policy is applied         
                String path = "C:\\Adobe\Loan.pdf"; 
                FileDataSource ds = new FileDataSource(path);  
                DataHandler handler = new DataHandler(ds); 
                BLOB inDoc = new BLOB(); 
                inDoc.setSwaRef(handler); 
                 
                //Apply a policy to a PDF document named Loan.pdf 
                BLOB outDoc = rightsManagementClient.applyPolicy( 
                    inDoc, 
                    "Loan.pdf", 
                    "Global Policy Set", 
                    "Allow Copy", 
                    null, 
                    null); 
 
                //Write the PDF file to the local file system 
                File f = new File("C:\\Adobe\PolicyProtectedLoanDoc.pdf"); 
                BLOB outBlob = outDoc; 
                DataHandler handler2 = outBlob.getSwaRef(); 
 
                //Get an InputStream from DataHandler and 
                //write to a file 
                InputStream inputStream = handler2.getInputStream(); 
                OutputStream out = new FileOutputStream(f); 
 
                //Iterate through the buffer 
                byte buf[] = new byte[1024]; 
                int len; 
                while ((len = inputStream.read(buf)) > 0) 
                    out.write(buf, 0, len); 
                out.close(); 
                inputStream.close(); 
 
 
 
          }catch(Exception e) 
          { 
                 e.printStackTrace();  
          } 
    } 
} 

// Ethnio survey code removed