org.apache.olingo.server.api.ODataApplicationException Java Examples

The following examples show how to use org.apache.olingo.server.api.ODataApplicationException. 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: ExpressionVisitorImpl.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public VisitorOperand visitMember(Member member) throws ExpressionVisitException, ODataApplicationException {
    final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();
    int size = uriResourceParts.size();
    if (uriResourceParts.get(0) instanceof UriResourceProperty) {
        EdmProperty currentEdmProperty = ((UriResourceProperty) uriResourceParts.get(0)).getProperty();
        Property currentProperty = entity.getProperty(currentEdmProperty.getName());
        return new TypedOperand(currentProperty.getValue(), currentEdmProperty.getType(), currentEdmProperty);
    } else if (uriResourceParts.get(size - 1) instanceof UriResourceLambdaAll) {
        return throwNotImplemented();
    } else if (uriResourceParts.get(size - 1) instanceof UriResourceLambdaAny) {
        return throwNotImplemented();
    } else {
        return throwNotImplemented();
    }
}
 
Example #2
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method return entity by searching from the entity collection according to keys and etag.
 *
 * @param entityType       EdmEntityType
 * @param entityCollection EntityCollection
 * @param keys             keys
 * @return Entity
 * @throws ODataApplicationException
 * @throws ODataServiceFault
 */
private Entity getEntity(EdmEntityType entityType, EntityCollection entityCollection, List<UriParameter> keys)
        throws ODataApplicationException, ODataServiceFault {
    List<Entity> search = null;
    if (entityCollection.getEntities().isEmpty()) {
        if (log.isDebugEnabled()) {
            StringBuilder message = new StringBuilder();
            message.append("Entity collection was null , For ");
            for (UriParameter parameter : keys) {
                message.append(parameter.getName()).append(" = ").append(parameter.getText()).append(" ,");
            }
            message.append(".");
            log.debug(message);
        }
        return null;
    }
    for (UriParameter param : keys) {
        search = getMatch(entityType, param, entityCollection.getEntities());
    }
    if (search == null) {
        return null;
    } else {
        return search.get(0);
    }
}
 
Example #3
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns the object which is the value of the property.
 *
 * @param edmProperty EdmProperty
 * @param value       String value
 * @return Object
 * @throws ODataApplicationException
 */
private String readPrimitiveValueInString(EdmProperty edmProperty, Object value) throws ODataApplicationException {
    if (value == null) {
        return null;
    }
    try {
        EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
        return edmPrimitiveType.valueToString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
                                              edmProperty.getPrecision(), edmProperty.getScale(),
                                              edmProperty.isUnicode());
    } catch (EdmPrimitiveTypeException e) {
        throw new ODataApplicationException("Invalid value: " + value + " for property: " + edmProperty.getName(),
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                                            Locale.getDefault());
    }
}
 
Example #4
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns the object which is the value of the property.
 *
 * @param edmProperty EdmProperty
 * @param value       String value
 * @return Object
 * @throws ODataApplicationException
 */
private Object readPrimitiveValue(EdmProperty edmProperty, String value) throws ODataApplicationException {
    if (value == null) {
        return null;
    }
    try {
        if (value.startsWith("'") && value.endsWith("'")) {
            value = value.substring(1, value.length() - 1);
        }
        EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
        Class<?> javaClass = getJavaClassForPrimitiveType(edmProperty, edmPrimitiveType);
        return edmPrimitiveType.valueOfString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
                                              edmProperty.getPrecision(), edmProperty.getScale(),
                                              edmProperty.isUnicode(), javaClass);
    } catch (EdmPrimitiveTypeException e) {
        throw new ODataApplicationException("Invalid value: " + value + " for property: " + edmProperty.getName(),
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                                            Locale.getDefault());
    }
}
 
Example #5
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns matched entity list, where it uses in getEntity method to get the matched entity.
 *
 * @param entityType EdmEntityType
 * @param param      UriParameter
 * @param entityList List of entities
 * @return list of entities
 * @throws ODataApplicationException
 * @throws ODataServiceFault
 */
private List<Entity> getMatch(EdmEntityType entityType, UriParameter param, List<Entity> entityList)
        throws ODataApplicationException, ODataServiceFault {
    ArrayList<Entity> list = new ArrayList<>();
    for (Entity entity : entityList) {
        EdmProperty property = (EdmProperty) entityType.getProperty(param.getName());
        EdmType type = property.getType();
        if (type.getKind() == EdmTypeKind.PRIMITIVE) {
            Object match = readPrimitiveValue(property, param.getText());
            Property entityValue = entity.getProperty(param.getName());
            if (match != null) {
                if (match.equals(entityValue.asPrimitive())) {
                    list.add(entity);
                }
            } else {
                if (null == entityValue.asPrimitive()) {
                    list.add(entity);
                }
            }
        } else {
            throw new ODataServiceFault("Complex elements are not supported, couldn't compare complex objects.");
        }
    }
    return list;
}
 
Example #6
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method return the entity collection which are able to navigate from the parent entity (source) using uri navigation properties.
 * <p/>
 * In this method we check the parent entities primary keys and return the entity according to the values.
 * we use ODataDataHandler, navigation properties to get particular foreign keys.
 *
 * @param metadata     Service Metadata
 * @param parentEntity parentEntity
 * @param navigation   UriResourceNavigation
 * @return EntityCollection
 * @throws ODataServiceFault
 */
private EntityCollection getNavigableEntitySet(ServiceMetadata metadata, Entity parentEntity,
                                               EdmNavigationProperty navigation, String url)
        throws ODataServiceFault, ODataApplicationException {
    EdmEntityType type = metadata.getEdm().getEntityType(new FullQualifiedName(parentEntity.getType()));
    String linkName = navigation.getName();
    List<Property> properties = new ArrayList<>();
    Map<String, EdmProperty> propertyMap = new HashMap<>();
    for (NavigationKeys keys : this.dataHandler.getNavigationProperties().get(type.getName())
                                               .getNavigationKeys(linkName)) {
        Property property = parentEntity.getProperty(keys.getPrimaryKey());
        if (property != null && !property.isNull()) {
            propertyMap.put(keys.getForeignKey(), (EdmProperty) type.getProperty(property.getName()));
            property.setName(keys.getForeignKey());
            properties.add(property);
        }
    }
    if(!properties.isEmpty()) {
        return createEntityCollectionFromDataEntryList(linkName, dataHandler
                .readTableWithKeys(linkName, wrapPropertiesToDataEntry(type, properties, propertyMap)), url);
    }
    return null;
}
 
Example #7
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method applies server-side paging to the given entity collection.
 *
 * @param skipTokenOption   Current skip token option (from a previous response's next link)
 * @param entityCollection  Entity collection
 * @param edmEntitySet      EDM entity set to decide whether paging must be done
 * @param rawRequestUri     Request URI (used to construct the next link)
 * @param preferredPageSize Preference for page size
 * @return Chosen page size
 * @throws ODataApplicationException
 */
public static Integer applyServerSidePaging(final SkipTokenOption skipTokenOption,
                                            EntityCollection entityCollection, final EdmEntitySet edmEntitySet,
                                            final String rawRequestUri, final Integer preferredPageSize)
        throws ODataApplicationException {
    if (edmEntitySet != null) {
        final int pageSize = getPageSize(preferredPageSize);
        final int page = getPage(skipTokenOption);
        final int itemsToSkip = pageSize * page;
        if (itemsToSkip <= entityCollection.getEntities().size()) {
            popAtMost(entityCollection, itemsToSkip);
            final int remainingItems = entityCollection.getEntities().size();
            reduceToSize(entityCollection, pageSize);
            // Determine if a new next Link has to be provided.
            if (remainingItems > pageSize) {
                entityCollection.setNext(createNextLink(rawRequestUri, edmEntitySet, page + 1));
            }
        } else {
            throw new ODataApplicationException("Nothing found.", HttpStatusCode.NOT_FOUND.getStatusCode(),
                                                Locale.ROOT);
        }
        return pageSize;
    }
    return null;
}
 
Example #8
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method applies filter query option to the given entity collection.
 *
 * @param filterOption Filter option
 * @param entitySet    Entity collection
 * @param edmEntitySet Entity set
 * @throws ODataApplicationException
 */
public static void applyFilterSystemQuery(final FilterOption filterOption, final EntityCollection entitySet,
                                          final EdmBindingTarget edmEntitySet) throws ODataApplicationException {
    try {
        final Iterator<Entity> iter = entitySet.getEntities().iterator();
        while (iter.hasNext()) {
            final VisitorOperand operand =
                    filterOption.getExpression().accept(new ExpressionVisitorImpl(iter.next(), edmEntitySet));
            final TypedOperand typedOperand = operand.asTypedOperand();

            if (typedOperand.is(ODataConstants.primitiveBoolean)) {
                if (Boolean.FALSE.equals(typedOperand.getTypedValue(Boolean.class))) {
                    iter.remove();
                }
            } else {
                throw new ODataApplicationException(
                        "Invalid filter expression. Filter expressions must return a value of " +
                        "type Edm.Boolean", HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
            }
        }

    } catch (ExpressionVisitException e) {
        throw new ODataApplicationException("Exception in filter evaluation",
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
    }
}
 
Example #9
Source File: BinaryOperator.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private BigDecimal decimalArithmeticOperation(final BinaryOperatorKind operator) throws ODataApplicationException {
    final BigDecimal left = this.left.getTypedValue(BigDecimal.class);
    final BigDecimal right = this.right.getTypedValue(BigDecimal.class);
    switch (operator) {
        case ADD:
            return left.add(right);
        case DIV:
            return left.divide(right);
        case MUL:
            return left.multiply(right);
        case SUB:
            return left.subtract(right);
        default:
            throw new ODataApplicationException("Operator not valid", HttpStatusCode.BAD_REQUEST.getStatusCode(),
                                                Locale.ROOT);
    }
}
 
Example #10
Source File: BinaryOperator.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private BigInteger integerArithmeticOperation(final BinaryOperatorKind operator) throws ODataApplicationException {
    final BigInteger left = this.left.getTypedValue(BigInteger.class);
    final BigInteger right = this.right.getTypedValue(BigInteger.class);
    switch (operator) {
        case ADD:
            return left.add(right);
        case DIV:
            return left.divide(right);
        case MUL:
            return left.multiply(right);
        case SUB:
            return left.subtract(right);
        case MOD:
            return left.mod(right);
        default:
            throw new ODataApplicationException("Operator not valid", HttpStatusCode.BAD_REQUEST.getStatusCode(),
                                                Locale.ROOT);
    }
}
 
Example #11
Source File: UntypedOperand.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public TypedOperand asTypedOperand(final EdmPrimitiveType... types) throws ODataApplicationException {
    final String literal = (String) value;
    Object newValue;
    // First try the null literal
    if ((newValue = tryCast(literal, ODataConstants.primitiveNull)) != null) {
        return new TypedOperand(newValue, ODataConstants.primitiveNull);
    }
    // Than try the given types
    for (EdmPrimitiveType type : types) {
        newValue = tryCast(literal, type);
        if (newValue != null) {
            return new TypedOperand(newValue, type);
        }
    }
    throw new ODataApplicationException("Cast failed", HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                                        Locale.ROOT);
}
 
Example #12
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public void upsertEntity(DataRequest request, Entity entity, boolean merge, String entityETag,
                         EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    EdmEntitySet edmEntitySet = request.getEntitySet();
    String baseUrl = request.getODataRequest().getRawBaseUri();
    Entity currentEntity;
    try {
        currentEntity = getEntity(edmEntitySet.getEntityType(), request.getKeyPredicates(), baseUrl);
        if (currentEntity == null) {
            createEntity(request, entity, response);
        } else {
            updateEntity(request, entity, merge, entityETag, response);
        }
    } catch (ODataServiceFault e) {
        response.writeNotModified();
        log.error("Error occurred while upserting entity. :" + e.getMessage(), e);
    }
}
 
Example #13
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public void createEntity(DataRequest request, Entity entity, EntityResponse response)
        throws ODataApplicationException {
    EdmEntitySet edmEntitySet = request.getEntitySet();
    String baseURL = request.getODataRequest().getRawBaseUri();
    try {
        Entity created = createEntityInTable(edmEntitySet.getEntityType(), entity);
        entity.setId(new URI(ODataUtils.buildLocation(baseURL, created, edmEntitySet.getName(),
                                                      edmEntitySet.getEntityType())));
        response.writeCreatedEntity(edmEntitySet, created);
    } catch (ODataServiceFault | SerializerException | URISyntaxException | EdmPrimitiveTypeException e) {
        response.writeNotModified();
        String error = "Error occurred while creating entity. :" + e.getMessage();
        throw new ODataApplicationException(error, 500, Locale.ENGLISH);
    }
}
 
Example #14
Source File: BinaryOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public BinaryOperator(final VisitorOperand leftOperand, final VisitorOperand rightOperand)
        throws ODataApplicationException {
    left = leftOperand.asTypedOperand();
    right = rightOperand.asTypedOperand();
    left = left.castToCommonType(right);
    right = right.castToCommonType(left);
}
 
Example #15
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private List<String> getParametersAsString() throws ODataApplicationException {
    List<String> result = new ArrayList<>();
    for (VisitorOperand param : parameters) {
        TypedOperand operand = param.asTypedOperand();
        if (operand.isNull()) {
            result.add(null);
        } else if (operand.is(ODataConstants.primitiveString)) {
            result.add(operand.getTypedValue(String.class));
        } else {
            throw new ODataApplicationException("Invalid parameter. Expected Edm.String",
                                                HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
        }
    }
    return result;
}
 
Example #16
Source File: BinaryOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand orOperator() throws ODataApplicationException {
    Boolean result = null;
    if (left.is(ODataConstants.primitiveBoolean) && right.is(ODataConstants.primitiveBoolean)) {
        if (Boolean.TRUE.equals(left.getValue()) || Boolean.TRUE.equals(right.getValue())) {
            result = true;
        } else if (Boolean.FALSE.equals(left.getValue()) && Boolean.FALSE.equals(right.getValue())) {
            result = false;
        }
        return new TypedOperand(result, ODataConstants.primitiveBoolean);
    } else {
        throw new ODataApplicationException("Or operator needs two binary operands",
                                            HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
    }
}
 
Example #17
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand minute() throws ODataApplicationException {
    return dateFunction(new DateFunction() {
        @Override
        public Object perform(final Calendar calendar, final TypedOperand operand) {
            return calendar.get(Calendar.MINUTE);
        }
    }, ODataConstants.primitiveInt32, ODataConstants.primitiveDateTimeOffset, ODataConstants.primitiveTimeOfDay);
}
 
Example #18
Source File: BinaryOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand andOperator() throws ODataApplicationException {
    Boolean result = null;
    if (left.is(ODataConstants.primitiveBoolean) && right.is(ODataConstants.primitiveBoolean)) {
        if (Boolean.TRUE.equals(left.getValue()) && Boolean.TRUE.equals(right.getValue())) {
            result = true;
        } else if (Boolean.FALSE.equals(left.getValue()) || Boolean.FALSE.equals(right.getValue())) {
            result = false;
        }
        return new TypedOperand(result, ODataConstants.primitiveBoolean);
    } else {
        throw new ODataApplicationException("Add operator needs two binary operands",
                                            HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
    }
}
 
Example #19
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand toUpper() throws ODataApplicationException {
    return stringFunction(new StringFunction() {
        @Override
        public Object perform(final List<String> params) {
            return params.get(0).toUpperCase();
        }
    }, ODataConstants.primitiveString);
}
 
Example #20
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private VisitorOperand stringFunction(final StringFunction function, final EdmType returnValue)
        throws ODataApplicationException {
    List<String> stringParameters = getParametersAsString();
    if (stringParameters.contains(null)) {
        return new TypedOperand(null, EdmNull.getInstance());
    } else {
        return new TypedOperand(function.perform(stringParameters), returnValue);
    }
}
 
Example #21
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand hour() throws ODataApplicationException {
    return dateFunction(new DateFunction() {
        @Override
        public Object perform(final Calendar calendar, final TypedOperand operand) {
            return calendar.get(Calendar.HOUR_OF_DAY);
        }
    }, ODataConstants.primitiveInt32, ODataConstants.primitiveDateTimeOffset, ODataConstants.primitiveTimeOfDay);
}
 
Example #22
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand day() throws ODataApplicationException {
    return dateFunction(new DateFunction() {
        @Override
        public Object perform(final Calendar calendar, final TypedOperand operand) {
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
    }, ODataConstants.primitiveInt32, ODataConstants.primitiveDateTimeOffset, ODataConstants.primitiveDate);
}
 
Example #23
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand month() throws ODataApplicationException {
    return dateFunction(new DateFunction() {
        @Override
        public Object perform(final Calendar calendar, final TypedOperand operand) {
            // Month is 0-based!
            return calendar.get(Calendar.MONTH) + 1;
        }
    }, ODataConstants.primitiveInt32, ODataConstants.primitiveDateTimeOffset, ODataConstants.primitiveDate);
}
 
Example #24
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand year() throws ODataApplicationException {
    return dateFunction(new DateFunction() {
        @Override
        public Object perform(final Calendar calendar, final TypedOperand operand) {
            return calendar.get(Calendar.YEAR);
        }
    }, ODataConstants.primitiveInt32, ODataConstants.primitiveDateTimeOffset, ODataConstants.primitiveDate);
}
 
Example #25
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand length() throws ODataApplicationException {
    return stringFunction(new StringFunction() {
        @Override
        public Object perform(final List<String> params) {
            return params.get(0).length();
        }
    }, ODataConstants.primitiveInt32);
}
 
Example #26
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand concat() throws ODataApplicationException {
    return stringFunction(new StringFunction() {
        @Override
        public Object perform(final List<String> params) {
            return params.get(0) + params.get(1);
        }
    }, ODataConstants.primitiveString);
}
 
Example #27
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand contains() throws ODataApplicationException {
    return stringFunction(new StringFunction() {
        @Override
        public Object perform(final List<String> params) {
            return params.get(0).contains(params.get(1));
        }
    }, ODataConstants.primitiveBoolean);
}
 
Example #28
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand substring() throws ODataApplicationException {
    final TypedOperand valueOperand = parameters.get(0).asTypedOperand();
    final TypedOperand startOperand = parameters.get(1).asTypedOperand();
    if (valueOperand.isNull() || startOperand.isNull()) {
        return new TypedOperand(null, ODataConstants.primitiveString);
    } else if (valueOperand.is(ODataConstants.primitiveString) && startOperand.isIntegerType()) {
        final String value = valueOperand.getTypedValue(String.class);
        int start = Math.min(startOperand.getTypedValue(BigInteger.class).intValue(), value.length());
        start = start < 0 ? 0 : start;
        int end = value.length();
        if (parameters.size() == 3) {
            final TypedOperand lengthOperand = parameters.get(2).asTypedOperand();
            if (lengthOperand.isNull()) {
                return new TypedOperand(null, ODataConstants.primitiveString);
            } else if (lengthOperand.isIntegerType()) {
                end = Math.min(start + lengthOperand.getTypedValue(BigInteger.class).intValue(), value.length());
                end = end < 0 ? 0 : end;
            } else {
                throw new ODataApplicationException("Third substring parameter should be Edm.Int32",
                                                    HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
            }
        }
        return new TypedOperand(value.substring(start, end), ODataConstants.primitiveString);
    } else {
        throw new ODataApplicationException(
                "Substring has invalid parameters. First parameter should be Edm.String," +
                " second parameter should be Edm.Int32", HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
    }
}
 
Example #29
Source File: MethodCallOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand trim() throws ODataApplicationException {
    return stringFunction(new StringFunction() {
        @Override
        public Object perform(final List<String> params) {
            return params.get(0).trim();
        }
    }, ODataConstants.primitiveString);
}
 
Example #30
Source File: UnaryOperator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public VisitorOperand notOperation() throws ODataApplicationException {
    if (operand.isNull()) {
        return operand;
    } else if (operand.is(ODataConstants.primitiveBoolean)) {
        return new TypedOperand(!operand.getTypedValue(Boolean.class), operand.getType());
    } else {
        throw new ODataApplicationException("Unsupported type", HttpStatusCode.BAD_REQUEST.getStatusCode(),
                                            Locale.ROOT);
    }
}