Search in sources :

Example 1 with Json

use of javax.json.Json in project nifi by apache.

the class SiteToSiteBulletinReportingTask 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;
    }
    if (lastSentBulletinId < 0) {
        Map<String, String> state;
        try {
            state = context.getStateManager().getState(Scope.LOCAL).toMap();
        } catch (IOException e) {
            getLogger().error("Failed to get state at start up due to:" + e.getMessage(), e);
            return;
        }
        if (state.containsKey(LAST_EVENT_ID_KEY)) {
            lastSentBulletinId = Long.parseLong(state.get(LAST_EVENT_ID_KEY));
        }
    }
    final BulletinQuery bulletinQuery = new BulletinQuery.Builder().after(lastSentBulletinId).build();
    final List<Bulletin> bulletins = context.getBulletinRepository().findBulletins(bulletinQuery);
    if (bulletins == null || bulletins.isEmpty()) {
        getLogger().debug("No events to send because no events are stored in the repository.");
        return;
    }
    final OptionalLong opMaxId = bulletins.stream().mapToLong(t -> t.getId()).max();
    final Long currMaxId = opMaxId.isPresent() ? opMaxId.getAsLong() : -1;
    if (currMaxId < lastSentBulletinId) {
        getLogger().warn("Current bulletin max id is {} which is less than what was stored in state as the last queried event, which was {}. " + "This means the bulletins repository restarted its ids. Restarting querying from the beginning.", new Object[] { currMaxId, lastSentBulletinId });
        lastSentBulletinId = -1;
    }
    if (currMaxId == lastSentBulletinId) {
        getLogger().debug("No events to send due to the current max id being equal to the last id that was sent.");
        return;
    }
    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"));
    final long start = System.nanoTime();
    // Create a JSON array of all the events in the current batch
    final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
    for (final Bulletin bulletin : bulletins) {
        if (bulletin.getId() > lastSentBulletinId) {
            arrayBuilder.add(serialize(factory, builder, bulletin, df, 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) {
            getLogger().info("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");
        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 {} Bulletins to destination in {} ms; Transaction ID = {}; First Event ID = {}", new Object[] { bulletins.size(), transferMillis, transactionId, bulletins.get(0).getId() });
    } catch (final IOException e) {
        throw new ProcessException("Failed to send Bulletins to destination due to IOException:" + e.getMessage(), e);
    }
    // Store the id of the last event so we know where we left off
    try {
        context.getStateManager().setState(Collections.singletonMap(LAST_EVENT_ID_KEY, String.valueOf(currMaxId)), Scope.LOCAL);
    } catch (final IOException ioe) {
        getLogger().error("Failed to update state to {} due to {}; this could result in events being re-sent after a restart.", new Object[] { currMaxId, ioe.getMessage() }, ioe);
    }
    lastSentBulletinId = currMaxId;
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) JsonArrayBuilder(javax.json.JsonArrayBuilder) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) JsonBuilderFactory(javax.json.JsonBuilderFactory) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Scope(org.apache.nifi.components.state.Scope) SchedulingStrategy(org.apache.nifi.scheduling.SchedulingStrategy) Map(java.util.Map) Json(javax.json.Json) DateFormat(java.text.DateFormat) Restricted(org.apache.nifi.annotation.behavior.Restricted) JsonObject(javax.json.JsonObject) Restriction(org.apache.nifi.annotation.behavior.Restriction) TransferDirection(org.apache.nifi.remote.TransferDirection) RequiredPermission(org.apache.nifi.components.RequiredPermission) TimeZone(java.util.TimeZone) JsonArray(javax.json.JsonArray) IOException(java.io.IOException) UUID(java.util.UUID) Transaction(org.apache.nifi.remote.Transaction) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Stateful(org.apache.nifi.annotation.behavior.Stateful) List(java.util.List) DefaultSchedule(org.apache.nifi.annotation.configuration.DefaultSchedule) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) JsonObjectBuilder(javax.json.JsonObjectBuilder) HashMap(java.util.HashMap) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) IOException(java.io.IOException) JsonArray(javax.json.JsonArray) ProcessException(org.apache.nifi.processor.exception.ProcessException) Transaction(org.apache.nifi.remote.Transaction) JsonBuilderFactory(javax.json.JsonBuilderFactory) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) OptionalLong(java.util.OptionalLong) OptionalLong(java.util.OptionalLong) JsonObject(javax.json.JsonObject) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Json

use of javax.json.Json in project docker-java-api by amihaiemil.

the class RtContainersTestCase method wellformedUriAndPayloadForCreateJson.

/**
 * Test for {@link RtContainers#create(JsonObject)}: request URI must be
 * well-formed and payload must include the input JSON.
 * @throws Exception If something goes wrong.
 */
@Test
public void wellformedUriAndPayloadForCreateJson() throws Exception {
    final JsonObject json = Json.createObjectBuilder().add("Image", "ubuntu").add("Entrypoint", "script.sh").add("StopSignal", "SIGTERM").build();
    new RtContainers(new AssertRequest(new Response(HttpStatus.SC_CREATED, "{ \"Id\": \"df2419f4\" }"), new Condition("Resource path must be /create", req -> req.getRequestLine().getUri().endsWith("/create")), new Condition("Payload must include the input JSON attributes.", req -> {
        final JsonObject payload = new PayloadOf(req);
        // @checkstyle LineLength (3 lines)
        return payload.getString("Image").equals(json.getString("Image")) && payload.getString("Entrypoint").equals(json.getString("Entrypoint")) && payload.getString("StopSignal").equals(json.getString("StopSignal"));
    })), URI.create("http://localhost/test")).create(json);
}
Also used : Response(com.amihaiemil.docker.mock.Response) Condition(com.amihaiemil.docker.mock.Condition) JsonObject(javax.json.JsonObject) Response(com.amihaiemil.docker.mock.Response) MatcherAssert(org.hamcrest.MatcherAssert) AssertRequest(com.amihaiemil.docker.mock.AssertRequest) Condition(com.amihaiemil.docker.mock.Condition) PayloadOf(com.amihaiemil.docker.mock.PayloadOf) Json(javax.json.Json) HttpStatus(org.apache.http.HttpStatus) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) URI(java.net.URI) AssertRequest(com.amihaiemil.docker.mock.AssertRequest) JsonObject(javax.json.JsonObject) PayloadOf(com.amihaiemil.docker.mock.PayloadOf) Test(org.junit.Test)

Example 3 with Json

use of javax.json.Json in project docker-java-api by amihaiemil.

the class RtContainersTestCase method wellformedUriAndPayloadForCreateNameAndJson.

/**
 * Test for {@link RtContainers#create(String, JsonObject)}: request URI
 * must be  well-formed and payload must include the input JSON.
 * @throws Exception If something goes wrong.
 */
@Test
public void wellformedUriAndPayloadForCreateNameAndJson() throws Exception {
    final JsonObject json = Json.createObjectBuilder().add("Image", "ubuntu").add("Entrypoint", "script.sh").add("StopSignal", "SIGTERM").build();
    new RtContainers(new AssertRequest(new Response(HttpStatus.SC_CREATED, "{ \"Id\": \"df2419f4\" }"), new Condition("Resource path must be /create?name=image_name", // @checkstyle LineLength (1 line)
    req -> req.getRequestLine().getUri().endsWith("/create?name=image_name")), new Condition("Payload must include the input JSON attributes.", req -> {
        final JsonObject payload = new PayloadOf(req);
        // @checkstyle LineLength (3 lines)
        return payload.getString("Image").equals(json.getString("Image")) && payload.getString("Entrypoint").equals(json.getString("Entrypoint")) && payload.getString("StopSignal").equals(json.getString("StopSignal"));
    })), URI.create("http://localhost/test")).create("image_name", json);
}
Also used : Response(com.amihaiemil.docker.mock.Response) Condition(com.amihaiemil.docker.mock.Condition) JsonObject(javax.json.JsonObject) Response(com.amihaiemil.docker.mock.Response) MatcherAssert(org.hamcrest.MatcherAssert) AssertRequest(com.amihaiemil.docker.mock.AssertRequest) Condition(com.amihaiemil.docker.mock.Condition) PayloadOf(com.amihaiemil.docker.mock.PayloadOf) Json(javax.json.Json) HttpStatus(org.apache.http.HttpStatus) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) URI(java.net.URI) AssertRequest(com.amihaiemil.docker.mock.AssertRequest) JsonObject(javax.json.JsonObject) PayloadOf(com.amihaiemil.docker.mock.PayloadOf) Test(org.junit.Test)

Example 4 with Json

use of javax.json.Json in project CoreNLP by stanfordnlp.

the class JSONAnnotationReader method toToken.

public CoreLabel toToken(JsonObject json) {
    CoreLabel token = tokenFactory.makeToken();
    token.setIndex(json.getInt("index"));
    token.setValue(json.getString("word", null));
    token.setWord(json.getString("word", null));
    token.setOriginalText(json.getString("originalText", null));
    token.setLemma(json.getString("lemma", null));
    token.setBeginPosition(json.getInt("characterOffsetBegin"));
    token.setEndPosition(json.getInt("characterOffsetEnd"));
    token.setTag(json.getString("pos", null));
    token.setNER(json.getString("ner", null));
    token.set(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class, json.getString("normalizedNER", null));
    token.set(CoreAnnotations.SpeakerAnnotation.class, json.getString("speaker", null));
    token.set(CoreAnnotations.TrueCaseAnnotation.class, json.getString("truecase", null));
    token.set(CoreAnnotations.TrueCaseTextAnnotation.class, json.getString("truecaseText", null));
    token.set(CoreAnnotations.BeforeAnnotation.class, json.getString("before", null));
    token.set(CoreAnnotations.AfterAnnotation.class, json.getString("after", null));
    token.set(CoreAnnotations.WikipediaEntityAnnotation.class, json.getString("entitylink", null));
    token.set(TimeAnnotations.TimexAnnotation.class, toNullable(json.getJsonObject("timex"), obj -> toTimex(obj)));
    return token;
}
Also used : TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) CoreLabel(edu.stanford.nlp.ling.CoreLabel) java.util(java.util) CorefChain(edu.stanford.nlp.coref.data.CorefChain) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) edu.stanford.nlp.util(edu.stanford.nlp.util) Trees(edu.stanford.nlp.trees.Trees) JsonArray(javax.json.JsonArray) NaturalLogicAnnotations(edu.stanford.nlp.naturalli.NaturalLogicAnnotations) Dictionaries(edu.stanford.nlp.coref.data.Dictionaries) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) TimeAnnotations(edu.stanford.nlp.time.TimeAnnotations) java.io(java.io) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) Json(javax.json.Json) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) CoreLabelTokenFactory(edu.stanford.nlp.process.CoreLabelTokenFactory) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) Timex(edu.stanford.nlp.time.Timex) CoreLabel(edu.stanford.nlp.ling.CoreLabel) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) TimeAnnotations(edu.stanford.nlp.time.TimeAnnotations)

Example 5 with Json

use of javax.json.Json in project dataverse by IQSS.

the class JsonPrinter method json.

public static JsonObjectBuilder json(IpGroup grp) {
    // collect single addresses
    List<String> singles = grp.getRanges().stream().filter(IpAddressRange::isSingleAddress).map(IpAddressRange::getBottom).map(IpAddress::toString).collect(toList());
    // collect "real" ranges
    List<List<String>> ranges = grp.getRanges().stream().filter(rng -> !rng.isSingleAddress()).map(rng -> Arrays.asList(rng.getBottom().toString(), rng.getTop().toString())).collect(toList());
    JsonObjectBuilder bld = jsonObjectBuilder().add("alias", grp.getPersistedGroupAlias()).add("identifier", grp.getIdentifier()).add("id", grp.getId()).add("name", grp.getDisplayName()).add("description", grp.getDescription());
    if (!singles.isEmpty()) {
        bld.add("addresses", asJsonArray(singles));
    }
    if (!ranges.isEmpty()) {
        JsonArrayBuilder rangesBld = Json.createArrayBuilder();
        ranges.forEach(r -> rangesBld.add(Json.createArrayBuilder().add(r.get(0)).add(r.get(1))));
        bld.add("ranges", rangesBld);
    }
    return bld;
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) NullSafeJsonBuilder.jsonObjectBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder) Arrays(java.util.Arrays) DatasetDistributor(edu.harvard.iq.dataverse.DatasetDistributor) DataFileTag(edu.harvard.iq.dataverse.DataFileTag) Date(java.util.Date) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) User(edu.harvard.iq.dataverse.authorization.users.User) Permission(edu.harvard.iq.dataverse.authorization.Permission) Map(java.util.Map) DataverseFacet(edu.harvard.iq.dataverse.DataverseFacet) ExplicitGroup(edu.harvard.iq.dataverse.authorization.groups.impl.explicit.ExplicitGroup) Collector(java.util.stream.Collector) DatasetField(edu.harvard.iq.dataverse.DatasetField) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) EnumSet(java.util.EnumSet) JsonObject(javax.json.JsonObject) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) Collection(java.util.Collection) DatasetFieldWalker(edu.harvard.iq.dataverse.util.DatasetFieldWalker) Set(java.util.Set) DataFile(edu.harvard.iq.dataverse.DataFile) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) MetadataBlock(edu.harvard.iq.dataverse.MetadataBlock) BinaryOperator(java.util.function.BinaryOperator) DataverseTheme(edu.harvard.iq.dataverse.DataverseTheme) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) List(java.util.List) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonObjectBuilder(javax.json.JsonObjectBuilder) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) StringUtil(edu.harvard.iq.dataverse.util.StringUtil) JsonArrayBuilder(javax.json.JsonArrayBuilder) Util(edu.harvard.iq.dataverse.api.Util) AuthenticationProviderRow(edu.harvard.iq.dataverse.authorization.providers.AuthenticationProviderRow) Deque(java.util.Deque) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) IpAddress(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress) ArrayList(java.util.ArrayList) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) IpAddressRange(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange) Workflow(edu.harvard.iq.dataverse.workflow.Workflow) BiConsumer(java.util.function.BiConsumer) Json(javax.json.Json) LinkedList(java.util.LinkedList) Dataverse(edu.harvard.iq.dataverse.Dataverse) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) BuiltinUser(edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Dataset(edu.harvard.iq.dataverse.Dataset) JsonArray(javax.json.JsonArray) TermsOfUseAndAccess(edu.harvard.iq.dataverse.TermsOfUseAndAccess) Collections(edu.emory.mathcs.backport.java.util.Collections) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) Collectors.toList(java.util.stream.Collectors.toList) WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) ShibGroup(edu.harvard.iq.dataverse.authorization.groups.impl.shib.ShibGroup) RoleAssigneeDisplayInfo(edu.harvard.iq.dataverse.authorization.RoleAssigneeDisplayInfo) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Collectors.toList(java.util.stream.Collectors.toList) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) IpAddressRange(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange)

Aggregations

Json (javax.json.Json)6 JsonObject (javax.json.JsonObject)6 IOException (java.io.IOException)3 AssertRequest (com.amihaiemil.docker.mock.AssertRequest)2 Condition (com.amihaiemil.docker.mock.Condition)2 PayloadOf (com.amihaiemil.docker.mock.PayloadOf)2 Response (com.amihaiemil.docker.mock.Response)2 URI (java.net.URI)2 JsonArray (javax.json.JsonArray)2 HttpStatus (org.apache.http.HttpStatus)2 MatcherAssert (org.hamcrest.MatcherAssert)2 Matchers (org.hamcrest.Matchers)2 Test (org.junit.Test)2 Collections (edu.emory.mathcs.backport.java.util.Collections)1 ControlledVocabularyValue (edu.harvard.iq.dataverse.ControlledVocabularyValue)1 DataFile (edu.harvard.iq.dataverse.DataFile)1 DataFileTag (edu.harvard.iq.dataverse.DataFileTag)1 Dataset (edu.harvard.iq.dataverse.Dataset)1 DatasetDistributor (edu.harvard.iq.dataverse.DatasetDistributor)1 DatasetField (edu.harvard.iq.dataverse.DatasetField)1