com.fasterxml.jackson.core.JsonPointer Java Examples

The following examples show how to use com.fasterxml.jackson.core.JsonPointer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void attach(String path, DataModelTree tree) throws WorkflowException {

    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }

    JsonPointer ptr = JsonPointer.compile(path);

    if (!(tree instanceof JsonDataModelTree)) {
        throw new WorkflowException("Invalid subTree(" + tree + ")");
    }
    JsonNode attachingNode = ((JsonDataModelTree) tree).root();

    attach(ptr, attachingNode);
}
 
Example #2
Source File: MetadataService.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the specified {@code member} to the {@link ProjectMetadata} of the specified {@code projectName}
 * with the specified {@code projectRole}.
 */
public CompletableFuture<Revision> addMember(Author author, String projectName,
                                             User member, ProjectRole projectRole) {
    requireNonNull(author, "author");
    requireNonNull(projectName, "projectName");
    requireNonNull(member, "member");
    requireNonNull(projectRole, "projectRole");

    final Member newMember = new Member(member, projectRole, UserAndTimestamp.of(author));
    final JsonPointer path = JsonPointer.compile("/members" + encodeSegment(newMember.id()));
    final Change<JsonNode> change =
            Change.ofJsonPatch(METADATA_JSON,
                               asJsonArray(new TestAbsenceOperation(path),
                                           new AddOperation(path, Jackson.valueToTree(newMember))));
    final String commitSummary =
            "Add a member '" + newMember.id() + "' to the project '" + projectName + '\'';
    return metadataRepo.push(projectName, Project.REPO_DOGMA, author, commitSummary, change);
}
 
Example #3
Source File: DiffProcessor.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
DiffProcessor(ReplaceMode replaceMode, final Supplier<Map<JsonPointer, JsonNode>> unchangedValuesSupplier) {
    this.replaceMode = replaceMode;
    this.unchangedValuesSupplier = new Supplier<Map<JsonPointer, JsonNode>>() {

        @Nullable
        private Map<JsonPointer, JsonNode> unchangedValues;

        @Override
        public Map<JsonPointer, JsonNode> get() {
            if (unchangedValues == null) {
                unchangedValues = unchangedValuesSupplier.get();
            }
            return unchangedValues;
        }
    };
}
 
Example #4
Source File: MetadataService.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a {@link Token} of the specified {@code appId} to the specified {@code projectName}.
 */
public CompletableFuture<Revision> addToken(Author author, String projectName,
                                            String appId, ProjectRole role) {
    requireNonNull(author, "author");
    requireNonNull(projectName, "projectName");
    requireNonNull(appId, "appId");
    requireNonNull(role, "role");

    return getTokens().thenCompose(tokens -> {
        final Token token = tokens.appIds().get(appId);
        checkArgument(token != null, "Token not found: " + appId);

        final TokenRegistration registration = new TokenRegistration(appId, role,
                                                                     UserAndTimestamp.of(author));
        final JsonPointer path = JsonPointer.compile("/tokens" + encodeSegment(registration.id()));
        final Change<JsonNode> change =
                Change.ofJsonPatch(METADATA_JSON,
                                   asJsonArray(new TestAbsenceOperation(path),
                                               new AddOperation(path, Jackson.valueToTree(registration))));
        final String commitSummary = "Add a token '" + registration.id() +
                                     "' to the project '" + projectName + "' with a role '" + role + '\'';
        return metadataRepo.push(projectName, Project.REPO_DOGMA, author, commitSummary, change);
    });
}
 
Example #5
Source File: LinkOperationHyperlinkDetector.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    Model model = doc.getModel();
    AbstractNode node = model.find(pointer);
    List<AbstractNode> nodes = model.findByType(JsonPointer.compile("/definitions/operation"));
    Iterator<AbstractNode> it = nodes.iterator();

    AbstractNode found = null;
    while (it.hasNext() && found == null) {
        AbstractNode current = it.next();
        AbstractNode value = current.get("operationId");

        if (value != null && Objects.equals(node.asValue().getValue(), value.asValue().getValue())) {
            found = value;
        }
    }

    if (found != null) {
        IRegion target = doc.getRegion(found.getPointer());
        if (target != null) {
            return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
        }
    }

    return null;
}
 
Example #6
Source File: OpenApi3Validator.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
protected void validateOperationIdReferences(Model model, AbstractNode node, Set<SwaggerError> errors) {
    JsonPointer schemaPointer = JsonPointer.compile("/definitions/link/properties/operationId");

    if (node != null && node.getType() != null && schemaPointer.equals(node.getType().getPointer())) {
        List<AbstractNode> nodes = model.findByType(operationPointer);
        Iterator<AbstractNode> it = nodes.iterator();

        boolean found = false;
        while (it.hasNext() && !found) {
            AbstractNode current = it.next();
            AbstractNode value = current.get("operationId");

            found = value != null && Objects.equals(node.asValue().getValue(), value.asValue().getValue());
        }

        if (!found) {
            errors.add(error(node, IMarker.SEVERITY_ERROR, Messages.error_invalid_operation_id));
        }
    }
}
 
Example #7
Source File: MetadataService.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<Revision> removeToken(String projectName, Author author, String appId,
                                                boolean quiet) {
    final String commitSummary = "Remove the token '" + appId + "' from the project '" + projectName + '\'';
    return metadataRepo.push(
            projectName, Project.REPO_DOGMA, author, commitSummary,
            () -> fetchMetadata(projectName).thenApply(metadataWithRevision -> {
                final ImmutableList.Builder<JsonPatchOperation> patches = ImmutableList.builder();
                final ProjectMetadata metadata = metadataWithRevision.object();
                metadata.repos().values()
                        .stream().filter(repo -> repo.perTokenPermissions().containsKey(appId))
                        .forEach(r -> patches.add(
                                new RemoveOperation(perTokenPermissionPointer(r.name(), appId))));
                if (quiet) {
                    patches.add(new RemoveIfExistsOperation(JsonPointer.compile("/tokens" +
                                                                                encodeSegment(appId))));
                } else {
                    patches.add(new RemoveOperation(JsonPointer.compile("/tokens" +
                                                                        encodeSegment(appId))));
                }
                final Change<JsonNode> change =
                        Change.ofJsonPatch(METADATA_JSON, Jackson.valueToTree(patches.build()));
                return HolderWithRevision.of(change, metadataWithRevision.revision());
            })
    );
}
 
Example #8
Source File: DiffProcessor.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
void valueReplaced(final JsonPointer pointer, final JsonNode oldValue, final JsonNode newValue) {
    switch (replaceMode) {
        case RFC6902:
            diffs.add(new ReplaceOperation(pointer, newValue));
            break;
        case SAFE:
            diffs.add(new SafeReplaceOperation(pointer, oldValue, newValue));
            break;
    }
}
 
Example #9
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public String getString(JsonPointer ptr, String defaultValue) {
    Objects.requireNonNull(defaultValue, "defaultValue is required");
    String result;
    if (exists(ptr)) {
        result = getString(ptr);
    } else {
        set(ptr, defaultValue);
        result = defaultValue;
    }
    return result;
}
 
Example #10
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public BigDecimal getBigDecimal(JsonPointer ptr, BigDecimal defaultValue) throws CacheException {
    Objects.requireNonNull(defaultValue, "defaultValue is required");
    BigDecimal result;
    if (exists(ptr)) {
        result = getBigDecimal(ptr);
    } else {
        set(ptr, defaultValue);
        result = defaultValue;
    }
    return result;
}
 
Example #11
Source File: JsonReferenceFactoryTest.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void test() throws URISyntaxException {
    JsonReferenceFactory factory = new JsonReferenceFactory();

    JsonReference result = factory.doCreate("#/definitions/Foo", null);

    assertEquals(new URI(null, null, "/definitions/Foo"), result.getUri());
    assertTrue(result.isLocal());
    assertFalse(result.isAbsolute());
    assertEquals(JsonPointer.compile("/definitions/Foo"), result.getPointer());
}
 
Example #12
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public BigInteger getBigInteger(JsonPointer ptr, BigInteger defaultValue) throws CacheException {
    Objects.requireNonNull(defaultValue, "defaultValue is required");
    BigInteger result;
    if (exists(ptr)) {
        result = getBigInteger(ptr);
    } else {
        set(ptr, defaultValue);
        result = defaultValue;
    }
    return result;
}
 
Example #13
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets json node on specific json pointer as ArrayNode.
 *
 * @param ptr json pointer
 * @return ArrayNode type json node on specific json pointer.
 * @throws WorkflowException workflow exception
 */
public ArrayNode arrayAt(JsonPointer ptr) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }
    JsonNode node = root.at(ptr);
    if (node instanceof MissingNode) {
        return null;
    }
    if (!(node instanceof ArrayNode)) {
        throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
    }
    return (ArrayNode) node;
}
 
Example #14
Source File: JsonDocument.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
public IRegion getRegion(JsonPointer pointer) {
    Model model = getModel();
    if (model == null) {
        return null;
    }

    AbstractNode node = model.find(pointer);
    if (node == null) {
        return new Region(0, 0);
    }

    Position position = node.getPosition(this);

    return new Region(position.getOffset(), position.getLength());
}
 
Example #15
Source File: NodeDeserializer.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
protected ValueNode deserializeValueNode(JsonParser p, DeserializationContext context, JsonLocation startLocation)
        throws IOException {
    final Model model = (Model) context.getAttribute(ATTRIBUTE_MODEL);
    final AbstractNode parent = (AbstractNode) context.getAttribute(ATTRIBUTE_PARENT);
    final JsonPointer ptr = (JsonPointer) context.getAttribute(ATTRIBUTE_POINTER);

    Object v = context.readValue(p, Object.class);

    ValueNode node = model.valueNode(parent, ptr, v);
    node.setStartLocation(createLocation(startLocation));
    node.setEndLocation(createLocation(p.getCurrentLocation()));

    return node;
}
 
Example #16
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public Number getNumber(JsonPointer ptr, Number defaultValue) throws CacheException {
    Objects.requireNonNull(defaultValue, "defaultValue is required");
    Number result;
    if (exists(ptr)) {
        result = getNumber(ptr);
    } else {
        set(ptr, defaultValue);
        result = defaultValue;
    }
    return result;
}
 
Example #17
Source File: JsonSchemaPreProcessorTest.java    From liiklus with MIT License 5 votes vote down vote up
private JsonSchemaPreProcessor getProcessor(boolean allowDeprecatedProperties) {
    return new JsonSchemaPreProcessor(
            getSchema("basic.yml"),
            JsonPointer.compile("/eventType"),
            allowDeprecatedProperties
    );
}
 
Example #18
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(JsonPointer ptr) throws CacheException {
    Object result;
    if (root == null) {
        result = null;
    } else {
        try {
            JsonNode node = root.at(ptr);
            switch (node.getNodeType()) {
                case ARRAY:
                case OBJECT:
                    result = node;
                    break;
                case BINARY:
                    result = node.binaryValue();
                    break;
                case BOOLEAN:
                    result = node.booleanValue();
                    break;
                case NUMBER:
                    result = node.numberValue();
                    break;
                case POJO:
                    result = ((POJONode) node).getPojo();
                    break;
                case STRING:
                    result = node.textValue();
                    break;
                default:
                    result = null;
                    break;
            }
        } catch (IOException e) {
            throw new CacheException(e);
        }
    }
    return result;
}
 
Example #19
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public double getDouble(JsonPointer ptr, double defaultValue) throws CacheException {
    Objects.requireNonNull(defaultValue, "defaultValue is required");
    double result;
    if (exists(ptr)) {
        result = getDouble(ptr);
    } else {
        set(ptr, defaultValue);
        result = defaultValue;
    }
    return result;
}
 
Example #20
Source File: JavaCXFExtServerCodegen.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
void addTestData(Object value) {
    JsonPointer ptr = getPointer(null, true, true);
    if (!testDataCache.exists(ptr)) {
        try {
            testDataCache.set(ptr, value);
        } catch (CacheException e) {
            LOGGER.error("Unable to update test data cache for " + ptr, e);
        }
    }
}
 
Example #21
Source File: JsonReferenceFactoryTest.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void test4() throws URISyntaxException {
    JsonReferenceFactory factory = new JsonReferenceFactory();

    JsonReference result = factory.doCreate("file://path/to/file/doc.yaml#/definitions/Foo", null);

    assertEquals(URI.create("file://path/to/file/doc.yaml#/definitions/Foo"), result.getUri());
    assertFalse(result.isLocal());
    assertTrue(result.isAbsolute());
    assertEquals(JsonPointer.compile("/definitions/Foo"), result.getPointer());
}
 
Example #22
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public JsonCache add(JsonPointer ptr, long value) {
    return add(ptr, nodeFor(value));
}
 
Example #23
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public boolean getBoolean(String path) throws CacheException {
    return parent.getBoolean(basePtr.append(JsonPointer.compile(path)));
}
 
Example #24
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public JsonCache set(JsonPointer ptr, int value) {
    return set(ptr, (Object) value);
}
 
Example #25
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] getBinary(JsonPointer ptr, byte[] defaultValue) throws CacheException {
    return parent.getBinary(basePtr.append(ptr), defaultValue);
}
 
Example #26
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public int size(String path) {
    return size(JsonPointer.compile(path));
}
 
Example #27
Source File: LinkOperationHyperlinkDetector.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean canDetect(JsonPointer pointer) {
    return pointer != null && pointer.toString().matches(".*/links/(\\w+)/operationId");
}
 
Example #28
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public String getString(String path) throws CacheException {
    return parent.getString(basePtr.append(JsonPointer.compile(path)));
}
 
Example #29
Source File: ComplexTypeDefinition.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 4 votes vote down vote up
public ComplexTypeDefinition(JsonSchema schema, JsonPointer pointer, JsonNode definition, JsonType type) {
    super(schema, pointer, definition, type);
    init();
}
 
Example #30
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public double getDouble(String path) {
    return getDouble(JsonPointer.compile(path));
}