LoginSignup
1
0

More than 5 years have passed since last update.

JobSchedulerのREST-APIを使ってみる - JavaのRestClientのTestクラス -

Last updated at Posted at 2018-01-08

JobSchedulerのREST-APIを使ってみる - JavaのRestClientのTestクラス実装 -

1.JobSchedulerのREST-API向けのTestクラスのソースです。

/tmp/kaeru/RestTest.java というソースファイル名で保存します。

詳細手順はこちらの記事をご確認ください。

本クラスから実際使用するClient本体のサンプルコードはこちらをご確認ください

/tmp/kaeru/RestTest.java

package kaeru;

/**
 * RESTリクエストを行うためのクライアントクラス(テスト用)
 *
 * @author S.Yatabe
 */

import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RestTest {

    public static void main(String[] args) {

        //リクエストボディに設定する文字列
        String bodyStr = "";

        //Rest-APIが払い出すAccessToken
        String autKey = "";

        //Jasonマッパー
        ObjectMapper mapper = null;

        //Jasonパース用
        JsonNode root = null;

        //Basic認証用クライアントをインスタンス
        RestClient client = new RestClient( "root", "root" );

        //Rest-APIのAccessTokenを払い出すためにまずBasic認証を行う
        String uri = "http://localhost:4446/joc/api/security/login"; // specify URI

        //Postリクエスト実行
        String data = client.post( uri, bodyStr, MediaType.APPLICATION_JSON_TYPE, null );

        try {
            //JsonParser生成
            mapper = new ObjectMapper();
            root   = mapper.readTree(data);

            //AccessTokenを取得
            autKey = root.get( "accessToken" ).asText();
            System.out.println("*****"+autKey);

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch ( Exception e){
            e.printStackTrace();
        }


        //払い出されたAccessTokenをセットしてRest-APIで問い合わせを実行する

        //JobSchedulerのオーダー一覧情報を取得
        uri = "http://localhost:4446/joc/api/orders/"; // specify URI

        //Requestボディを生成
        StringBuilder bodyStrBuffer = new StringBuilder();
        bodyStrBuffer.append(" { \"jobschedulerId\": \"scheduler\" }" );
        bodyStr = bodyStrBuffer.toString();

        //HeadeFieldにAccessTokenを埋め込む版のクライアントをインスタンス
        client = new RestClient( null, null );

        //POSTリクエストを実行
        data = client.post( uri, bodyStr, MediaType.APPLICATION_JSON_TYPE, autKey );
        System.out.println( data );

        //階層構造を持つJson形式のデータを簡単パース
        try {

            //JsonParser生成
            mapper = new ObjectMapper();
            root = mapper.readTree( data );

            //Json形式の返却データを格納する変数
            String job     = "";
            String path    = "";
            String orderId = "";
            String pState  = "";
            String nextStartTime = "";

            //orders のノードを階層的にパース
            for ( JsonNode n : root.get("orders") ) {
                    job            = n.get( "job"     ).asText();
                    path           = n.get( "path"    ).asText();
                    orderId        = n.get( "orderId" ).asText();
                    pState         = n.get( "processingState" ).get( "_text" ).asText();
                    nextStartTime  = "";
                    if ( n.get( "nextStartTime" ) != null ){
                        nextStartTime = n.get( "nextStartTime" ).asText();
                    }

                    //結果出力
                    System.out.println( "[orderId]: " +orderId+ " [job]: " +job+ " [path]: "
                     +path+ " [status]: "+pState+ " [nextStart]: "+nextStartTime );

                }

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch ( Exception e){
            e.printStackTrace();
        }
    }
}


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0