Java Code Examples for javax.json.JsonArray#listIterator()

The following examples show how to use javax.json.JsonArray#listIterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobParameter.java    From FHIR with Apache License 2.0 6 votes vote down vote up
/**
 * converts back from input string to objects
 * 
 * @param input
 * @return
 * @throws IOException
 */
public static List<Input> parseInputsFromString(String input) throws IOException {
    List<Input> inputs = new ArrayList<>();
    Decoder decoder = Base64.getDecoder();
    byte[] bytes = decoder.decode(input);

    try (ByteArrayInputStream in = new ByteArrayInputStream(bytes)) {
        try (JsonReader jsonReader = JSON_READER_FACTORY.createReader(in, StandardCharsets.UTF_8)) {
            JsonArray jsonArray = jsonReader.readArray();
            /*
             * the input is intentionally base64 to capture the JSON Array and maintain integrity as part of a
             * name:value pair when submitted to the Batch framework.
             */
            ListIterator<JsonValue> iter = jsonArray.listIterator();
            while (iter.hasNext()) {
                JsonObject obj = iter.next().asJsonObject();
                inputs.add(new Input(obj.getString("type"), obj.getString("url")));
            }
        }
    }
    return inputs;
}
 
Example 2
Source File: JobExecutionResponse.java    From FHIR with Apache License 2.0 4 votes vote down vote up
public static JobExecutionResponse parse(InputStream in) throws FHIROperationException {
    try (JsonReader jsonReader =
            JSON_READER_FACTORY.createReader(in, StandardCharsets.UTF_8)) {
        JsonObject jsonObject = jsonReader.readObject();
        JobExecutionResponse.Builder builder = JobExecutionResponse.builder();

        if (jsonObject.containsKey("jobName")) {
            String jobName = jsonObject.getString("jobName");
            builder.jobName(jobName);
        }

        if (jsonObject.containsKey("instanceId")) {
            Integer instanceId = jsonObject.getInt("instanceId");
            builder.instanceId(instanceId);
        }

        if (jsonObject.containsKey("appName")) {
            String appName = jsonObject.getString("appName");
            builder.appName(appName);
        }

        if (jsonObject.containsKey("batchStatus")) {
            String batchStatus = jsonObject.getString("batchStatus");
            builder.batchStatus(batchStatus);
        }

        if (jsonObject.containsKey("exitStatus")) {
            String exitStatus = jsonObject.getString("exitStatus");
            builder.exitStatus(exitStatus);
        }

        if (jsonObject.containsKey("jobXMLName")) {
            String jobXMLName = jsonObject.getString("jobXMLName");
            builder.jobXMLName(jobXMLName);
        }

        if (jsonObject.containsKey("instanceName")) {
            String instanceName = jsonObject.getString("instanceName");
            builder.instanceName(instanceName);
        }

        if (jsonObject.containsKey("lastUpdatedTime")) {
            String lastUpdatedTime = jsonObject.getString("lastUpdatedTime");
            builder.lastUpdatedTime(lastUpdatedTime);
        }

        if (jsonObject.containsKey("_links")) {
            JsonArray arr = jsonObject.getJsonArray("_links");
            ListIterator<JsonValue> iter = arr.listIterator();
            while (iter.hasNext()) {
                JsonValue v = iter.next();
                JsonObject vObj = v.asJsonObject();

                if (vObj.containsKey("rel") && vObj.containsKey("href")) {
                    String rel = vObj.getString("rel");
                    String href = vObj.getString("href");
                    builder.link(rel, href);
                }
            }
        }

        if (jsonObject.containsKey("submitter")) {
            String submitter = jsonObject.getString("submitter");
            builder.submitter(submitter);
        }

        if (jsonObject.containsKey("instanceState")) {
            String instanceState = jsonObject.getString("instanceState");
            builder.instanceState(instanceState);
        }

        if (jsonObject.containsKey("jobParameters")) {
            JsonObject obj = jsonObject.getJsonObject("jobParameters");
            JobParameter.Parser.parse(builder, obj);
        }
        return builder.build();
    } catch (Exception e) {
        throw new FHIROperationException("Problem parsing the Bulk Export Job's response from the server", e);
    }
}
 
Example 3
Source File: JobInstanceResponse.java    From FHIR with Apache License 2.0 4 votes vote down vote up
public static JobInstanceResponse parse(InputStream in) throws FHIROperationException {
    try (JsonReader jsonReader = JSON_READER_FACTORY.createReader(in, StandardCharsets.UTF_8)) {
        JsonObject jsonObject = jsonReader.readObject();
        JobInstanceResponse.Builder builder = JobInstanceResponse.builder();

        if (jsonObject.containsKey("jobName")) {
            String jobName = jsonObject.getString("jobName");
            builder.jobName(jobName);
        }

        if (jsonObject.containsKey("instanceId")) {
            Integer instanceId = jsonObject.getInt("instanceId");
            builder.instanceId(instanceId);
        }

        if (jsonObject.containsKey("appName")) {
            String appName = jsonObject.getString("appName");
            builder.appName(appName);
        }

        if (jsonObject.containsKey("batchStatus")) {
            String batchStatus = jsonObject.getString("batchStatus");
            builder.batchStatus(batchStatus);
        }

        if (jsonObject.containsKey("jobXMLName")) {
            String jobXMLName = jsonObject.getString("jobXMLName");
            builder.jobXMLName(jobXMLName);
        }

        if (jsonObject.containsKey("instanceName")) {
            String instanceName = jsonObject.getString("instanceName");
            builder.instanceName(instanceName);
        }

        if (jsonObject.containsKey("instanceState")) {
            String instanceState = jsonObject.getString("instanceState");
            builder.instanceState(instanceState);
        }

        if (jsonObject.containsKey("submitter")) {
            String submitter = jsonObject.getString("submitter");
            builder.submitter(submitter);
        }

        if (jsonObject.containsKey("lastUpdatedTime")) {
            String lastUpdatedTime = jsonObject.getString("lastUpdatedTime");
            builder.lastUpdatedTime(lastUpdatedTime);
        }

        int jobExecutionId = 0;
        if (jsonObject.containsKey("_links")) {
            JsonArray arr = jsonObject.getJsonArray("_links");
            ListIterator<JsonValue> iter = arr.listIterator();
            while (iter.hasNext()) {
                JsonValue v = iter.next();
                JsonObject vObj = v.asJsonObject();

                if (vObj.containsKey("rel") && vObj.containsKey("href")) {
                    String rel = vObj.getString("rel");
                    String href = vObj.getString("href");
                    builder.link(rel, href);
                    if (rel.equalsIgnoreCase("job execution")) {
                        // e.g, https://localhost:9443/ibm/api/batch/jobinstances/9/jobexecutions/2
                        // Get the job execution id of the job instance at the end of the url, because the same job instance can be
                        // started, stopped and then restarted multipe times, so need to find the last job execution id and use it
                        // as the current job execution id.
                        int tmpJobExecutionId =
                                Integer.parseInt(href.substring(href.indexOf("jobexecutions") + 14));
                        jobExecutionId =
                                jobExecutionId < tmpJobExecutionId ? tmpJobExecutionId : jobExecutionId;
                    }
                }
            }
        }
        builder.executionId(jobExecutionId);

        return builder.build();
    } catch (Exception e) {
        throw new FHIROperationException("Problem parsing the Bulk Export Job's response from the server", e);
    }
}