Quick Start: Invoking a service using SwaRef in a Java project

The following Java code example invokes a process named MyApplication/EncryptDocument from a Java project. This Java project using proxy classes that were created using JAX-WS and SwaRef as the encoding type. (See Invoking LiveCycle using SwaRef.)

An unsecured PDF document based on a PDF file named Loan.pdf is passed to the LiveCycle process using SwaRef. The encrypted PDF document is saved as a PDF file named EncryptedDocument.pdf.

/** 
    * Ensure that you create Java proxy files that consume 
    * the the LiveCycle service WSDL. You can use JAX-WS to create 
    * the Java proxy files.   
    *    
    * This Java quick start uses SwaRef to invoke a short-lived process named 
    * EncryptDocument. For information, see  
    * "Invoking LiveCycle using SwaRef" in Programming with LiveCycle.   
    */ 
 
import javax.xml.ws.BindingProvider; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource;  
import java.io.*; 
 
import com.adobe.idp.services.*; 
 
public class InvokeEncryptDocumentSwaRef { 
 
public static void main(String[] args) { 
         
        try{ 
             
        //Specify connection values required to invoke the MyApplication/EncryptDocument process  
        //using SwaRef     
        String url = "http://hiro-xp:8080/soap/services/MyApplication/EncryptDocument?blob=swaref"; 
        String username = "administrator"; 
        String password = "password"; 
        String pdfFile = "C:\\Adobe\Loan.pdf"; 
         
        //Create a MyApplicationEncryptDocument object 
        MyApplicationEncryptDocumentService encClient = new MyApplicationEncryptDocumentService(); 
        MyApplicationEncryptDocument encryptDocClient = encClient.getEncryptDocument(); 
         
        ((BindingProvider)encryptDocClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
        ((BindingProvider)encryptDocClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); 
        ((BindingProvider)encryptDocClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); 
          
         //Create a file object 
         File pdf = new File(pdfFile);  
          
         //Create a DataSource object 
         DataSource myDS = new FileDataSource(pdf); 
          
         //Create a DataHandler object 
         DataHandler dataHandler = new DataHandler(myDS);  
                                        
        //Create a BLOB object and populate it with the DataHandler 
        BLOB inDoc = new BLOB(); 
        inDoc.setSwaRef(dataHandler); 
         
        //Invoke the EncryptDocument process 
        BLOB outDoc = encryptDocClient.invoke(inDoc);   
         
        //Save the encrypted file as a PDF file 
        DataHandler handler = outDoc.getSwaRef(); 
         
        //Create a new file containing the returned PDF document 
        File f = new File("C:\\Adobe\EncryptedDocument.pdf"); 
        InputStream inputStream = handler.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(); 
 
         System.out.println("The short-lived process named MyApplication/EncryptDocument was successfully invoked.");             
             
        }catch (Exception e) { 
             e.printStackTrace(); 
            }     
        } 
} 
 
注意: Many quick starts that show how to perform service operations include a SwaRef code example.