Page History
...
Introduction
tbd
Implementation
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package lh;
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 GetAccessToken2 {
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) {
GetAccessToken2 getAccessToken = new GetAccessToken2();
String accessToken;
try {
accessToken = getAccessToken.login("root", "root");
System.out.println(accessToken);
System.out.println(getAccessToken.addOrder(accessToken, "lh_exercise1"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
Overview
Content Tools