use of javax.json.JsonArrayBuilder in project nifi by apache.
the class SiteToSiteProvenanceReportingTask method onTrigger.
@Override
public void onTrigger(final ReportingContext context) {
final boolean isClustered = context.isClustered();
final String nodeId = context.getClusterNodeIdentifier();
if (nodeId == null && isClustered) {
getLogger().debug("This instance of NiFi is configured for clustering, but the Cluster Node Identifier is not yet available. " + "Will wait for Node Identifier to be established.");
return;
}
final ProcessGroupStatus procGroupStatus = context.getEventAccess().getControllerStatus();
final String rootGroupName = procGroupStatus == null ? null : procGroupStatus.getName();
final String nifiUrl = context.getProperty(INSTANCE_URL).evaluateAttributeExpressions().getValue();
URL url;
try {
url = new URL(nifiUrl);
} catch (final MalformedURLException e1) {
// already validated
throw new AssertionError();
}
final String hostname = url.getHost();
final String platform = context.getProperty(PLATFORM).evaluateAttributeExpressions().getValue();
final Map<String, ?> config = Collections.emptyMap();
final JsonBuilderFactory factory = Json.createBuilderFactory(config);
final JsonObjectBuilder builder = factory.createObjectBuilder();
final DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
df.setTimeZone(TimeZone.getTimeZone("Z"));
consumer.consumeEvents(context, (mapHolder, events) -> {
final long start = System.nanoTime();
// Create a JSON array of all the events in the current batch
final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
for (final ProvenanceEventRecord event : events) {
final String componentName = mapHolder.getComponentName(event.getComponentId());
final String processGroupId = mapHolder.getProcessGroupId(event.getComponentId(), event.getComponentType());
final String processGroupName = mapHolder.getComponentName(processGroupId);
arrayBuilder.add(serialize(factory, builder, event, df, componentName, processGroupId, processGroupName, hostname, url, rootGroupName, platform, nodeId));
}
final JsonArray jsonArray = arrayBuilder.build();
// Send the JSON document for the current batch
try {
final Transaction transaction = getClient().createTransaction(TransferDirection.SEND);
if (transaction == null) {
// Throw an exception to avoid provenance event id will not proceed so that those can be consumed again.
throw new ProcessException("All destination nodes are penalized; will attempt to send data later");
}
final Map<String, String> attributes = new HashMap<>();
final String transactionId = UUID.randomUUID().toString();
attributes.put("reporting.task.transaction.id", transactionId);
attributes.put("reporting.task.name", getName());
attributes.put("reporting.task.uuid", getIdentifier());
attributes.put("reporting.task.type", this.getClass().getSimpleName());
attributes.put("mime.type", "application/json");
final byte[] data = jsonArray.toString().getBytes(StandardCharsets.UTF_8);
transaction.send(data, attributes);
transaction.confirm();
transaction.complete();
final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
getLogger().info("Successfully sent {} Provenance Events to destination in {} ms; Transaction ID = {}; First Event ID = {}", new Object[] { events.size(), transferMillis, transactionId, events.get(0).getEventId() });
} catch (final IOException e) {
throw new ProcessException("Failed to send Provenance Events to destination due to IOException:" + e.getMessage(), e);
}
});
}
use of javax.json.JsonArrayBuilder in project nifi by apache.
the class SiteToSiteStatusReportingTask method onTrigger.
@Override
public void onTrigger(final ReportingContext context) {
final boolean isClustered = context.isClustered();
final String nodeId = context.getClusterNodeIdentifier();
if (nodeId == null && isClustered) {
getLogger().debug("This instance of NiFi is configured for clustering, but the Cluster Node Identifier is not yet available. " + "Will wait for Node Identifier to be established.");
return;
}
componentTypeFilter = Pattern.compile(context.getProperty(COMPONENT_TYPE_FILTER_REGEX).evaluateAttributeExpressions().getValue());
componentNameFilter = Pattern.compile(context.getProperty(COMPONENT_NAME_FILTER_REGEX).evaluateAttributeExpressions().getValue());
final ProcessGroupStatus procGroupStatus = context.getEventAccess().getControllerStatus();
final String rootGroupName = procGroupStatus == null ? null : procGroupStatus.getName();
final String nifiUrl = context.getProperty(INSTANCE_URL).evaluateAttributeExpressions().getValue();
URL url;
try {
url = new URL(nifiUrl);
} catch (final MalformedURLException e1) {
// already validated
throw new AssertionError();
}
final String hostname = url.getHost();
final String platform = context.getProperty(PLATFORM).evaluateAttributeExpressions().getValue();
final Map<String, ?> config = Collections.emptyMap();
final JsonBuilderFactory factory = Json.createBuilderFactory(config);
final DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
df.setTimeZone(TimeZone.getTimeZone("Z"));
final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
serializeProcessGroupStatus(arrayBuilder, factory, procGroupStatus, df, hostname, rootGroupName, platform, null, new Date());
final JsonArray jsonArray = arrayBuilder.build();
final int batchSize = context.getProperty(BATCH_SIZE).asInteger();
int fromIndex = 0;
int toIndex = Math.min(batchSize, jsonArray.size());
List<JsonValue> jsonBatch = jsonArray.subList(fromIndex, toIndex);
while (!jsonBatch.isEmpty()) {
// Send the JSON document for the current batch
try {
long start = System.nanoTime();
final Transaction transaction = getClient().createTransaction(TransferDirection.SEND);
if (transaction == null) {
getLogger().debug("All destination nodes are penalized; will attempt to send data later");
return;
}
final Map<String, String> attributes = new HashMap<>();
final String transactionId = UUID.randomUUID().toString();
attributes.put("reporting.task.transaction.id", transactionId);
attributes.put("reporting.task.name", getName());
attributes.put("reporting.task.uuid", getIdentifier());
attributes.put("reporting.task.type", this.getClass().getSimpleName());
attributes.put("mime.type", "application/json");
JsonArrayBuilder jsonBatchArrayBuilder = factory.createArrayBuilder();
for (JsonValue jsonValue : jsonBatch) {
jsonBatchArrayBuilder.add(jsonValue);
}
final JsonArray jsonBatchArray = jsonBatchArrayBuilder.build();
final byte[] data = jsonBatchArray.toString().getBytes(StandardCharsets.UTF_8);
transaction.send(data, attributes);
transaction.confirm();
transaction.complete();
final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
getLogger().info("Successfully sent {} Status Records to destination in {} ms; Transaction ID = {}", new Object[] { jsonArray.size(), transferMillis, transactionId });
fromIndex = toIndex;
toIndex = Math.min(fromIndex + batchSize, jsonArray.size());
jsonBatch = jsonArray.subList(fromIndex, toIndex);
} catch (final IOException e) {
throw new ProcessException("Failed to send Status Records to destination due to IOException:" + e.getMessage(), e);
}
}
}
use of javax.json.JsonArrayBuilder in project nifi by apache.
the class PutSlack method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
JsonObjectBuilder builder = Json.createObjectBuilder();
String text = context.getProperty(WEBHOOK_TEXT).evaluateAttributeExpressions(flowFile).getValue();
if (text != null && !text.isEmpty()) {
builder.add("text", text);
} else {
// Slack requires the 'text' attribute
getLogger().error("FlowFile should have non-empty " + WEBHOOK_TEXT.getName());
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_FAILURE);
return;
}
String channel = context.getProperty(CHANNEL).evaluateAttributeExpressions(flowFile).getValue();
if (channel != null && !channel.isEmpty()) {
String error = validateChannel(channel);
if (error == null) {
builder.add("channel", channel);
} else {
getLogger().error("Invalid channel '{}': {}", new Object[] { channel, error });
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_FAILURE);
return;
}
}
String username = context.getProperty(USERNAME).evaluateAttributeExpressions(flowFile).getValue();
if (username != null && !username.isEmpty()) {
builder.add("username", username);
}
String iconUrl = context.getProperty(ICON_URL).evaluateAttributeExpressions(flowFile).getValue();
if (iconUrl != null && !iconUrl.isEmpty()) {
builder.add("icon_url", iconUrl);
}
String iconEmoji = context.getProperty(ICON_EMOJI).evaluateAttributeExpressions(flowFile).getValue();
if (iconEmoji != null && !iconEmoji.isEmpty()) {
builder.add("icon_emoji", iconEmoji);
}
try {
// Get Attachments Array
if (!attachments.isEmpty()) {
JsonArrayBuilder jsonArrayBuiler = Json.createArrayBuilder();
for (PropertyDescriptor attachment : attachments) {
String s = context.getProperty(attachment).evaluateAttributeExpressions(flowFile).getValue();
JsonReader reader = Json.createReader(new StringReader(s));
JsonObject attachmentJson = reader.readObject();
jsonArrayBuiler.add(attachmentJson);
}
builder.add("attachments", jsonArrayBuiler);
}
JsonObject jsonObject = builder.build();
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(stringWriter);
jsonWriter.writeObject(jsonObject);
jsonWriter.close();
URL url = new URL(context.getProperty(WEBHOOK_URL).getValue());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
String payload = "payload=" + URLEncoder.encode(stringWriter.getBuffer().toString(), "UTF-8");
outputStream.writeBytes(payload);
outputStream.close();
int responseCode = conn.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
getLogger().info("Successfully posted message to Slack");
session.transfer(flowFile, REL_SUCCESS);
session.getProvenanceReporter().send(flowFile, context.getProperty(WEBHOOK_URL).getValue());
} else {
getLogger().error("Failed to post message to Slack with response code {}", new Object[] { responseCode });
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_FAILURE);
context.yield();
}
} catch (JsonParsingException e) {
getLogger().error("Failed to parse JSON", e);
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_FAILURE);
} catch (IOException e) {
getLogger().error("Failed to open connection", e);
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_FAILURE);
context.yield();
}
}
use of javax.json.JsonArrayBuilder in project felix by apache.
the class JSONUtil method build.
public static JsonStructure build(final Object value) {
if (value instanceof List) {
@SuppressWarnings("unchecked") final List<Object> list = (List<Object>) value;
final JsonArrayBuilder builder = Json.createArrayBuilder();
for (final Object obj : list) {
if (obj instanceof String) {
builder.add(obj.toString());
} else if (obj instanceof Long) {
builder.add((Long) obj);
} else if (obj instanceof Double) {
builder.add((Double) obj);
} else if (obj instanceof Boolean) {
builder.add((Boolean) obj);
} else if (obj instanceof Map) {
builder.add(build(obj));
} else if (obj instanceof List) {
builder.add(build(obj));
}
}
return builder.build();
} else if (value instanceof Map) {
@SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
final JsonObjectBuilder builder = Json.createObjectBuilder();
for (final Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof String) {
builder.add(entry.getKey(), entry.getValue().toString());
} else if (entry.getValue() instanceof Long) {
builder.add(entry.getKey(), (Long) entry.getValue());
} else if (entry.getValue() instanceof Double) {
builder.add(entry.getKey(), (Double) entry.getValue());
} else if (entry.getValue() instanceof Boolean) {
builder.add(entry.getKey(), (Boolean) entry.getValue());
} else if (entry.getValue() instanceof Map) {
builder.add(entry.getKey(), build(entry.getValue()));
} else if (entry.getValue() instanceof List) {
builder.add(entry.getKey(), build(entry.getValue()));
}
}
return builder.build();
}
return null;
}
use of javax.json.JsonArrayBuilder in project dataverse by IQSS.
the class DatasetVersion method getJsonLd.
// TODO: Consider moving this comment into the Exporter code.
// The export subsystem assumes there is only
// one metadata export in a given format per dataset (it uses the current
// released (published) version. This JSON fragment is generated for a
// specific released version - and we can have multiple released versions.
// So something will need to be modified to accommodate this. -- L.A.
public String getJsonLd() {
// We show published datasets only for "datePublished" field below.
if (!this.isPublished()) {
return "";
}
if (jsonLd != null) {
return jsonLd;
}
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("@context", "http://schema.org");
job.add("@type", "Dataset");
job.add("identifier", this.getDataset().getPersistentURL());
job.add("name", this.getTitle());
JsonArrayBuilder authors = Json.createArrayBuilder();
for (DatasetAuthor datasetAuthor : this.getDatasetAuthors()) {
JsonObjectBuilder author = Json.createObjectBuilder();
String name = datasetAuthor.getName().getValue();
DatasetField authorAffiliation = datasetAuthor.getAffiliation();
String affiliation = null;
if (authorAffiliation != null) {
affiliation = datasetAuthor.getAffiliation().getValue();
}
// We are aware of "givenName" and "familyName" but instead of a person it might be an organization such as "Gallup Organization".
// author.add("@type", "Person");
author.add("name", name);
// This logic could be moved into the `if (authorAffiliation != null)` block above.
if (!StringUtil.isEmpty(affiliation)) {
author.add("affiliation", affiliation);
}
authors.add(author);
}
job.add("author", authors);
/**
* We are aware that there is a "datePublished" field but it means "Date
* of first broadcast/publication." This only makes sense for a 1.0
* version.
*/
String datePublished = this.getDataset().getPublicationDateFormattedYYYYMMDD();
if (datePublished != null) {
job.add("datePublished", datePublished);
}
/**
* "dateModified" is more appropriate for a version: "The date on which
* the CreativeWork was most recently modified or when the item's entry
* was modified within a DataFeed."
*/
job.add("dateModified", this.getPublicationDateAsString());
job.add("version", this.getVersionNumber().toString());
job.add("description", this.getDescriptionPlainText());
/**
* "keywords" - contains subject(s), datasetkeyword(s) and topicclassification(s)
* metadata fields for the version. -- L.A.
* (see #2243 for details/discussion/feedback from Google)
*/
JsonArrayBuilder keywords = Json.createArrayBuilder();
for (String subject : this.getDatasetSubjects()) {
keywords.add(subject);
}
for (String topic : this.getTopicClassifications()) {
keywords.add(topic);
}
for (String keyword : this.getKeywords()) {
keywords.add(keyword);
}
job.add("keywords", keywords);
/**
* citation:
* (multiple) publicationCitation values, if present:
*/
List<String> publicationCitations = getPublicationCitationValues();
if (publicationCitations.size() > 0) {
JsonArrayBuilder citation = Json.createArrayBuilder();
for (String pubCitation : publicationCitations) {
// citationEntry.add("@type", "Dataset");
// citationEntry.add("text", pubCitation);
citation.add(pubCitation);
}
job.add("citation", citation);
}
/**
* temporalCoverage:
* (if available)
*/
List<String> timePeriodsCovered = this.getTimePeriodsCovered();
if (timePeriodsCovered.size() > 0) {
JsonArrayBuilder temporalCoverage = Json.createArrayBuilder();
for (String timePeriod : timePeriodsCovered) {
temporalCoverage.add(timePeriod);
}
job.add("temporalCoverage", temporalCoverage);
}
/**
* spatialCoverage (if available)
* TODO
* (punted, for now - see #2243)
*/
/**
* funder (if available)
* TODO
* (punted, for now - see #2243)
*/
job.add("schemaVersion", "https://schema.org/version/3.3");
TermsOfUseAndAccess terms = this.getTermsOfUseAndAccess();
if (terms != null) {
JsonObjectBuilder license = Json.createObjectBuilder().add("@type", "Dataset");
if (TermsOfUseAndAccess.License.CC0.equals(terms.getLicense())) {
license.add("text", "CC0").add("url", "https://creativecommons.org/publicdomain/zero/1.0/");
} else {
String termsOfUse = terms.getTermsOfUse();
// Terms of use can be null if you create the dataset with JSON.
if (termsOfUse != null) {
license.add("text", termsOfUse);
}
}
job.add("license", license);
}
job.add("includedInDataCatalog", Json.createObjectBuilder().add("@type", "DataCatalog").add("name", this.getRootDataverseNameforCitation()).add("url", SystemConfig.getDataverseSiteUrlStatic()));
job.add("provider", Json.createObjectBuilder().add("@type", "Organization").add("name", "Dataverse"));
jsonLd = job.build().toString();
return jsonLd;
}
Aggregations