Quick Start(SOAP mode): Authenticating a user using the Java API

The following code example authenticates a user and obtains the user’s context. The user’s content is used to create a Context object. The Context object is used to invoke the Encryption service and encrypt a PDF document with a password. (See Authenticating Users.)

/* 
    * This Java Quick Start uses the SOAP mode and contains the following JAR files 
    * in the class path: 
    * 1. adobe-livecycle-client.jar 
    * 2. adobe-usermanager-client.jar 
    * 3. activation.jar (required for SOAP mode) 
    * 4. axis.jar (required for SOAP mode) 
    * 5. axis.jar (required for SOAP mode) 
    * 6. commons-codec-1.3.jar (required for SOAP mode) 
    * 7. commons-collections-3.2.jar  (required for SOAP mode) 
    * 8. commons-discovery.jar (required for SOAP mode) 
    * 9. commons-logging.jar (required for SOAP mode) 
    * 10. dom3-xml-apis-2.5.0.jar (required for SOAP mode) 
    * 11. jaxen-1.1-beta-9.jar (required for SOAP mode) 
    * 12. jaxrpc.jar (required for SOAP mode) 
    * 13. log4j.jar (required for SOAP mode) 
    * 14. mail.jar (required for SOAP mode) 
    * 15. saaj.jar (required for SOAP mode) 
    * 16. wsdl4j.jar (required for SOAP mode) 
    * 17. xalan.jar (required for SOAP mode) 
    * 18. xbean.jar (required for SOAP mode) 
    * 19. xercesImpl.jar (required for SOAP mode) 
    * 20. adobe-encryption-client.jar 
    *  
    *  The JBoss files must be kept in the jboss\client folder. You can copy the client folder to  
    *  your local development environment and then include the 3 JBoss JAR files in your class path 
    *   
    *  These JAR files are located in the following path: 
    * <install directory>/sdk/client-libs/common 
    *  
    *  
    * <install directory>/jboss/client 
    *  
    * If you want to invoke a remote forms server instance and there is a 
    * firewall between the client application and the server, then it is  
    * recommended that you use the SOAP mode. When using the SOAP mode,  
    * you have to include additional JAR files located in the following  
    * path 
    * <install directory>/sdk/client-libs/thirdparty 
    *  
    * For information about the SOAP  
    * mode and the additional JAR files that need to be included,  
    * see "Setting connection properties" in Programming  
    * with LiveCycle 
    *  
    * For complete details about the location of the LiveCycle JAR files,  
    * see "Including LiveCycle Java library files" in Programming  
    * with LiveCycle 
    */ 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.util.*; 
 
import com.adobe.idp.Context; 
import com.adobe.idp.Document; 
import com.adobe.idp.um.api.infomodel.*; 
import com.adobe.livecycle.encryption.client.EncryptionServiceClient; 
import com.adobe.livecycle.encryption.client.PasswordEncryptionCompatability; 
import com.adobe.livecycle.encryption.client.PasswordEncryptionOption; 
import com.adobe.livecycle.encryption.client.PasswordEncryptionOptionSpec; 
import com.adobe.livecycle.encryption.client.PasswordEncryptionPermission; 
import com.adobe.livecycle.usermanager.client.AuthenticationManagerServiceClient; 
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory; 
import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties; 
 
public class AuthenticateUsersTest 
{ 
    public static void main(String[] args) { 
        try { 
            //Set connection properties required to invoke LiveCycle 
            Properties connectionProps = new Properties(); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_SOAP_ENDPOINT, "jnp://[server]:[port]"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL, ServiceClientFactoryProperties.DSC_SOAP_PROTOCOL); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, ServiceClientFactoryProperties.DSC_JBOSS_SERVER_TYPE); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_USERNAME, "administrator"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "password"); 
             
            //Create a ServiceClientFactory object 
            ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps); 
 
            // Create an AuthenticationManagerServiceClient object 
            AuthenticationManagerServiceClient authClient = new AuthenticationManagerServiceClient(myFactory); 
 
            // Authenticate the user 
            String username = "wblue"; 
            String password = "password"; 
            AuthResult authResult = authClient.authenticate(username, password.getBytes()); 
 
            // Get the authenticated user's information 
            User authUser = authResult.getAuthenticatedUser(); 
            System.out.println("Authenticated user's canonical name: " + authUser.getCanonicalName()); 
            System.out.println("Authenticated user's domain name: " + authUser.getDomainName()); 
             
            //Set a Content object that represents the authenticated user 
            //Use the Context object to invoke the Encryption service 
            Context myCtx = new Context();  
            myCtx.initPrincipal(authResult); 
             
            Properties connectionProps2 = new Properties(); 
            connectionProps2.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_SOAP_ENDPOINT, "jnp://[server]:[port]"); 
            connectionProps2.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL, ServiceClientFactoryProperties.DSC_SOAP_PROTOCOL); 
            connectionProps2.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, ServiceClientFactoryProperties.DSC_JBOSS_SERVER_TYPE); 
                         
            //Create a ServiceClientFactory object 
            ServiceClientFactory myFactory2 = ServiceClientFactory.createInstance(connectionProps2); 
            myFactory2.setContext(myCtx); 
                         
            //Encryption a PDF document with a password as wblue 
            EncryptionServiceClient encryptClient  = new EncryptionServiceClient(myFactory2); 
             
            //Specify the PDF document to encrypt with a password 
            FileInputStream fileInputStream = new FileInputStream("C:\\Adobe\Loan.pdf");     
            Document inDoc = new Document (fileInputStream);  
 
            //Create a PasswordEncryptionOptionSpec object that stores encryption run-time values 
            PasswordEncryptionOptionSpec passSpec = new PasswordEncryptionOptionSpec(); 
             
            //Specify the PDF document resource to encrypt 
            passSpec.setEncryptOption(PasswordEncryptionOption.ALL); 
             
            //Specify the permission associated with the password 
            //These permissions enable data to be extracted from a password 
            //protected PDF form 
            List<PasswordEncryptionPermission> encrypPermissions = new ArrayList<PasswordEncryptionPermission>();  
            encrypPermissions.add(PasswordEncryptionPermission.PASSWORD_EDIT_ADD); 
            encrypPermissions.add(PasswordEncryptionPermission.PASSWORD_EDIT_MODIFY); 
            passSpec.setPermissionsRequested(encrypPermissions); 
             
            //Specify the Acrobat version 
            passSpec.setCompatability(PasswordEncryptionCompatability.ACRO_7); 
             
            //Specify the password values 
            passSpec.setDocumentOpenPassword("OpenPassword"); 
            passSpec.setPermissionPassword("PermissionPassword"); 
             
            //Encrypt the PDF document with a password 
            Document encryptDoc = encryptClient.encryptPDFUsingPassword(inDoc,passSpec);  
             
            //Save the password-encrypted PDF document 
            File outFile = new File("C:\\Adobe\EncryptLoan.pdf"); 
            encryptDoc.copyToFile (outFile); 
        } 
        catch (Exception e) 
        { 
            System.out.println("Error Occurred: " + e.getMessage()); 
        } 
    }  
} 
 

// Ethnio survey code removed