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

The following Java code example invokes a process named MyApplication/EncryptDocument using DIME. (See Invoking LiveCycle using DIME.)

An unsecured PDF document based on a PDF file named Loan.pdf is passed to the AEM Forms process using DIME. The process returns a password-encrypted PDF document that is saved as a PDF file named EncryptLoan.pdf .

/** 
    * Ensure that you create Java Axis files that  
    * are required to send a LiveCycle process  
    * an attachment using DIME. 
    *  
    * For information, see "Invoking LiveCycle using DIME" in Programming with AEM forms.   
    */ 
import com.adobe.idp.services.*; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.net.URL; 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
 
import org.apache.axis.attachments.AttachmentPart; 
 
public class InvokeDocumentEncryptDime { 
    public static void main(String[] args) { 
     
    try{ 
         
        //Create a MyApplicationEncryptDocumentServiceLocator object 
        MyApplicationEncryptDocumentServiceLocator locate = new MyApplicationEncryptDocumentServiceLocator (); 
 
        //specify the service target URL and object type 
        URL serviceURL = new URL("http://[server]:[port]/soap/services/MyApplication/EncryptDocument?blob=dime"); 
         
        //Use the binding stub with the locator 
        EncryptDocumentSoapBindingStub encryptionClientStub = new EncryptDocumentSoapBindingStub(serviceURL,locate); 
        encryptionClientStub.setUsername("administrator"); 
        encryptionClientStub.setPassword("password");     
             
         //Get the DIME Attachments - which is the PDF document to encrypt 
         java.io.File file = new java.io.File("C:\\Adobe\Loan.pdf"); 
                       
         //Create a DataHandler object 
         DataHandler buildFile = new DataHandler(new FileDataSource(file)); 
          
         //Use the DataHandler object to create an AttachmentPart object  
         AttachmentPart part = new AttachmentPart(buildFile); 
        //get the attachment ID 
        String attachmentID = part.getContentId();     
         
        //Add the attachment to the encryption service stub 
        encryptionClientStub.addAttachment(part);  
                                 
        //Inform ES where the attachment is stored by providing the attachment id 
        BLOB inDoc = new BLOB(); 
        inDoc.setAttachmentID(attachmentID); 
                         
        BLOB outDoc = encryptionClientStub.invoke(inDoc); 
 
        //Go through the returned attachments and get the encrypted PDF document 
        byte[] resultByte = null;         
        attachmentID = outDoc.getAttachmentID(); 
         
        //Find the proper attachment 
        Object[] parts = encryptionClientStub.getAttachments(); 
        for (int i=0;i<parts.length;i++){ 
            AttachmentPart attPart = (AttachmentPart)  parts[i]; 
            if (attPart.getContentId().equals(attachmentID)) { 
                //DataHandler 
                buildFile = attPart.getDataHandler(); 
                InputStream stream = buildFile.getInputStream();  
                 
                byte[] pdfStream = new byte[stream.available()]; 
                stream.read(pdfStream); 
                 
                //Create a File object 
                File outFile = new File("C:\\Adobe\EncryptLoan.pdf"); 
                 
                //Create a FileOutputStream object. 
                FileOutputStream myFileW = new FileOutputStream(outFile); 
                 
                //Call the FileOutputStream object?s write method and pass the pdf data 
                myFileW.write(pdfStream); 
                 
                //Close the FileOutputStream object 
                myFileW.close(); 
                 
            } 
        } 
    } 
    catch(Exception e) 
    { 
        e.printStackTrace(); 
    } 
    } 
 
} 

// Ethnio survey code removed