Example usage for org.apache.commons.lang3 StringUtils substringBefore

List of usage examples for org.apache.commons.lang3 StringUtils substringBefore

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBefore.

Prototype

public static String substringBefore(final String str, final String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:org.apache.olingo.client.core.serialization.AbstractODataBinder.java

private EdmEntityType findEntityType(final String entitySetOrSingletonOrType,
        final EdmEntityContainer container) {

    EdmEntityType type = null;/*from   w w  w  .  j  av  a2 s .c  o  m*/

    final String firstToken = StringUtils.substringBefore(entitySetOrSingletonOrType, "/");
    EdmBindingTarget bindingTarget = container.getEntitySet(firstToken);
    if (bindingTarget == null) {
        bindingTarget = container.getSingleton(firstToken);
    }
    if (bindingTarget != null) {
        type = bindingTarget.getEntityType();
    }

    if (entitySetOrSingletonOrType.indexOf('/') != -1) {
        final String[] splitted = entitySetOrSingletonOrType.split("/");
        if (splitted.length > 1) {
            for (int i = 1; i < splitted.length && type != null; i++) {
                final EdmNavigationProperty navProp = type.getNavigationProperty(splitted[i]);
                if (navProp == null) {
                    type = null;
                } else {
                    type = navProp.getType();
                }
            }
        }
    }

    return type;
}

From source file:org.apache.olingo.client.core.serialization.ContextURLParser.java

public static ContextURL parse(final URI contextURL) {
    if (contextURL == null) {
        return null;
    }/*w  w w .j  a va2 s  .  com*/

    final ContextURL.Builder contextUrl = ContextURL.with();

    String contextURLasString = contextURL.toASCIIString();

    boolean isEntity = false;
    if (contextURLasString.endsWith("/$entity") || contextURLasString.endsWith("/@Element")) {
        isEntity = true;
        contextUrl.suffix(Suffix.ENTITY);
        contextURLasString = contextURLasString.replace("/$entity", StringUtils.EMPTY).replace("/@Element",
                StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$ref")) {
        contextUrl.suffix(Suffix.REFERENCE);
        contextURLasString = contextURLasString.replace("/$ref", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$delta")) {
        contextUrl.suffix(Suffix.DELTA);
        contextURLasString = contextURLasString.replace("/$delta", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$deletedEntity")) {
        contextUrl.suffix(Suffix.DELTA_DELETED_ENTITY);
        contextURLasString = contextURLasString.replace("/$deletedEntity", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$link")) {
        contextUrl.suffix(Suffix.DELTA_LINK);
        contextURLasString = contextURLasString.replace("/$link", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$deletedLink")) {
        contextUrl.suffix(Suffix.DELTA_DELETED_LINK);
        contextURLasString = contextURLasString.replace("/$deletedLink", StringUtils.EMPTY);
    }

    contextUrl.serviceRoot(URI.create(StringUtils.substringBefore(contextURLasString, Constants.METADATA)));

    final String rest = StringUtils.substringAfter(contextURLasString, Constants.METADATA + "#");

    String firstToken;
    String entitySetOrSingletonOrType;
    if (rest.startsWith("Collection(")) {
        firstToken = rest.substring(0, rest.indexOf(')') + 1);
        entitySetOrSingletonOrType = firstToken;
    } else {
        final int openParIdx = rest.indexOf('(');
        if (openParIdx == -1) {
            firstToken = StringUtils.substringBeforeLast(rest, "/");

            entitySetOrSingletonOrType = firstToken;
        } else {
            firstToken = isEntity ? rest : StringUtils.substringBeforeLast(rest, ")") + ")";

            final List<String> parts = new ArrayList<String>();
            for (String split : firstToken.split("\\)/")) {
                parts.add(split.replaceAll("\\(.*", ""));
            }
            entitySetOrSingletonOrType = StringUtils.join(parts, '/');
            final int commaIdx = firstToken.indexOf(',');
            if (commaIdx != -1) {
                contextUrl.selectList(firstToken.substring(openParIdx + 1, firstToken.length() - 1));
            }
        }
    }
    contextUrl.entitySetOrSingletonOrType(entitySetOrSingletonOrType);

    final int slashIdx = entitySetOrSingletonOrType.lastIndexOf('/');
    if (slashIdx != -1 && entitySetOrSingletonOrType.substring(slashIdx + 1).indexOf('.') != -1) {
        contextUrl.entitySetOrSingletonOrType(entitySetOrSingletonOrType.substring(0, slashIdx));
        contextUrl.derivedEntity(entitySetOrSingletonOrType.substring(slashIdx + 1));
    }

    if (!firstToken.equals(rest)) {
        final String[] pathElems = StringUtils.substringAfter(rest, "/").split("/");
        if (pathElems.length > 0 && pathElems[0].length() > 0) {
            if (pathElems[0].indexOf('.') == -1) {
                contextUrl.navOrPropertyPath(pathElems[0]);
            } else {
                contextUrl.derivedEntity(pathElems[0]);
            }

            if (pathElems.length > 1) {
                contextUrl.navOrPropertyPath(pathElems[1]);
            }
        }
    }

    return contextUrl.build();
}

From source file:org.apache.olingo.client.core.serialization.JsonDeltaDeserializer.java

protected ResWrap<Delta> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = parser.getCodec().readTree(parser);

    final Delta delta = new Delta();

    final URI contextURL = tree.hasNonNull(Constants.JSON_CONTEXT)
            ? URI.create(tree.get(Constants.JSON_CONTEXT).textValue())
            : null;/*from   ww w  . j ava2s  . c  o  m*/
    if (contextURL != null) {
        delta.setBaseURI(
                URI.create(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA)));
    }

    if (tree.hasNonNull(Constants.JSON_COUNT)) {
        delta.setCount(tree.get(Constants.JSON_COUNT).asInt());
    }
    if (tree.hasNonNull(Constants.JSON_NEXT_LINK)) {
        delta.setNext(URI.create(tree.get(Constants.JSON_NEXT_LINK).textValue()));
    }
    if (tree.hasNonNull(Constants.JSON_DELTA_LINK)) {
        delta.setDeltaLink(URI.create(tree.get(Constants.JSON_DELTA_LINK).textValue()));
    }

    if (tree.hasNonNull(Constants.VALUE)) {
        JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(serverMode);
        for (JsonNode jsonNode : tree.get(Constants.VALUE)) {
            final ObjectNode item = (ObjectNode) jsonNode;
            final ContextURL itemContextURL = item.hasNonNull(Constants.JSON_CONTEXT)
                    ? ContextURLParser.parse(URI.create(item.get(Constants.JSON_CONTEXT).textValue()))
                    : null;
            item.remove(Constants.JSON_CONTEXT);

            if (itemContextURL == null || itemContextURL.isEntity()) {
                delta.getEntities()
                        .add(entityDeserializer.doDeserialize(item.traverse(parser.getCodec())).getPayload());
            } else if (itemContextURL.isDeltaDeletedEntity()) {
                delta.getDeletedEntities().add(parser.getCodec().treeToValue(item, DeletedEntity.class));
            } else if (itemContextURL.isDeltaLink()) {
                delta.getAddedLinks().add(parser.getCodec().treeToValue(item, DeltaLink.class));
            } else if (itemContextURL.isDeltaDeletedLink()) {
                delta.getDeletedLinks().add(parser.getCodec().treeToValue(item, DeltaLink.class));
            }
        }
    }

    return new ResWrap<Delta>(contextURL, null, delta);
}

From source file:org.apache.olingo.client.core.serialization.JsonEntityDeserializer.java

protected ResWrap<Entity> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = parser.getCodec().readTree(parser);

    if (tree.has(Constants.VALUE) && tree.get(Constants.VALUE).isArray()) {
        throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
    }// w w w.  jav a 2s  .c om

    final Entity entity = new Entity();

    final URI contextURL;
    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }
    if (contextURL != null) {
        entity.setBaseURI(
                URI.create(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA)));
    }

    final String metadataETag;
    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {
        metadataETag = null;
    }

    if (tree.hasNonNull(Constants.JSON_ETAG)) {
        entity.setETag(tree.get(Constants.JSON_ETAG).textValue());
        tree.remove(Constants.JSON_ETAG);
    }

    if (tree.hasNonNull(Constants.JSON_TYPE)) {
        entity.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(Constants.JSON_TYPE).textValue())
                .build().internal());
        tree.remove(Constants.JSON_TYPE);
    }

    if (tree.hasNonNull(Constants.JSON_ID)) {
        entity.setId(URI.create(tree.get(Constants.JSON_ID).textValue()));
        tree.remove(Constants.JSON_ID);
    }

    if (tree.hasNonNull(Constants.JSON_READ_LINK)) {
        final Link link = new Link();
        link.setRel(Constants.SELF_LINK_REL);
        link.setHref(tree.get(Constants.JSON_READ_LINK).textValue());
        entity.setSelfLink(link);

        tree.remove(Constants.JSON_READ_LINK);
    }

    if (tree.hasNonNull(Constants.JSON_EDIT_LINK)) {
        final Link link = new Link();
        if (serverMode) {
            link.setRel(Constants.EDIT_LINK_REL);
        }
        link.setHref(tree.get(Constants.JSON_EDIT_LINK).textValue());
        entity.setEditLink(link);

        tree.remove(Constants.JSON_EDIT_LINK);
    }

    if (tree.hasNonNull(Constants.JSON_MEDIA_READ_LINK)) {
        entity.setMediaContentSource(URI.create(tree.get(Constants.JSON_MEDIA_READ_LINK).textValue()));
        tree.remove(Constants.JSON_MEDIA_READ_LINK);
    }
    if (tree.hasNonNull(Constants.JSON_MEDIA_EDIT_LINK)) {
        entity.setMediaContentSource(URI.create(tree.get(Constants.JSON_MEDIA_EDIT_LINK).textValue()));
        tree.remove(Constants.JSON_MEDIA_EDIT_LINK);
    }
    if (tree.hasNonNull(Constants.JSON_MEDIA_CONTENT_TYPE)) {
        entity.setMediaContentType(tree.get(Constants.JSON_MEDIA_CONTENT_TYPE).textValue());
        tree.remove(Constants.JSON_MEDIA_CONTENT_TYPE);
    }
    if (tree.hasNonNull(Constants.JSON_MEDIA_ETAG)) {
        entity.setMediaETag(tree.get(Constants.JSON_MEDIA_ETAG).textValue());
        tree.remove(Constants.JSON_MEDIA_ETAG);
    }

    final Set<String> toRemove = new HashSet<String>();

    final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());

        links(field, entity, toRemove, tree, parser.getCodec());
        if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_READ_LINK))) {
            final Link link = new Link();
            link.setTitle(getTitle(field));
            link.setRel(Constants.NS_MEDIA_READ_LINK_REL + getTitle(field));
            link.setType(Constants.MEDIA_EDIT_LINK_TYPE);
            link.setHref(field.getValue().textValue());
            entity.getMediaEditLinks().add(link);

            if (tree.has(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_ETAG))) {
                link.setMediaETag(
                        tree.get(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_ETAG)).asText());
                toRemove.add(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_ETAG));
            }

            if (tree.has(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE))) {
                link.setType(tree.get(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE))
                        .asText());
                toRemove.add(link.getTitle() + getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE));
            }

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), getJSONAnnotation(Constants.JSON_MEDIA_READ_LINK), tree,
                    parser.getCodec(), link));
        } else if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_EDIT_LINK))) {
            final Link link = getOrCreateMediaLink(entity, getTitle(field));
            link.setRel(Constants.NS_MEDIA_EDIT_LINK_REL + getTitle(field));
            link.setHref(field.getValue().textValue());
            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), getJSONAnnotation(Constants.JSON_MEDIA_EDIT_LINK), tree,
                    parser.getCodec(), link));
        } else if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_CONTENT_TYPE))) {
            final Link link = getOrCreateMediaLink(entity, getTitle(field));
            link.setType(field.getValue().asText());
            toRemove.add(field.getKey());
        } else if (field.getKey().endsWith(getJSONAnnotation(Constants.JSON_MEDIA_ETAG))) {
            final Link link = getOrCreateMediaLink(entity, getTitle(field));
            link.setMediaETag(field.getValue().asText());
            toRemove.add(field.getKey());
        } else if (field.getKey().charAt(0) == '#') {
            final Operation operation = new Operation();
            operation.setMetadataAnchor(field.getKey());

            final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
            operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText()));

            entity.getOperations().add(operation);

            toRemove.add(field.getKey());
        } else if (customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
            final Annotation annotation = new Annotation();
            annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }

            if (!annotations.containsKey(customAnnotation.group(1))) {
                annotations.put(customAnnotation.group(1), new ArrayList<Annotation>());
            }
            annotations.get(customAnnotation.group(1)).add(annotation);
        }
    }

    for (Link link : entity.getNavigationLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }
    for (Link link : entity.getMediaEditLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }

    tree.remove(toRemove);

    try {
        populate(entity, entity.getProperties(), tree, parser.getCodec());
    } catch (final EdmPrimitiveTypeException e) {
        throw new IOException(e);
    }

    return new ResWrap<Entity>(contextURL, metadataETag, entity);
}

From source file:org.apache.olingo.client.core.serialization.JsonEntitySetDeserializer.java

protected ResWrap<EntityCollection> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    if (!tree.has(Constants.VALUE)) {
        return null;
    }//from  w  ww.  j  a va 2  s. c o  m

    final EntityCollection entitySet = new EntityCollection();

    URI contextURL;
    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }
    if (contextURL != null) {
        entitySet.setBaseURI(
                URI.create(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA)));
    }

    final String metadataETag;
    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {
        metadataETag = null;
    }

    if (tree.hasNonNull(Constants.JSON_COUNT)) {
        entitySet.setCount(tree.get(Constants.JSON_COUNT).asInt());
        tree.remove(Constants.JSON_COUNT);
    }
    if (tree.hasNonNull(Constants.JSON_NEXT_LINK)) {
        entitySet.setNext(URI.create(tree.get(Constants.JSON_NEXT_LINK).textValue()));
        tree.remove(Constants.JSON_NEXT_LINK);
    }
    if (tree.hasNonNull(Constants.JSON_DELTA_LINK)) {
        entitySet.setDeltaLink(URI.create(tree.get(Constants.JSON_DELTA_LINK).textValue()));
        tree.remove(Constants.JSON_DELTA_LINK);
    }

    if (tree.hasNonNull(Constants.VALUE)) {
        final JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(serverMode);
        for (JsonNode jsonNode : tree.get(Constants.VALUE)) {
            entitySet.getEntities()
                    .add(entityDeserializer.doDeserialize(jsonNode.traverse(parser.getCodec())).getPayload());
        }
        tree.remove(Constants.VALUE);
    }
    final Set<String> toRemove = new HashSet<String>();
    // any remaining entry is supposed to be an annotation or is ignored
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        if (field.getKey().charAt(0) == '@') {
            final Annotation annotation = new Annotation();
            annotation.setTerm(field.getKey().substring(1));

            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }
            entitySet.getAnnotations().add(annotation);
        } else if (field.getKey().charAt(0) == '#') {
            final Operation operation = new Operation();
            operation.setMetadataAnchor(field.getKey());

            final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
            operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText()));
            entitySet.getOperations().add(operation);
            toRemove.add(field.getKey());
        }
    }
    tree.remove(toRemove);
    return new ResWrap<EntityCollection>(contextURL, metadataETag, entitySet);
}

From source file:org.apache.olingo.client.core.uri.URIUtils.java

public static URI buildFunctionInvokeURI(final URI uri, final Map<String, ClientValue> parameters) {
    final String rawQuery = uri.getRawQuery();
    String baseURI = StringUtils.substringBefore(uri.toASCIIString(), "?" + rawQuery);
    if (baseURI.endsWith("()")) {
        baseURI = baseURI.substring(0, baseURI.length() - 2);
    }//from   w w  w.  jav  a  2 s. com

    final StringBuilder inlineParams = new StringBuilder();
    for (Map.Entry<String, ClientValue> param : parameters.entrySet()) {
        inlineParams.append(param.getKey()).append("=");

        Object value = null;
        if (param.getValue().isPrimitive()) {
            value = param.getValue().asPrimitive().toValue();
        } else if (param.getValue().isComplex()) {
            value = param.getValue().asComplex().asJavaMap();
        } else if (param.getValue().isCollection()) {
            value = param.getValue().asCollection().asJavaCollection();
        } else if (param.getValue().isEnum()) {
            value = param.getValue().asEnum().toString();
        }

        inlineParams.append(URIUtils.escape(value)).append(',');
    }

    if (inlineParams.length() > 0) {
        inlineParams.deleteCharAt(inlineParams.length() - 1);
    }

    try {
        return URI.create(baseURI + "(" + URLEncoder.encode(inlineParams.toString(), Constants.UTF8) + ")"
                + (StringUtils.isNotBlank(rawQuery) ? "?" + rawQuery : StringUtils.EMPTY));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("While adding GET parameters", e);
    }

}

From source file:org.apache.olingo.commons.core.serialization.ContextURLParser.java

public static ContextURL parse(final URI contextURL) {
    if (contextURL == null) {
        return null;
    }/*  ww  w  . ja v  a2 s.  c o  m*/

    final ContextURL.Builder builder = ContextURL.Builder.create();

    String contextURLasString = contextURL.toASCIIString();

    boolean isEntity = false;
    if (contextURLasString.endsWith("/$entity") || contextURLasString.endsWith("/@Element")) {
        isEntity = true;
        builder.suffix(Suffix.ENTITY);
        contextURLasString = contextURLasString.replace("/$entity", StringUtils.EMPTY).replace("/@Element",
                StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$ref")) {
        builder.suffix(Suffix.REFERENCE);
        contextURLasString = contextURLasString.replace("/$ref", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$delta")) {
        builder.suffix(Suffix.DELTA);
        contextURLasString = contextURLasString.replace("/$delta", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$deletedEntity")) {
        builder.suffix(Suffix.DELTA_DELETED_ENTITY);
        contextURLasString = contextURLasString.replace("/$deletedEntity", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$link")) {
        builder.suffix(Suffix.DELTA_LINK);
        contextURLasString = contextURLasString.replace("/$link", StringUtils.EMPTY);
    } else if (contextURLasString.endsWith("/$deletedLink")) {
        builder.suffix(Suffix.DELTA_DELETED_LINK);
        contextURLasString = contextURLasString.replace("/$deletedLink", StringUtils.EMPTY);
    }

    builder.serviceRoot(URI.create(StringUtils.substringBefore(contextURLasString, Constants.METADATA)));

    final String rest = StringUtils.substringAfter(contextURLasString, Constants.METADATA + "#");

    String firstToken;
    String entitySetOrSingletonOrType = null;
    if (rest.startsWith("Collection(")) {
        firstToken = rest.substring(0, rest.indexOf(')') + 1);
        entitySetOrSingletonOrType = firstToken;
    } else {
        final int openParIdx = rest.indexOf('(');
        if (openParIdx == -1) {
            firstToken = StringUtils.substringBeforeLast(rest, "/");

            entitySetOrSingletonOrType = firstToken;
        } else {
            firstToken = isEntity ? rest : StringUtils.substringBeforeLast(rest, ")") + ")";

            final List<String> parts = new ArrayList<String>();
            for (String split : firstToken.split("\\)/")) {
                parts.add(split.replaceAll("\\(.*", ""));
            }
            entitySetOrSingletonOrType = StringUtils.join(parts, '/');
            final int commaIdx = firstToken.indexOf(',');
            if (commaIdx != -1) {
                builder.selectList(firstToken.substring(openParIdx + 1, firstToken.length() - 1));
            }
        }
    }
    builder.entitySetOrSingletonOrType(entitySetOrSingletonOrType);

    final int slashIdx = entitySetOrSingletonOrType.lastIndexOf('/');
    if (slashIdx != -1 && entitySetOrSingletonOrType.substring(slashIdx + 1).indexOf('.') != -1) {
        final String clone = entitySetOrSingletonOrType;
        builder.entitySetOrSingletonOrType(clone.substring(0, slashIdx));
        builder.derivedEntity(clone.substring(slashIdx + 1));
    }

    if (!firstToken.equals(rest)) {
        final String[] pathElems = StringUtils.substringAfter(rest, "/").split("/");
        if (pathElems.length > 0 && pathElems[0].length() > 0) {
            if (pathElems[0].indexOf('.') == -1) {
                builder.navOrPropertyPath(pathElems[0]);
            } else {
                builder.derivedEntity(pathElems[0]);
            }

            if (pathElems.length > 1) {
                builder.navOrPropertyPath(pathElems[1]);
            }
        }
    }

    return builder.build();
}

From source file:org.apache.olingo.commons.core.serialization.JsonDeltaDeserializer.java

protected ResWrap<Delta> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = parser.getCodec().readTree(parser);

    final DeltaImpl delta = new DeltaImpl();

    final URI contextURL = tree.hasNonNull(Constants.JSON_CONTEXT)
            ? URI.create(tree.get(Constants.JSON_CONTEXT).textValue())
            : null;//from  w w  w  .  j  a v  a  2 s  .  c o m
    if (contextURL != null) {
        delta.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
    }

    if (tree.hasNonNull(jsonCount)) {
        delta.setCount(tree.get(jsonCount).asInt());
    }
    if (tree.hasNonNull(jsonNextLink)) {
        delta.setNext(URI.create(tree.get(jsonNextLink).textValue()));
    }
    if (tree.hasNonNull(jsonDeltaLink)) {
        delta.setDeltaLink(URI.create(tree.get(jsonDeltaLink).textValue()));
    }

    if (tree.hasNonNull(Constants.VALUE)) {
        JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(version, serverMode);
        for (JsonNode jsonNode : tree.get(Constants.VALUE)) {
            final ObjectNode item = (ObjectNode) jsonNode;
            final ContextURL itemContextURL = item.hasNonNull(Constants.JSON_CONTEXT)
                    ? ContextURLParser.parse(URI.create(item.get(Constants.JSON_CONTEXT).textValue()))
                    : null;
            item.remove(Constants.JSON_CONTEXT);

            if (itemContextURL == null || itemContextURL.isEntity()) {
                delta.getEntities()
                        .add(entityDeserializer.doDeserialize(item.traverse(parser.getCodec())).getPayload());
            } else if (itemContextURL.isDeltaDeletedEntity()) {
                delta.getDeletedEntities().add(parser.getCodec().treeToValue(item, DeletedEntityImpl.class));
            } else if (itemContextURL.isDeltaLink()) {
                delta.getAddedLinks().add(parser.getCodec().treeToValue(item, DeltaLinkImpl.class));
            } else if (itemContextURL.isDeltaDeletedLink()) {
                delta.getDeletedLinks().add(parser.getCodec().treeToValue(item, DeltaLinkImpl.class));
            }
        }
    }

    return new ResWrap<Delta>(contextURL, null, delta);
}

From source file:org.apache.olingo.commons.core.serialization.JsonEntityDeserializer.java

protected ResWrap<Entity> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = parser.getCodec().readTree(parser);

    if (tree.has(Constants.VALUE) && tree.get(Constants.VALUE).isArray()) {
        throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
    }/*w ww  .j ava2s  .c  o  m*/

    final EntityImpl entity = new EntityImpl();

    final URI contextURL;
    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }
    if (contextURL != null) {
        entity.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
    }

    final String metadataETag;
    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {
        metadataETag = null;
    }

    if (tree.hasNonNull(jsonETag)) {
        entity.setETag(tree.get(jsonETag).textValue());
        tree.remove(jsonETag);
    }

    if (tree.hasNonNull(jsonType)) {
        entity.setType(
                new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
        tree.remove(jsonType);
    }

    if (tree.hasNonNull(jsonId)) {
        entity.setId(URI.create(tree.get(jsonId).textValue()));
        tree.remove(jsonId);
    }

    if (tree.hasNonNull(jsonReadLink)) {
        final LinkImpl link = new LinkImpl();
        link.setRel(Constants.SELF_LINK_REL);
        link.setHref(tree.get(jsonReadLink).textValue());
        entity.setSelfLink(link);

        tree.remove(jsonReadLink);
    }

    if (tree.hasNonNull(jsonEditLink)) {
        final LinkImpl link = new LinkImpl();
        if (serverMode) {
            link.setRel(Constants.EDIT_LINK_REL);
        }
        link.setHref(tree.get(jsonEditLink).textValue());
        entity.setEditLink(link);

        tree.remove(jsonEditLink);
    }

    if (tree.hasNonNull(jsonMediaReadLink)) {
        entity.setMediaContentSource(URI.create(tree.get(jsonMediaReadLink).textValue()));
        tree.remove(jsonMediaReadLink);
    }
    if (tree.hasNonNull(jsonMediaEditLink)) {
        entity.setMediaContentSource(URI.create(tree.get(jsonMediaEditLink).textValue()));
        tree.remove(jsonMediaEditLink);
    }
    if (tree.hasNonNull(jsonMediaContentType)) {
        entity.setMediaContentType(tree.get(jsonMediaContentType).textValue());
        tree.remove(jsonMediaContentType);
    }
    if (tree.hasNonNull(jsonMediaETag)) {
        entity.setMediaETag(tree.get(jsonMediaETag).textValue());
        tree.remove(jsonMediaETag);
    }

    final Set<String> toRemove = new HashSet<String>();

    final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());

        links(field, entity, toRemove, tree, parser.getCodec());
        if (field.getKey().endsWith(getJSONAnnotation(jsonMediaEditLink))) {
            final LinkImpl link = new LinkImpl();
            link.setTitle(getTitle(field));
            link.setRel(version.getNamespace(ODataServiceVersion.NamespaceKey.MEDIA_EDIT_LINK_REL)
                    + getTitle(field));
            link.setHref(field.getValue().textValue());
            link.setType(ODataLinkType.MEDIA_EDIT.toString());
            entity.getMediaEditLinks().add(link);

            if (tree.has(link.getTitle() + getJSONAnnotation(jsonMediaETag))) {
                link.setMediaETag(tree.get(link.getTitle() + getJSONAnnotation(jsonMediaETag)).asText());
                toRemove.add(link.getTitle() + getJSONAnnotation(jsonMediaETag));
            }

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), getJSONAnnotation(jsonMediaEditLink), tree,
                    parser.getCodec(), link));
        } else if (field.getKey().endsWith(getJSONAnnotation(jsonMediaContentType))) {
            final String linkTitle = getTitle(field);
            for (Link link : entity.getMediaEditLinks()) {
                if (linkTitle.equals(link.getTitle())) {
                    ((LinkImpl) link).setType(field.getValue().asText());
                }
            }
            toRemove.add(field.getKey());
        } else if (field.getKey().charAt(0) == '#') {
            final ODataOperation operation = new ODataOperation();
            operation.setMetadataAnchor(field.getKey());

            final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
            operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText()));

            entity.getOperations().add(operation);

            toRemove.add(field.getKey());
        } else if (customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
            final Annotation annotation = new AnnotationImpl();
            annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }

            if (!annotations.containsKey(customAnnotation.group(1))) {
                annotations.put(customAnnotation.group(1), new ArrayList<Annotation>());
            }
            annotations.get(customAnnotation.group(1)).add(annotation);
        }
    }

    for (Link link : entity.getNavigationLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }
    for (Link link : entity.getMediaEditLinks()) {
        if (annotations.containsKey(link.getTitle())) {
            link.getAnnotations().addAll(annotations.get(link.getTitle()));
            for (Annotation annotation : annotations.get(link.getTitle())) {
                toRemove.add(link.getTitle() + "@" + annotation.getTerm());
            }
        }
    }

    tree.remove(toRemove);

    try {
        populate(entity, entity.getProperties(), tree, parser.getCodec());
    } catch (final EdmPrimitiveTypeException e) {
        throw new IOException(e);
    }

    return new ResWrap<Entity>(contextURL, metadataETag, entity);
}

From source file:org.apache.olingo.commons.core.serialization.JsonEntitySetDeserializer.java

protected ResWrap<EntitySet> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    if (!tree.has(Constants.VALUE)) {
        return null;
    }//from   w  ww  .j  av  a2  s. com

    final EntitySetImpl entitySet = new EntitySetImpl();

    URI contextURL;
    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }
    if (contextURL != null) {
        entitySet.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
    }

    final String metadataETag;
    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {
        metadataETag = null;
    }

    if (tree.hasNonNull(jsonCount)) {
        entitySet.setCount(tree.get(jsonCount).asInt());
        tree.remove(jsonCount);
    }
    if (tree.hasNonNull(jsonNextLink)) {
        entitySet.setNext(URI.create(tree.get(jsonNextLink).textValue()));
        tree.remove(jsonNextLink);
    }
    if (tree.hasNonNull(jsonDeltaLink)) {
        entitySet.setDeltaLink(URI.create(tree.get(jsonDeltaLink).textValue()));
        tree.remove(jsonDeltaLink);
    }

    if (tree.hasNonNull(Constants.VALUE)) {
        final JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(version, serverMode);
        for (JsonNode jsonNode : tree.get(Constants.VALUE)) {
            entitySet.getEntities()
                    .add(entityDeserializer.doDeserialize(jsonNode.traverse(parser.getCodec())).getPayload());
        }
        tree.remove(Constants.VALUE);
    }

    // any remaining entry is supposed to be an annotation or is ignored
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        if (field.getKey().charAt(0) == '@') {
            final Annotation annotation = new AnnotationImpl();
            annotation.setTerm(field.getKey().substring(1));

            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }
            entitySet.getAnnotations().add(annotation);
        }
    }

    return new ResWrap<EntitySet>(contextURL, metadataETag, entitySet);
}