Java Code Examples for org.apache.olingo.commons.api.data.ContextURL#Builder

The following examples show how to use org.apache.olingo.commons.api.data.ContextURL#Builder . 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: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private String serialize(final EdmAssistedSerializer serializer, final ServiceMetadata metadata,
    final EdmEntitySet edmEntitySet, final AbstractEntityCollection entityCollection, final String selectList)
    throws SerializerException, IOException {
  ContextURL.Builder contextURLBuilder = ContextURL.with();
  contextURLBuilder = edmEntitySet == null ?
      contextURLBuilder.entitySetOrSingletonOrType("EntitySet") :
      contextURLBuilder.entitySet(edmEntitySet);
  if (selectList == null) {
    if (edmEntitySet == null) {
      StringBuilder names = new StringBuilder();
      for (final Property property : entityCollection.iterator().next().getProperties()) {
        names.append(names.length() > 0 ? ',' : "").append(property.getName());
      }
      contextURLBuilder = contextURLBuilder.selectList(names.toString());
    }
  } else {
    contextURLBuilder = contextURLBuilder.selectList(selectList);
  }
  return IOUtils.toString(
      serializer.entityCollection(metadata,
          edmEntitySet == null ? null : edmEntitySet.getEntityType(),
          entityCollection,
          EdmAssistedSerializerOptions.with().contextURL(contextURLBuilder.build()).build())
          .getContent());
}
 
Example 2
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public ContextURL getContextURL(OData odata) throws SerializerException {
  final UriHelper helper = odata.createUriHelper();
  EdmProperty edmProperty = getUriResourceProperty().getProperty();

  ContextURL.Builder builder =
      ContextURL.with().entitySetOrSingletonOrType(getTargetEntitySet(getEntitySet(), getNavigations()));
  builder.keyPath(helper.buildContextURLKeyPredicate(getUriResourceEntitySet()
      .getKeyPredicates()));
  String navPath = buildNavPath(helper, getEntitySet().getEntityType(), getNavigations(), true);
  if (navPath != null && !navPath.isEmpty()) {
    builder.navOrPropertyPath(navPath+"/"+edmProperty.getName());
  } else {
    builder.navOrPropertyPath(edmProperty.getName());
  }
  setServiceRoot(builder, getODataRequest());      
  if (isPropertyComplex()) {
    EdmComplexType complexType = ((UriResourceComplexProperty) uriResourceProperty).getComplexType();
    String select = helper.buildContextURLSelectList(complexType, getUriInfo().getExpandOption(),
        getUriInfo().getSelectOption());
    builder.selectList(select);
  }
  return builder.build();
}
 
Example 3
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ContextURL getContextURL(OData odata) throws SerializerException {
  // EntitySet based return
  final UriHelper helper = odata.createUriHelper();
  ContextURL.Builder builder = buildEntitySetContextURL(helper, getEntitySet(),
      getKeyPredicates(), getUriInfo(), getNavigations(), isCollection(), false, getODataRequest());
  return builder.build();
}
 
Example 4
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ContextURL getContextURL(OData odata) throws SerializerException {
  ContextURL.Builder builder = ContextURL.with().suffix(Suffix.REFERENCE);
  if (isCollection()) {
    builder.asCollection();
  }
  setServiceRoot(builder, getODataRequest());
  return builder.build();
}
 
Example 5
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ContextURL getContextURL(OData odata) throws SerializerException {
  final UriHelper helper = odata.createUriHelper();
  ContextURL.Builder builder = buildEntitySetContextURL(helper,
      uriResourceSingleton.getSingleton(), null, getUriInfo(), getNavigations(), isCollection(), true, 
      getODataRequest());
  return builder.build();
}
 
Example 6
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
static ContextURL.Builder buildEntitySetContextURL(UriHelper helper,
    EdmBindingTarget edmEntitySet, List<UriParameter> keyPredicates, UriInfo uriInfo,
    LinkedList<UriResourceNavigation> navigations, boolean collectionReturn, boolean singleton, ODataRequest request)
    throws SerializerException {

  ContextURL.Builder builder =
      ContextURL.with().entitySetOrSingletonOrType(getTargetEntitySet(edmEntitySet, navigations));
  String select = helper.buildContextURLSelectList(edmEntitySet.getEntityType(),
      uriInfo.getExpandOption(), uriInfo.getSelectOption());
  if (!singleton) {
    builder.suffix(collectionReturn ? null : Suffix.ENTITY);
  }

  setServiceRoot(builder, request);    
  builder.selectList(select);

  final UriInfoResource resource = uriInfo.asUriInfoResource();
  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final List<String> path = getPropertyPath(resourceParts);
  String propertyPath = buildPropertyPath(path);
  final String navPath = buildNavPath(helper, edmEntitySet.getEntityType(), navigations, collectionReturn);
  if (navPath != null && !navPath.isEmpty()) {
    if (keyPredicates != null) {
      builder.keyPath(helper.buildContextURLKeyPredicate(keyPredicates));
    }
    if (propertyPath != null) {
      propertyPath = navPath+"/"+propertyPath;
    } else {
      propertyPath = navPath;
    }
  }
  builder.navOrPropertyPath(propertyPath);
  return builder;
}
 
Example 7
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private static void setServiceRoot(ContextURL.Builder builder, ODataRequest request) {
  String serviceRoot = request.getRawBaseUri();
  if (serviceRoot != null) {
    try {
      if (!serviceRoot.endsWith("/")) {
        serviceRoot = serviceRoot + "/";
      }
      builder.serviceRoot(URI.create(serviceRoot));
    } catch (IllegalArgumentException e) {
      // ignore
    }
  }
}
 
Example 8
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public ContextURL getContextURL(OData odata) throws SerializerException {
  ContextURL.Builder builder = ContextURL.with().asCollection();
  return builder.build();
}
 
Example 9
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public ContextURL getContextURL(OData odata) throws SerializerException {
  ContextURL.Builder builder = ContextURL.with().asCollection();
  return builder.build();
}
 
Example 10
Source File: ContextURLParser.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static ContextURL parse(final URI contextURL) {
  if (contextURL == null) {
    return null;
  }

  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<>();
      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();
}