Try using JobScheduler's REST-API --Java RestClient implementation--

Try using JobScheduler's REST-API --Java RestClient implementation--

1. JobScheduler REST-Client body source for API.

Save it with the source file name /tmp/kaeru/RestClient.java.

Please check this article for the detailed procedure.

https://qiita.com/yatabekaeru/items/1eda657e5a24189fbcdf

Please check here for sample code when actually using the Client itself.

https://qiita.com/yatabekaeru/items/06e7d40a12935f107458

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

/**
 *Client class for making REST requests
 *
 */
public class RestClient extends Client {

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

    //Account and password for basic authentication
    public RestClient(String account, String password) {
        this.account  = account;
        this.password = password;
    }

    //Client generation by adding BasicAuthFilter when setting account and password
    private Client getClient() {
        //Client generation
        Client client = new Client();
        //When setting an account and password
        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);

            //Add to header field if AccessToken is set
            if ( hkey != null ){
                builder.header( "access_token", hkey );
            }

            //Request generation
            ClientRequest  request  = builder.build( new URI( uri ), method );

            //Get response
            ClientResponse response = client.handle( request );

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

            default:    // OK,Other than 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 );
    }
}


Recommended Posts

Try using JobScheduler's REST-API --Java RestClient implementation--
Try using JobScheduler's REST-API --Java RestClient Test class-
Try using JobScheduler's REST-API
Try scraping using java [Notes]
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Try using Java framework Nablarch [Web application]
Try using the Stream API in Java
Study Java Try using Scanner or Map
Try using JSON format API in Java
Try using the Wii remote with Java
Try using Firebase Cloud Functions on Android (Java)
Try using libGDX
Try using Maven
Try Java 8 Stream
Try using sql-migrate
Try using Sourcetrail (win version) in Java code
Try using Sourcetrail (macOS version) in Java code
Try similar search of Image Search using Java SDK [Search]
Try accessing the dataset from Java using JZOS
Try using SwiftLint
Try using Log4j 2.0
Try communication using gRPC on Android + Java server
Try using the COTOHA API parsing in Java
Roughly try Java 9
Try implementing the Eratosthenes sieve using the Java standard library
Try image classification using TensorFlow Lite on Android (JAVA)
Try global hooking in Java using the JNativeHook library
Try to build a Java development environment using Docker
Try using Axon Framework
Sorting using java comparator
Interpreter implementation in Java
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
Try using Talend Part 2
Scraping practice using Java ②
Boyer-Moore implementation in Java
[OkHttp] REST-API Java SSL
Heapsort implementation (in java)
Scraping practice using Java ①
Try using Talend Part 1
Try using F # list
Try using each_with_index method
Try Java return value
Database implementation using Realm
Try using Spring JDBC
Implement Thread in Java and try using anonymous class, lambda
[Java] Try editing the elements of the Json string using the library
Try Hello World using plain Java on a Docker container
Try Spark Submit to EMR using AWS SDK for Java