Quick Start(EJB 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 EJB mode and contains the following JAR files 
    * in the class path: 
    * 1. adobe-livecycle-client.jar 
    * 2. adobe-usermanager-client.jar 
    * 3. adobe-utilities.jar 
    * 4. jbossall-client.jar (use a different JAR file if the LiveCycle server is not deployed 
    * on JBoss) 
    * 5. adobe-encryption-client.jar 
    * 6. jacorb.jar (use a different JAR file if the LiveCycle server is not deployed on JBoss) 
    * 7. jnp-client.jar (use a different JAR file if the LiveCycle server is not deployed on JBoss) 
    *  
    *  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 
    *  
    * The adobe-utilities.jar file is located in the following path: 
    * <install directory>/sdk/client-libs/jboss 
    *  
    * The jbossall-client.jar file is located in the following path: 
    * <install directory>/jboss/client 
    *  
    * If you want to invoke a remote LiveCycle 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_EJB_ENDPOINT, "jnp://hiro-xp:1099"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL, ServiceClientFactoryProperties.DSC_EJB_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_EJB_ENDPOINT, "jnp://hiro-xp:1099"); 
            connectionProps2.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL, ServiceClientFactoryProperties.DSC_EJB_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()); 
        } 
    }  
}