ResponseEntity processInputDataForJsonLines()

in src/main/java/com/amazonaws/sagemaker/controller/ServingController.java [237:285]


    ResponseEntity<String> processInputDataForJsonLines(
            final String jsonLinesAsString, final String acceptVal) throws IOException {

        final String lines[] = jsonLinesAsString.split("\\r?\\n");
        final ObjectMapper mapper = new ObjectMapper();

        // first line is special since it could contain the schema as well. Extract the schema.
        final SageMakerRequestObject firstLine = mapper.readValue(lines[0], SageMakerRequestObject.class);
        final DataSchema schema = this.retrieveAndVerifySchema(firstLine.getSchema(), mapper);

        List<List<Object>> inputDatas = Lists.newArrayList();

        for(String jsonStringLine : lines) {
            try {

                final SageMakerRequestListObject sro = mapper.readValue(jsonStringLine, SageMakerRequestListObject.class);

                for(int idx = 0; idx < sro.getData().size(); ++idx) {
                    inputDatas.add(sro.getData().get(idx));
                }

            } catch (final JsonMappingException ex) {

                final SageMakerRequestObject sro = mapper.readValue(jsonStringLine, SageMakerRequestObject.class);
                inputDatas.add(sro.getData());
            }
        }

        List<ResponseEntity<String>> responseList = Lists.newArrayList();

        // Process each input separately and add response to a list
        for (int idx = 0; idx < inputDatas.size(); ++idx) {
            responseList.add(this.processInputData(inputDatas.get(idx), schema, acceptVal));
        }

        // Merge response body to a new output response
        List<List<String>> bodyList = Lists.newArrayList();

        // All response should be valid if no exception got catch
        // which all headers should be the same and extract the first one to construct responseEntity
        HttpHeaders headers = responseList.get(0).getHeaders();

        //combine body in responseList
        for (ResponseEntity<String> response: responseList) {
            bodyList.add(Lists.newArrayList(response.getBody()));
        }

        return ResponseEntity.ok().headers(headers).body(bodyList.toString());
    }