You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2013/10/15 22:36:10 UTC
[5/6] git commit: Added @EdmMediaResource annotations
Added @EdmMediaResource annotations
Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/5af55593
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/5af55593
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/5af55593
Branch: refs/heads/PocEdmAnnotationsExtension
Commit: 5af555934e1715827cc6db377bd634ab1aa96e28
Parents: f614199
Author: Michael Bolz <mi...@sap.com>
Authored: Tue Oct 15 09:38:41 2013 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Tue Oct 15 21:58:34 2013 +0200
----------------------------------------------------------------------
.../annotation/edm/EdmMediaResourceContent.java | 29 +++++
.../edm/EdmMediaResourceMimeType.java | 29 +++++
.../annotation/edm/EdmMediaResourceSource.java | 29 +++++
.../annotation/edm/AnnotationEdmProvider.java | 10 +-
.../core/annotation/edm/AnnotationHelper.java | 40 ++++++
.../odata2/core/annotation/edm/ClassHelper.java | 2 +
.../processor/AnnotationProcessor.java | 27 ++++
.../processor/json/EdmAnnotationSerializer.java | 114 ++++++++++++-----
.../annotation/processor/json/JsonWriter.java | 125 ++++++-------------
.../odata2/ref/annotation/model/Photo.java | 91 +++++++-------
.../ref/annotation/model/ResourceHelper.java | 47 +++++++
.../odata2/ref/annotation/model/ds/PhotoDs.java | 63 ++++++++++
.../processor/ScenarioServiceFactory.java | 15 ++-
13 files changed, 452 insertions(+), 169 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceContent.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceContent.java b/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceContent.java
new file mode 100644
index 0000000..80e9cd8
--- /dev/null
+++ b/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceContent.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.olingo.odata2.api.annotation.edm;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface EdmMediaResourceContent {
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceMimeType.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceMimeType.java b/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceMimeType.java
new file mode 100644
index 0000000..d255ff4
--- /dev/null
+++ b/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceMimeType.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.olingo.odata2.api.annotation.edm;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface EdmMediaResourceMimeType {
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceSource.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceSource.java b/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceSource.java
new file mode 100644
index 0000000..78e72b5
--- /dev/null
+++ b/odata2-edm-annotation/edm-annotation-api/src/main/java/org/apache/olingo/odata2/api/annotation/edm/EdmMediaResourceSource.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.olingo.odata2.api.annotation.edm;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface EdmMediaResourceSource {
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
index 8bd80ad..d4ef369 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationEdmProvider.java
@@ -29,10 +29,10 @@ import java.util.Map;
import org.apache.olingo.odata2.api.annotation.edm.EdmComplexEntity;
import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.api.annotation.edm.EdmMediaResourceContent;
import org.apache.olingo.odata2.api.annotation.edm.EdmNavigationProperty;
import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
import org.apache.olingo.odata2.api.annotation.edm.NavigationEnd;
-import org.apache.olingo.odata2.api.edm.EdmEntitySet;
import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
import org.apache.olingo.odata2.api.edm.FullQualifiedName;
@@ -316,6 +316,7 @@ public class AnnotationEdmProvider extends EdmProvider {
final private String namespace;
final private String name;
private boolean isAbstract = false;
+ private boolean isMediaResource = false;
private FullQualifiedName baseEntityType = null;
private final List<PropertyRef> keyProperties = new ArrayList<PropertyRef>();
private final List<Property> properties = new ArrayList<Property>();
@@ -359,6 +360,10 @@ public class AnnotationEdmProvider extends EdmProvider {
Association association = createAssociation(field, navProperty);
associations.add(association);
}
+ EdmMediaResourceContent emrc = field.getAnnotation(EdmMediaResourceContent.class);
+ if(emrc != null) {
+ isMediaResource = true;
+ }
}
return this;
@@ -406,6 +411,7 @@ public class AnnotationEdmProvider extends EdmProvider {
}
return entityType.setName(name)
.setAbstract(isAbstract)
+ .setHasStream(isMediaResource)
.setProperties(properties);
}
@@ -502,6 +508,8 @@ public class AnnotationEdmProvider extends EdmProvider {
return EdmSimpleTypeKind.Int32;
} else if (type == Integer.class) {
return EdmSimpleTypeKind.Int32;
+ } else if (type == Byte[].class || type == byte[].class) {
+ return EdmSimpleTypeKind.Binary;
} else {
throw new UnsupportedOperationException("Not supported type '" + type
+ "' yet."); //To change body of generated methods, choose Tools | Templates.
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
index 184685e..e30e382 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/AnnotationHelper.java
@@ -18,16 +18,56 @@
******************************************************************************/
package org.apache.olingo.odata2.core.annotation.edm;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Locale;
import org.apache.olingo.odata2.api.annotation.edm.EdmComplexEntity;
import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
/**
*
*/
public class AnnotationHelper {
+ public Object getValueForField(Object result, Class<? extends Annotation> annotation) {
+ if(result == null) {
+ return null;
+ }
+ return getValueForField(result, result.getClass(), annotation, true);
+ }
+
+ private Object getValueForField(Object result, Class<?> resultClass,
+ Class<? extends Annotation> annotation, boolean inherited) {
+ if(result == null) {
+ return null;
+ }
+
+ Field[] fields = resultClass.getDeclaredFields();
+ for (Field field : fields) {
+ if(field.getAnnotation(annotation) != null) {
+ try {
+ boolean access = field.isAccessible();
+ field.setAccessible(true);
+ Object value = field.get(result);
+ field.setAccessible(access);
+ return value;
+ } catch (IllegalArgumentException ex) { // should never happen
+ throw new ODataRuntimeException(ex);
+ } catch (IllegalAccessException ex) { // should never happen
+ throw new ODataRuntimeException(ex);
+ }
+ }
+ }
+
+ Class<?> superClass = result.getClass().getSuperclass();
+ if(inherited && superClass != Object.class) {
+ return getValueForField(result, superClass, annotation, true);
+ }
+
+ return null;
+ }
+
public boolean isEdmAnnotated(Object object) {
if(object == null) {
return false;
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/ClassHelper.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/ClassHelper.java b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/ClassHelper.java
index 2094591..2a4c706 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/ClassHelper.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/edm/ClassHelper.java
@@ -75,6 +75,8 @@ public class ClassHelper {
annotatedClasses.add(c);
}
} catch (ClassNotFoundException ex) {
+ throw new IllegalArgumentException("Exception during class loading of class '" + fqn +
+ "' with message '" + ex.getMessage() + "'.");
}
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/AnnotationProcessor.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/AnnotationProcessor.java b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/AnnotationProcessor.java
index faf129c..d06bd3f 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/AnnotationProcessor.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/AnnotationProcessor.java
@@ -29,6 +29,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.annotation.edm.EdmMediaResourceContent;
+import org.apache.olingo.odata2.api.annotation.edm.EdmMediaResourceMimeType;
import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
import org.apache.olingo.odata2.api.exception.ODataException;
@@ -47,6 +49,7 @@ import org.apache.olingo.odata2.api.annotation.edm.ds.EntityRead;
import org.apache.olingo.odata2.api.annotation.edm.ds.EntitySetRead;
import org.apache.olingo.odata2.api.annotation.edm.ds.EntityUpdate;
import org.apache.olingo.odata2.api.processor.ODataContext;
+import org.apache.olingo.odata2.api.uri.info.GetMediaResourceUriInfo;
import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
import org.apache.olingo.odata2.core.annotation.edm.AnnotationHelper;
import org.apache.olingo.odata2.core.annotation.edm.ClassHelper;
@@ -149,6 +152,30 @@ public class AnnotationProcessor extends ODataSingleProcessor {
return ODataResponse.status(HttpStatusCodes.NOT_FOUND).build();
}
+ @Override
+ public ODataResponse readEntityMedia(GetMediaResourceUriInfo uriInfo, String contentType) throws ODataException {
+ final String name = uriInfo.getTargetType().getName();
+
+ List<KeyPredicate> keys = uriInfo.getKeyPredicates();
+ Object result = readEntity(name, keys);
+ if (result != null) {
+ Object content = new AnnotationHelper().getValueForField(result, EdmMediaResourceContent.class);
+ Object mimeType = new AnnotationHelper().getValueForField(result, EdmMediaResourceMimeType.class);
+ return ODataResponse.status(HttpStatusCodes.OK).entity(content).contentHeader(String.valueOf(mimeType)).build();
+ }
+
+ return ODataResponse.status(HttpStatusCodes.NOT_FOUND).build();
+ }
+
+ private Object readEntity(String entityName, List<KeyPredicate> keys) {
+ DataSourceHolder holder = dataSources.get(entityName);
+ if (holder != null) {
+ return holder.readEntity(keys);
+ }
+ return null;
+ }
+
+
private ODataResponse createODataResponse(Object result, HttpStatusCodes statusCode) throws ODataException {
// StringBuilder resultAsString = new StringBuilder("{\"d\":");
// if (result != null) {
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/EdmAnnotationSerializer.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/EdmAnnotationSerializer.java b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/EdmAnnotationSerializer.java
index 01ccf2c..10b8945 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/EdmAnnotationSerializer.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/EdmAnnotationSerializer.java
@@ -1,21 +1,17 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
+/**
+ * *****************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+ * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ *****************************************************************************
+ */
package org.apache.olingo.odata2.core.annotation.processor.json;
import java.io.ByteArrayInputStream;
@@ -29,11 +25,18 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
import org.apache.olingo.odata2.api.annotation.edm.EdmNavigationProperty;
import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeException;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
import org.apache.olingo.odata2.core.annotation.edm.AnnotationHelper;
import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
@@ -66,7 +69,7 @@ public class EdmAnnotationSerializer {
}
}
- private String handleEdmAnnotations(Object entity)
+ private String handleEdmAnnotations(Object entity)
throws IllegalArgumentException, IllegalAccessException, IOException {
//
Writer writer = new StringWriter();
@@ -80,10 +83,10 @@ public class EdmAnnotationSerializer {
}
private void writeObject(Object entity, JsonWriter json) throws IllegalAccessException {
- if(entity.getClass().isArray()) {
+ if (entity.getClass().isArray()) {
List<Object> entities = Arrays.asList(entity);
writeCollection(entities, json);
- } else if(Collection.class.isAssignableFrom(entity.getClass())) {
+ } else if (Collection.class.isAssignableFrom(entity.getClass())) {
Collection<Object> entities = (Collection<Object>) entity;
writeCollection(entities, json);
} else {
@@ -97,7 +100,7 @@ public class EdmAnnotationSerializer {
json.startArray();
boolean writeSeperator = false;
for (Object object : entities) {
- if(writeSeperator) {
+ if (writeSeperator) {
json.writeSeparator();
} else {
writeSeperator = true;
@@ -107,7 +110,7 @@ public class EdmAnnotationSerializer {
json.endArray();
json.endObject();
}
-
+
private void writeSingleObject(Object entity, JsonWriter json) throws IllegalAccessException {
List<Field> fields = getAllFields(entity.getClass());
json.startObject();
@@ -120,14 +123,14 @@ public class EdmAnnotationSerializer {
json.endObject();
}
- private boolean writeEdmNavigationProperty(Object entity, JsonWriter json, Field field)
+ private boolean writeEdmNavigationProperty(Object entity, JsonWriter json, Field field)
throws IllegalArgumentException, IllegalAccessException {
EdmNavigationProperty navProperty = field.getAnnotation(EdmNavigationProperty.class);
EdmEntityType entityType = entity.getClass().getAnnotation(EdmEntityType.class);
if (navProperty != null) {
field.setAccessible(true);
Object keyValue = extractEdmKey(entity);
- json.writeProperty("uri", baseUri + entityType.entitySetName() + "('" + keyValue.toString() + "')"
+ json.writeStringProperty("uri", baseUri + entityType.entitySetName() + "('" + keyValue.toString() + "')"
+ "/" + navProperty.relationship());
return true;
}
@@ -140,18 +143,63 @@ public class EdmAnnotationSerializer {
field.setAccessible(true);
Object fieldValue = field.get(entity);
String name = getName(property, field);
- Class<?> defaultType = getType(property);
- json.writeProperty(name, fieldValue, defaultType);
+ EdmSimpleTypeKind defaultType = getDefaultSimpleTypeKind(field, property);
+ try {
+ String value = defaultType.getEdmSimpleTypeInstance().valueToString(fieldValue, EdmLiteralKind.JSON, null);
+ write(defaultType, name, value, json);
+ } catch (EdmSimpleTypeException e) {
+ throw new IllegalArgumentException("Illegal argument for valueToString for EdmSimpleType = '"
+ + property.type() + "' with message = '" + e.getMessage() + "'.");
+ }
return true;
}
return false;
}
+ public void write(EdmSimpleTypeKind kind, String name, String value, JsonWriter jsonStreamWriter) {
+ switch (kind) {
+ case String:
+ jsonStreamWriter.writeStringProperty(name, value);
+ break;
+ case Boolean:
+ case Byte:
+ case SByte:
+ case Int16:
+ case Int32:
+ jsonStreamWriter.writeRawProperty(name, value);
+ break;
+ case DateTime:
+ case DateTimeOffset:
+ // Although JSON escaping is (and should be) done in the JSON
+ // serializer, we backslash-escape the forward slash here explicitly
+ // because it is not required to escape it in JSON but in OData.
+ jsonStreamWriter.writeRawStringProperty(name, value == null ? null : value.replace("/", "\\/"));
+ break;
+ default:
+ jsonStreamWriter.writeRawStringProperty(name, value);
+ break;
+ }
+ }
+
+ private EdmSimpleTypeKind getDefaultSimpleTypeKind(Field field, EdmProperty property) {
+ final EdmSimpleTypeKind type = property.type();
+ if (type == EdmSimpleTypeKind.Null) {
+ Class<?> fieldType = field.getType();
+ if (fieldType == String.class) {
+ return EdmSimpleTypeKind.String;
+ } else if (fieldType == Long.class) {
+ return EdmSimpleTypeKind.Int64;
+ } else if (fieldType == Integer.class) {
+ return EdmSimpleTypeKind.Int32;
+ }
+ }
+ return type;
+ }
+
private Class<?> getType(EdmProperty property) {
Class<?> defaultType = property.type().getEdmSimpleTypeInstance().getDefaultType();
return defaultType;
}
-
private Object extractEdmKey(Object value) throws IllegalArgumentException, IllegalAccessException {
Field idField = getFieldWithAnnotation(value.getClass(), EdmKey.class);
@@ -192,13 +240,13 @@ public class EdmAnnotationSerializer {
}
private boolean isConsumable(Object entity) {
- if(entity == null) {
+ if (entity == null) {
return false;
- } else if(ANNOTATION_HELPER.isEdmAnnotated(entity)) {
+ } else if (ANNOTATION_HELPER.isEdmAnnotated(entity)) {
return true;
- } else if(entity.getClass().isArray() || entity.getClass().isAssignableFrom(Collection.class)) {
+ } else if (entity.getClass().isArray() || entity.getClass().isAssignableFrom(Collection.class)) {
return true;
- } else if(Collection.class.isAssignableFrom(entity.getClass())) {
+ } else if (Collection.class.isAssignableFrom(entity.getClass())) {
return true;
}
return false;
@@ -206,9 +254,9 @@ public class EdmAnnotationSerializer {
private String getName(EdmProperty property, Field field) {
String name = property.name();
- if(name.isEmpty()) {
+ if (name.isEmpty()) {
name = ANNOTATION_HELPER.getCanonicalName(field);
}
return name;
}
-}
\ No newline at end of file
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/JsonWriter.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/JsonWriter.java b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/JsonWriter.java
index 1c1cc21..b972fed 100644
--- a/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/JsonWriter.java
+++ b/odata2-edm-annotation/edm-annotation-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/json/JsonWriter.java
@@ -1,21 +1,17 @@
-/*******************************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
+/**
+ * *****************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
+ * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- ******************************************************************************/
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ * ****************************************************************************
+ */
package org.apache.olingo.odata2.core.annotation.processor.json;
import java.io.IOException;
@@ -97,78 +93,47 @@ public class JsonWriter {
}
}
- public void writeString(String value) {
+ public void writeRawString(String value) {
try {
- writer.write("\"" + encode(value) + "\"");
+ writer.write("\"" + value + "\"");
} catch (IOException e) {
throw new ODataRuntimeException(e.getMessage(), e);
}
}
- public void writeNull() {
+ public void writeString(String value) {
try {
- writer.write("null");
+ writer.write("\"" + encode(value) + "\"");
} catch (IOException e) {
throw new ODataRuntimeException(e.getMessage(), e);
}
}
- public void writeNumber(int value) {
+ public void writeNull() {
try {
- writer.write(Integer.toString(value));
+ writer.write("null");
} catch (IOException e) {
throw new ODataRuntimeException(e.getMessage(), e);
}
}
- public void writeNumber(float value) {
- try {
- String fvalue = Float.toString(value);
- while (fvalue.contains(".") && fvalue.endsWith("0")) {
- fvalue = fvalue.substring(0, fvalue.length() - 1);
- }
- if (fvalue.endsWith(".")) {
- fvalue = fvalue.substring(0, fvalue.length() - 1);
- }
- writer.write(fvalue);
- } catch (IOException e) {
- throw new ODataRuntimeException(e.getMessage(), e);
+ public void writeStringProperty(String name, String value) {
+ if (value == null) {
+ writeProperty(name, value, false);
+ } else {
+ writeProperty(name, encode(value), false);
}
}
- public void writeNumber(double value) {
- try {
- String fvalue = Double.toString(value);
- while (fvalue.contains(".") && fvalue.endsWith("0")) {
- fvalue = fvalue.substring(0, fvalue.length() - 1);
- }
- if (fvalue.endsWith(".")) {
- fvalue = fvalue.substring(0, fvalue.length() - 1);
- }
- writer.write(fvalue);
- } catch (IOException e) {
- throw new ODataRuntimeException(e.getMessage(), e);
- }
+ public void writeRawStringProperty(String name, String value) {
+ writeProperty(name, value, false);
}
- public void writeBoolean(boolean value) {
- try {
- writer.write(value ? "true" : "false");
- } catch (IOException e) {
- throw new ODataRuntimeException(e.getMessage(), e);
- }
+ public void writeRawProperty(String name, String value) {
+ writeProperty(name, value, true);
}
- public void writeProperty(String name, Object value) {
- if(value == null) {
- writeProperty(name, value, null);
- } else {
- writeProperty(name, value, value.getClass());
- }
- }
-
-
- void writeProperty(String name, Object value, Class<?> defaultType) {
+ private void writeProperty(String name, String value, boolean raw) {
if (firstProperty) {
firstProperty = false;
} else {
@@ -181,10 +146,10 @@ public class JsonWriter {
writeNull();
} else {
try {
- if(defaultType == null) {
- writeValue(value);
+ if (raw) {
+ writeRaw(value);
} else {
- writeValueAs(value, defaultType);
+ writeString(value);
}
} catch (Exception e) {
writeString("exception for property with name '" + name
@@ -192,10 +157,10 @@ public class JsonWriter {
+ "' and toString '" + value.toString() + "'.");
}
}
+
}
-
- public void writeRaw(String value) {
+ private void writeRaw(String value) {
try {
writer.write(value);
} catch (IOException e) {
@@ -228,29 +193,11 @@ public class JsonWriter {
return sb.toString();
}
- void start() {
+ public void start() {
writeRaw("{\n\"d\":");
}
- void finish() {
+ public void finish() {
writeRaw("\n}");
}
-
- private void writeValueAs(Object value, Class<?> clazz) {
- if(clazz.isAssignableFrom(String.class)) {
- writeString(String.valueOf(value));
- } else if(clazz.isAssignableFrom(Float.class)) {
- writeNumber((Float) value);
- } else if(clazz.isAssignableFrom(Double.class)) {
- writeNumber((Double) value);
- } else if(clazz.isAssignableFrom(Integer.class)) {
- writeNumber((Integer) value);
- } else {
- writeString(value.toString());
- }
- }
-
- private void writeValue(Object value) {
- writeValueAs(value, value.getClass());
- }
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/Photo.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/Photo.java b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/Photo.java
index fd74c30..7fab9bc 100644
--- a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/Photo.java
+++ b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/Photo.java
@@ -19,33 +19,42 @@
package org.apache.olingo.odata2.ref.annotation.model;
import java.util.Arrays;
+import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.api.annotation.edm.EdmMediaResourceContent;
+import org.apache.olingo.odata2.api.annotation.edm.EdmMediaResourceMimeType;
+import org.apache.olingo.odata2.api.annotation.edm.EdmMediaResourceSource;
+import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
/**
*
*/
+@EdmEntityType(name = "Photo", namespace = ModelSharedConstants.NAMESPACE_1,
+ entitySetName = "Photos", container = ModelSharedConstants.CONTAINER_1)
public class Photo {
private static final String RESOURCE = "/male_1_WinterW.jpg";
- private static byte[] defaultImage = ResourceHelper.loadAsByte(RESOURCE);
- private int id;
+ @EdmProperty
+ @EdmKey
private String name;
- private String type = "image/jpeg";
+ @EdmProperty
+ private String type;
+ @EdmProperty
+ @EdmMediaResourceMimeType
+ private String mimeType;
+ @EdmProperty
+ @EdmMediaResourceSource
private String imageUrl = "http://localhost" + RESOURCE;
- private byte[] image = defaultImage;
- private String imageType = type;
- private byte[] binaryData;
- private String content;
+ @EdmProperty(type = EdmSimpleTypeKind.Binary)
+ @EdmMediaResourceContent
+ private byte[] image = ResourceHelper.generateImage();
- public Photo(final int id, final String name, final String type) {
- this.id = id;
+ public Photo(final String name, final String type) {
this.name = name;
this.type = type;
}
- public int getId() {
- return id;
- }
-
public String getName() {
return name;
}
@@ -79,53 +88,45 @@ public class Photo {
}
public String getImageType() {
- return imageType;
+ return mimeType;
}
public void setImageType(final String imageType) {
- this.imageType = imageType;
- }
-
- public byte[] getBinaryData() {
- if (binaryData == null) {
- return null;
- } else {
- return binaryData.clone();
- }
- }
-
- public void setBinaryData(final byte[] binaryData) {
- this.binaryData = binaryData;
- }
-
- public void setContent(final String content) {
- this.content = content;
- }
-
- public String getContent() {
- return content;
+ this.mimeType = imageType;
}
@Override
public int hashCode() {
- return id;
+ int hash = 5;
+ hash = 83 * hash + (this.name != null ? this.name.hashCode() : 0);
+ hash = 83 * hash + (this.type != null ? this.type.hashCode() : 0);
+ return hash;
}
@Override
- public boolean equals(final Object obj) {
- return this == obj
- || obj != null && getClass() == obj.getClass() && id == ((Photo) obj).id;
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final Photo other = (Photo) obj;
+ if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
+ return false;
+ }
+ if ((this.type == null) ? (other.type != null) : !this.type.equals(other.type)) {
+ return false;
+ }
+ return true;
}
-
+
@Override
public String toString() {
- return "{\"Id\":" + id + ","
- + "\"Name\":\"" + name + "\","
+ return "{\"Name\":\"" + name + "\","
+ "\"Type\":\"" + type + "\","
+ "\"ImageUrl\":\"" + imageUrl + "\","
+ "\"Image\":\"" + Arrays.toString(image) + "\","
- + "\"ImageType\":\"" + imageType + "\","
- + "\"Content:\"" + content + "\","
- + "\"BinaryData\":\"" + Arrays.toString(binaryData) + "\"}";
+ + "\"MimeType\":\"" + mimeType + "\"";
}
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ResourceHelper.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ResourceHelper.java b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ResourceHelper.java
index 97ace81..07ee5b4 100644
--- a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ResourceHelper.java
+++ b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ResourceHelper.java
@@ -18,11 +18,16 @@
******************************************************************************/
package org.apache.olingo.odata2.ref.annotation.model;
+import java.awt.image.BufferedImage;
+import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.imageio.ImageIO;
+import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
/**
*
@@ -57,4 +62,46 @@ class ResourceHelper {
}
}
}
+
+ static byte[] generateImage() {
+ try {
+ // ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+// String format = "PNG";
+// int width = 200;
+// int height = 200;
+// int imageType = BufferedImage.TYPE_BYTE_BINARY;
+// BufferedImage image = new BufferedImage(width, height, imageType);
+// ImageIO.write(image, format, outStream);
+// return null;
+
+ return test();
+ } catch (IOException ex) {
+ return new byte[0];
+ }
+ }
+
+ private static byte[] test() throws IOException {
+ int width = 400; // Dimensions of the image
+ int height = 400;
+ // Let's create a BufferedImage for a binary image.
+ BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
+ // We need its raster to set the pixels' values.
+ WritableRaster raster = im.getRaster();
+ // Put the pixels on the raster. Note that only values 0 and 1 are used for the pixels.
+ // You could even use other values: in this type of image, even values are black and odd
+ // values are white.
+ for (int h = 0; h < height; h++) {
+ for (int w = 0; w < width; w++) {
+ if (((h / 50) + (w / 50)) % 2 == 0) {
+ raster.setSample(w, h, 0, 0); // checkerboard pattern.
+ } else {
+ raster.setSample(w, h, 0, 1);
+ }
+ }
+ }
+ // Store the image using the PNG format.
+ ByteArrayOutputStream out = new ByteArrayOutputStream(1024*10);
+ ImageIO.write(im, "PNG", out);
+ return out.toByteArray();
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ds/PhotoDs.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ds/PhotoDs.java b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ds/PhotoDs.java
new file mode 100644
index 0000000..660f59c
--- /dev/null
+++ b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/model/ds/PhotoDs.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.olingo.odata2.ref.annotation.model.ds;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.olingo.odata2.api.annotation.edm.ds.EntityCreate;
+import org.apache.olingo.odata2.api.annotation.edm.ds.EntityDataSource;
+import org.apache.olingo.odata2.api.annotation.edm.ds.EntityRead;
+import org.apache.olingo.odata2.api.annotation.edm.ds.EntitySetRead;
+import org.apache.olingo.odata2.api.annotation.edm.ds.EntityUpdate;
+import org.apache.olingo.odata2.ref.annotation.model.Photo;
+
+/**
+ *
+ */
+@EntityDataSource(entityType = Photo.class)
+public class PhotoDs {
+ private static final Map<String, Photo> photoDb = new HashMap<String, Photo>();
+
+ private String createPhotoId(Photo p) {
+ if(p == null) {
+ return null;
+ }
+ return p.getName() + "-" + p.getType();
+ }
+
+ @EntityCreate
+ @EntityUpdate
+ public Photo storePhoto(Photo photo) {
+ Photo p = new Photo(photo.getName(), photo.getType());
+ p.setImageUri(photo.getImageUri());
+ p.setImageType(photo.getImageType());
+
+ photoDb.put(createPhotoId(p), p);
+
+ return p;
+ }
+
+ @EntityRead
+ public Photo loadPhoto(String name) {
+ return photoDb.get(createPhotoId(new Photo(name, "PNG")));
+ }
+
+ @EntitySetRead
+ public Collection<Photo> loadAllPhotos() {
+ return photoDb.values();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/5af55593/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/processor/ScenarioServiceFactory.java
----------------------------------------------------------------------
diff --git a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/processor/ScenarioServiceFactory.java b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/processor/ScenarioServiceFactory.java
index 7ff3ee4..d04f6f4 100644
--- a/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/processor/ScenarioServiceFactory.java
+++ b/odata2-edm-annotation/edm-annotation-webref/src/main/java/org/apache/olingo/odata2/ref/annotation/processor/ScenarioServiceFactory.java
@@ -27,8 +27,10 @@ import org.apache.olingo.odata2.api.processor.ODataContext;
import org.apache.olingo.odata2.core.annotation.edm.AnnotationEdmProvider;
import org.apache.olingo.odata2.core.annotation.processor.AnnotationProcessor;
import org.apache.olingo.odata2.ref.annotation.model.Building;
+import org.apache.olingo.odata2.ref.annotation.model.Photo;
import org.apache.olingo.odata2.ref.annotation.model.Team;
import org.apache.olingo.odata2.ref.annotation.model.ds.BuildingDs;
+import org.apache.olingo.odata2.ref.annotation.model.ds.PhotoDs;
import org.apache.olingo.odata2.ref.annotation.model.ds.TeamDs;
/**
@@ -58,7 +60,6 @@ public class ScenarioServiceFactory extends ODataServiceFactory {
? new ScenarioDebugCallback() : super.getCallback(callbackInterface));
}
-
private final class ScenarioDebugCallback implements ODataDebugCallback {
@Override
@@ -80,6 +81,11 @@ public class ScenarioServiceFactory extends ODataServiceFactory {
buildingsDs.createOrUpdate(createBuilding("Red Building"));
buildingsDs.createOrUpdate(createBuilding("Green Building"));
buildingsDs.createOrUpdate(createBuilding("Blue Building"));
+
+ PhotoDs photoDs = new PhotoDs();
+ photoDs.storePhoto(createPhoto("Small picture"));
+ photoDs.storePhoto(createPhoto("Medium picture"));
+ photoDs.storePhoto(createPhoto("Big picture"));
}
private Team createTeam(String teamName, boolean isScrumTeam) {
@@ -94,4 +100,11 @@ public class ScenarioServiceFactory extends ODataServiceFactory {
b.setName(buildingName);
return b;
}
+
+ private Photo createPhoto(String name) {
+ Photo p = new Photo(name, "PNG");
+ p.setImageType("image/png");
+ p.setImageUri("http://localhost/image/" + name);
+ return p;
+ }
}