Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleAuthenticating and adding orders to a workflow
linenumberstrue
collapsetrue
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:4446/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:4446/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, "lhmy_exercise1workflow"));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

...