LoginSignup
1
0

More than 5 years have passed since last update.

JobSchedulerのREST-APIを使ってみる - JavaのRestClient実装 -

Last updated at Posted at 2018-01-08

JobSchedulerのREST-APIを使ってみる - JavaのRestClient実装 -

1.JobSchedulerのREST-API向けのClient本体のソースです。

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

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

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

/tmp/kaeru/RestClient.java

package kaeru;

import java.net.URI;
import java.net.URISyntaxException;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

/**
 * RESTリクエストを行うためのクライアントクラス
 *
 */
public class RestClient extends Client {

    private String account  = null;
    private String password = null;

    //Basic認証用のアカウントとパスワード
    public RestClient(String account, String password) {
        this.account  = account;
        this.password = password;
    }

    //アカウントとパスワード設定時はBasicAuthFilterを付加してClient生成
    private Client getClient() {
        //Client生成
        Client client = new Client();
        //アカウントとパスワード設定時
        if( this.account != null && this.password != null ){
            client.addFilter( new HTTPBasicAuthFilter( this.account, this.password ) );
        }
        return client;
    }


    /**
     * Send Post Request to RESTful Web Service.
     *
     */
        private <E> String sendRequestQuery( String uri, E entity, String method,
                        MediaType type, String hkey ) {

        Client client = getClient();
        ClientRequest.Builder builder = ClientRequest.create();

        try {

            builder.type(type).entity(entity);

            //AccessTokenが設定されている場合はヘッダフィールドに追加
            if ( hkey != null ){
                builder.header( "access_token", hkey );
            }

            //リクエスト生成
            ClientRequest  request  = builder.build( new URI( uri ), method );

            //レスポンス取得
            ClientResponse response = client.handle( request );

            switch (response.getStatus()) {
            case 200:   // OK
            case 201:   // CREATED
                return response.getEntity( String.class );

            default:    // OK, CREATED 以外
                String error = response.getEntity( String.class );
                throw new RuntimeException( error );
            }
        } catch (URISyntaxException e) {
                e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Send POST method.
     *
     */
    public <E> String post( String uri, E entity, MediaType type, String hkey ) {
        return sendRequestQuery( uri, entity, HttpMethod.POST, type, hkey );
    }
}


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