Quick Start (Base64): Handling a form containing a calculation script using web service API

The following code example processes a form that contains a calculation script and writes the results back to the client web browser. (See Calculating Form Data.)

/* 
    * Ensure that you create the Java proxy classes to use  
    * base64 encoding. This is required to populate a BLOB  
    * object with data or retrieve data from a BLOB object. 
    *  
    * For information, see "Creating Java proxy classes using Apache Axis"  
    * in Programming with LiveCycle.   
    */ 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintWriter; 
import javax.servlet.Servlet; 
import javax.servlet.ServletException; 
import javax.servlet.ServletOutputStream; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.adobe.idp.services.BLOB; 
import com.adobe.idp.services.FormsResult; 
import com.adobe.idp.services.FormsService; 
import com.adobe.idp.services.FormsServiceServiceLocator; 
import com.adobe.idp.services.RenderOptionsSpec; 
import com.adobe.idp.services.holders.BLOBHolder; 
import com.adobe.idp.services.holders.FormsResultHolder; 
import com.adobe.idp.services.holders.MyArrayOf_xsd_anyTypeHolder; 
 
public class CalculateDataWS extends HttpServlet implements Servlet { 
 
        public void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 
            doPost(req,resp); 
    } 
     
    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 
        try{ 
     
            //Create a FormsService object 
            com.adobe.idp.services.FormsServiceServiceLocator sl = new FormsServiceServiceLocator(); 
            FormsService formsOb = sl.getFormsService(); 
                 
            ((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub. 
            USERNAME_PROPERTY, "administrator"); 
            ((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub. 
            PASSWORD_PROPERTY, "password"); 
                     
            //Create a BLOB object to pass 
            //to processFormSubmission 
            BLOB inForm = new BLOB();  
         
            //Get the input stream passed to the servlet and copy 
            //its contents into a byte array 
             int readBytes = -1; 
             int bufLength = req.getContentLength(); 
             InputStream input = req.getInputStream(); 
             byte[] buffer = new byte[bufLength]; 
             ByteArrayOutputStream output = new ByteArrayOutputStream(bufLength); 
             while((readBytes = input.read(buffer, 0, bufLength)) != -1) { 
                 output.write(buffer, 0, readBytes); 
              } 
             byte[] finalOutput = output.toByteArray(); 
         
            //Populate the BLOB object  
            inForm.setBinaryData(finalOutput); 
                         
            //Set run-time options 
            RenderOptionsSpec processSpec = new RenderOptionsSpec(); 
            processSpec.setLocale("en_US"); 
         
            //Specify user agent values 
            String agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;.NET CLR 1.1.4322)"; 
         
            //Specify env values 
            String envValues = "CONTENT_TYPE=application/pdf&CONTENT_TYPE=application/vnd.adobe.xdp+xml"; 
             
            //Create class holders to pass to ProcessFormSubmission 
            BLOBHolder outProcessFormSubmissionResultDoc = new BLOBHolder();  
            javax.xml.rpc.holders.StringHolder clickedButton = new javax.xml.rpc.holders.StringHolder(); 
            BLOBHolder outXML = new BLOBHolder(); 
            BLOBHolder validationErrorsList = new BLOBHolder(); 
            javax.xml.rpc.holders.ShortHolder action = new javax.xml.rpc.holders.ShortHolder();  
            MyArrayOf_xsd_anyTypeHolder attachments = new MyArrayOf_xsd_anyTypeHolder(); 
            FormsResultHolder processFormSubmissionResult = new FormsResultHolder();  
         
            //Invoke the processFormSubmission method 
             formsOb.processFormSubmission( 
                inForm, 
                envValues , 
                agent, 
                processSpec, 
                outProcessFormSubmissionResultDoc, 
                clickedButton, 
                outXML, 
                validationErrorsList, 
                action, 
                attachments, 
                processFormSubmissionResult); 
         
            //Get the FormResult from the FormsResultHolder     
            FormsResult formResult = processFormSubmissionResult.value ;  
             
            //Get the processing state 
             short processState = formResult.getAction(); 
              
             //Determine if the form data is calculated 
            if (processState == 1) 
             { 
         
                //Get the BLOB object that represents the processed form 
                 BLOB formData= formResult.getOutputContent(); 
                  
                 //Get the content type of the response and 
                //set the HttpServletResponse object's content type 
                String contentType = formData.getContentType(); 
                resp.setContentType(contentType); 
         
                //Create a ServletOutputStream object 
                ServletOutputStream oOutput = resp.getOutputStream(); 
                             
                //Create a byte array that stores form data in the BLOB object 
                byte[] cContent = formData.getBinaryData(); 
         
                //Write a byte stream back to the web browser.  
                //Pass the byte array 
                oOutput.write(cContent); 
         
                  } 
        }catch (Exception e) { 
             System.out.println("The following error occurred during this operation " +e.getMessage()); 
        } 
    } 
}

// Ethnio survey code removed