[OLINGO-260] provided primitive keys integration test on proxy + some refactoring ... still missing EdmTime review: what about BigDecimal in place of Duration?
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/22f6a6a2 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/22f6a6a2 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/22f6a6a2 Branch: refs/heads/master Commit: 22f6a6a2ad96adbb14875f4f9020caa9d00ec9e0 Parents: ad77086 Author: fmartelli <[email protected]> Authored: Tue May 13 18:59:14 2014 +0200 Committer: Stephan Klevenz <[email protected]> Committed: Mon May 19 14:32:45 2014 +0200 ---------------------------------------------------------------------- .../commons/AbstractInvocationHandler.java | 58 ++-- .../proxy/commons/ComplexInvocationHandler.java | 47 +-- .../proxy/commons/EntityInvocationHandler.java | 44 +-- .../olingo/ext/proxy/utils/CoreUtils.java | 289 +++++-------------- fit/src/it/primitiveKeysServiceV3/pom.xml | 2 +- .../org/apache/olingo/fit/AbstractServices.java | 7 +- .../olingo/fit/proxy/v3/AbstractTestITCase.java | 3 + .../olingo/fit/proxy/v4/PropertyTestITCase.java | 8 +- .../client/core/v3/PrimitiveValueTest.java | 14 +- .../core/edm/primitivetype/EdmDuration.java | 1 + 10 files changed, 118 insertions(+), 355 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractInvocationHandler.java ---------------------------------------------------------------------- diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractInvocationHandler.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractInvocationHandler.java index e3a2bc9..9da6aab 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractInvocationHandler.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/AbstractInvocationHandler.java @@ -26,6 +26,7 @@ import java.lang.reflect.Proxy; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -49,22 +50,22 @@ import org.apache.olingo.ext.proxy.api.annotations.Parameter; import org.apache.olingo.ext.proxy.utils.ClassUtils; import org.apache.olingo.ext.proxy.utils.CoreUtils; -abstract class AbstractInvocationHandler implements InvocationHandler { +abstract class AbstractInvocationHandler<C extends CommonEdmEnabledODataClient<?>> implements InvocationHandler { private static final long serialVersionUID = 358520026931462958L; - protected final CommonEdmEnabledODataClient<?> client; + protected final C client; - protected EntityContainerInvocationHandler containerHandler; + protected EntityContainerInvocationHandler<C> containerHandler; protected AbstractInvocationHandler( - final CommonEdmEnabledODataClient<?> client, final EntityContainerInvocationHandler containerHandler) { + final C client, final EntityContainerInvocationHandler<C> containerHandler) { this.client = client; this.containerHandler = containerHandler; } - protected CommonEdmEnabledODataClient<?> getClient() { + protected C getClient() { return client; } @@ -87,7 +88,7 @@ abstract class AbstractInvocationHandler implements InvocationHandler { } @SuppressWarnings({"unchecked", "rawtypes"}) - protected Object getEntityCollectionProxy( + protected Object getEntityCollection( final Class<?> typeRef, final Class<?> typeCollectionRef, final String entityContainerName, @@ -99,47 +100,36 @@ abstract class AbstractInvocationHandler implements InvocationHandler { for (CommonODataEntity entityFromSet : entitySet.getEntities()) { items.add(getEntityProxy( - entityFromSet.getEditLink(), entityFromSet, entityContainerName, null, typeRef, checkInTheContext)); + entityFromSet, entityContainerName, null, typeRef, checkInTheContext)); } return Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class<?>[] {typeCollectionRef}, - new EntityCollectionInvocationHandler(containerHandler, items, typeRef, uri)); + new EntityCollectionInvocationHandler(containerHandler, items, typeRef, entityContainerName, uri)); } - protected Object getEntitySetProxy( - final Class<?> typeRef, - final URI uri) { - - return Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class<?>[] {typeRef}, - EntitySetInvocationHandler.getInstance(typeRef, containerHandler, uri)); - } - - protected Object getEntityProxy( - final URI entityURI, + protected <T> T getEntityProxy( final CommonODataEntity entity, final String entityContainerName, - final URI entitySetURI, + final String entitySetName, final Class<?> type, final boolean checkInTheContext) { - return getEntityProxy(entityURI, entity, entityContainerName, entitySetURI, type, null, checkInTheContext); + return getEntityProxy(entity, entityContainerName, entitySetName, type, null, checkInTheContext); } - protected Object getEntityProxy( - final URI entityURI, + @SuppressWarnings({"unchecked"}) + protected <T> T getEntityProxy( final CommonODataEntity entity, final String entityContainerName, - final URI entitySetURI, + final String entitySetName, final Class<?> type, final String eTag, final boolean checkInTheContext) { - EntityInvocationHandler handler = - EntityInvocationHandler.getInstance(entityURI, entity, entitySetURI, type, containerHandler); + EntityTypeInvocationHandler<C> handler = (EntityTypeInvocationHandler<C>) EntityTypeInvocationHandler.getInstance( + entity, entitySetName, type, containerHandler); if (StringUtils.isNotBlank(eTag)) { // override ETag into the wrapped object. @@ -147,10 +137,11 @@ abstract class AbstractInvocationHandler implements InvocationHandler { } if (checkInTheContext && EntityContainerFactory.getContext().entityContext().isAttached(handler)) { - handler = EntityContainerFactory.getContext().entityContext().getEntity(handler.getUUID()); + handler = (EntityTypeInvocationHandler<C>) EntityContainerFactory.getContext().entityContext(). + getEntity(handler.getUUID()); } - return Proxy.newProxyInstance( + return (T) Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class<?>[] {type}, handler); @@ -166,7 +157,7 @@ abstract class AbstractInvocationHandler implements InvocationHandler { IllegalArgumentException, InvocationTargetException { // 1. invoke params (if present) - final Map<String, ODataValue> parameterValues = new LinkedHashMap<String, ODataValue>(); + final Map<String, ODataValue> parameterValues = new HashMap<String, ODataValue>(); if (!parameters.isEmpty()) { for (Map.Entry<Parameter, Object> parameter : parameters.entrySet()) { @@ -202,12 +193,12 @@ abstract class AbstractInvocationHandler implements InvocationHandler { final EdmTypeInfo edmType = new EdmTypeInfo.Builder(). setEdm(client.getCachedEdm()).setTypeExpression(annotation.returnType()).build(); - + if (edmType.isEntityType()) { if (edmType.isCollection()) { final ParameterizedType collType = (ParameterizedType) method.getReturnType().getGenericInterfaces()[0]; final Class<?> collItemType = (Class<?>) collType.getActualTypeArguments()[0]; - return getEntityCollectionProxy( + return getEntityCollection( collItemType, method.getReturnType(), null, @@ -216,14 +207,13 @@ abstract class AbstractInvocationHandler implements InvocationHandler { false); } else { return getEntityProxy( - ((CommonODataEntity) result).getEditLink(), (CommonODataEntity) result, null, null, method.getReturnType(), false); } - } else { + }else{ return CoreUtils.getValueFromProperty(client, (CommonODataProperty) result, method.getGenericReturnType(), null); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ComplexInvocationHandler.java ---------------------------------------------------------------------- diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ComplexInvocationHandler.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ComplexInvocationHandler.java index 28bf280..fe645ae 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ComplexInvocationHandler.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ComplexInvocationHandler.java @@ -20,7 +20,6 @@ package org.apache.olingo.ext.proxy.commons; import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.ArrayList; @@ -32,7 +31,6 @@ import org.apache.olingo.client.api.CommonEdmEnabledODataClient; import org.apache.olingo.commons.api.domain.CommonODataProperty; import org.apache.olingo.commons.api.domain.ODataComplexValue; import org.apache.olingo.commons.api.domain.ODataLinked; -import org.apache.olingo.commons.api.domain.ODataValue; import org.apache.olingo.commons.api.edm.EdmElement; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.core.edm.EdmTypeInfo; @@ -77,7 +75,7 @@ public class ComplexTypeInvocationHandler<C extends CommonEdmEnabledODataClient< } @SuppressWarnings({"unchecked", "rawtypes"}) - static ComplexTypeInvocationHandler<?> getInstance( + public static ComplexTypeInvocationHandler<?> getInstance( final CommonEdmEnabledODataClient<?> client, final ODataComplexValue<?> complex, final Class<?> typeRef, @@ -112,48 +110,7 @@ public class ComplexTypeInvocationHandler<C extends CommonEdmEnabledODataClient< @Override protected Object getPropertyValue(final String name, final Type type) { try { - final Object res; - - final CommonODataProperty property = getComplex().get(name); - if (property == null) { - res = null; - } else if (property.hasComplexValue()) { - res = Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class<?>[] {(Class<?>) type}, - ComplexTypeInvocationHandler.getInstance( - client, property.getValue().asComplex(), (Class<?>) type, targetHandler)); - - } else if (property.hasCollectionValue()) { - final ParameterizedType collType = (ParameterizedType) type; - final Class<?> collItemClass = (Class<?>) collType.getActualTypeArguments()[0]; - - final ArrayList<Object> collection = new ArrayList<Object>(); - - final Iterator<ODataValue> collPropItor = property.getValue().asCollection().iterator(); - while (collPropItor.hasNext()) { - final ODataValue value = collPropItor.next(); - if (value.isPrimitive()) { - collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive())); - } else if (value.isComplex()) { - final Object collItem = Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class<?>[] {collItemClass}, - ComplexTypeInvocationHandler.getInstance( - client, value.asComplex(), collItemClass, targetHandler)); - - collection.add(collItem); - } - } - - res = collection; - } else { - res = type == null - ? CoreUtils.getValueFromProperty(client, property) - : CoreUtils.getValueFromProperty(client, property, type); - } - - return res; + return CoreUtils.getValueFromProperty(client, getComplex().get(name), type, targetHandler); } catch (Exception e) { throw new IllegalArgumentException("Error getting value for property '" + name + "'", e); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityInvocationHandler.java ---------------------------------------------------------------------- diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityInvocationHandler.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityInvocationHandler.java index 0e093b4..9f81634 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityInvocationHandler.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityInvocationHandler.java @@ -21,15 +21,11 @@ package org.apache.olingo.ext.proxy.commons; import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.net.URI; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.commons.io.IOUtils; @@ -40,7 +36,6 @@ import org.apache.olingo.client.core.uri.URIUtils; import org.apache.olingo.commons.api.domain.CommonODataEntity; import org.apache.olingo.commons.api.domain.CommonODataProperty; import org.apache.olingo.commons.api.domain.ODataLinked; -import org.apache.olingo.commons.api.domain.ODataValue; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.ODataMediaFormat; @@ -194,45 +189,8 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? Object res; if (propertyChanges.containsKey(name)) { res = propertyChanges.get(name); - } else if (property == null) { - res = null; - } else if (property.hasComplexValue()) { - res = Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class<?>[] {(Class<?>) type}, - ComplexTypeInvocationHandler.getInstance( - client, property.getValue().asComplex(), (Class<?>) type, this)); - - addPropertyChanges(name, res); - } else if (property.hasCollectionValue()) { - final ParameterizedType collType = (ParameterizedType) type; - final Class<?> collItemClass = (Class<?>) collType.getActualTypeArguments()[0]; - - final ArrayList<Object> collection = new ArrayList<Object>(); - - final Iterator<ODataValue> collPropItor = property.getValue().asCollection().iterator(); - while (collPropItor.hasNext()) { - final ODataValue value = collPropItor.next(); - if (value.isPrimitive()) { - collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive())); - } else if (value.isComplex()) { - final Object collItem = Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class<?>[] {collItemClass}, - ComplexTypeInvocationHandler.getInstance( - client, value.asComplex(), collItemClass, this)); - - collection.add(collItem); - } - } - - res = collection; - - addPropertyChanges(name, res); } else { - res = type == null - ? CoreUtils.getValueFromProperty(client, property) - : CoreUtils.getValueFromProperty(client, property, type); + res = CoreUtils.getValueFromProperty(client, property, type, this); if (res != null) { addPropertyChanges(name, res); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java ---------------------------------------------------------------------- diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java index 3c4843a..981d285 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/utils/CoreUtils.java @@ -33,36 +33,25 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.olingo.client.api.CommonEdmEnabledODataClient; import org.apache.olingo.client.api.v3.UnsupportedInV3Exception; -import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.domain.CommonODataEntity; import org.apache.olingo.commons.api.domain.CommonODataProperty; import org.apache.olingo.commons.api.domain.ODataLink; import org.apache.olingo.commons.api.domain.ODataPrimitiveValue; import org.apache.olingo.commons.api.domain.ODataValue; -import org.apache.olingo.commons.api.domain.v4.ODataEnumValue; -import org.apache.olingo.commons.api.domain.v4.ODataObjectFactory; -import org.apache.olingo.commons.api.domain.v4.ODataProperty; import org.apache.olingo.commons.api.edm.EdmElement; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; -import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.core.edm.EdmTypeInfo; -import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; -import org.apache.olingo.ext.proxy.api.annotations.ComplexType; import org.apache.olingo.ext.proxy.api.annotations.CompoundKeyElement; -import org.apache.olingo.ext.proxy.api.annotations.EnumType; import org.apache.olingo.ext.proxy.api.annotations.Key; -import org.apache.olingo.ext.proxy.api.annotations.Namespace; import org.apache.olingo.ext.proxy.api.annotations.Property; -import org.apache.olingo.ext.proxy.commons.AbstractStructuredInvocationHandler; -import org.apache.olingo.ext.proxy.commons.ComplexInvocationHandler; -import org.apache.olingo.ext.proxy.commons.EntityInvocationHandler; +import org.apache.olingo.ext.proxy.commons.AbstractTypeInvocationHandler; +import org.apache.olingo.ext.proxy.commons.ComplexTypeInvocationHandler; +import org.apache.olingo.ext.proxy.commons.EntityTypeInvocationHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -102,35 +91,42 @@ public final class CoreUtils { } } else { - throw new UnsupportedOperationException("Unsupported object type " + intType.getFullQualifiedName()); + throw new UnsupportedOperationException("Usupported object type " + intType.getFullQualifiedName()); } } } else if (type.isComplexType()) { - Object objHandler; + value = client.getObjectFactory().newComplexValue(type.getFullQualifiedName().toString()); + + final Object oo; if (obj instanceof Proxy) { - objHandler = Proxy.getInvocationHandler(obj); + oo = Proxy.getInvocationHandler(obj); } else { - objHandler = obj; + oo = obj; } - if (objHandler instanceof ComplexInvocationHandler) { - value = ((ComplexInvocationHandler) objHandler).getComplex(); - final Class<?> typeRef = ((ComplexInvocationHandler) objHandler).getTypeRef(); + if (oo instanceof ComplexTypeInvocationHandler<?>) { + final Class<?> typeRef = ((ComplexTypeInvocationHandler<?>) oo).getTypeRef(); + final Object complex = Proxy.newProxyInstance( + Thread.currentThread().getContextClassLoader(), + new Class<?>[] {typeRef}, + (ComplexTypeInvocationHandler<?>) oo); + for (Method method : typeRef.getMethods()) { - final Property propAnn = method.getAnnotation(Property.class); - if (propAnn != null) { - try { + final Property complexPropertyAnn = method.getAnnotation(Property.class); + try { + if (complexPropertyAnn != null) { value.asComplex().add(getODataComplexProperty( - client, type.getFullQualifiedName(), propAnn.name(), method.invoke(objHandler))); - } catch (Exception ignore) { - // ignore value - LOG.warn("Error attaching complex {} for field '{}.{}'", - type.getFullQualifiedName(), typeRef.getName(), propAnn.name(), ignore); + client, type.getFullQualifiedName(), complexPropertyAnn.name(), method.invoke(complex))); } + } catch (Exception ignore) { + // ignore value + LOG.warn("Error attaching complex {} for field '{}.{}'", + type.getFullQualifiedName(), typeRef.getName(), complexPropertyAnn.name(), ignore); } } } else { - throw new IllegalArgumentException(objHandler.getClass().getName() + "' is not a complex value"); + throw new IllegalArgumentException( + "Object '" + oo.getClass().getSimpleName() + "' is not a complex value"); } } else if (type.isEnumType()) { if (client.getServiceVersion().compareTo(ODataServiceVersion.V30) <= 0) { @@ -173,18 +169,12 @@ public final class CoreUtils { final String property, final Object obj) { - final EdmTypeInfo type; - if (edmProperty == null) { - // maybe opentype ... - type = null; - } else { - final EdmType edmType = edmProperty.getType(); + final EdmType edmType = edmProperty.getType(); - type = new EdmTypeInfo.Builder().setEdm(client.getCachedEdm()).setTypeExpression( - edmProperty.isCollection() - ? "Collection(" + edmType.getFullQualifiedName().toString() + ")" - : edmType.getFullQualifiedName().toString()).build(); - } + final EdmTypeInfo type = new EdmTypeInfo.Builder().setEdm(client.getCachedEdm()).setTypeExpression( + edmProperty.isCollection() + ? "Collection(" + edmType.getFullQualifiedName().toString() + ")" + : edmType.getFullQualifiedName().toString()).build(); return getODataProperty(client, property, type, obj); } @@ -195,39 +185,28 @@ public final class CoreUtils { CommonODataProperty oprop; try { - if (obj == null) { + if (type == null || obj == null) { oprop = client.getObjectFactory().newPrimitiveProperty(name, null); - } else { - final EdmTypeInfo valueType; - if (type == null) { - valueType = guessTypeFromObject(client, obj); - } else { - valueType = type; - } - - if (valueType.isCollection()) { - // create collection property - oprop = client.getObjectFactory().newCollectionProperty(name, getODataValue(client, valueType, obj). - asCollection()); - } else if (valueType.isPrimitiveType()) { - // create a primitive property - oprop = client.getObjectFactory().newPrimitiveProperty(name, getODataValue(client, valueType, obj). - asPrimitive()); - } else if (valueType.isComplexType()) { - // create a complex property - oprop = client.getObjectFactory().newComplexProperty(name, getODataValue(client, valueType, obj). - asComplex()); - } else if (valueType.isEnumType()) { - if (client.getServiceVersion().compareTo(ODataServiceVersion.V30) <= 0) { - throw new UnsupportedInV3Exception(); - } else { - oprop = ((ODataObjectFactory) client.getObjectFactory()).newEnumProperty(name, - ((org.apache.olingo.commons.api.domain.v4.ODataValue) getODataValue(client, valueType, obj)). - asEnum()); - } + } else if (type.isCollection()) { + // create collection property + oprop = client.getObjectFactory().newCollectionProperty(name, getODataValue(client, type, obj).asCollection()); + } else if (type.isPrimitiveType()) { + // create a primitive property + oprop = client.getObjectFactory().newPrimitiveProperty(name, getODataValue(client, type, obj).asPrimitive()); + } else if (type.isComplexType()) { + // create a complex property + oprop = client.getObjectFactory().newComplexProperty(name, getODataValue(client, type, obj).asComplex()); + } else if (type.isEnumType()) { + if (client.getServiceVersion().compareTo(ODataServiceVersion.V30) <= 0) { + throw new UnsupportedInV3Exception(); } else { - throw new UnsupportedOperationException("Usupported object type " + valueType.getFullQualifiedName()); + oprop = ((org.apache.olingo.commons.api.domain.v4.ODataObjectFactory) client.getObjectFactory()). + newEnumProperty(name, + ((org.apache.olingo.commons.api.domain.v4.ODataValue) getODataValue(client, type, obj)). + asEnum()); } + } else { + throw new UnsupportedOperationException("Usupported object type " + type.getFullQualifiedName()); } return oprop; @@ -236,51 +215,6 @@ public final class CoreUtils { } } - private static EdmTypeInfo guessTypeFromObject( - final CommonEdmEnabledODataClient<?> client, final Object obj) { - - final EdmTypeInfo.Builder edmTypeInfo = new EdmTypeInfo.Builder().setEdm(client.getCachedEdm()); - - if (Collection.class.isAssignableFrom(obj.getClass())) { - final EdmTypeInfo type = guessPrimitiveType(client, ClassUtils.extractTypeArg(obj.getClass())); - return edmTypeInfo.setTypeExpression("Collection(" + type.getFullQualifiedName() + ")").build(); - } else if (obj instanceof Proxy) { - final Class<?> typeRef = obj.getClass().getInterfaces()[0]; - final String ns = typeRef.getAnnotation(Namespace.class).value(); - final String name = typeRef.getAnnotation(ComplexType.class).name(); - return edmTypeInfo.setTypeExpression(new FullQualifiedName(ns, name).toString()).build(); - } else if (obj.getClass().getAnnotation(EnumType.class) != null) { - final Class<?> typeRef = obj.getClass(); - final String ns = typeRef.getAnnotation(Namespace.class).value(); - final String name = typeRef.getAnnotation(EnumType.class).name(); - return edmTypeInfo.setTypeExpression(new FullQualifiedName(ns, name).toString()).build(); - } else { - return guessPrimitiveType(client, obj.getClass()); - } - } - - private static EdmTypeInfo guessPrimitiveType(final CommonEdmEnabledODataClient<?> client, final Class<?> clazz) { - EdmPrimitiveTypeKind bckCandidate = null; - - for (EdmPrimitiveTypeKind kind : EdmPrimitiveTypeKind.values()) { - if (kind.getSupportedVersions().contains(client.getServiceVersion())) { - final Class<?> target = EdmPrimitiveTypeFactory.getInstance(kind).getDefaultType(); - - if (clazz.equals(target)) { - return new EdmTypeInfo.Builder().setEdm(client.getCachedEdm()).setTypeExpression(kind.toString()).build(); - } else if (target.isAssignableFrom(clazz)) { - bckCandidate = kind; - } - } - } - - if (bckCandidate == null) { - throw new IllegalArgumentException(clazz.getSimpleName() + " is not a simple type"); - } else { - return new EdmTypeInfo.Builder().setEdm(client.getCachedEdm()).setTypeExpression(bckCandidate.toString()).build(); - } - } - @SuppressWarnings("unchecked") public static void addProperties( final CommonEdmEnabledODataClient<?> client, @@ -299,28 +233,15 @@ public final class CoreUtils { } } - @SuppressWarnings({"unchecked", "rawtypes"}) - private static Enum<?> enumValueToObject(final ODataEnumValue value, final Class<?> reference) { - final Namespace namespace = reference.getAnnotation(Namespace.class); - final EnumType enumType = reference.getAnnotation(EnumType.class); - if (value.getTypeName().equals(namespace.value() + "." + enumType.name())) { - return Enum.valueOf((Class<Enum>) reference, value.getValue()); - } - - return null; - } - - private static Object primitiveValueToObject(final ODataPrimitiveValue value, final Class<?> reference) { + public static Object primitiveValueToObject(final ODataPrimitiveValue value) { Object obj; try { obj = value.toValue() instanceof Timestamp ? value.toCastValue(Calendar.class) - : reference == null - ? value.toValue() - : value.toCastValue(reference); + : value.toValue(); } catch (EdmPrimitiveTypeException e) { - LOG.warn("While casting primitive value {} to {}", value, reference, e); + LOG.warn("Could not read temporal value as Calendar, reverting to Timestamp", e); obj = value.toValue(); } @@ -335,19 +256,6 @@ public final class CoreUtils { bean.getClass().getMethod(setterName, getter.getReturnType()).invoke(bean, value); } - private static Class<?> getPropertyClass(final Class<?> entityClass, final String propertyName) { - Class<?> propertyClass = null; - try { - final Method getter = entityClass.getMethod("get" + StringUtils.capitalize(propertyName)); - if (getter != null) { - propertyClass = getter.getReturnType(); - } - } catch (Exception e) { - LOG.error("Could not determine the Java type of {}", propertyName, e); - } - return propertyClass; - } - public static Object getKey( final CommonEdmEnabledODataClient<?> client, final Class<?> entityTypeRef, final CommonODataEntity entity) { @@ -358,8 +266,7 @@ public final class CoreUtils { if (keyRef == null) { final CommonODataProperty property = entity.getProperty(firstValidEntityKey(entityTypeRef)); if (property != null && property.hasPrimitiveValue()) { - res = primitiveValueToObject( - property.getPrimitiveValue(), getPropertyClass(entityTypeRef, property.getName())); + res = primitiveValueToObject(property.getPrimitiveValue()); } } else { try { @@ -385,8 +292,8 @@ public final class CoreUtils { final Class<?> typeRef; if (bean instanceof Proxy) { final InvocationHandler handler = Proxy.getInvocationHandler(bean); - if (handler instanceof AbstractStructuredInvocationHandler) { - typeRef = ((ComplexInvocationHandler) handler).getTypeRef(); + if (handler instanceof AbstractTypeInvocationHandler) { + typeRef = ((ComplexTypeInvocationHandler<?>) handler).getTypeRef(); } else { throw new IllegalStateException("Invalid bean " + bean); } @@ -419,13 +326,12 @@ public final class CoreUtils { if (property.hasNullValue()) { setPropertyValue(bean, getter, null); } else if (property.hasPrimitiveValue()) { - setPropertyValue(bean, getter, primitiveValueToObject( - property.getPrimitiveValue(), getPropertyClass(reference, property.getName()))); + setPropertyValue(bean, getter, primitiveValueToObject(property.getPrimitiveValue())); } else if (property.hasComplexValue()) { final Object complex = Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class<?>[] {getter.getReturnType()}, - ComplexInvocationHandler.getInstance( + ComplexTypeInvocationHandler.getInstance( client, property.getName(), getter.getReturnType(), null)); populate(client, complex, Property.class, property.getValue().asComplex().iterator()); @@ -444,13 +350,12 @@ public final class CoreUtils { while (collPropItor.hasNext()) { final ODataValue value = collPropItor.next(); if (value.isPrimitive()) { - collection.add(primitiveValueToObject( - value.asPrimitive(), getPropertyClass(reference, property.getName()))); + collection.add(primitiveValueToObject(value.asPrimitive())); } else if (value.isComplex()) { final Object collItem = Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class<?>[] {collItemClass}, - ComplexInvocationHandler.getInstance( + ComplexTypeInvocationHandler.getInstance( client, property.getName(), collItemClass, null)); populate(client, collItem, Property.class, value.asComplex().iterator()); @@ -471,9 +376,11 @@ public final class CoreUtils { final CommonEdmEnabledODataClient<?> client, final CommonODataProperty property, final Type typeRef, - final EntityInvocationHandler entityHandler) + final EntityTypeInvocationHandler<?> entityHandler) throws InstantiationException, IllegalAccessException { + final Object res; + Class<?> internalRef; if (typeRef == null) { internalRef = null; @@ -485,18 +392,15 @@ public final class CoreUtils { } } - final Object res; - if (property == null || property.hasNullValue()) { res = null; } else if (property.hasComplexValue()) { - // complex types supports inheritance in V4, best to re-read actual type - internalRef = getComplexTypeRef(property); res = Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class<?>[] {internalRef}, - ComplexInvocationHandler.getInstance( + ComplexTypeInvocationHandler.getInstance( client, property.getValue().asComplex(), internalRef, entityHandler)); + } else if (property.hasCollectionValue()) { final ArrayList<Object> collection = new ArrayList<Object>(); @@ -504,13 +408,12 @@ public final class CoreUtils { while (collPropItor.hasNext()) { final ODataValue value = collPropItor.next(); if (value.isPrimitive()) { - collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive(), internalRef)); + collection.add(CoreUtils.primitiveValueToObject(value.asPrimitive())); } else if (value.isComplex()) { - internalRef = getComplexTypeRef(property); final Object collItem = Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class<?>[] {internalRef}, - ComplexInvocationHandler.getInstance( + ComplexTypeInvocationHandler.getInstance( client, value.asComplex(), internalRef, entityHandler)); collection.add(collItem); @@ -518,61 +421,13 @@ public final class CoreUtils { } res = collection; - } else if (property instanceof ODataProperty && ((ODataProperty) property).hasEnumValue()) { - if (internalRef == null) { - internalRef = getEnumTypeRef(property); - } - res = enumValueToObject(((ODataProperty) property).getEnumValue(), internalRef); } else { - res = primitiveValueToObject(property.getPrimitiveValue(), internalRef); + res = CoreUtils.primitiveValueToObject(property.getPrimitiveValue()); } return res; } - private static Class<?> getEnumTypeRef(final CommonODataProperty property) { - return getTypeRef(property, "META-INF/" + Constants.PROXY_ENUM_CLASS_LIST, EnumType.class); - } - - private static Class<?> getComplexTypeRef(final CommonODataProperty property) { - return getTypeRef(property, "META-INF/" + Constants.PROXY_COMPLEX_CLASS_LIST, ComplexType.class); - } - - private static Class<?> getTypeRef( - final CommonODataProperty property, - final String proxyClassListFile, - final Class<? extends Annotation> annType) { - - if (!annType.isAssignableFrom(EnumType.class) && !annType.isAssignableFrom(ComplexType.class)) { - throw new IllegalArgumentException("Invalid annotation type " + annType); - } - - try { - final List<String> pkgs = IOUtils.readLines( - CoreUtils.class.getClassLoader().getResourceAsStream(proxyClassListFile), - Constants.UTF8); - - for (String pkg : pkgs) { - final Class<?> clazz = Class.forName(pkg); - final Annotation ann = clazz.getAnnotation(annType); - final Namespace ns = clazz.getAnnotation(Namespace.class); - - if (ns != null && ann != null) { - if (property.getValue().getTypeName().replaceAll("^Collection\\(", "").replaceAll("\\)$", "").equals( - new FullQualifiedName(ns.value(), annType.isAssignableFrom(EnumType.class) - ? EnumType.class.cast(ann).name() - : ComplexType.class.cast(ann).name()).toString())) { - return clazz; - } - } - } - } catch (Exception e) { - LOG.warn("Error retrieving proxy complex class list", e); - } - - throw new IllegalArgumentException("Provided property '" + property + "' is not complex"); - } - private static String firstValidEntityKey(final Class<?> entityTypeRef) { for (Method method : entityTypeRef.getDeclaredMethods()) { if (method.getAnnotation(Key.class) != null) { @@ -585,10 +440,10 @@ public final class CoreUtils { return null; } - public static URI getMediaEditLink(final String name, final CommonODataEntity entity) { - for (ODataLink link : entity.getMediaEditLinks()) { - if (name.equalsIgnoreCase(link.getName())) { - return link.getLink(); + public static URI getEditMediaLink(final String name, final CommonODataEntity entity) { + for (ODataLink editMediaLink : entity.getMediaEditLinks()) { + if (name.equalsIgnoreCase(editMediaLink.getName())) { + return editMediaLink.getLink(); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/fit/src/it/primitiveKeysServiceV3/pom.xml ---------------------------------------------------------------------- diff --git a/fit/src/it/primitiveKeysServiceV3/pom.xml b/fit/src/it/primitiveKeysServiceV3/pom.xml index 6a8efd0..a01701f 100644 --- a/fit/src/it/primitiveKeysServiceV3/pom.xml +++ b/fit/src/it/primitiveKeysServiceV3/pom.xml @@ -27,7 +27,7 @@ <groupId>org.apache.olingo</groupId> <version>@project.version@</version> <name>${project.artifactId}</name> - <description>A simple IT verifying the basic use case of pojogen-maven-plugin.</description> + <description>A simple IT verifying the basic use case of pojogen-man-plugin.</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java ---------------------------------------------------------------------- diff --git a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java index 4ccea54..0bb7c7d 100644 --- a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java +++ b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java @@ -116,7 +116,7 @@ public abstract class AbstractServices { private static final Pattern REQUEST_PATTERN = Pattern.compile("(.*) (http://.*) HTTP/.*"); - private static final Pattern BATCH_REQUEST_REF_PATTERN = Pattern.compile("(.*) ([$]\\d+)(.*) HTTP/.*"); + private static final Pattern BATCH_REQUEST_REF_PATTERN = Pattern.compile("(.*) ([$].*) HTTP/.*"); private static final Pattern REF_PATTERN = Pattern.compile("([$]\\d+)"); @@ -242,7 +242,7 @@ public abstract class AbstractServices { return xml.createResponse(new ByteArrayInputStream(content.toByteArray()), null, Accept.JSON_FULLMETA); } catch (Exception e) { LOG.error("While creating StoredPI", e); - return xml.createFaultResponse(Accept.JSON_FULLMETA.toString(version), e); + return xml.createFaultResponse(Accept.JSON_FULLMETA.toString(version),e); } } @@ -259,6 +259,7 @@ public abstract class AbstractServices { } protected Response bodyPartRequest(final MimeBodyPart body, final Map<String, String> references) throws Exception { + @SuppressWarnings("unchecked") final Enumeration<Header> en = (Enumeration<Header>) body.getAllHeaders(); @@ -284,7 +285,7 @@ public abstract class AbstractServices { url = matcher.group(2); method = matcher.group(1); } else if (matcherRef.find()) { - url = references.get(matcherRef.group(2)) + matcherRef.group(3); + url = references.get(matcherRef.group(2)); method = matcherRef.group(1); } else { url = null; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AbstractTestITCase.java ---------------------------------------------------------------------- diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AbstractTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AbstractTestITCase.java index e9afca9..73d5c32 100644 --- a/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AbstractTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AbstractTestITCase.java @@ -55,6 +55,8 @@ public abstract class AbstractTestITCase { protected static String testStaticServiceRootURL; + protected static String testPrimitiveKeysServiceRootURL; + protected static String testKeyAsSegmentServiceRootURL; protected static String testActionOverloadingServiceRootURL; @@ -74,6 +76,7 @@ public abstract class AbstractTestITCase { @BeforeClass public static void setUpODataServiceRoot() throws IOException { testStaticServiceRootURL = "http://localhost:9080/stub/StaticService/V30/Static.svc"; + testPrimitiveKeysServiceRootURL = "http://localhost:9080/stub/StaticService/V30/PrimitiveKeys.svc"; testKeyAsSegmentServiceRootURL = "http://localhost:9080/stub/StaticService/V30/KeyAsSegment.svc"; testActionOverloadingServiceRootURL = "http://localhost:9080/stub/StaticService/V30/ActionOverloading.svc"; testOpenTypeServiceRootURL = "http://localhost:9080/stub/StaticService/V30/OpenType.svc"; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/fit/src/test/java/org/apache/olingo/fit/proxy/v4/PropertyTestITCase.java ---------------------------------------------------------------------- diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/PropertyTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/PropertyTestITCase.java index 7c7fde7..e8f7158 100644 --- a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/PropertyTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/PropertyTestITCase.java @@ -18,12 +18,12 @@ */ package org.apache.olingo.fit.proxy.v4; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - import org.apache.olingo.ext.proxy.EntityContainerFactory; import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Customer; import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.StoredPI; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + import org.junit.Test; /** @@ -33,7 +33,7 @@ public class PropertyTestITCase extends AbstractTestITCase { @Test public void nullNullableProperty() { - final Customer customer = container.getCustomers().get(1); + Customer customer = container.getCustomers().get(1); customer.setFirstName(null); container.flush(); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java index 12a3678..f902a0d 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java @@ -29,12 +29,12 @@ import java.util.Iterator; import java.util.List; import java.util.TimeZone; import java.util.UUID; +import javax.xml.datatype.Duration; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.RandomStringUtils; import org.apache.olingo.commons.api.domain.ODataValue; import org.apache.olingo.client.api.v3.ODataClient; import org.apache.olingo.client.core.AbstractTest; -import org.apache.olingo.commons.api.domain.ODataPrimitiveValue; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.geo.Geospatial; @@ -122,14 +122,12 @@ public class PrimitiveValueTest extends AbstractTest { @Test public void time() throws EdmPrimitiveTypeException { final String primitive = "-P9DT51M10.5063807S"; - final ODataValue value = getClient().getObjectFactory().newPrimitiveValueBuilder(). - setType(EdmPrimitiveTypeKind.Time).setText(primitive).build(); + final ODataValue value = + getClient().getObjectFactory().newPrimitiveValueBuilder().setType(EdmPrimitiveTypeKind.Time). + setText(primitive).build(); assertEquals(EdmPrimitiveTypeKind.Time, value.asPrimitive().getTypeKind()); - assertEquals(BigDecimal.valueOf(-780670.5063807), value.asPrimitive().toCastValue(BigDecimal.class)); - - final ODataPrimitiveValue write = getClient().getObjectFactory().newPrimitiveValueBuilder(). - setType(EdmPrimitiveTypeKind.Time).setValue(BigDecimal.valueOf(-780670.5063807)).build(); - assertEquals(primitive, write.toString()); + // performed cast to improve the check + assertEquals("-780670.5063807", value.asPrimitive().toCastValue(BigDecimal.class).toString()); } @Test http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/22f6a6a2/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDuration.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDuration.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDuration.java index cd0aaea..e39b8e7 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDuration.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDuration.java @@ -81,6 +81,7 @@ public class EdmDuration extends SingletonPrimitiveType { throw new EdmPrimitiveTypeException( "EdmPrimitiveTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value, returnType), e"); } catch (final ClassCastException e) { + e.printStackTrace(); throw new EdmPrimitiveTypeException( "EdmPrimitiveTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType), e"); }
