private void checkNavigationPathSupported(final List<UriResource> resourceParts) throws ODataApplicationException { for (final UriResource resourceItem : resourceParts) { if (resourceItem.getKind() != UriResourceKind.complexProperty && resourceItem.getKind() != UriResourceKind.primitiveProperty && resourceItem.getKind() != UriResourceKind.navigationProperty && resourceItem.getKind() != UriResourceKind.entitySet && resourceItem.getKind() != UriResourceKind.value) throw new ODataJPAProcessorException(ODataJPAProcessorException.MessageKeys.NOT_SUPPORTED_RESOURCE_TYPE, HttpStatusCode.NOT_IMPLEMENTED, resourceItem.getKind().toString()); } }
private boolean isAction(final UriInfo uriInfo) { List<UriResource> uriResourceParts = uriInfo.getUriResourceParts(); return !uriResourceParts.isEmpty() && UriResourceKind.action == uriResourceParts.get(uriResourceParts.size() - 1).getKind(); }
private boolean isAction(final UriInfo uriInfo) { List<UriResource> uriResourceParts = uriInfo.getUriResourceParts(); return !uriResourceParts.isEmpty() && UriResourceKind.action == uriResourceParts.get(uriResourceParts.size() - 1).getKind(); }
/** * Finds an entity type from a navigation property */ public static EdmEntityType determineTargetEntityType(final List<UriResource> resources) { EdmEntityType targetEdmEntity = null; for (final UriResource resourceItem : resources) { if (resourceItem.getKind() == UriResourceKind.navigationProperty) { // first try the simple way like in the example targetEdmEntity = (EdmEntityType) ((UriResourceNavigation) resourceItem).getType(); } } return targetEdmEntity; }
Expression determineExpression() { for (final UriResource uriResource : member.getUriResourceParts()) { if (uriResource.getKind() == UriResourceKind.lambdaAny) return ((UriResourceLambdaAny) uriResource).getExpression(); else if (uriResource.getKind() == UriResourceKind.lambdaAll) return ((UriResourceLambdaAll) uriResource).getExpression(); } return null; } }
@Override public List<UriResource> getUriResourceParts() { final List<UriResource> result = new ArrayList<>(); final List<UriResource> source = parentMember.getMember().getResourcePath().getUriResourceParts(); for (int i = source.size() - 1; i > 0; i--) { if (source.get(i).getKind() == UriResourceKind.navigationProperty || source.get(i).getKind() == UriResourceKind.entitySet || (source.get(i) instanceof UriResourceProperty && ((UriResourceProperty) source.get(i)).isCollection())) { break; } result.add(0, source.get(i)); } return result; }
private boolean isAggregation(final UriInfoResource resourcePath) { return (resourcePath.getUriResourceParts().size() == 1 && resourcePath.getUriResourceParts().get(0).getKind() == UriResourceKind.count); }
public JPASerializer createSerializer(final ContentType responseFormat, final UriInfo uriInfo) throws ODataApplicationException, SerializerException { // Assumption: Type of last resource path item rules the type of the response final List<UriResource> resourceParts = uriInfo.getUriResourceParts(); final UriResource lastItem = resourceParts.get(resourceParts.size() - 1); final boolean isColletion = determineIsCollection(lastItem); return createSerializer(responseFormat, uriInfo, lastItem.getKind(), isColletion); }
private void validatePropertyOperations(final UriInfo uriInfo, final HttpMethod method) throws UriValidationException { final List<UriResource> parts = uriInfo.getUriResourceParts(); final UriResource last = !parts.isEmpty() ? parts.get(parts.size() - 1) : null; final UriResource previous = parts.size() > 1 ? parts.get(parts.size() - 2) : null; if (last != null && (last.getKind() == UriResourceKind.primitiveProperty || last.getKind() == UriResourceKind.complexProperty || (last.getKind() == UriResourceKind.value && previous != null && previous.getKind() == UriResourceKind.primitiveProperty))) { final EdmProperty property = ((UriResourceProperty) (last.getKind() == UriResourceKind.value ? previous : last)).getProperty(); if (method == HttpMethod.PATCH && property.isCollection()) { throw new UriValidationException("Attempt to patch collection property.", UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, method.toString()); } if (method == HttpMethod.DELETE && !property.isNullable()) { throw new UriValidationException("Attempt to delete non-nullable property.", UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, method.toString()); } } } }
private void validatePropertyOperations(final UriInfo uriInfo, final HttpMethod method) throws UriValidationException { final List<UriResource> parts = uriInfo.getUriResourceParts(); final UriResource last = !parts.isEmpty() ? parts.get(parts.size() - 1) : null; final UriResource previous = parts.size() > 1 ? parts.get(parts.size() - 2) : null; if (last != null && (last.getKind() == UriResourceKind.primitiveProperty || last.getKind() == UriResourceKind.complexProperty || (last.getKind() == UriResourceKind.value && previous != null && previous.getKind() == UriResourceKind.primitiveProperty))) { final EdmProperty property = ((UriResourceProperty) (last.getKind() == UriResourceKind.value ? previous : last)).getProperty(); if (method == HttpMethod.PATCH && property.isCollection()) { throw new UriValidationException("Attempt to patch collection property.", UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, method.toString()); } if (method == HttpMethod.DELETE && !property.isNullable()) { throw new UriValidationException("Attempt to delete non-nullable property.", UriValidationException.MessageKeys.UNSUPPORTED_HTTP_METHOD, method.toString()); } } } }
@Override public UriResourceEntitySet parseEntityId(final Edm edm, final String entityId, final String rawServiceRoot) throws DeserializerException { String oDataPath = entityId; if (rawServiceRoot != null && entityId.startsWith(rawServiceRoot)) { oDataPath = entityId.substring(rawServiceRoot.length()); } oDataPath = oDataPath.startsWith("/") ? oDataPath : "/" + oDataPath; try { final List<UriResource> uriResourceParts = new Parser(edm, new ODataImpl()).parseUri(oDataPath, null, null, rawServiceRoot).getUriResourceParts(); if (uriResourceParts.size() == 1 && uriResourceParts.get(0).getKind() == UriResourceKind.entitySet) { final UriResourceEntitySet entityUriResource = (UriResourceEntitySet) uriResourceParts.get(0); return entityUriResource; } throw new DeserializerException("Invalid entity binding link", MessageKeys.INVALID_ENTITY_BINDING_LINK, entityId); } catch (final ODataLibraryException e) { throw new DeserializerException("Invalid entity binding link", e, MessageKeys.INVALID_ENTITY_BINDING_LINK, entityId); } } }
@Override public UriResourceEntitySet parseEntityId(final Edm edm, final String entityId, final String rawServiceRoot) throws DeserializerException { String oDataPath = entityId; if (rawServiceRoot != null && entityId.startsWith(rawServiceRoot)) { oDataPath = entityId.substring(rawServiceRoot.length()); } oDataPath = oDataPath.startsWith("/") ? oDataPath : "/" + oDataPath; try { final List<UriResource> uriResourceParts = new Parser(edm, new ODataImpl()).parseUri(oDataPath, null, null, rawServiceRoot).getUriResourceParts(); if (uriResourceParts.size() == 1 && uriResourceParts.get(0).getKind() == UriResourceKind.entitySet) { final UriResourceEntitySet entityUriResource = (UriResourceEntitySet) uriResourceParts.get(0); return entityUriResource; } throw new DeserializerException("Invalid entity binding link", MessageKeys.INVALID_ENTITY_BINDING_LINK, entityId); } catch (final ODataLibraryException e) { throw new DeserializerException("Invalid entity binding link", e, MessageKeys.INVALID_ENTITY_BINDING_LINK, entityId); } } }
public static List<UriParameter> determineKeyPredicates(final UriResource uriResourceItem) throws ODataApplicationException { if (uriResourceItem instanceof UriResourceEntitySet) return ((UriResourceEntitySet) uriResourceItem).getKeyPredicates(); else if (uriResourceItem instanceof UriResourceNavigation) return ((UriResourceNavigation) uriResourceItem).getKeyPredicates(); else throw new ODataJPAQueryException(ODataJPAQueryException.MessageKeys.NOT_SUPPORTED_RESOURCE_TYPE, HttpStatusCode.BAD_REQUEST, uriResourceItem.getKind().name()); } }
private UriType getUriTypeForValue(final UriResource secondLastPathSegment) throws UriValidationException { UriType uriType; switch (secondLastPathSegment.getKind()) { case primitiveProperty: uriType = UriType.propertyPrimitiveValue; break; case entitySet: case navigationProperty: case singleton: uriType = UriType.mediaStream; break; case function: UriResourceFunction uriFunction = (UriResourceFunction) secondLastPathSegment; final EdmFunction function = uriFunction.getFunction(); uriType = function.getReturnType().getType().getKind() == EdmTypeKind.ENTITY ? UriType.mediaStream : UriType.propertyPrimitiveValue; break; default: throw new UriValidationException( "Unexpected kind in path segment before $value: " + secondLastPathSegment.getKind(), UriValidationException.MessageKeys.UNALLOWED_KIND_BEFORE_VALUE, secondLastPathSegment.toString()); } return uriType; }
private UriType getUriTypeForValue(final UriResource secondLastPathSegment) throws UriValidationException { UriType uriType; switch (secondLastPathSegment.getKind()) { case primitiveProperty: uriType = UriType.propertyPrimitiveValue; break; case entitySet: case navigationProperty: case singleton: uriType = UriType.mediaStream; break; case function: UriResourceFunction uriFunction = (UriResourceFunction) secondLastPathSegment; final EdmFunction function = uriFunction.getFunction(); uriType = function.getReturnType().getType().getKind() == EdmTypeKind.ENTITY ? UriType.mediaStream : UriType.propertyPrimitiveValue; break; default: throw new UriValidationException( "Unexpected kind in path segment before $value: " + secondLastPathSegment.getKind(), UriValidationException.MessageKeys.UNALLOWED_KIND_BEFORE_VALUE, secondLastPathSegment.toString()); } return uriType; }
@Override public void retrieveData(final ODataRequest request, final ODataResponse response, final ContentType responseFormat) throws ODataException { final UriResource uriResource = uriInfo.getUriResourceParts().get(0); if (uriResource instanceof UriResourceEntitySet) { final EntityCollection result = countEntities(request, response, uriInfo); createSuccessResponce(response, ContentType.TEXT_PLAIN, serializer.serialize(request, result)); } else { throw new ODataJPAProcessorException(ODataJPAProcessorException.MessageKeys.NOT_SUPPORTED_RESOURCE_TYPE, HttpStatusCode.NOT_IMPLEMENTED, uriResource.getKind().toString()); } }
private Property determineProperty(final EdmEntitySet targetEdmEntitySet, final EntityCollection result) { UriResourceProperty uriProperty = null; Property property = null; boolean found = false; List<Property> properties = result.getEntities().get(0).getProperties(); for (UriResource hop : uriInfo.getUriResourceParts()) { if (hop.getKind().equals(UriResourceKind.entitySet) && ((UriResourceEntitySet) hop).getEntitySet() == targetEdmEntitySet || hop.getKind().equals(UriResourceKind.navigationProperty) && ((UriResourceNavigation) hop).getType() == targetEdmEntitySet.getEntityType()) found = true; if (found && hop.getKind().equals(UriResourceKind.complexProperty)) { uriProperty = (UriResourceProperty) hop; property = getProperty(uriProperty.getProperty().getName(), properties); if (!uriProperty.isCollection() && property != null)// Here it is assumed that the collection is the last hop // anyhow properties = ((ComplexValue) property.getValue()).getValue(); } } return property; }
protected UriResourceKind getAggregationType(final VisitableExpression expression) { UriInfoResource member = null; if (expression != null && expression instanceof Binary) { if (((Binary) expression).getLeftOperand() instanceof JPAMemberOperator) member = ((JPAMemberOperator) ((Binary) expression).getLeftOperand()).getMember().getResourcePath(); else if (((Binary) expression).getRightOperand() instanceof JPAMemberOperator) member = ((JPAMemberOperator) ((Binary) expression).getRightOperand()).getMember().getResourcePath(); } else if (expression != null && expression instanceof JPAFilterExpression) member = ((JPAFilterExpression) expression).getMember(); if (member != null) { for (final UriResource r : member.getUriResourceParts()) { if (r.getKind() == UriResourceKind.count) return r.getKind(); } } return null; } }
@Override public JPAPath visitMember(final Member member) throws ExpressionVisitException, ODataApplicationException { final UriResourceKind uriResourceKind = member.getResourcePath().getUriResourceParts().get(0).getKind(); if (uriResourceKind == UriResourceKind.primitiveProperty || uriResourceKind == UriResourceKind.complexProperty) { if (!Util.hasNavigation(member.getResourcePath().getUriResourceParts())) { final String path = Util.determineProptertyNavigationPath(member.getResourcePath().getUriResourceParts()); JPAPath selectItemPath = null; try { selectItemPath = jpaEntityType.getPath(path); } catch (ODataJPAModelException e) { throw new ODataJPAFilterException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } if (selectItemPath != null) { pathList.add(selectItemPath); return selectItemPath; } } } return null; }