You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 4
Next »
Introduction
It is described how to access the API Restservices
Implementation
To access the JS7 API Restservices an access token is needed. To get a valid access token a login is neccessary.
To make the object mapper available the dependency jackson-core
is neccessary.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0</version>
</dependency>
This class implements the method callJoc with the parameters
- accessToken: A valid access token or null
- myAddOrder: An instance from the class MyAddOrder or null
- additionalHeaders: A map with additional headers or null
The login expects the Authorization header "Authorization" with the base64 encoded user:pwd String.
package js7;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JS7ApiCaller{
private String callJoc(String accessToken, String urlString, MyAddOrder myAddOrder,
Map<String, String> additionalHeaders) throws IOException {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("X-Access-Token", accessToken);
if (additionalHeaders != null) {
for (Entry<String, String> entry : additionalHeaders.entrySet()) {
con.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (myAddOrder != null) {
ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false);
String body = "{}";
try {
body = objectMapper.writeValueAsString(myAddOrder);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
;
if (body != null && !body.isEmpty()) {
con.setDoOutput(true);
try (OutputStream os = con.getOutputStream()) {
byte[] input = body.getBytes("utf-8");
os.write(input, 0, input.length);
}
}
}
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
}
private String getJsonValue(String jsonBody, String jsonKey) {
JsonReader json = Json.createReader(new StringReader(jsonBody));
JsonObject jsonObject = json.readObject();
return jsonObject.getString(jsonKey);
}
public String login(String user, String pwd) throws IOException {
String toBeEncoded = user + ":" + pwd;
String encodedAuth = Base64.getEncoder().encodeToString(toBeEncoded.getBytes());
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Basic " + encodedAuth);
return getJsonValue(this.callJoc("", "http://localhost:4426/joc/api/authentication/login", null, headers),
"accessToken");
}
public String addOrder(String accessToken, String workflow) throws IOException {
MyAddOrder myAddOrder = new MyAddOrder();
myAddOrder.setControllerId("controller");
MyOrderItem myOrderItem = new MyOrderItem();
myOrderItem.setScheduledFor("now");
myOrderItem.setWorkflowPath(workflow);
myAddOrder.getOrders().add(myOrderItem);
return callJoc(accessToken, "http://localhost:4426/joc/api/orders/add", myAddOrder, null);
}
public static void main(String[] args) {
JS7ApiCaller js7ApiCaller = new JS7ApiCaller();
String accessToken;
try {
accessToken = js7ApiCaller.login("root", "root");
System.out.println(accessToken);
System.out.println(js7ApiCaller.addOrder(accessToken, "lh_exercise1"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
package js7;
import java.util.ArrayList;
import java.util.List;
public class MyAddOrder {
String controllerId;
List<MyOrderItem> orders = new ArrayList<MyOrderItem>();
public String getControllerId() {
return controllerId;
}
public void setControllerId(String controllerId) {
this.controllerId = controllerId;
}
public List<MyOrderItem> getOrders() {
return orders;
}
}
package js7;
public class MyOrderItem {
String workflowPath;
String scheduledFor;
public String getWorkflowPath() {
return workflowPath;
}
public void setWorkflowPath(String workflowPath) {
this.workflowPath = workflowPath;
}
public String getScheduledFor() {
return scheduledFor;
}
public void setScheduledFor(String scheduledFor) {
this.scheduledFor = scheduledFor;
}
}