Contact Free trial Login

Box Connector Examples - Mule 4

Try out the following examples using Studio or XML.

Anypoint Studio Example: Folders

This example shows how to Create a Folder, Retrieve a Folder, and Delete a Folder.

  1. Create a new Mule project in Anypoint Studio.

  2. Add the following properties to the mule-artifact.properties file and place it in the project’s src/main/resources directory.

    config.clientId=<Client ID>
    config.clientSecret=<Client Secret>
  3. Configure the HTTP Listener by adding a new HTTP global element. Click the + next to the Connector configuration field.

  4. Configure the global element according to the following table:

    Parameter Value

    Name

    HTTP_Listener_config

    Protocol

    HTTP

    Host

    localhost

    Port

    8081

  5. Drag an HTTP Listener onto the canvas and configure the following parameters:

    Parameter Value

    Display Name

    HTTP Listener

    Connector configuration

    Select the newly created HTTP Listener Configuration.

    Path

    /boxdemo

  6. Set the folder properties for the folder you are going to create using a DataWeave component. Drag the DataWeave component next to the HTTP Listener and use the following script.

    %dw 2.0
    output application/json
    ---
    {
    	parent: {
    		id: 0
    	},
    	"type": "folder",
    	name: "SampleFolder"
    }
  7. Drag the Create folder operation of the Box connector next to the DataWeave component to create a folder.

  8. Configure the Box connector by adding a new Box global element. Click the + next to the Connector configuration field.

  9. Configure the global element according to the following table.

    Parameter Value Comments

    clientId

    ${config.clientId}

    The client identifier as assigned by the authorization server when the client application was registered.

    clientSecret

    ${config.clientSecret}

    The client application’s client secret.

    host

    api.box.com

    The default value. Leave as is.

    port

    443

    The default value. Don’t change.

    basePath

    /2.0

    The default value. Don’t change.

    protocol

    HTTPS

    The default value. Don’t change.

    localCallbackPath

    /callback

    The default value. Don’t change.

    localCallbackConfig

    HTTP_Listener_config

    Select the HTTP Listener configuration created earlier.

    externalCallbackUrl

    http://localhost:8081/callback

    Use the redirect URL that is configured in the client application.

    localAuthorizationUrl

    http://localhost:8081/authorize

    Enter this URL after deploying the Mule application to initiate OAUTH2 dance.

    authorizationUrl

    https://account.box.com/api/oauth2/authorize

    The authorization URL to request for an authorization code.

    accessTokenUrl

    https://api.box.com/oauth2/token

    The access token URL to request an access token.

    The corresponding XML configuration should be as follows:

    <mule-box-connector:config name="Mule_box_connector_Config"
     doc:name="Mule-box-connector Config"
     property_clientId="#{config.clientId}"
     property_clientSecret="#{config.clientSecret}"
     property_localCallbackConfig="HTTP_Listener_config"
     property_externalCallbackUrl="http://localhost:8081/callback" />
  10. In the properties editor of the Box connector, configure the parameters required for the Create folder operation:

    Parameter Value

    Display Name

    Create folder

    Basic Settings

    Connector configuration

    Select the global Box connector element that you created.

    General

    Create folder request data

    #[payload]

  11. Drag a Logger component to log the newly created folder ID and configure the following properties.

    Parameter Value

    Display Name

    Specifies the logger to use to log the created folder ID.

    Generic

    Message

    Specifies the folder created with ID : #[payload.id]

  12. Drag the Get folder operation of the Box connector next to the Logger component. This retrieves the folder information and configures the following properties:

    Parameter Value

    Display Name

    Get folder

    Basic Settings

    Connector configuration

    Select the global Box connector element that you created.

    General

    Folder id

    #[payload.id]

  13. Drag a Logger component to log the retrieved folder information and configure the following properties:

    Parameter Value

    Display Name

    Specifies the logger that logs the retrieved folder information.

    Generic

    Message

    #[payload]

  14. Drag the Delete folder operation of the Box connector to delete the folder that was just created and configure the following properties:

    Parameter Value

    Display Name

    Delete folder

    Basic Settings

    Connector configuration

    Select the global Box connector element that you created.

    General

    Folder id

    #[payload.id]

  15. Drag the DataWeave component to set the payload to display the result of the flow to the user, and use the following script:

    %dw 2.0
    output application/json
    ---
    {
    	result : "Folder was created, retrieved and deleted successfully."
    }
  16. Save and run the project as a Mule app.

  17. Use the URL http://localhost:8081/authorize from a browser that you set for localAuthorizationUrl in the global Box configuration element to initiate the OAUTH2 dance.

    This displays the page asking the Box user to either grant or deny access to the client application (which reads and writes all of files and folders stored in Box).

  18. Click Grant access to Box to grant the read and write permissions.

  19. Open a web browser and access http://localhost:8081/boxdemo.

    You should get a JSON response with the following content:

    Result: "Folder created, retrieved and deleted successfully."

XML Example: Demonstrate Create Folder, Retrieve Folder, and Delete Folder Operations

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:mule-box-connector="http://www.mulesoft.org/schema/mule/mule-box-connector"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
  xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
  http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mule-box-connector
http://www.mulesoft.org/schema/mule/mule-box-connector/current/mule-mule-box-connector.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

	<http:listener-config name="HTTP_Listener_config"
		doc:name="HTTP Listener config">
		<http:listener-connection host="localhost"
			port="8081" />
	</http:listener-config>

	<mule-box-connector:config name="Mule_box_connector_Config"
		doc:name="Mule-box-connector Config" property_clientId="#{config.clientId}"
		property_clientSecret="#{config.clientSecret}"
		property_localCallbackConfig="HTTP_Listener_config"
		property_externalCallbackUrl="http://localhost:8081/callback" />
	<flow name="Create-Get-Delete-Folder-Flow">
		<http:listener doc:name="HTTP Listener" path="/boxdemo"
			config-ref="HTTP_Listener_config" />
		<ee:transform doc:name="DataWeave to set folder properties">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
	parent: {
		id: 0
	},
	"type": "folder",
	name: "SampleFolder"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<mule-box-connector:create-folder
			doc:name="Create folder" config-ref="Mule_box_connector_Config" />

		<logger level="INFO" doc:name="Logger to log the created Folder ID"
			message="Folder created with ID : #[payload.id]" />
		<mule-box-connector:get-folder doc:name="Get folder"
			config-ref="Mule_box_connector_Config" folder-id="#[payload.id]" />
		<logger level="INFO" doc:name="Logger to log the retrieved folder info"
			message="#[payload]" />
		<mule-box-connector:delete-folder
			doc:name="Delete folder" config-ref="Mule_box_connector_Config"
			folder-id="#[payload.id]" />
		<ee:transform doc:name="DataWeave to show the result">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
	result : "Folder got created, retrieved and deleted successfully"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
	</flow>
</mule>

Example: Upload a File

This example demonstrates how to upload files to use with other Box operations you have created.

  1. Create a new Mule project in Anypoint Studio.

  2. Create a folder called input in src/main/resources directory.

  3. Copy the file you want to upload to the input directory.

  4. Configure the HTTP Listener by adding a new HTTP global element.

  5. Click the Global Elements tab at the bottom of the canvas.

  6. In the Global Configuration Elements screen, click Create.

  7. In the Choose Global Type wizard, expand the Connector Configuration.

  8. Select HTTP Listener and click OK.

  9. In the HTTP Listener screen configure the following parameters:

    Parameter Value

    Name

    HTTP_Listener_config

    Protocol

    HTTP

    Host

    localhost

    Port

    8081

  10. Drag an HTTP Listener onto the canvas and configure the following parameters:

    Parameter Value

    Display Name

    HTTP Listener

    Connector configuration

    Select the HTTP Listener Configuration that has been previously created.

    Path

    /boxUploadFile

  11. Login to Box and generate the Box developer token which is active for 60 minutes.

  12. Set this Developer Token using a DataWeave component.

  13. Drag the DataWeave component next to the HTTP Listener and configure as follows.

    %dw 2.0
    output application/java
    		//Set the generated developer token here
    var developerToken = "{developer Token}"
    ---
    "Bearer " ++ developerToken
  14. Drag Set Variable onto the Canvas and configure the following parameters:

    Parameter Value

    Display Name

    Save developer token

    Name

    developerToken

    Value

    #[payload]

  15. Configure the File connector by adding a new File Global Element.

  16. Click the Global Elements tab at the bottom of the canvas.

  17. From the Global Configuration Elements screen, click Create.

  18. From the Choose Global Type wizard, expand Connector Configuration.

  19. Select File Config and click OK.

  20. In the File Config screen configure the following parameter:

    Parameter Value

    Working Directory

    ${mule.home}/apps/${app.name}/

  21. Drag a Read File operation onto canvas and configure the following parameters:

    Parameter Value

    Display Name

    Read File

    Connector configuration

    Select the File Configuration that has been previously created in global elements.

    File Path

    input/yourFileName

  22. Transform this payload to a multipart/form-data type of request.

  23. Specify a folder to upload by setting the ID of the parent folder.

  24. Drag a DataWeave component next to the Read File component and use the following script:

    %dw 2.0
    output multipart/form-data
    ---
    {
      parts : {
      	attributes : {
      			headers : {
            		"Content-Type": "application/json"},
          		content : {
          			"name": message.attributes.fileName ,
    	// Set the ID of the parent folder. Use "0" for the root folder.
          	 		"parent": {
          	 		  "id":"0"
    				}
    			}
    	},
        file : {
          headers : {
            "Content-Disposition" : {
                "name": "file",
                "filename": message.attributes.fileName
            }
          },
          content : payload
        }
      }
    }
  25. Configure the HTTP Request by adding a new HTTP Request global element.

  26. Click the Global Elements tab at the bottom of the canvas.

  27. In the Global Configuration Elements screen, click Create.

  28. In the Choose Global Type wizard expand Connector Configuration, select HTTP Request Configuration and click OK.

  29. In the HTTP Request Configuration screen configure the following parameters:

    Parameter Value

    Name

    HTTP_Request_configuration

    Base path

    /

    Protocol

    HTTP(Default)

    Host

    80

    Port

    Use persistent connections

    Max Connections

    -1

    Connection idle timeout

    30000

    Response buffer size

    1024

    TLS configuration

    None

    Proxy

    None

    Authentication

    None

    Client socket properties

    None

    Use reconnection

    unchecked

  30. Drag an HTTP request onto the canvas and configure the following parameters:

    Parameter Value

    Display Name

    Request - Box Upload File

    Configuration

    Select the HTTP request configuration that you created in global elements.

    Method

    POST

    Path

    URL

    https://upload.box.com/api/2.0/files/content

    Body

    payload

  31. Create an Authorization header in the HTTP Request.

  32. Click the Headers tab next to the Body in the HTTP request and click + to configure the following parameter:

    Name Value

    "Authorization"

    vars.developerToken

  33. Drag the DataWeave component to set the payload to display the result of the flow to the user and configure as follows:

    %dw 2.0
    output application/json
    ---
    {
    	result : "File has been successfully created"
    }
  34. Save and run the project as a Mule application.

  35. Enter the URL http://localhost:8081/boxUploadFile in the browser. You should get a JSON response with the following content:

    result : "File has been successfully created"

XML Example: Upload File

Here is the XML example to upload a file.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mule-box-connector="http://www.mulesoft.org/schema/mule/mule-box-connector" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:file="http://www.mulesoft.org/schema/mule/file"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/mule-box-connector http://www.mulesoft.org/schema/mule/mule-box-connector/current/mule-mule-box-connector.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<file:config name="File_Config" doc:name="File Config">
		<file:connection workingDir="${mule.home}/apps/${app.name}/" />
	</file:config>
	<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" >
		<http:request-connection host="80" />
	</http:request-config>
	<flow name="UploadFileToBox" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/boxUploadFile" />
		<ee:transform doc:name="Set Developer Token Variable">
			<ee:message>
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
		//Set the generated developer token here
var developerToken = "{developer Token}"
---
"Bearer " ++ developerToken]]></ee:set-payload>
			</ee:message>
			<ee:variables>
			</ee:variables>
		</ee:transform>
		<set-variable value="#[payload]" doc:name="Save developer token" variableName="developerToken"/>
		<file:read doc:name="Read File" config-ref="File_Config" path="input/yourFileName"/>
		<ee:transform doc:name="Transform to multipart/form-data">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output multipart/form-data
---
{
  parts : {
  	attributes : {
  			headers : {
        		"Content-Type": "application/json"},
      		content : {
      			"name": message.attributes.fileName ,
	// Set the ID of the parent folder. Use "0" for the root folder.
      	 		"parent": {
      	 		  "id":"0"
				}
			}
	},
    file : {
      headers : {
        "Content-Disposition" : {
            "name": "file",
            "filename": message.attributes.fileName
        }
      },
      content : payload
    }
  }
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<http:request method="POST" doc:name="Request - Box Upload File" url="https://upload.box.com/api/2.0/files/content" config-ref="HTTP_Request_configuration">
			<http:headers ><![CDATA[#[output application/java
---
{
	"Authorization" : vars.developerToken,
	"Key" : "Value"
}]]]></http:headers>
		</http:request>
		<ee:transform doc:name="Transform Response">
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	result : "File has been successfully created"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
	</flow>
</mule>

See Also

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.