Quick Start (Java SWAref): Removing certificate-based encryption using the web service API

The following Java code example removes certificate-based encryption from a PDF document named EncryptLoanCert.pdf. The alias of the public key that is used to remove encryption is Encryption. The unsecured PDF document is saved as a PDF file named noEncryptionLoan.pdf. (See Removing Certificate Based Encryption.)

/** 
    * Ensure that you create Java proxy files that consume 
    * the Encryption 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 java.io.*; 
import java.io.InputStream; 
 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.xml.ws.BindingProvider; 
 
import com.adobe.idp.services.*; 
 
public class RemovePKIFromPDFSWAref { 
 
    public static void main(String[] args) { 
         
        try{ 
            // Setting configurations to retrieve the Web service 
            String url = "http://hiro-xp:8080/soap/services/EncryptionService?blob=swaRef"; 
            String username = "administrator"; 
            String password = "password"; 
             
            // Create the service Client objects needed 
            EncryptionServiceService encryptionService = new EncryptionServiceService(); 
            EncryptionService encryptionClient = encryptionService.getEncryptionService(); 
             
            // Retrieve the Web services from the LiveCycle Server. 
            ((BindingProvider) encryptionClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
            ((BindingProvider) encryptionClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); 
            ((BindingProvider) encryptionClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); 
             
            //Get the PDF document 
            String path1 = "C:\\Adobe\EncryptedLoanCert.pdf"; 
            FileDataSource ds1 = new FileDataSource(path1);  
            DataHandler handler1 = new DataHandler(ds1); 
            BLOB inDoc = new BLOB(); 
            inDoc.setSwaRef(handler1); 
             
            //Remove certificate-based encryption from the PDF document  
            BLOB encryptDoc = encryptionClient.removePDFCertificateSecurity(inDoc, "Encryption"); 
 
            File f = new File("C:\\Adobe\noEncryptionLoan.pdf"); 
            BLOB outBlob = encryptDoc; 
            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();  
            }     
    } 
}