Page History
...
Authentication expects the authorization header "Authorization" with the base64 encoded user:password
String.
The Json body is create using the object mapper. The object mapper uses the JAVA Class MyAddOrderto create the String representation.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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:44264446/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:44264446/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(); } } } |
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
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; } } |
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
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; } } |
...
Overview
Content Tools