Skip to content

Creating a REST Web Service

Create a Job that provides a RESTful Web Service endpoint to create a Formbird Document.

Items used

Type Name
Routines FormbirdRoutine
Components tRESTRequest, tRESTResponse, tJava, tJavaRow, tRunJob

Overview

  1. Setup a Main Job (Job 1) as a REST endpoint that calls a Sub Job (Job 2)
  2. Setup a Sub Job (Job 2) that creates a Formbird Document using provided input

1. REST endpoint (Main Job) ==> 2. Formbird Insert (Sub Job)

Setup Main Job

Create a new Job, and name it "CreateAssetService". This Job sets up a REST endpoint to trigger the Sub Job.

  1. Setup your Job's Context Variables

  2. Select the Jobs Context tab, and create the Context Variables as defined in Contexts and Configuration Files section of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio.

  3. Add a tRESTRequest component

  4. Configure the REST Endpoint to "/rest/FormbirdService"

  5. Configure a REST API Mapping as follows:
    1. HTTP Verb: POST
    2. URL Pattern: "/createAsset"
    3. Consumes: JSON
    4. Produces: JSON
  6. Select the ellipses button on the new ReST API Mapping and add a record as follows:

    1. Column: body
    2. Type: String
  7. Add an Object context variable named requestDocument.

  8. Add an Object context variable named responseDocument.

  9. Add a tJavaRow component to the Job, and copy the following code to it.

  10. Under the component's Advanced settings panel, add:

    ```java import org.apache.log4j.Logger;

    import com.fieldtec.webclient.config.WebclientConfig; import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.service.FieldtecService; import com.fieldtec.webclient.service.impl.FieldtecServiceImpl; import com.fieldtec.webclient.util.UtilJson; ```

  11. Under the component's Basic settings panel, add:

    ```java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent;

    // clear context context.queueItem = null; context.requestDocument = null; context.responseDocument = null;

    logger.info(method + " Received Request"); logger.debug(method + " Received Request body is " + input_row.body);

    // set context.requestDocument context.requestDocument = input_row.body;

    // set context.responseBean com.fieldtec.webclient.model.Document responseDocument = new com.fieldtec.webclient.model.Document(); context.responseDocument = responseDocument;

    logger.info(method + " call subJob"); ```

  12. Add a tRunJob component and select a sub Job to run. Select Transmit whole context to pass context values to the sub Job. Deselect Die on child error.

  13. Add a tJava component, and copy the following code to it.

  14. Under the component's Advanced settings panel, add:

    java import org.apache.log4j.Logger;

  15. Under the component's Basic settings panel, add:

    java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent; logger.info(method + " onSubjobOk");

  16. Add a tJavaRow component, and copy the following code to it.

  17. Under the component's Advanced settings panel, add:

    ```java import org.apache.log4j.Logger;

    import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.util.UtilJson; ```

  18. Under the component's Basic settings panel, add:

    ```java method = jobName + "." + currentComponent; logger.info(method + " process responseDocument");

    com.fieldtec.webclient.model.Document responseDocument = (com.fieldtec.webclient.model.Document) context.responseDocument; String result = UtilJson.prettyPrint(responseDocument); logger.info(method + " result is " + result);

    output_row.body = result; logger.info(method + " return result in response body"); ```

  19. Add a tRESTResponse component and configure as follows:

  20. Return Body Type: String

  21. Return status code: OK (200)
  22. Pass body as the schema

  23. Connect the tRESTRequest component [step 2] to the tJavaRow component [step 5] using a Main Row link. Pass body as the schema.

  24. Connect the tJavaRow component [step 5] to the tRunJob component [step 6] using an Run If trigger, and specify the trigger as: "context.requestDocument != null".

  25. Connect the tRunJob component [step 6] to the tJava component [step 7] using an On Subjob Ok trigger.

  26. Connect the tJava component [step 7] to the tJavaRow component [step 8] using a Main Row link. Pass body as the schema.

  27. Connect the tJavaRow component [step 8] to the tRESTResponse component [step 9] using a Main Row link. Pass body as the schema.

Setup Sub Job

Create a new Job, and name it "CreateAssetService_action". This Job creates a Formbird document.

  1. Setup your Job's Context Variables

  2. Select the Jobs Context tab, and create the Context Variables as defined in Contexts and Configuration Files section of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio.

  3. Add an Object context variable named requestDocument.

  4. Add an Object context variable named responseDocument.

  5. Add a tJava component to the Job, and copy the following code to it.

  6. Under the component's Advanced settings panel, add:

    ```java import org.apache.log4j.Logger;

    import com.fieldtec.webclient.config.WebclientConfig; import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.service.FieldtecService; import com.fieldtec.webclient.service.impl.FieldtecServiceImpl; import com.fieldtec.webclient.util.UtilJson; ```

  7. Under the component's Basic settings panel, add:

    ```java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent;

    FieldtecService ftService = null;

    com.fieldtec.webclient.model.Document responseDoc = null;

    try { // Initialise ftService (always synchronize context and then setup webclientConfig) context.synchronizeContext(); FormbirdRoutine.setupWebClientConfig(context); ftService = new FieldtecServiceImpl();

    String assetTemplateId = "5ba9bbee5b116026f1b23dad";
    
      com.fieldtec.webclient.model.Document requestDocument = (com.fieldtec.webclient.model.Document) context.requestDocument;
    
    com.fieldtec.webclient.model.Document doc = new Document();
    doc.setValue("assetId", requestDocument.getValue("assetId"));
    doc.setValue("name", requestDocument.getValue("assetName");
    doc.setValue("description", requestDocument.getValue("assetDesc");
    doc.setValue(FormbirdRoutine.KEY_SYSTEMHEADER_TEMPLATE_ID, assetTemplateId);
    
    // Insert document
    Document responseDocument = ftService.insert(doc);
    if (responseDocument != null) {
        // Get documentId to log
          String documentId = (String) responseDocument.getValue(FormbirdRoutine.KEY_DOCUMENT_ID);
        logger.info("Inserted: " + documentId);
        logger.info(UtilJson.prettyPrint(responseDocument));
    
          responseDoc = new com.fieldtec.webclient.model.Document();
          responseDoc.setValue("status", "Success");
          responseDoc.setValue("message", documentId);
    } else {
        // Handle error
        logger.error("Error inserting document");
    
          responseDoc = new com.fieldtec.webclient.model.Document();
          responseDoc.setValue("status", "Error");
          responseDoc.setValue("message", "Error encountered inserting document");
    }
    

    } finally { if (responseDoc == null) { responseDoc = new com.fieldtec.webclient.model.Document(); responseDoc.setValue("status", "Error"); responseDoc.setValue("message", "Error encountered inserting document"); }

      // Set context variable with response for REST caller
      context.responseDocument = responseDoc;
    
    if (ftService != null) {
        ftService.close();
    }
    

    } ```

Run the Main Job

  1. Return to the Main Job.
  2. Navigate to the Job's Run tab, and select Run. Make sure your Formbird URLs and credentials are correct and point to a running Formbird environment.

Congratulations! You have setup a REST endpoint to integrate with Formbird!