Try using JobScheduler's REST-API --Java RestClient implementation--
Save it with the source file name /tmp/kaeru/RestClient.java.
https://qiita.com/yatabekaeru/items/1eda657e5a24189fbcdf
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