Box Connector 5.2 の例 - Mule 4

Studio または XML を使用して次の例を試してください。

Anypoint Studio の例: フォルダー

この例では、フォルダーの作成、取得、および削除方法について説明します。

  1. Anypoint Studio で新しい Mule プロジェクトを作成します。

  2. 次のプロパティを ​mule-artifact.properties​ ファイルに追加し、このファイルをプロジェクトの ​src/main/resources​ ディレクトリに置きます。

    config.clientId=<Client ID>
    config.clientSecret=<Client Secret>
  3. 新しい HTTP グローバル要素を追加して、HTTP リスナーを設定します。[Connector configuration (コネクタ設定)] 項目の横にある ​[+]​ をクリックします。

  4. 次の表に従ってグローバル要素を設定します。

    パラメーター

    Name (名前)

    HTTP_Listener_config

    Protocol (プロトコル)

    HTTP

    Host (ホスト)

    localhost

    Port (ポート)

    8081

  5. [HTTP Listener (HTTP リスナー)] をキャンバスにドラッグし、次のパラメーターを設定します。

    パラメーター

    Display Name (表示名)

    HTTP Listener (HTTP リスナー)

    Connector configuration (コネクタ設定)

    新しく作成した HTTP リスナー設定を選択します。

    Path (パス)

    /boxdemo

  6. DataWeave コンポーネントを使用して作成するフォルダーのフォルダープロパティを設定します。 [DataWeave]​ コンポーネントを HTTP リスナーの横にドラッグし、次のスクリプトを使用します。

    %dw 2.0
    output application/json
    ---
    {
    	parent: {
    		id: 0
    	},
    	"type": "folder",
    	name: "SampleFolder"
    }
  7. Box Connector の ​[Create folder]​ 操作を、フォルダーを作成する [DataWeave] コンポーネントの横にドラッグします。

  8. 新しい Box グローバル要素を追加して、Box Connector を設定します。[Connector configuration (コネクタ設定)] 項目の横にある ​[+]​ をクリックします。

  9. 次の表に従ってグローバル要素を設定します。

    パラメーター コメント

    clientId

    ${config.clientId}

    クライアントアプリケーションの登録時に認証サーバーにより割り当てられたクライアント識別子。

    clientSecret

    ${config.clientSecret}

    クライアントアプリケーションのクライアントシークレット。

    host (ホスト)

    api.box.com

    デフォルト値。そのままにしてください。

    port (ポート)

    443

    デフォルト値。変更しないでください。

    basePath

    /2.0

    デフォルト値。変更しないでください。

    protocol (プロトコル)

    HTTPS

    デフォルト値。変更しないでください。

    localCallbackPath

    /callback

    デフォルト値。変更しないでください。

    localCallbackConfig

    HTTP_Listener_config

    以前作成した HTTP リスナー設定を選択します。

    externalCallbackUrl

    http://localhost:8081/callback

    クライアントアプリケーション内で設定されるリダイレクト URL を使用します。

    localAuthorizationUrl

    http://localhost:8081/authorize

    OAUTH2​ ダンスを開始するには、Mule アプリケーションをデプロイした後、この URL を入力します。

    authorizationUrl

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

    認証コードを要求するための認証 URL。

    accessTokenUrl

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

    アクセストークンを要求するためのアクセストークン URL。

    対応する XML 設定は次のようになります。

    <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. Box Connector のプロパティエディターで、​Create folder​ 操作に必要な次のパラメーターを設定します。

    パラメーター

    Display Name (表示名)

    Create folder

    Basic Settings (基本設定)

    Connector configuration (コネクタ設定)

    作成したグローバル Box Connector 要素を選択します。

    General (一般)

    Create folder request data (Create folder 要求データ)

    #[payload]

  11. 新しく作成したフォルダー ID を記録する ​[Logger]​ コンポーネントをドラッグし、次のプロパティを設定します。

    パラメーター

    Display Name (表示名)

    作成したフォルダー ID を記録するために使用するロガーを指定します。

    Generic

    Message (メッセージ)

    ID ​#[payload.id]​ で作成されたフォルダーを指定します。

  12. Box Connector の ​[Get folder]​ 操作を ​[Logger]​ コンポーネントの横にドラッグします。これにより、フォルダー情報が取得され、次のプロパティが設定されます。

    パラメーター

    Display Name (表示名)

    Get folder (フォルダーの取得)

    Basic Settings (基本設定)

    Connector configuration (コネクタ設定)

    作成したグローバル Box Connector 要素を選択します。

    General (一般)

    Folder id (フォルダー ID)

    #[payload.id]

  13. 取得したフォルダー情報を記録する ​[Logger]​ コンポーネントをドラッグし、次のプロパティを設定します。

    パラメーター

    Display Name (表示名)

    取得したフォルダー情報を記録するロガーを指定します。

    Generic

    Message (メッセージ)

    #[payload]

  14. 作成したフォルダーを削除する Box Connector の ​[Delete folder]​ 操作をドラッグし、次のプロパティを設定します。

    パラメーター

    Display Name (表示名)

    Delete folder (フォルダーの削除)

    Basic Settings (基本設定)

    Connector configuration (コネクタ設定)

    作成したグローバル Box Connector 要素を選択します。

    General (一般)

    Folder id (フォルダー ID)

    #[payload.id]

  15. ユーザーにフローの結果を表示するためのペイロードを設定する ​[DataWeave]​ コンポーネントをドラッグし、次のスクリプトを使用します。

    %dw 2.0
    output application/json
    ---
    {
    	result : "Folder was created, retrieved and deleted successfully."
    }
  16. プロジェクトを保存し、Mule アプリケーションとして実行します。

  17. OAUTH2​ ダンスを開始するためのグローバル Box 設定要素の ​localAuthorizationUrl​ に設定した URL ​http://localhost:8081/authorize​ をブラウザーから使用します。

    これにより表示されるページで、Box ユーザーは、Box に保存されたすべてのファイルとフォルダーの読み取りおよび書き込みアクセス権をクライアントアプリケーションに付与するか却下するかを求められます。

  18. [Grant access to Box (Box へのアクセス権を付与)]​ をクリックして、読み取りおよび書き込み権限を付与します。

  19. Web ブラウザーを開いて、​http://localhost:8081/boxdemo​ にアクセスします。

    次のコンテンツの JSON 応答を取得します。

    結果: "Folder created, retrieved and deleted successfully."

XML の例: Create Folder、Retrieve Folder、Delete Folder 操作についての説明

<?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>
		<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]" />

		<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]" />

		<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>

例: ファイルのアップロード

この例では、作成した他の Box 操作で使用するファイルをアップロードする方法について説明します。

  1. Anypoint Studio で新しい Mule プロジェクトを作成します。

  2. src/main/resources directory​ に ​input​ というフォルダーを作成します。

  3. input directory​ にアップロードするファイルをコピーします。

  4. 新しい HTTP グローバル要素を追加して、HTTP リスナーを設定します。

  5. キャンバスの下部にある ​[Global Elements (グローバル要素)]​ タブをクリックします。

  6. [Global Configuration Elements (グローバル設定要素)]​ 画面で、​[Create (作成)]​ をクリックします。

  7. [Choose Global Type (グローバル種別の選択)]​ ウィザードで、[Connector Configuration (コネクタ設定)] を展開します。

  8. [HTTP Listener (HTTP リスナー)]​ を選択して ​[OK]​ をクリックします。

  9. [HTTP Listener (HTTP リスナー)]​ 画面で、次のパラメーターを設定します。

    パラメーター

    Name (名前)

    HTTP_Listener_config

    Protocol (プロトコル)

    HTTP

    Host (ホスト)

    localhost

    Port (ポート)

    8081

  10. [HTTP Listener (HTTP リスナー)] をキャンバスにドラッグし、次のパラメーターを設定します。

    パラメーター

    Display Name (表示名)

    HTTP Listener (HTTP リスナー)

    Connector configuration (コネクタ設定)

    以前作成した HTTP リスナー設定を選択します。

    Path (パス)

    /boxUploadFile

  11. Box にログインし、60 分間アクティブな Box 開発者トークンを生成します。

  12. DataWeave コンポーネントを使用して、開発者トークンを設定します。

  13. DataWeave コンポーネントを HTTP リスナーの横にドラッグし、次のように設定します。

    %dw 2.0
    output application/java
    		//Set the generated developer token here
    var developerToken = "{developer Token}"
    ---
    "Bearer " ++ developerToken
  14. [Set Variable]​ コンポーネントをキャンバスにドラッグし、次のパラメーターを設定します。

    パラメーター

    Display Name (表示名)

    Save developer token (開発者トークンの保存)

    Name (名前)

    developerToken

    Value (値)

    #[payload]

  15. 新しい​ファイルグローバル要素​を追加して、File Connector を設定します。

  16. キャンバスの下部にある ​[Global Elements (グローバル要素)]​ タブをクリックします。

  17. [Global Configuration Elements (グローバル設定要素)]​ 画面で、​[Create (作成)]​ をクリックします。

  18. [Choose Global Type (グローバル種別の選択)]​ ウィザードで、[Connector Configuration (コネクタ設定)] を展開します。

  19. [File Config (ファイル設定)]​ を選択して ​[OK]​ をクリックします。

  20. [File Config (ファイル設定)] 画面で、次のパラメーターを設定します。

    パラメーター

    Working Directory (作業ディレクトリ)

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

  21. [Read File]​ 操作をキャンバスにドラッグし、次のパラメーターを設定します。

    パラメーター

    Display Name (表示名)

    Read File (ファイルの読み取り)

    Connector configuration (コネクタ設定)

    グローバル要素で以前作成したファイル設定を選択します。

    File Path (ファイルパス)

    input/yourFileName

  22. このペイロードを ​multipart/form-data​ 型の要求に変換します。

  23. 親フォルダーの ID を設定し、アップロードするフォルダーを指定します。

  24. [DataWeave] コンポーネントを ​[Read File]​ コンポーネントの横にドラッグし、次のスクリプトを使用します。

    %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. 新しい HTTP 要求グローバル要素を追加して、HTTP 要求を設定します。

  26. キャンバスの下部にある ​[Global Elements (グローバル要素)]​ タブをクリックします。

  27. [Global Configuration Elements (グローバル設定要素)]​ 画面で、​[Create (作成)]​ をクリックします。

  28. [Choose Global Type (グローバル種別の選択)]​ ウィザードで、[Connector Configuration (コネクタ設定)] を展開し、​[HTTP Request Configuration (HTTP 要求設定)]​ を選択して ​[OK]​ をクリックします。

  29. [HTTP Request Configuration (HTTP 要求設定)] 画面で、次のパラメーターを設定します。

    パラメーター

    Name (名前)

    HTTP_R​equest_configuration

    Base Path (ベースパス)

    /

    Protocol (プロトコル)

    HTTP (デフォルト)

    Host (ホスト)

    80

    Port (ポート)

    Use persistent connections (永続接続を使用)

    Max Connections (最大接続数)

    -1

    Connection idle timeout (接続アイドルタイムアウト)

    30000

    Response buffer size (応答バッファサイズ)

    1024

    TLS configuration (TLS 設定)

    なし

    Proxy (プロキシ)

    なし

    Authentication (認証)

    なし

    Client socket properties (クライアントソケットのプロパティ)

    なし

    Use reconnection (再接続を使用)

    unchecked (未チェック)

  30. [HTTP Request (HTTP 要求)]​ をキャンバスにドラッグし、次のパラメーターを設定します。

    パラメーター

    Display Name (表示名)

    Request - Box Upload File (要求 - Box アップロードファイル)

    Configuration (設定)

    グローバル要素で作成した HTTP 要求設定を選択します。

    Method (メソッド)

    POST

    Path (パス)

    URL

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

    Body (本文)

    payload (ペイロード)

  31. HTTP 要求の認証ヘッダーを作成します。

  32. HTTP 要求の [Body (本文)] の横にある ​[Headers (ヘッダー)]​ タブをクリックし、​[+]​ をクリックして次のパラメーターを設定します。

    名前

    "Authorization (認証)"

    vars.developerToken

  33. ユーザーにフローの結果を表示するためのペイロードを設定する DataWeave コンポーネントをドラッグし、次のように設定します。

    %dw 2.0
    output application/json
    ---
    {
    	result : "File has been successfully created"
    }
  34. プロジェクトを保存し、Mule アプリケーションとして実行します。

  35. ブラウザーに URL ​http://localhost:8081/boxUploadFile​ を入力します。次のコンテンツの JSON 応答を取得します。

    result : "File has been successfully created"

XML の例: ファイルのアップロード

ファイルをアップロードする XML の例を次に示します。

<?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>