Example usage for javax.json.stream JsonGenerator flush

List of usage examples for javax.json.stream JsonGenerator flush

Introduction

In this page you can find the example usage for javax.json.stream JsonGenerator flush.

Prototype

@Override
void flush();

Source Link

Document

Flushes the underlying output source.

Usage

From source file:com.mapr.data.sputnik.RandomJsonGenerator.java

public String generateJson() {
    StringWriter w = new StringWriter();
    javax.json.stream.JsonGenerator gen = factory.createGenerator(w);
    generatedValues = new LinkedHashMap<>();

    processProperties(gen, config, "");

    gen.flush();
    return w.toString();
}

From source file:de.tu_dortmund.ub.data.dswarm.Init.java

public String call() {

    final String serviceName = config.getProperty(TPUStatics.SERVICE_NAME_IDENTIFIER);
    final String engineDswarmAPI = config.getProperty(TPUStatics.ENGINE_DSWARM_API_IDENTIFIER);
    final Optional<Boolean> optionalEnhanceInputDataResource = TPUUtil
            .getBooleanConfigValue(TPUStatics.ENHANCE_INPUT_DATA_RESOURCE, config);

    LOG.info(String.format("[%s][%d] Starting 'Init (Task)' ...", serviceName, cnt));

    try {// w  w  w  . j  a v  a2 s.co  m

        final boolean doIngest;

        final String doIngestString = config.getProperty(TPUStatics.DO_INITIAL_DATA_MODEL_INGEST_IDENTIFIER);

        if (doIngestString != null && !doIngestString.trim().isEmpty()) {

            doIngest = Boolean.valueOf(doIngestString);
        } else {

            // default = true
            doIngest = true;
        }

        if (doIngest) {

            LOG.debug("[{}][{}] do data model creation with data ingest", serviceName, cnt);

            TPUUtil.initSchemaIndices(serviceName, config);
        }

        final String configurationFileName = config.getProperty(TPUStatics.CONFIGURATION_NAME_IDENTIFIER);
        final String configurationJSONString = readFile(configurationFileName, Charsets.UTF_8);
        final JsonObject configurationJSON = TPUUtil.getJsonObject(configurationJSONString);

        final String finalInputResourceFile;

        if (optionalEnhanceInputDataResource.isPresent()
                && Boolean.TRUE.equals(optionalEnhanceInputDataResource.get())) {

            final Optional<String> optionalUpdatedInputResourceFile = enhanceInputDataResource(initResourceFile,
                    configurationJSON);

            if (optionalUpdatedInputResourceFile.isPresent()) {

                finalInputResourceFile = optionalUpdatedInputResourceFile.get();
            } else {

                finalInputResourceFile = initResourceFile;
            }
        } else {

            finalInputResourceFile = initResourceFile;
        }

        final String name = String.format("resource for project '%s'", initResourceFile);
        final String description = String.format("'resource does not belong to a project' - case %d", cnt);
        final String inputResourceJson = uploadFileAndCreateResource(finalInputResourceFile, name, description,
                serviceName, engineDswarmAPI);

        if (inputResourceJson == null) {

            final String message = "something went wrong at resource creation";

            LOG.error(message);

            throw new RuntimeException(message);
        }

        final JsonObject inputResourceJSON = TPUUtil.getJsonObject(inputResourceJson);
        final String inputResourceID = inputResourceJSON.getString(DswarmBackendStatics.UUID_IDENTIFIER);
        LOG.info(String.format("[%s][%d] input resource id = %s", serviceName, cnt, inputResourceID));

        if (inputResourceID == null) {

            final String message = "something went wrong at resource creation, no resource uuid available";

            LOG.error(message);

            throw new RuntimeException(message);
        }

        // TODO: refactor this, so that a configuration only needs to be create once per TPU task
        // create configuration
        final String finalConfigurationJSONString = createConfiguration(configurationJSONString, serviceName,
                engineDswarmAPI);

        if (finalConfigurationJSONString == null) {

            final String message = "something went wrong at configuration creation";

            LOG.error(message);

            throw new RuntimeException(message);
        }

        final JsonObject finalConfigurationJSON = TPUUtil.getJsonObject(finalConfigurationJSONString);
        final String configurationID = finalConfigurationJSON.getString(DswarmBackendStatics.UUID_IDENTIFIER);
        LOG.info(String.format("[%s][%d] configuration id = %s", serviceName, cnt, configurationID));

        if (configurationID == null) {

            final String message = "something went wrong at configuration creation, no configuration uuid available";

            LOG.error(message);

            throw new RuntimeException(message);
        }

        // check for existing input schema
        final Optional<JsonObject> optionalInputSchema = getInputSchema(serviceName, engineDswarmAPI);

        // create the datamodel (will use it's resource)
        final String dataModelName = String.format("data model %d", cnt);
        final String dataModelDescription = String.format("data model description %d", cnt);
        final String dataModelJSONString = createDataModel(inputResourceJSON, finalConfigurationJSON,
                optionalInputSchema, dataModelName, dataModelDescription, serviceName, engineDswarmAPI,
                doIngest);

        if (dataModelJSONString == null) {

            final String message = "something went wrong at data model creation";

            LOG.error(message);

            throw new RuntimeException(message);
        }

        final JsonObject dataModelJSON = TPUUtil.getJsonObject(dataModelJSONString);
        final String dataModelID = dataModelJSON.getString(DswarmBackendStatics.UUID_IDENTIFIER);
        LOG.info(String.format("[%s][%d] data model id = %s", serviceName, cnt, dataModelID));

        if (dataModelID == null) {

            final String message = "something went wrong at data model creation, no data model uuid available";

            LOG.error(message);

            throw new RuntimeException(message);
        }

        // we don't need to transform after each ingest of a slice of records,
        // so transform and export will be done separately
        LOG.info(String.format("[%s][%d] (Note: Only ingest, but no transformation or export done.)",
                serviceName, cnt));

        final StringWriter stringWriter = new StringWriter();
        final JsonGenerator jp = Json.createGenerator(stringWriter);

        jp.writeStartObject();
        jp.write(DATA_MODEL_ID, dataModelID);
        jp.write(RESOURCE_ID, inputResourceID);
        jp.write(CONFIGURATION_ID, configurationID);
        jp.writeEnd();

        jp.flush();
        jp.close();

        final String result = stringWriter.toString();

        stringWriter.flush();
        stringWriter.close();

        return result;
    } catch (final Exception e) {

        final String message = String.format("[%s][%d] Processing resource '%s' failed with a %s", serviceName,
                cnt, initResourceFile, e.getClass().getSimpleName());

        LOG.error(message, e);

        throw new RuntimeException(message, e);
    }
}

From source file:de.tu_dortmund.ub.data.dswarm.Init.java

/**
 * creates a data model from given resource + configuration JSON (+ optional input schema)
 *
 * @param resourceJSON/* w  w w  .  ja  v a  2 s  .  c o  m*/
 * @param configurationJSON
 * @param optionalInputSchema
 * @param name
 * @param description
 * @return responseJson
 * @throws Exception
 */
private String createDataModel(final JsonObject resourceJSON, final JsonObject configurationJSON,
        final Optional<JsonObject> optionalInputSchema, final String name, final String description,
        final String serviceName, final String engineDswarmAPI, final boolean doIngest) throws Exception {

    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {

        final String uri = engineDswarmAPI + DswarmBackendStatics.DATAMODELS_ENDPOINT + APIStatics.QUESTION_MARK
                + DswarmBackendStatics.DO_DATA_MODEL_INGEST_IDENTIFIER + APIStatics.EQUALS + doIngest;

        final HttpPost httpPost = new HttpPost(uri);

        final StringWriter stringWriter = new StringWriter();
        final JsonGenerator jp = Json.createGenerator(stringWriter);

        jp.writeStartObject();
        jp.write(DswarmBackendStatics.NAME_IDENTIFIER, name);
        jp.write(DswarmBackendStatics.DESCRIPTION_IDENTIFIER, description);
        jp.write(CONFIGURATION_IDENTIFIER, configurationJSON);
        jp.write(DswarmBackendStatics.DATA_RESOURCE_IDENTIFIER, resourceJSON);

        if (optionalInputSchema.isPresent()) {

            LOG.info("[{}][{}] add existing input schema to input data model", serviceName, cnt);

            jp.write(DswarmBackendStatics.SCHEMA_IDENTIFIER, optionalInputSchema.get());
        }

        jp.writeEnd();

        jp.flush();
        jp.close();

        final StringEntity reqEntity = new StringEntity(stringWriter.toString(),
                ContentType.create(APIStatics.APPLICATION_JSON_MIMETYPE, Consts.UTF_8));

        stringWriter.flush();
        stringWriter.close();

        httpPost.setEntity(reqEntity);

        LOG.info(String.format("[%s][%d] request : %s", serviceName, cnt, httpPost.getRequestLine()));

        try (final CloseableHttpResponse httpResponse = httpclient.execute(httpPost)) {

            final int statusCode = httpResponse.getStatusLine().getStatusCode();

            final String message = String.format("[%s][%d] %d : %s", serviceName, cnt, statusCode,
                    httpResponse.getStatusLine().getReasonPhrase());

            final String response = TPUUtil.getResponseMessage(httpResponse);

            switch (statusCode) {

            case 201: {

                LOG.info(message);

                LOG.debug(String.format("[%s][%d] responseJson : %s", serviceName, cnt, response));

                return response;
            }
            default: {

                LOG.error(message);

                throw new Exception("something went wrong at data model creation: " + message + " " + response);
            }
            }
        }
    }
}

From source file:de.tu_dortmund.ub.data.dswarm.Transform.java

/**
 * configuration and processing of the task
 *
 * @param inputDataModelID/*from  w  w w .  ja v  a2  s . c  o  m*/
 * @param projectIDs
 * @param outputDataModelID
 * @return
 */
private String executeTask(final String inputDataModelID, final Collection<String> projectIDs,
        final String outputDataModelID, final String serviceName, final String engineDswarmAPI,
        final Optional<Boolean> optionalDoIngestOnTheFly, final Optional<Boolean> optionalDoExportOnTheFly,
        final Optional<String> optionalExportMimeType, final Optional<String> optionalExportFileExtension)
        throws Exception {

    final JsonArray mappings = getMappingsFromProjects(projectIDs, serviceName, engineDswarmAPI);
    final JsonObject inputDataModel = getDataModel(inputDataModelID, serviceName, engineDswarmAPI);
    final JsonObject outputDataModel = getDataModel(outputDataModelID, serviceName, engineDswarmAPI);
    final Optional<JsonObject> optionalSkipFilter = getSkipFilter(serviceName, engineDswarmAPI);

    // erzeuge Task-JSON
    final String persistString = config.getProperty(TPUStatics.PERSIST_IN_DMP_IDENTIFIER);

    final boolean persist;

    if (persistString != null && !persistString.trim().isEmpty()) {

        persist = Boolean.valueOf(persistString);
    } else {

        // default is false
        persist = false;
    }

    final StringWriter stringWriter = new StringWriter();
    final JsonGenerator jp = Json.createGenerator(stringWriter);

    jp.writeStartObject();
    jp.write(DswarmBackendStatics.PERSIST_IDENTIFIER, persist);
    // default for now: true, i.e., no content will be returned
    jp.write(DswarmBackendStatics.DO_NOT_RETURN_DATA_IDENTIFIER, true);
    // default for now: true, i.e., if a schema is attached it will utilised (instead of being derived from the data resource)
    jp.write(DswarmBackendStatics.UTILISE_EXISTING_INPUT_IDENTIFIER, true);

    if (optionalDoIngestOnTheFly.isPresent()) {

        final Boolean doIngestOnTheFly = optionalDoIngestOnTheFly.get();

        if (doIngestOnTheFly) {

            LOG.info(String.format("[%s][%d] do ingest on-the-fly", serviceName, cnt));
        }

        jp.write(DswarmBackendStatics.DO_INGEST_ON_THE_FLY, doIngestOnTheFly);
    }

    if (optionalDoExportOnTheFly.isPresent()) {

        final Boolean doExportOnTheFly = optionalDoExportOnTheFly.get();

        if (doExportOnTheFly) {

            LOG.info(String.format("[%s][%d] do export on-the-fly", serviceName, cnt));
        }

        jp.write(DswarmBackendStatics.DO_EXPORT_ON_THE_FLY, doExportOnTheFly);
    }

    jp.write(DswarmBackendStatics.DO_VERSIONING_ON_RESULT_IDENTIFIER, false);

    // task
    jp.writeStartObject(DswarmBackendStatics.TASK_IDENTIFIER);
    jp.write(DswarmBackendStatics.NAME_IDENTIFIER, "Task Batch-Prozess 'CrossRef'");
    jp.write(DswarmBackendStatics.DESCRIPTION_IDENTIFIER,
            "Task Batch-Prozess 'CrossRef' zum InputDataModel 'inputDataModelID '");

    // job
    jp.writeStartObject(DswarmBackendStatics.JOB_IDENTIFIER);
    jp.write(DswarmBackendStatics.UUID_IDENTIFIER, UUID.randomUUID().toString());
    jp.write(DswarmBackendStatics.MAPPINGS_IDENTIFIER, mappings);

    if (optionalSkipFilter.isPresent()) {

        jp.write(DswarmBackendStatics.SKIP_FILTER_IDENTIFIER, optionalSkipFilter.get());
    }

    jp.writeEnd();

    jp.write(DswarmBackendStatics.INPUT_DATA_MODEL_IDENTIFIER, inputDataModel);
    jp.write(DswarmBackendStatics.OUTPUT_DATA_MODEL_IDENTIFIER, outputDataModel);

    // end task
    jp.writeEnd();

    // end request
    jp.writeEnd();

    jp.flush();
    jp.close();

    final String task = stringWriter.toString();
    stringWriter.flush();
    stringWriter.close();

    LOG.debug(String.format("[%s][%d] task : %s", serviceName, cnt, task));

    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {

        // POST /dmp/tasks/
        final HttpPost httpPost = new HttpPost(engineDswarmAPI + DswarmBackendStatics.TASKS_ENDPOINT);
        final StringEntity stringEntity = new StringEntity(task, ContentType.APPLICATION_JSON);
        stringEntity.setChunked(true);

        final String mimetype;

        if (optionalDoExportOnTheFly.isPresent() && optionalDoExportOnTheFly.get()) {

            if (optionalExportMimeType.isPresent()) {

                mimetype = optionalExportMimeType.get();
            } else {

                // default export mime type is XML
                mimetype = APIStatics.APPLICATION_XML_MIMETYPE;
            }
        } else {

            mimetype = APIStatics.APPLICATION_JSON_MIMETYPE;
        }

        httpPost.setHeader(HttpHeaders.ACCEPT, mimetype);
        //httpPost.setHeader(HttpHeaders.TRANSFER_ENCODING, CHUNKED_TRANSFER_ENCODING);

        httpPost.setEntity(stringEntity);

        final Header[] requestHeaders = httpPost.getAllHeaders();

        final String printedRequestHeaders = printHeaders(requestHeaders);

        LOG.info(String.format("[%s][%d] request : %s :: request headers : \n'%s' :: body : '%s'", serviceName,
                cnt, httpPost.getRequestLine(), printedRequestHeaders, stringEntity));

        try (final CloseableHttpResponse httpResponse = httpclient.execute(httpPost)) {

            final Header[] responseHeaders = httpResponse.getAllHeaders();

            final String printedResponseHeaders = printHeaders(responseHeaders);

            LOG.info(String.format("[%s][%d]  response headers : \n'%s'", serviceName, cnt,
                    printedResponseHeaders));

            final int statusCode = httpResponse.getStatusLine().getStatusCode();

            switch (statusCode) {

            case 204: {

                LOG.info(String.format("[%s][%d] %d : %s", serviceName, cnt, statusCode,
                        httpResponse.getStatusLine().getReasonPhrase()));

                EntityUtils.consume(httpResponse.getEntity());

                return "success";
            }
            case 200: {

                if (optionalDoExportOnTheFly.isPresent() && optionalDoExportOnTheFly.get()) {

                    LOG.info(String.format("[%s][%d] %d : %s", serviceName, cnt, statusCode,
                            httpResponse.getStatusLine().getReasonPhrase()));

                    final String exportFileExtension;

                    if (optionalExportFileExtension.isPresent()) {

                        exportFileExtension = optionalExportFileExtension.get();
                    } else {

                        // XML as default file ending
                        exportFileExtension = TaskProcessingUnit.XML_FILE_ENDING;
                    }

                    // write result to file
                    final String fileName = TPUUtil.writeResultToFile(httpResponse, config,
                            outputDataModelID + "-" + inputDataModelID + "-" + cnt, exportFileExtension);

                    return "success - exported XML to '" + fileName + "'";
                }
            }
            default: {

                LOG.error(String.format("[%s][%d] %d : %s", serviceName, cnt, statusCode,
                        httpResponse.getStatusLine().getReasonPhrase()));

                final String response = TPUUtil.getResponseMessage(httpResponse);

                throw new Exception("something went wrong at task execution" + response);
            }
            }
        }
    }
}

From source file:org.dcm4che3.tool.dcm2json.Dcm2Json.java

public void convert(DicomInputStream dis, OutputStream out) throws IOException {
    dis.setIncludeBulkData(includeBulkData);
    if (blkAttrs != null)
        dis.setBulkDataDescriptor(BulkDataDescriptor.valueOf(blkAttrs));
    dis.setBulkDataDirectory(blkDirectory);
    dis.setBulkDataFilePrefix(blkFilePrefix);
    dis.setBulkDataFileSuffix(blkFileSuffix);
    dis.setConcatenateBulkDataFiles(catBlkFiles);
    JsonGenerator jsonGen = createGenerator(out);
    JSONWriter jsonWriter = new JSONWriter(jsonGen);
    dis.setDicomInputHandler(jsonWriter);
    dis.readDataset(-1, -1);//from   www.j a va 2s  . c  o  m
    jsonGen.flush();
}

From source file:org.dcm4che3.tool.qc.QC.java

private static JsonObject toAttributesObject(Attributes targetSeriesAttrs) {
    StringWriter strWriter = new StringWriter();
    JsonGenerator gen = Json.createGenerator(strWriter);
    JSONWriter writer = new JSONWriter(gen);
    writer.write(targetSeriesAttrs);/*  w  w w  .j av a 2  s  .  c  o  m*/
    gen.flush();
    gen.close();
    return Json.createReader(new StringReader(strWriter.toString())).readObject();
}

From source file:org.dcm4che3.tool.stowrs.StowRS.java

private StowRSResponse sendMetaDataAndBulkData(Attributes metadata, ExtractedBulkData extractedBulkData)
        throws IOException {
    Attributes responseAttrs = new Attributes();

    URL newUrl;/*from  ww  w. ja  v a  2  s  . com*/
    try {
        newUrl = new URL(URL);
    } catch (MalformedURLException e2) {
        throw new RuntimeException(e2);
    }

    HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection();
    connection.setChunkedStreamingMode(2048);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");

    String metaDataType = mediaType == StowMetaDataType.XML ? "application/dicom+xml" : "application/json";
    connection.setRequestProperty("Content-Type",
            "multipart/related; type=" + metaDataType + "; boundary=" + MULTIPART_BOUNDARY);
    String bulkDataTransferSyntax = "transfer-syntax=" + transferSyntax;

    MediaType pixelDataMediaType = getBulkDataMediaType(metadata);
    connection.setRequestProperty("Accept", "application/dicom+xml");
    connection.setRequestProperty("charset", "utf-8");
    connection.setUseCaches(false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());

    // write metadata
    wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "\r\n");

    if (mediaType == StowMetaDataType.XML)
        wr.writeBytes("Content-Type: application/dicom+xml; " + bulkDataTransferSyntax + " \r\n");
    else
        wr.writeBytes("Content-Type: application/json; " + bulkDataTransferSyntax + " \r\n");
    wr.writeBytes("\r\n");

    coerceAttributes(metadata, keys);

    try {
        if (mediaType == StowMetaDataType.XML)
            SAXTransformer.getSAXWriter(new StreamResult(wr)).write(metadata);
        else {
            JsonGenerator gen = Json.createGenerator(wr);
            JSONWriter writer = new JSONWriter(gen);
            writer.write(metadata);
            gen.flush();
        }
    } catch (TransformerConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }

    // write bulkdata

    for (BulkData chunk : extractedBulkData.otherBulkDataChunks) {
        writeBulkDataPart(MediaType.APPLICATION_OCTET_STREAM_TYPE, wr, chunk.getURIOrUUID(),
                Collections.singletonList(chunk));
    }

    if (!extractedBulkData.pixelDataBulkData.isEmpty()) {
        // pixeldata as a single bulk data part

        if (extractedBulkData.pixelDataBulkData.size() > 1) {
            LOG.info("Combining bulk data of multiple pixel data fragments");
        }

        writeBulkDataPart(pixelDataMediaType, wr, extractedBulkData.pixelDataBulkDataURI,
                extractedBulkData.pixelDataBulkData);
    }

    // end of multipart message
    wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "--\r\n");
    wr.close();
    String response = connection.getResponseMessage();
    int rspCode = connection.getResponseCode();
    LOG.info("response: " + response);
    try {
        responseAttrs = SAXReader.parse(connection.getInputStream());
    } catch (Exception e) {
        LOG.error("Error creating response attributes", e);
    }
    connection.disconnect();

    return new StowRSResponse(rspCode, response, responseAttrs);
}