(任意Webサイト): ファイル ダウンロード ((Any Website): Download File)
この工程は、ファイルをダウンロードし、ファイル型データに追加します。指定したURLへGETリクエストが投げられ、そのレスポンスがファイルとして保存されます。
Configs:共通設定
  • 工程名
  • メモ
Configs
  • C1: ダウンロードファイルの URL *#{EL}
  • C2: Basic 認証設定 / OAuth2 設定
  • C3: ダウンロードファイルを追加保存するデータ項目 (ファイル) *
  • C4: 保存する際のファイル名 *#{EL}

Notes

  • (v14.1~)Basic 認証が必要な場合は「HTTP 認証設定」にて設定を追加します

Capture

See also

Script (click to open)
  • 下記のスクリプトを記述した XML ファイルをダウンロードできます
    • any-website-file-download.xml (C) Questetra, Inc. (MIT License)
    • Professional をご利用であればファイルの内容を改変することでオリジナルのアドオンとして活用できます

main();
function main() {
  //// == Config Retrieving / 工程コンフィグの参照 ==
  const oauth2 = configs.get("conf_OAuth2");
  
  const fileUrl = configs.get( "conf_DataIdA" );
  const dataIdB = configs.get( "conf_DataIdB" );
  const saveAs  = configs.get( "conf_SaveAs" );

  //// == Data Retrieving / ワークフローデータの参照 == 
  let processFiles = engine.findDataByNumber( dataIdB );
  // java.util.ArrayList <com.questetra.bpms.core.event.scripttask.QfileView>
  if (processFiles === null) {
    processFiles = new java.util.ArrayList();
  }

  //// == Calculating / 演算 ==
  if( saveAs === "" ){
    throw "Downloaded File Name isn't set";
  }
  if ( ! fileUrl.startsWith('http') ) {
    throw "Invalid URL. URL must start with either http or https.";
  }

  const response = accessToTheUrl( oauth2, fileUrl );

  const qfile = saveFile( saveAs, response );

  //// == Data Retrieving / ワークフローデータへの代入 == 
  updateData( processFiles, dataIdB, qfile )
}

/**
  * ダウンロードファイルの URL に GET リクエストを送信し、ファイルを取得する
  * @param {String} oauth2
  * @param {String} fileUrl  ダウンロードファイルの URL
  * @return {HttpResponseWrapper} response  レスポンス
  */
function accessToTheUrl( oauth2, fileUrl ) {
  let response;
  
  try {
    let httpRequest = httpClient.begin()
    if ( oauth2 !== "" && oauth2 !== null ) {
      httpRequest = httpRequest.authSetting(oauth2);
    }
    response = httpRequest.get( fileUrl );
  } catch (e) {
      throw `Unable to access ${fileUrl}.`;
  } 

  const httpStatus  = response.getStatusCode();
  engine.log( `STATUS: ${httpStatus}` );
  if (httpStatus >= 300) {
    engine.log( response.getResponseAsString() );
    throw `Failed to download. STATUS: ${httpStatus}`;
  }

  return response;
}


/**
  * ダウンロードしたファイルを名前を付けて保存する
  * @param {String} saveAs  保存する際のファイル名
  * @param {HttpResponseWrapper} response  レスポンス
  * @return {Qfile} qfile  ファイル
  */
function saveFile( saveAs, response ) {
  const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    saveAs, response.getContentType(), response.getResponse()
  );
  return qfile;
}

/**
  * ダウンロードしたファイルをデータ項目に出力する
  * @param {Array<Qfile>} processFiles  ファイルの配列
  * @param {String} dataIdB  ダウンロードファイルを追加保存するデータ項目のデータ定義番号
  * @param {Qfile} qfile  ファイル
  */
function updateData( processFiles, dataIdB, qfile ) {
  processFiles.add( qfile );
  engine.setDataByNumber( dataIdB, processFiles );
}

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

%d人のブロガーが「いいね」をつけました。