Try using JobScheduler's REST-API --Java RestClient Test class implementation--
Save it with the source file name /tmp/kaeru/RestTest.java.
https://qiita.com/yatabekaeru/items/1eda657e5a24189fbcdf
https://qiita.com/yatabekaeru/items/b8f03cccfb8b904efe99
/tmp/kaeru/RestTest.java
package kaeru;
/**
*Client class for making REST requests (for testing)
*
* @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) {
//Character string to be set in the request body
String bodyStr = "";
//Rest-Access Token issued by API
String autKey = "";
//Jason Mapper
ObjectMapper mapper = null;
//For Jason Perth
JsonNode root = null;
//Instance client for basic authentication
RestClient client = new RestClient( "root", "root" );
//Rest-First perform Basic authentication to issue API Access Token
String uri = "http://localhost:4446/joc/api/security/login"; // specify URI
//Post request execution
String data = client.post( uri, bodyStr, MediaType.APPLICATION_JSON_TYPE, null );
try {
//JsonParser generation
mapper = new ObjectMapper();
root = mapper.readTree(data);
//Get an Access Token
autKey = root.get( "accessToken" ).asText();
System.out.println("*****"+autKey);
} catch (JsonParseException e) {
e.printStackTrace();
} catch ( Exception e){
e.printStackTrace();
}
//Set the issued Access Token and Rest-Execute a query with API
//Get JobScheduler order list information
uri = "http://localhost:4446/joc/api/orders/"; // specify URI
//Generate Request body
StringBuilder bodyStrBuffer = new StringBuilder();
bodyStrBuffer.append(" { \"jobschedulerId\": \"scheduler\" }" );
bodyStr = bodyStrBuffer.toString();
//Instance a version of the client that embeds an Access Token in HeadeField
client = new RestClient( null, null );
//Execute a POST request
data = client.post( uri, bodyStr, MediaType.APPLICATION_JSON_TYPE, autKey );
System.out.println( data );
//Easy parsing of Json format data with hierarchical structure
try {
//JsonParser generation
mapper = new ObjectMapper();
root = mapper.readTree( data );
//Variable to store return data in Json format
String job = "";
String path = "";
String orderId = "";
String pState = "";
String nextStartTime = "";
//Hierarchically parse the orders node
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();
}
//Result output
System.out.println( "[orderId]: " +orderId+ " [job]: " +job+ " [path]: "
+path+ " [status]: "+pState+ " [nextStart]: "+nextStartTime );
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch ( Exception e){
e.printStackTrace();
}
}
}
Recommended Posts