Java Code Examples for com.fasterxml.jackson.databind.node.ArrayNode#remove()

The following examples show how to use com.fasterxml.jackson.databind.node.ArrayNode#remove() . 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: SwaggerSchema.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
public void allowJsonRefInContext(String jsonReferenceContext, boolean allow) {
    if (jsonRefContexts.get(jsonReferenceContext) == null) {
        throw new IllegalArgumentException("Invalid JSON Reference Context: " + jsonReferenceContext);
    }
    // special case
    if (SwaggerPreferenceConstants.VALIDATION_REF_SECURITY_DEFINITIONS_OBJECT.equals(jsonReferenceContext)) {
        allowJsonRefInSecurityDefinitionsObject(allow);
        return;
    }
    ArrayNode definition = (ArrayNode) jsonRefContexts.get(jsonReferenceContext);
    // should preserve order of the original ArrayNode._children
    List<JsonNode> children = new ArrayList<>();
    definition.elements().forEachRemaining(children::add);
    
    int indexOfJsonReference = children.indexOf(refToJsonReferenceNode);
    boolean alreadyHasJsonReference = indexOfJsonReference > -1;
    if (allow) {
        if (!alreadyHasJsonReference) {
            definition.add(refToJsonReferenceNode.deepCopy());
        }
    } else { // disallow
        if (alreadyHasJsonReference) {
            definition.remove(indexOfJsonReference);
        }
    }
}
 
Example 2
Source File: PrintingController.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void removeNulls(JsonNode tree) {
	if (tree instanceof ArrayNode) {
		ArrayNode array = (ArrayNode) tree;
		for (int i = array.size() - 1; i >= 0; i--) {
			if (array.get(i).isNull()) {
				array.remove(i);
			} else {
				removeNulls(array.get(i));
			}
		}
	} else if (tree instanceof ObjectNode) {
		ObjectNode object = (ObjectNode) tree;
		Set<String> nulls = new HashSet<String>();
		for (Iterator<String> it = object.fieldNames(); it.hasNext();) {
			String name = it.next();
			if (object.get(name).isNull()) {
				nulls.add(name);
			} else {
				removeNulls(object.get(name));
			}
		}
		object.remove(nulls);
	}
}
 
Example 3
Source File: SpecificationResourceCustomizer.java    From syndesis with Apache License 2.0 5 votes vote down vote up
/**
 * Scan the specification and leave only the security method picked by user, if any present.
 * The change is required to avoid exposure of security configuration (such as apikey) through other channels not requested by user,
 * for example, query parameters when the user only wants to provide api key through http headers.
 *
 * @param specification                    the swagger/openapi original specification
 * @param securityDefinitionSelectedByUser the securityDefinition picked by the user
 * @return the updated swagger/openapi specification or the original specification
 */
private static String updateSecuritySpecification(final String specification, final String securityDefinitionSelectedByUser) throws JsonProcessingException {
    final Optional<String> securityDefinitionName = getNameFromDefinition(securityDefinitionSelectedByUser);
    if (securityDefinitionName.isPresent()) {
        JsonNode rootNode = OBJECT_MAPPER.readTree(specification);
        List<JsonNode> securities = rootNode.findValues("security");
        if (!securities.isEmpty()) {
            LOG.info("Updating specification to accept only {} user selected security method", securityDefinitionName.get());
            for (JsonNode endpointSecurity : securities) {
                if (endpointSecurity.isArray()) {
                    ArrayNode securityArray = (ArrayNode) endpointSecurity;
                    for (int i = 0; i < securityArray.size(); i++) {
                        String securityName = securityArray.get(i).fieldNames().next();
                        if (!securityDefinitionName.get().equals(securityName)) {
                            securityArray.remove(i);
                        }
                    }
                } else {
                    // Security section must be an array
                    throw new IllegalArgumentException("Swagger/OpenAPI specification requires endpoint security to be an array of elements!");
                }
            }
            return OBJECT_MAPPER.writeValueAsString(rootNode);
        }
    }

    // no changes
    LOG.debug("Specification was provided with no security method");
    return specification;
}
 
Example 4
Source File: JacksonJsonNode.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
public SpinJsonNode removeAt(int index) {
  if(this.isArray()) {
    ArrayNode node = (ArrayNode) jsonNode;

    node.remove(getCorrectIndex(index));

    return this;
  } else {
    throw LOG.unableToModifyNode(jsonNode.getNodeType().name());
  }
}
 
Example 5
Source File: BgpConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Removes BGP speaker from configuration.
 *
 * @param speakerName BGP speaker name
 */
public void removeSpeaker(String speakerName) {
    ArrayNode speakersArray = (ArrayNode) object.get(SPEAKERS);

    for (int i = 0; i < speakersArray.size(); i++) {
        if (speakersArray.get(i).hasNonNull(NAME) &&
                speakersArray.get(i).get(NAME).asText().equals(speakerName)) {
            speakersArray.remove(i);
            return;
        }
    }
}
 
Example 6
Source File: VplsAppConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a VPLS from the configuration.
 *
 * @param vplsName the vplsName of the VPLS to be removed
 */
public void removeVpls(String vplsName) {
    ArrayNode configuredVpls = (ArrayNode) object.get(VPLS);

    for (int i = 0; i < configuredVpls.size(); i++) {
        if (configuredVpls.get(i).hasNonNull(NAME) &&
                configuredVpls.get(i).get(NAME).asText().equals(vplsName)) {
            configuredVpls.remove(i);
            return;
        }
    }
}
 
Example 7
Source File: ReflectionUtil.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static void removeArrayItem(ArrayNode list, int index) {
    if (index == -1) {
        throw new IllegalArgumentException("Internal error - should never be called with index == -1");
    }
    list.remove(index);
}
 
Example 8
Source File: TinkerPopOperations.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int deleteEntity(Collection collection, UUID id, Change modified)
  throws NotFoundException {

  requireCommit = true;

  GraphTraversal<Vertex, Vertex> entityTraversal = entityFetcher.getEntity(traversal, id, null,
    collection.getCollectionName());

  if (!entityTraversal.hasNext()) {
    throw new NotFoundException();
  }

  Vertex entity = entityTraversal.next();
  String entityTypesStr = getProp(entity, "types", String.class).orElse("[]");
  boolean wasRemoved = false;
  if (entityTypesStr.contains("\"" + collection.getEntityTypeName() + "\"")) {
    try {
      ArrayNode entityTypes = arrayToEncodedArray.tinkerpopToJson(entityTypesStr);
      if (entityTypes.size() == 1) {
        entity.property("deleted", true);
        wasRemoved = true;
      } else {
        for (int i = entityTypes.size() - 1; i >= 0; i--) {
          JsonNode val = entityTypes.get(i);
          if (val != null && val.asText("").equals(collection.getEntityTypeName())) {
            entityTypes.remove(i);
            wasRemoved = true;
          }
        }
        entity.property("types", entityTypes.toString());
      }
    } catch (IOException e) {
      LOG.error(Logmarkers.databaseInvariant, "property 'types' was not parseable: " + entityTypesStr);
    }
  } else {
    throw new NotFoundException();
  }

  int newRev = getProp(entity, "rev", Integer.class).orElse(1) + 1;
  entity.property("rev", newRev);

  entity.edges(Direction.BOTH).forEachRemaining(edge -> {
    // Skip the hasEntity and the VERSION_OF edge, which are not real relations, but system edges
    if (edge.label().equals(HAS_ENTITY_RELATION_NAME) || edge.label().equals(VERSION_OF)) {
      return;
    }
    Optional<Collection> ownEdgeCol = getOwnCollectionOfElement(collection.getVre(), edge);
    if (ownEdgeCol.isPresent()) {
      edge.property(ownEdgeCol.get().getEntityTypeName() + "_accepted", false);
    }
  });

  setModified(entity, modified);
  entity.property("pid").remove();

  Vertex duplicate = duplicateVertex(traversal, entity, indexHandler);

  if (wasRemoved) {
    listener.onRemoveFromCollection(collection, Optional.of(entity), duplicate);
  }

  return newRev;
}