Example usage for com.fasterxml.jackson.core JsonGenerator close

List of usage examples for com.fasterxml.jackson.core JsonGenerator close

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator close.

Prototype

@Override
public abstract void close() throws IOException;

Source Link

Document

Method called to close this generator, so that no more content can be written.

Usage

From source file:com.cedarsoft.serialization.test.performance.JacksonTest.java

@Test
public void testIt() throws IOException {
    FileType fileType = new FileType("Canon Raw", new Extension(".", "cr2", true), false);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);

    generator.writeStartObject();//w  ww.j  a  v a2 s.co m
    generator.writeStringField("id", fileType.getId());
    generator.writeBooleanField("dependent", fileType.isDependent());

    generator.writeFieldName("extension");
    generator.writeStartObject();

    generator.writeStringField("extension", fileType.getExtension().getExtension());
    generator.writeBooleanField("default", fileType.getExtension().isDefault());
    generator.writeStringField("delimiter", fileType.getExtension().getDelimiter());

    generator.writeEndObject();

    //    generator.writeFieldName( "id" );
    //    generator.writeString( fileType.getId() );

    generator.writeEndObject();

    generator.close();
    assertEquals(JSON, out.toString());
}

From source file:org.apache.nifi.processors.elasticsearch.PutElasticsearchHttpRecord.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

    FlowFile flowFile = session.get();//from   w  w w  . java  2s.com
    if (flowFile == null) {
        return;
    }

    final RecordReaderFactory readerFactory = context.getProperty(RECORD_READER)
            .asControllerService(RecordReaderFactory.class);

    // Authentication
    final String username = context.getProperty(USERNAME).evaluateAttributeExpressions(flowFile).getValue();
    final String password = context.getProperty(PASSWORD).evaluateAttributeExpressions(flowFile).getValue();

    OkHttpClient okHttpClient = getClient();
    final ComponentLog logger = getLogger();

    final String baseUrl = trimToEmpty(context.getProperty(ES_URL).evaluateAttributeExpressions().getValue());
    HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder().addPathSegment("_bulk");

    // Find the user-added properties and set them as query parameters on the URL
    for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
        PropertyDescriptor pd = property.getKey();
        if (pd.isDynamic()) {
            if (property.getValue() != null) {
                urlBuilder = urlBuilder.addQueryParameter(pd.getName(),
                        context.getProperty(pd).evaluateAttributeExpressions().getValue());
            }
        }
    }
    final URL url = urlBuilder.build().url();

    final String index = context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue();
    if (StringUtils.isEmpty(index)) {
        logger.error("No value for index in for {}, transferring to failure", new Object[] { flowFile });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    final String docType = context.getProperty(TYPE).evaluateAttributeExpressions(flowFile).getValue();
    String indexOp = context.getProperty(INDEX_OP).evaluateAttributeExpressions(flowFile).getValue();
    if (StringUtils.isEmpty(indexOp)) {
        logger.error("No Index operation specified for {}, transferring to failure.",
                new Object[] { flowFile });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    switch (indexOp.toLowerCase()) {
    case "index":
    case "update":
    case "upsert":
    case "delete":
        break;
    default:
        logger.error("Index operation {} not supported for {}, transferring to failure.",
                new Object[] { indexOp, flowFile });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    final String id_path = context.getProperty(ID_RECORD_PATH).evaluateAttributeExpressions(flowFile)
            .getValue();
    final RecordPath recordPath = StringUtils.isEmpty(id_path) ? null : recordPathCache.getCompiled(id_path);
    final StringBuilder sb = new StringBuilder();

    try (final InputStream in = session.read(flowFile);
            final RecordReader reader = readerFactory.createRecordReader(flowFile, in, getLogger())) {

        Record record;
        while ((record = reader.nextRecord()) != null) {

            final String id;
            if (recordPath != null) {
                Optional<FieldValue> idPathValue = recordPath.evaluate(record).getSelectedFields().findFirst();
                if (!idPathValue.isPresent() || idPathValue.get().getValue() == null) {
                    throw new IdentifierNotFoundException(
                            "Identifier Record Path specified but no value was found, transferring {} to failure.");
                }
                id = idPathValue.get().getValue().toString();
            } else {
                id = null;
            }

            // The ID must be valid for all operations except "index". For that case,
            // a missing ID indicates one is to be auto-generated by Elasticsearch
            if (id == null && !indexOp.equalsIgnoreCase("index")) {
                throw new IdentifierNotFoundException(
                        "Index operation {} requires a valid identifier value from a flow file attribute, transferring to failure.");
            }

            final StringBuilder json = new StringBuilder();

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            JsonGenerator generator = factory.createJsonGenerator(out);
            writeRecord(record, record.getSchema(), generator);
            generator.flush();
            generator.close();
            json.append(out.toString());

            if (indexOp.equalsIgnoreCase("index")) {
                sb.append("{\"index\": { \"_index\": \"");
                sb.append(index);
                sb.append("\", \"_type\": \"");
                sb.append(docType);
                sb.append("\"");
                if (!StringUtils.isEmpty(id)) {
                    sb.append(", \"_id\": \"");
                    sb.append(id);
                    sb.append("\"");
                }
                sb.append("}}\n");
                sb.append(json);
                sb.append("\n");
            } else if (indexOp.equalsIgnoreCase("upsert") || indexOp.equalsIgnoreCase("update")) {
                sb.append("{\"update\": { \"_index\": \"");
                sb.append(index);
                sb.append("\", \"_type\": \"");
                sb.append(docType);
                sb.append("\", \"_id\": \"");
                sb.append(id);
                sb.append("\" }\n");
                sb.append("{\"doc\": ");
                sb.append(json);
                sb.append(", \"doc_as_upsert\": ");
                sb.append(indexOp.equalsIgnoreCase("upsert"));
                sb.append(" }\n");
            } else if (indexOp.equalsIgnoreCase("delete")) {
                sb.append("{\"delete\": { \"_index\": \"");
                sb.append(index);
                sb.append("\", \"_type\": \"");
                sb.append(docType);
                sb.append("\", \"_id\": \"");
                sb.append(id);
                sb.append("\" }\n");
            }
        }
    } catch (IdentifierNotFoundException infe) {
        logger.error(infe.getMessage(), new Object[] { flowFile });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;

    } catch (final IOException | SchemaNotFoundException | MalformedRecordException e) {
        logger.error("Could not parse incoming data", e);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), sb.toString());
    final Response getResponse;
    try {
        getResponse = sendRequestToElasticsearch(okHttpClient, url, username, password, "PUT", requestBody);
    } catch (final Exception e) {
        logger.error("Routing to {} due to exception: {}", new Object[] { REL_FAILURE.getName(), e }, e);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    final int statusCode = getResponse.code();

    if (isSuccess(statusCode)) {
        ResponseBody responseBody = getResponse.body();
        try {
            final byte[] bodyBytes = responseBody.bytes();

            JsonNode responseJson = parseJsonResponse(new ByteArrayInputStream(bodyBytes));
            boolean errors = responseJson.get("errors").asBoolean(false);
            // ES has no rollback, so if errors occur, log them and route the whole flow file to failure
            if (errors) {
                ArrayNode itemNodeArray = (ArrayNode) responseJson.get("items");
                if (itemNodeArray.size() > 0) {
                    // All items are returned whether they succeeded or failed, so iterate through the item array
                    // at the same time as the flow file list, logging failures accordingly
                    for (int i = itemNodeArray.size() - 1; i >= 0; i--) {
                        JsonNode itemNode = itemNodeArray.get(i);
                        int status = itemNode.findPath("status").asInt();
                        if (!isSuccess(status)) {
                            String reason = itemNode.findPath("//error/reason").asText();
                            logger.error(
                                    "Failed to insert {} into Elasticsearch due to {}, transferring to failure",
                                    new Object[] { flowFile, reason });
                        }
                    }
                }
                session.transfer(flowFile, REL_FAILURE);
            } else {
                session.transfer(flowFile, REL_SUCCESS);
                session.getProvenanceReporter().send(flowFile, url.toString());
            }

        } catch (IOException ioe) {
            // Something went wrong when parsing the response, log the error and route to failure
            logger.error("Error parsing Bulk API response: {}", new Object[] { ioe.getMessage() }, ioe);
            session.transfer(flowFile, REL_FAILURE);
            context.yield();
        }
    } else if (statusCode / 100 == 5) {
        // 5xx -> RETRY, but a server error might last a while, so yield
        logger.warn(
                "Elasticsearch returned code {} with message {}, transferring flow file to retry. This is likely a server problem, yielding...",
                new Object[] { statusCode, getResponse.message() });
        session.transfer(flowFile, REL_RETRY);
        context.yield();
    } else { // 1xx, 3xx, 4xx, etc. -> NO RETRY
        logger.warn("Elasticsearch returned code {} with message {}, transferring flow file to failure",
                new Object[] { statusCode, getResponse.message() });
        session.transfer(flowFile, REL_FAILURE);
    }
    getResponse.close();
}

From source file:com.cedarsoft.serialization.test.performance.Bson4JacksonTest.java

@Test
public void testIt() throws IOException {
    FileType fileType = new FileType("Canon Raw", new Extension(".", "cr2", true), false);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);

    generator.writeStartObject();/*from  w  w w  .  j a v a 2s.c  o  m*/
    generator.writeStringField("id", fileType.getId());
    generator.writeBooleanField("dependent", fileType.isDependent());

    generator.writeFieldName("extension");
    generator.writeStartObject();

    generator.writeStringField("extension", fileType.getExtension().getExtension());
    generator.writeBooleanField("default", fileType.getExtension().isDefault());
    generator.writeStringField("delimiter", fileType.getExtension().getDelimiter());

    generator.writeEndObject();

    //    generator.writeFieldName( "id" );
    //    generator.writeString( fileType.getId() );

    generator.writeEndObject();

    generator.close();
    assertEquals(BSON, Hex.encodeHexString(out.toByteArray()));
}

From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java

public static String dumpConfigurationSectionAsFlatJson(String section) {
    Class en = CONFIGURATION_SECTIONS.get(section);
    try {/*from w  w w .java  2 s  .  com*/
        JsonFactory jfactory = new JsonFactory();
        StringWriter sw = new StringWriter();
        String enumDescription = "";
        JsonGenerator gen = jfactory.createJsonGenerator(sw);
        gen.writeStartArray();
        EnumSet values = EnumSet.allOf(en);
        for (Object v : values) {
            String key = (String) (en.getMethod("getKey")).invoke(v);

            boolean isVisible = (Boolean) (en.getMethod("isVisible")).invoke(v);
            String valueAsString;
            if (isVisible)
                valueAsString = (String) (en.getMethod("getValueAsString")).invoke(v);
            else
                valueAsString = "--HIDDEN--";
            boolean isEditable = (Boolean) (en.getMethod("isEditable")).invoke(v);
            boolean isOverridden = (Boolean) (en.getMethod("isOverridden")).invoke(v);
            String valueDescription = (String) (en.getMethod("getValueDescription")).invoke(v);
            Class type = (Class) en.getMethod("getType").invoke(v);

            gen.writeStartObject(); //               {
            gen.writeStringField("key", key);
            gen.writeStringField("value", valueAsString);
            gen.writeStringField("description", valueDescription); //                  ,"description":"description"
            gen.writeStringField("type", type.getSimpleName()); //                  ,"type":"type"
            gen.writeBooleanField("editable", isEditable);
            gen.writeBooleanField("overridden", isOverridden);
            gen.writeEndObject(); //               }
        }
        if (gen.getOutputContext().inArray())
            gen.writeEndArray(); //            ]
        gen.close();
        return sw.toString();
    } catch (Exception e) {
        BaasBoxLogger.error("Cannot generate a json for " + en.getSimpleName()
                + " Enum. Is it an Enum that implements the IProperties interface?", e);
    }
    return "{}";
}

From source file:org.dswarm.graph.resources.MaintainResource.java

/**
 * note utilise this endpoint with care, because it cleans your complete db!
 *
 * @param database the graph database/*w  w  w .j a  va2s. c om*/
 */
@DELETE
@Path("/delete")
@Produces("application/json")
public Response cleanGraph(@Context final GraphDatabaseService database) throws IOException, DMPGraphException {

    MaintainResource.LOG.debug("start cleaning up the db");

    final long deleted = deleteSomeStatements(database);

    MaintainResource.LOG.debug("finished delete-all-entities TXs");

    MaintainResource.LOG.debug("start legacy indices clean-up");

    // TODO: maybe separate index clean-up + observe index clean-up
    // => maybe we also need to do a label + relationship types clean-up ... => this is not supported right now ...

    deleteSomeLegacyIndices(database);

    MaintainResource.LOG.debug("finished legacy indices clean-up");

    MaintainResource.LOG.debug("start schema indices clean-up");

    deleteSomeSchemaIndices(database);

    MaintainResource.LOG.debug("finished schema indices clean-up");

    MaintainResource.LOG.debug("finished cleaning up the db");

    final StringWriter out = new StringWriter();
    final JsonGenerator generator = simpleObjectMapper.getFactory().createGenerator(out);

    generator.writeStartObject();
    generator.writeNumberField("deleted", deleted);
    generator.writeEndObject();
    generator.flush();
    generator.close();

    final String result = out.toString();

    out.flush();
    out.close();

    return Response.ok(result, MediaType.APPLICATION_JSON_TYPE).build();
}

From source file:com.castlemock.web.mock.rest.converter.swagger.SwaggerRestDefinitionConverter.java

/**
 * The method generates a body based on the provided {@link Response} and a map of {@link Model}.
 * @param response The response which the body will be based on.
 * @param definitions The map of definitions that might be required to generate the response.
 * @return A HTTP response body based on the provided {@link Response}.
 * @since 1.13//from w  w w  .  ja v  a 2s. co  m
 * @see {@link #generateJsonBody(String, Property, Map, JsonGenerator)}
 */
private String generateJsonBody(final Response response, final Map<String, Model> definitions) {
    final StringWriter writer = new StringWriter();
    final Property schema = response.getSchema();
    if (schema == null) {
        return writer.toString();
    }

    final JsonFactory factory = new JsonFactory();
    JsonGenerator generator = null;
    try {
        generator = factory.createGenerator(writer);
        generateJsonBody(null, schema, definitions, generator);
    } catch (IOException e) {
        LOGGER.error("Unable to generate a response body", e);
    } finally {
        if (generator != null) {
            try {
                generator.close();
            } catch (IOException e) {
                LOGGER.error("Unable to close the JsonGenerator", e);
            }
        }
    }

    return writer.toString();
}

From source file:tds.student.web.controls.dummy.GlobalJavascriptWriter.java

public void writeCustomSettings() throws IOException {
    StringWriter sw = new StringWriter();
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jsonWriter = jsonFactory.createGenerator(sw);

    jsonWriter.writeStartObject();/*from   w w w.jav a  2s .  c  om*/

    // TODO Shajib:
    // jsonWriter.writeBooleanField ("showExceptions",
    // DebugSettings.showClientExceptions());
    jsonWriter.writeBooleanField("ignoreForbiddenApps", DebugSettings.ignoreForbiddenApps());
    jsonWriter.writeBooleanField("ignoreBrowserChecks", DebugSettings.ignoreBrowserChecks());
    jsonWriter.writeEndObject();
    jsonWriter.close();
    _writer.write(String.format("TDS.Debug = %s; ", sw.toString()));
    _writer.write("\n\r");
}

From source file:com.sdl.odata.renderer.json.writer.JsonServiceDocumentWriter.java

/**
 * The main method for Writer./*  w ww. j  a  v a2  s.c  o  m*/
 * It builds the service root document according to spec.
 *
 * @return output in json
 * @throws ODataRenderException If unable to render the json service document
 */
public String buildJson() throws ODataRenderException {
    LOG.debug("Start building Json service root document");
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(stream, JsonEncoding.UTF8);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField(CONTEXT, getContextURL(uri, entityDataModel));
        jsonGenerator.writeArrayFieldStart(VALUE);

        List<EntitySet> entities = entityDataModel.getEntityContainer().getEntitySets();
        for (EntitySet entity : entities) {
            if (entity.isIncludedInServiceDocument()) {
                writeObject(jsonGenerator, entity);
            }
        }

        List<Singleton> singletons = entityDataModel.getEntityContainer().getSingletons();
        for (Singleton singleton : singletons) {
            writeObject(jsonGenerator, singleton);
        }

        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
        return stream.toString(StandardCharsets.UTF_8.name());
    } catch (IOException e) {
        throw new ODataRenderException("It is unable to render service document", e);
    }
}