Quick Start: Invoking a short-lived process by passing a secure document using Remoting

The following code example invokes the MyApplication/EncryptDocument.A user must login to click the Select File button that is used to upload a PDF file and invoke the process. That is, once the user is authenticated, the Select File button is enabled. The following illustration shows the Flex client application after a user is authenticated. Notice that the Authenticated CheckBox is enabled.

if LiveCycle is configured to only allow secure documents to be uploaded and the user not have the Document Upload Application User role, then an exception is thrown. If the user does have this role, then the file is uploaded and the process is invoked.

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"  
     creationComplete="initializeChannelSet();"> 
       <mx:Script> 
       <![CDATA[ 
     import mx.rpc.livecycle.DocumentReference; 
     import flash.net.FileReference; 
     import flash.net.URLRequest; 
     import flash.events.Event; 
     import flash.events.DataEvent; 
     import mx.messaging.ChannelSet; 
     import mx.messaging.channels.AMFChannel;   
     import mx.rpc.events.ResultEvent; 
     import mx.collections.ArrayCollection; 
     import mx.rpc.AsyncToken; 
     import mx.controls.Alert; 
     import mx.rpc.events.FaultEvent; 
     import mx.rpc.AsyncResponder; 
 
     // Classes used in file retrieval   
     private var fileRef:FileReference = new FileReference(); 
     private var docRef:DocumentReference = new DocumentReference(); 
     private var parentResourcePath:String = "/"; 
     private var now1:Date;    
     private var serverPort:String = "hiro-xp:8080"; 
     
     // Define a ChannelSet object. 
     public var cs:ChannelSet;  
     
     // Define an AsyncToken object. 
     public var token:AsyncToken; 
     
      // Holds information returned from LiveCycle  
     [Bindable] 
     public var progressList:ArrayCollection = new ArrayCollection(); 
 
          
     // Handles a successful login 
    private function LoginResultEvent(event:ResultEvent, 
        token:Object=null):void  { 
            switch(event.result) { 
                case "success": 
                    authenticatedCB.selected = true; 
                    btnFile.enabled = true; 
                    btnLogout.enabled = true; 
                    btnLogin.enabled = false; 
                        break; 
                    default: 
                } 
            } 
             
             
// Handle login failure. 
private function LoginFaultEvent(event:FaultEvent, 
    token:Object=null):void { 
    switch(event.fault.faultCode) { 
                case "Client.Authentication": 
                        default: 
                        authenticatedCB.selected = false; 
                        Alert.show("Login failure: " + event.fault.faultString); 
                } 
            } 
                 
     
     // Set up channel set to invoke LiveCycle  
     private function initializeChannelSet():void { 
       cs = new ChannelSet();  
       cs.addChannel(new AMFChannel("remoting-amf", "http://" + serverPort + "/remoting/messagebroker/amf"));  
       EncryptDocument2.channelSet = cs; 
     } 
 
    // Call this method to upload the file. 
     // This creates a file picker and lets the user select a PDF file to pass to the EncryptDocument process. 
     private function uploadFile():void { 
       fileRef.addEventListener(Event.SELECT, selectHandler); 
       fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeHandler); 
       fileRef.browse(); 
     } 
 
     // Gets called for selected file. Does the actual upload via the file upload servlet. 
     private function selectHandler(event:Event):void { 
             var authTokenService:RemoteObject = new RemoteObject("LC.FileUploadAuthenticator"); 
        authTokenService.addEventListener("result", authTokenReceived); 
        authTokenService.channelSet = cs; 
        authTokenService.getFileUploadToken(); 
     } 
 
    private function authTokenReceived(event:ResultEvent):void 
    { 
    var token:String = event.result as String; 
    var request:URLRequest = DocumentReference.constructRequestForUpload("http://hiro-xp:8080", token); 
             
    try 
    { 
          fileRef.upload(request); 
    } 
    catch (error:Error) 
    { 
        trace("Unable to upload file."); 
    }                               
} 
 
     // Called once the file is completely uploaded. 
     private function completeHandler(event:DataEvent):void {                     
 
       // Set the docRef's url and referenceType parameters 
       docRef.url = event.data as String;       
       docRef.referenceType=DocumentReference.REF_TYPE_URL; 
       executeInvokeProcess();  
     }       
 
    //This method invokes the EncryptDocument process 
     public function executeInvokeProcess():void { 
        //Create an Object to store the input value for the EncryptDocument process 
          now1 = new Date(); 
     
        var params:Object = new Object(); 
        params["inDoc"]=docRef; 
 
        // Invoke the EncryptDocument process 
        var token:AsyncToken;             
        token = EncryptDocument2.invoke(params); 
        token.name = name; 
     } 
 
     // LiveCycle  login method 
     private function ROLogin():void { 
        // Make sure that the user is not already logged in. 
         
        //Get the User and Password 
        var userName:String = txtUser.text; 
        var pass:String = txtPassword.text; 
         
       if (cs.authenticated == false) { 
            token = cs.login(userName, pass); 
         
        // Add result and fault handlers. 
        token.addResponder(new AsyncResponder(LoginResultEvent,    LoginFaultEvent)); 
                } 
            } 
     
     // This method handles a successful process invocation 
     public function handleResult(event:ResultEvent):void 
     { 
           //Retrieve information returned from the service invocation 
         var token:AsyncToken = event.token;         
         var res:Object = event.result; 
         var dr:DocumentReference = res["outDoc"] as DocumentReference; 
         var now2:Date = new Date(); 
 
          // These fields map to columns in the DataGrid 
         var progObject:Object = new Object(); 
         progObject.filename = token.name; 
         progObject.timing = (now2.time - now1.time).toString(); 
         progObject.state = "Success"; 
         progObject.link = "<a href='" + dr.url + "'> open </a>"; 
         progressList.addItem(progObject); 
     } 
     
     // Prompt user to login on a fault.          
      private function faultHandler(event:FaultEvent):void 
           { 
            if(event.fault.faultCode=="Client.Authentication") 
            { 
                Alert.show( 
                    event.fault.faultString + "\n" + 
                    event.fault.faultCode + "\n" + 
                    "Please login to continue."); 
            } 
           } 
     
      // LiveCycle  logout method 
    private function ROLogout():void { 
        // Add result and fault handlers. 
        token = cs.logout(); 
        token.addResponder(new AsyncResponder(LogoutResultEvent,LogoutFaultEvent)); 
    } 
 
    // Handle successful logout. 
    private function LogoutResultEvent(event:ResultEvent, 
        token:Object=null):void { 
        switch (event.result) { 
        case "success": 
                authenticatedCB.selected = false; 
                btnFile.enabled = false; 
                btnLogout.enabled = false; 
                btnLogin.enabled = true; 
                break; 
                default: 
            } 
    } 
 
    // Handle logout failure. 
    private function LogoutFaultEvent(event:FaultEvent, 
            token:Object=null):void { 
            Alert.show("Logout failure: " + event.fault.faultString); 
    } 
 
         private function resultHandler(event:ResultEvent):void { 
         // Do anything else here. 
         } 
       ]]> 
 
     </mx:Script> 
     <mx:RemoteObject id="EncryptDocument" destination="MyApplication/EncryptDocument" result="resultHandler(event);"> 
         <mx:method name="invoke" result="handleResult(event)"/> 
     </mx:RemoteObject> 
     
      <!--//This consists of what is displayed on the webpage--> 
     <mx:Panel id="lcPanel" title="EncryptDocument LiveCycle Remoting Example"  
          height="25%" width="25%" paddingTop="10" paddingLeft="10" paddingRight="10"  
          paddingBottom="10"> 
        <mx:Label width="100%" color="blue" 
               text="Select a PDF file to pass to the EncryptDocument process"/>  
       <mx:DataGrid x="10" y="0" width="500" id="idProgress" editable="false"  
          dataProvider="{progressList}" height="231" selectable="false" > 
         <mx:columns> 
           <mx:DataGridColumn headerText="Filename" width="200" dataField="filename" editable="false"/> 
           <mx:DataGridColumn headerText="State" width="75" dataField="state" editable="false"/> 
           <mx:DataGridColumn headerText="Timing" width="75" dataField="timing" editable="false"/> 
           <mx:DataGridColumn headerText="Click to Open" dataField="link" editable="false" > 
            <mx:itemRenderer> 
               <mx:Component> 
                  <mx:Text x="0" y="0" width="100%" htmlText="{data.link}"/> 
               </mx:Component> 
            </mx:itemRenderer> 
           </mx:DataGridColumn> 
         </mx:columns> 
       </mx:DataGrid> 
       <mx:Button label="Select File" click="uploadFile()"  id="btnFile" enabled="false"/> 
       <mx:Button label="Login" click="ROLogin();" id="btnLogin"/> 
       <mx:Button label="LogOut" click="ROLogout();" enabled="false" id="btnLogout"/> 
       <mx:HBox> 
        <mx:Label text="User:"/> 
        <mx:TextInput id="txtUser" text=""/> 
        <mx:Label text="Password:"/> 
        <mx:TextInput id="txtPassword" text="" displayAsPassword="true"/> 
        <mx:CheckBox id="authenticatedCB"  
            label="Authenticated?"  
            enabled="false"/> 
    </mx:HBox> 
     </mx:Panel> 
</mx:Application>