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 2014/06/09 14:47:21 UTC
[3/9] [OLINGO-317] First presentable state
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonDeserializer.java
deleted file mode 100644
index 133c916..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonDeserializer.java
+++ /dev/null
@@ -1,372 +0,0 @@
-/*
- * 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.commons.core.data;
-
-import com.fasterxml.jackson.core.ObjectCodec;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import java.io.IOException;
-import java.util.AbstractMap.SimpleEntry;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.olingo.commons.api.Constants;
-import org.apache.olingo.commons.api.data.Annotatable;
-import org.apache.olingo.commons.api.data.Annotation;
-import org.apache.olingo.commons.api.data.CollectionValue;
-import org.apache.olingo.commons.api.data.ComplexValue;
-import org.apache.olingo.commons.api.data.Linked;
-import org.apache.olingo.commons.api.data.Property;
-import org.apache.olingo.commons.api.data.ResWrap;
-import org.apache.olingo.commons.api.data.Valuable;
-import org.apache.olingo.commons.api.data.Value;
-import org.apache.olingo.commons.api.domain.ODataLinkType;
-import org.apache.olingo.commons.api.domain.ODataPropertyType;
-import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
-import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
-import org.apache.olingo.commons.core.edm.EdmTypeInfo;
-
-abstract class AbstractJsonDeserializer<T> extends ODataJacksonDeserializer<ResWrap<T>> {
-
- protected final Pattern CUSTOM_ANNOTATION = Pattern.compile("(.+)@(.+)\\.(.+)");
-
- private JSONGeoValueDeserializer geoDeserializer;
-
- private JSONGeoValueDeserializer getGeoDeserializer() {
- if (geoDeserializer == null) {
- geoDeserializer = new JSONGeoValueDeserializer(version);
- }
- return geoDeserializer;
- }
-
- protected String getTitle(final Map.Entry<String, JsonNode> entry) {
- return entry.getKey().substring(0, entry.getKey().indexOf('@'));
- }
-
- protected String setInline(final String name, final String suffix, final JsonNode tree,
- final ObjectCodec codec, final LinkImpl link) throws IOException {
-
- final String entityNamePrefix = name.substring(0, name.indexOf(suffix));
- if (tree.has(entityNamePrefix)) {
- final JsonNode inline = tree.path(entityNamePrefix);
-
- if (inline instanceof ObjectNode) {
- link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
-
- link.setInlineEntity(inline.traverse(codec).<ResWrap<JSONEntityImpl>>readValueAs(
- new TypeReference<JSONEntityImpl>() {
- }).getPayload());
- }
-
- if (inline instanceof ArrayNode) {
- link.setType(ODataLinkType.ENTITY_SET_NAVIGATION.toString());
-
- final JSONEntitySetImpl entitySet = new JSONEntitySetImpl();
- final Iterator<JsonNode> entries = ((ArrayNode) inline).elements();
- while (entries.hasNext()) {
- entitySet.getEntities().add(entries.next().traverse(codec).<ResWrap<JSONEntityImpl>>readValuesAs(
- new TypeReference<JSONEntityImpl>() {
- }).next().getPayload());
- }
-
- link.setInlineEntitySet(entitySet);
- }
- }
- return entityNamePrefix;
- }
-
- protected void links(final Map.Entry<String, JsonNode> field, final Linked linked, final Set<String> toRemove,
- final JsonNode tree, final ObjectCodec codec) throws IOException {
- if (serverMode) {
- serverLinks(field, linked, toRemove, tree, codec);
- } else {
- clientLinks(field, linked, toRemove, tree, codec);
- }
- }
-
- private void clientLinks(final Map.Entry<String, JsonNode> field, final Linked linked, final Set<String> toRemove,
- final JsonNode tree, final ObjectCodec codec) throws IOException {
-
- if (field.getKey().endsWith(jsonNavigationLink)) {
- final LinkImpl link = new LinkImpl();
- link.setTitle(getTitle(field));
- link.setRel(version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL) + getTitle(field));
-
- if (field.getValue().isValueNode()) {
- link.setHref(field.getValue().textValue());
- link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
- }
-
- linked.getNavigationLinks().add(link);
-
- toRemove.add(field.getKey());
- toRemove.add(setInline(field.getKey(), jsonNavigationLink, tree, codec, link));
- } else if (field.getKey().endsWith(jsonAssociationLink)) {
- final LinkImpl link = new LinkImpl();
- link.setTitle(getTitle(field));
- link.setRel(version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL) + getTitle(field));
- link.setHref(field.getValue().textValue());
- link.setType(ODataLinkType.ASSOCIATION.toString());
- linked.getAssociationLinks().add(link);
-
- toRemove.add(field.getKey());
- }
- }
-
- private void serverLinks(final Map.Entry<String, JsonNode> field, final Linked linked, final Set<String> toRemove,
- final JsonNode tree, final ObjectCodec codec) throws IOException {
-
- if (field.getKey().endsWith(Constants.JSON_BIND_LINK_SUFFIX)
- || field.getKey().endsWith(jsonNavigationLink)) {
-
- if (field.getValue().isValueNode()) {
- final String suffix = field.getKey().replaceAll("^.*@", "@");
-
- final LinkImpl link = new LinkImpl();
- link.setTitle(getTitle(field));
- link.setRel(version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL) + getTitle(field));
- link.setHref(field.getValue().textValue());
- link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
- linked.getNavigationLinks().add(link);
-
- toRemove.add(setInline(field.getKey(), suffix, tree, codec, link));
- } else if (field.getValue().isArray()) {
- for (final Iterator<JsonNode> itor = field.getValue().elements(); itor.hasNext();) {
- final JsonNode node = itor.next();
-
- final LinkImpl link = new LinkImpl();
- link.setTitle(getTitle(field));
- link.setRel(version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL) + getTitle(field));
- link.setHref(node.asText());
- link.setType(ODataLinkType.ENTITY_SET_NAVIGATION.toString());
- linked.getNavigationLinks().add(link);
- toRemove.add(setInline(field.getKey(), Constants.JSON_BIND_LINK_SUFFIX, tree, codec, link));
- }
- }
- toRemove.add(field.getKey());
- }
- }
-
- private Map.Entry<ODataPropertyType, EdmTypeInfo> guessPropertyType(final JsonNode node) {
- ODataPropertyType type;
- EdmTypeInfo typeInfo = null;
-
- if (node.isValueNode() || node.isNull()) {
- type = ODataPropertyType.PRIMITIVE;
-
- EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.String;
- if (node.isShort()) {
- kind = EdmPrimitiveTypeKind.Int16;
- } else if (node.isInt()) {
- kind = EdmPrimitiveTypeKind.Int32;
- } else if (node.isLong()) {
- kind = EdmPrimitiveTypeKind.Int64;
- } else if (node.isBoolean()) {
- kind = EdmPrimitiveTypeKind.Boolean;
- } else if (node.isFloat()) {
- kind = EdmPrimitiveTypeKind.Single;
- } else if (node.isDouble()) {
- kind = EdmPrimitiveTypeKind.Double;
- } else if (node.isBigDecimal()) {
- kind = EdmPrimitiveTypeKind.Decimal;
- }
- typeInfo = new EdmTypeInfo.Builder().setTypeExpression(kind.getFullQualifiedName().toString()).build();
- } else if (node.isArray()) {
- type = ODataPropertyType.COLLECTION;
- } else if (node.isObject()) {
- if (node.has(Constants.ATTR_TYPE)) {
- type = ODataPropertyType.PRIMITIVE;
- typeInfo = new EdmTypeInfo.Builder().
- setTypeExpression("Edm.Geography" + node.get(Constants.ATTR_TYPE).asText()).build();
- } else {
- type = ODataPropertyType.COMPLEX;
- }
- } else {
- type = ODataPropertyType.EMPTY;
- }
-
- return new SimpleEntry<ODataPropertyType, EdmTypeInfo>(type, typeInfo);
- }
-
- protected void populate(final Annotatable annotatable, final List<Property> properties,
- final ObjectNode tree, final ObjectCodec codec) throws IOException {
-
- String type = null;
- Annotation annotation = null;
- for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
- final Map.Entry<String, JsonNode> field = itor.next();
- final Matcher customAnnotation = CUSTOM_ANNOTATION.matcher(field.getKey());
-
- if (field.getKey().charAt(0) == '@') {
- final Annotation entityAnnot = new AnnotationImpl();
- entityAnnot.setTerm(field.getKey().substring(1));
-
- value(entityAnnot, field.getValue(), codec);
- if (annotatable != null) {
- annotatable.getAnnotations().add(entityAnnot);
- }
- } else if (type == null && field.getKey().endsWith(getJSONAnnotation(jsonType))) {
- type = field.getValue().asText();
- } else if (annotation == null && customAnnotation.matches() && !"odata".equals(customAnnotation.group(2))) {
- annotation = new AnnotationImpl();
- annotation.setTerm(customAnnotation.group(2) + "." + customAnnotation.group(3));
- value(annotation, field.getValue(), codec);
- } else {
- final JSONPropertyImpl property = new JSONPropertyImpl();
- property.setName(field.getKey());
- property.setType(type == null
- ? null
- : new EdmTypeInfo.Builder().setTypeExpression(type).build().internal());
- type = null;
-
- value(property, field.getValue(), codec);
- properties.add(property);
-
- if (annotation != null) {
- property.getAnnotations().add(annotation);
- annotation = null;
- }
- }
- }
- }
-
- private Value fromPrimitive(final JsonNode node, final EdmTypeInfo typeInfo) {
- final Value value;
-
- if (node.isNull()) {
- value = new NullValueImpl();
- } else {
- if (typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) {
- value = new GeospatialValueImpl(getGeoDeserializer().deserialize(node, typeInfo));
- } else {
- value = new PrimitiveValueImpl(node.asText());
- }
- }
-
- return value;
- }
-
- private ComplexValue fromComplex(final ObjectNode node, final ObjectCodec codec) throws IOException {
- final ComplexValue value = version.compareTo(ODataServiceVersion.V40) < 0
- ? new ComplexValueImpl()
- : new LinkedComplexValueImpl();
-
- if (value.isLinkedComplex()) {
- final Set<String> toRemove = new HashSet<String>();
- for (final Iterator<Map.Entry<String, JsonNode>> itor = node.fields(); itor.hasNext();) {
- final Map.Entry<String, JsonNode> field = itor.next();
-
- links(field, value.asLinkedComplex(), toRemove, node, codec);
- }
- node.remove(toRemove);
- }
-
- populate(value.asLinkedComplex(), value.get(), node, codec);
-
- return value;
- }
-
- private CollectionValue fromCollection(final Iterator<JsonNode> nodeItor, final EdmTypeInfo typeInfo,
- final ObjectCodec codec) throws IOException {
-
- final CollectionValueImpl value = new CollectionValueImpl();
-
- final EdmTypeInfo type = typeInfo == null
- ? null
- : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();
-
- while (nodeItor.hasNext()) {
- final JsonNode child = nodeItor.next();
-
- if (child.isValueNode()) {
- if (typeInfo == null || typeInfo.isPrimitiveType()) {
- value.get().add(fromPrimitive(child, type));
- } else {
- value.get().add(new EnumValueImpl(child.asText()));
- }
- } else if (child.isContainerNode()) {
- if (child.has(jsonType)) {
- ((ObjectNode) child).remove(jsonType);
- }
- value.get().add(fromComplex((ObjectNode) child, codec));
- }
- }
-
- return value;
- }
-
- protected void value(final Valuable valuable, final JsonNode node, final ObjectCodec codec)
- throws IOException {
-
- EdmTypeInfo typeInfo = StringUtils.isBlank(valuable.getType())
- ? null
- : new EdmTypeInfo.Builder().setTypeExpression(valuable.getType()).build();
-
- final Map.Entry<ODataPropertyType, EdmTypeInfo> guessed = guessPropertyType(node);
- if (typeInfo == null) {
- typeInfo = guessed.getValue();
- }
-
- final ODataPropertyType propType = typeInfo == null
- ? guessed.getKey()
- : typeInfo.isCollection()
- ? ODataPropertyType.COLLECTION
- : typeInfo.isPrimitiveType()
- ? ODataPropertyType.PRIMITIVE
- : node.isValueNode()
- ? ODataPropertyType.ENUM
- : ODataPropertyType.COMPLEX;
-
- switch (propType) {
- case COLLECTION:
- valuable.setValue(fromCollection(node.elements(), typeInfo, codec));
- break;
-
- case COMPLEX:
- if (node.has(jsonType)) {
- valuable.setType(node.get(jsonType).asText());
- ((ObjectNode) node).remove(jsonType);
- }
- valuable.setValue(fromComplex((ObjectNode) node, codec));
- break;
-
- case ENUM:
- valuable.setValue(new EnumValueImpl(node.asText()));
- break;
-
- case PRIMITIVE:
- if (valuable.getType() == null && typeInfo != null) {
- valuable.setType(typeInfo.getFullQualifiedName().toString());
- }
- valuable.setValue(fromPrimitive(node, typeInfo));
- break;
-
- case EMPTY:
- default:
- valuable.setValue(new PrimitiveValueImpl(StringUtils.EMPTY));
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonSerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonSerializer.java
deleted file mode 100644
index 1713a7c..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractJsonSerializer.java
+++ /dev/null
@@ -1,248 +0,0 @@
-/*
- * 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.commons.core.data;
-
-import com.fasterxml.jackson.core.JsonGenerator;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import org.apache.commons.lang3.ArrayUtils;
-import org.apache.commons.lang3.BooleanUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.math.NumberUtils;
-import org.apache.olingo.commons.api.Constants;
-import org.apache.olingo.commons.api.data.Annotatable;
-import org.apache.olingo.commons.api.data.Annotation;
-import org.apache.olingo.commons.api.data.CollectionValue;
-import org.apache.olingo.commons.api.data.Entity;
-import org.apache.olingo.commons.api.data.Link;
-import org.apache.olingo.commons.api.data.Linked;
-import org.apache.olingo.commons.api.data.PrimitiveValue;
-import org.apache.olingo.commons.api.data.Property;
-import org.apache.olingo.commons.api.data.Valuable;
-import org.apache.olingo.commons.api.data.Value;
-import org.apache.olingo.commons.api.domain.ODataLinkType;
-import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
-import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
-import org.apache.olingo.commons.core.edm.EdmTypeInfo;
-
-abstract class AbstractJsonSerializer<T> extends ODataJacksonSerializer<T> {
-
- private static final EdmPrimitiveTypeKind[] NUMBER_TYPES = {
- EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte,
- EdmPrimitiveTypeKind.Single, EdmPrimitiveTypeKind.Double,
- EdmPrimitiveTypeKind.Int16, EdmPrimitiveTypeKind.Int32, EdmPrimitiveTypeKind.Int64,
- EdmPrimitiveTypeKind.Decimal
- };
-
- private final JSONGeoValueSerializer geoSerializer = new JSONGeoValueSerializer();
-
- protected void links(final Linked linked, final JsonGenerator jgen) throws IOException {
- if (serverMode) {
- serverLinks(linked, jgen);
- } else {
- clientLinks(linked, jgen);
- }
- }
-
- protected void clientLinks(final Linked linked, final JsonGenerator jgen) throws IOException {
- final Map<String, List<String>> entitySetLinks = new HashMap<String, List<String>>();
- for (Link link : linked.getNavigationLinks()) {
- for (Annotation annotation : link.getAnnotations()) {
- valuable(jgen, annotation, link.getTitle() + "@" + annotation.getTerm());
- }
-
- ODataLinkType type = null;
- try {
- type = ODataLinkType.fromString(version, link.getRel(), link.getType());
- } catch (IllegalArgumentException e) {
- // ignore
- }
-
- if (type == ODataLinkType.ENTITY_SET_NAVIGATION) {
- final List<String> uris;
- if (entitySetLinks.containsKey(link.getTitle())) {
- uris = entitySetLinks.get(link.getTitle());
- } else {
- uris = new ArrayList<String>();
- entitySetLinks.put(link.getTitle(), uris);
- }
- if (StringUtils.isNotBlank(link.getHref())) {
- uris.add(link.getHref());
- }
- } else {
- if (StringUtils.isNotBlank(link.getHref())) {
- jgen.writeStringField(link.getTitle() + Constants.JSON_BIND_LINK_SUFFIX, link.getHref());
- }
- }
-
- if (link.getInlineEntity() != null) {
- jgen.writeObjectField(link.getTitle(), link.getInlineEntity());
- } else if (link.getInlineEntitySet() != null) {
- jgen.writeArrayFieldStart(link.getTitle());
- for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
- jgen.writeObject(subEntry);
- }
- jgen.writeEndArray();
- }
- }
- for (Map.Entry<String, List<String>> entitySetLink : entitySetLinks.entrySet()) {
- if (!entitySetLink.getValue().isEmpty()) {
- jgen.writeArrayFieldStart(entitySetLink.getKey() + Constants.JSON_BIND_LINK_SUFFIX);
- for (String uri : entitySetLink.getValue()) {
- jgen.writeString(uri);
- }
- jgen.writeEndArray();
- }
- }
- }
-
- protected void serverLinks(final Linked linked, final JsonGenerator jgen) throws IOException {
- if (linked instanceof JSONEntityImpl) {
- for (Link link : ((JSONEntityImpl) linked).getMediaEditLinks()) {
- if (StringUtils.isNotBlank(link.getHref())) {
- jgen.writeStringField(
- link.getTitle() + StringUtils.prependIfMissing(
- version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAEDIT_LINK), "@"),
- link.getHref());
- }
- }
- }
-
- for (Link link : linked.getAssociationLinks()) {
- if (StringUtils.isNotBlank(link.getHref())) {
- jgen.writeStringField(
- link.getTitle() + version.getJSONMap().get(ODataServiceVersion.JSON_ASSOCIATION_LINK),
- link.getHref());
- }
- }
-
- for (Link link : linked.getNavigationLinks()) {
- for (Annotation annotation : link.getAnnotations()) {
- valuable(jgen, annotation, link.getTitle() + "@" + annotation.getTerm());
- }
-
- if (StringUtils.isNotBlank(link.getHref())) {
- jgen.writeStringField(
- link.getTitle() + version.getJSONMap().get(ODataServiceVersion.JSON_NAVIGATION_LINK),
- link.getHref());
- }
-
- if (link.getInlineEntity() != null) {
- jgen.writeObjectField(link.getTitle(), link.getInlineEntity());
- } else if (link.getInlineEntitySet() != null) {
- jgen.writeArrayFieldStart(link.getTitle());
- for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
- jgen.writeObject(subEntry);
- }
- jgen.writeEndArray();
- }
- }
- }
-
- private void collection(final JsonGenerator jgen, final String itemType, final CollectionValue value)
- throws IOException {
-
- jgen.writeStartArray();
- for (Value item : value.get()) {
- value(jgen, itemType, item);
- }
- jgen.writeEndArray();
- }
-
- protected void primitiveValue(final JsonGenerator jgen, final EdmTypeInfo typeInfo, final PrimitiveValue value)
- throws IOException {
-
- final boolean isNumber = typeInfo == null
- ? NumberUtils.isNumber(value.get())
- : ArrayUtils.contains(NUMBER_TYPES, typeInfo.getPrimitiveTypeKind());
- final boolean isBoolean = typeInfo == null
- ? (value.get().equalsIgnoreCase(Boolean.TRUE.toString())
- || value.get().equalsIgnoreCase(Boolean.FALSE.toString()))
- : typeInfo.getPrimitiveTypeKind() == EdmPrimitiveTypeKind.Boolean;
-
- if (isNumber) {
- jgen.writeNumber(value.get());
- } else if (isBoolean) {
- jgen.writeBoolean(BooleanUtils.toBoolean(value.get()));
- } else {
- jgen.writeString(value.get());
- }
- }
-
- private void value(final JsonGenerator jgen, final String type, final Value value) throws IOException {
- final EdmTypeInfo typeInfo = type == null
- ? null
- : new EdmTypeInfo.Builder().setTypeExpression(type).build();
-
- if (value == null || value.isNull()) {
- jgen.writeNull();
- } else if (value.isPrimitive()) {
- primitiveValue(jgen, typeInfo, value.asPrimitive());
- } else if (value.isEnum()) {
- jgen.writeString(value.asEnum().get());
- } else if (value.isGeospatial()) {
- jgen.writeStartObject();
- geoSerializer.serialize(jgen, value.asGeospatial().get());
- jgen.writeEndObject();
- } else if (value.isCollection()) {
- collection(jgen, typeInfo == null ? null : typeInfo.getFullQualifiedName().toString(), value.asCollection());
- } else if (value.isComplex()) {
- jgen.writeStartObject();
-
- if (typeInfo != null) {
- jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE), typeInfo.external(version));
- }
-
- for (Property property : value.asComplex().get()) {
- valuable(jgen, property, property.getName());
- }
- if (value.isLinkedComplex()) {
- links(value.asLinkedComplex(), jgen);
- }
-
- jgen.writeEndObject();
- }
- }
-
- protected void valuable(final JsonGenerator jgen, final Valuable valuable, final String name) throws IOException {
- if (!Constants.VALUE.equals(name) && !(valuable instanceof Annotation) && !valuable.getValue().isComplex()) {
- String type = valuable.getType();
- if (StringUtils.isBlank(type) && valuable.getValue().isPrimitive() || valuable.getValue().isNull()) {
- type = EdmPrimitiveTypeKind.String.getFullQualifiedName().toString();
- }
- if (StringUtils.isNotBlank(type)) {
- jgen.writeFieldName(
- name + StringUtils.prependIfMissing(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE), "@"));
- jgen.writeString(new EdmTypeInfo.Builder().setTypeExpression(type).build().external(version));
- }
- }
-
- if (valuable instanceof Annotatable) {
- for (Annotation annotation : ((Annotatable) valuable).getAnnotations()) {
- valuable(jgen, annotation, name + "@" + annotation.getTerm());
- }
- }
-
- jgen.writeFieldName(name);
- value(jgen, valuable.getType(), valuable.getValue());
- }
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java
deleted file mode 100644
index 1e9cf51..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.commons.core.data;
-
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.olingo.commons.api.domain.ODataError;
-import org.apache.olingo.commons.api.domain.ODataErrorDetail;
-
-/**
- * Example:
- * <tt>
- * {
- * "error": { "code": "501", "message": "Unsupported functionality", "target": "query", "details": [ { "code": "301",
- * "target": "$search", "message": "$search query option not supported" } ], "innererror": { "trace": [...], "context":
- * {...} } } }
- * </tt>.
- */
-public abstract class AbstractODataError implements ODataError {
-
- private String code;
-
- private String message;
-
- private String target;
-
- private List<ODataErrorDetail> details;
-
- private Map<String, String> innerError = new LinkedHashMap<String, String>();
-
- @Override
- public String getCode() {
- return code;
- }
-
- public void setCode(final String code) {
- this.code = code;
- }
-
- @Override
- public String getMessage() {
- return message;
- }
-
- public void setMessage(final String message) {
- this.message = message;
- }
-
- @Override
- public String getTarget() {
- return target;
- }
-
- public void setTarget(final String target) {
- this.target = target;
- }
-
- @Override
- public List<ODataErrorDetail> getDetails() {
- return details;
- }
-
- public void setDetails(final List<ODataErrorDetail> detail) {
- this.details = detail;
- }
-
- @Override
- public Map<String, String> getInnerError() {
- return innerError;
- }
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractProperty.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractProperty.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractProperty.java
deleted file mode 100644
index 2363c0b..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractProperty.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * 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.commons.core.data;
-
-import org.apache.olingo.commons.api.data.Property;
-import org.apache.olingo.commons.api.data.Value;
-
-public abstract class AbstractProperty extends AbstractAnnotatedObject implements Property {
-
- private static final long serialVersionUID = -7175704800169997060L;
-
- private String name;
-
- private String type;
-
- private Value value;
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public void setName(final String name) {
- this.name = name;
- }
-
- @Override
- public String getType() {
- return type;
- }
-
- @Override
- public void setType(final String type) {
- this.type = type;
- }
-
- @Override
- public Value getValue() {
- return value;
- }
-
- @Override
- public void setValue(final Value value) {
- this.value = value;
- }
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java
index 51a5465..dec0028 100644
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java
@@ -18,8 +18,6 @@
*/
package org.apache.olingo.commons.core.data;
-import com.fasterxml.aalto.stax.InputFactoryImpl;
-import org.apache.olingo.commons.core.data.v4.AtomDeltaImpl;
import java.io.InputStream;
import java.net.URI;
import java.text.ParseException;
@@ -27,6 +25,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
@@ -34,25 +33,35 @@ import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
+
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.data.Annotation;
import org.apache.olingo.commons.api.data.CollectionValue;
import org.apache.olingo.commons.api.data.DeletedEntity.Reason;
+import org.apache.olingo.commons.api.data.Delta;
+import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntitySet;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.data.Valuable;
import org.apache.olingo.commons.api.data.Value;
+import org.apache.olingo.commons.api.data.v3.LinkCollection;
+import org.apache.olingo.commons.api.domain.ODataError;
import org.apache.olingo.commons.api.domain.ODataOperation;
import org.apache.olingo.commons.api.domain.ODataPropertyType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ContentType;
-import org.apache.olingo.commons.core.data.v3.XMLLinkCollectionImpl;
+import org.apache.olingo.commons.api.op.ODataDeserializer;
+import org.apache.olingo.commons.api.op.ODataDeserializerException;
+import org.apache.olingo.commons.core.data.v3.LinkCollectionImpl;
+import org.apache.olingo.commons.core.data.v4.DeltaImpl;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
-public class AtomDeserializer extends AbstractAtomDealer {
+import com.fasterxml.aalto.stax.InputFactoryImpl;
+
+public class AtomDeserializer extends AbstractAtomDealer implements ODataDeserializer {
protected static final XMLInputFactory FACTORY = new InputFactoryImpl();
@@ -68,7 +77,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private Value fromPrimitive(final XMLEventReader reader, final StartElement start,
- final EdmTypeInfo typeInfo) throws XMLStreamException {
+ final EdmTypeInfo typeInfo) throws XMLStreamException {
Value value = null;
@@ -78,12 +87,12 @@ public class AtomDeserializer extends AbstractAtomDealer {
if (event.isStartElement() && typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) {
final EdmPrimitiveTypeKind geoType = EdmPrimitiveTypeKind.valueOfFQN(
- version, typeInfo.getFullQualifiedName().toString());
+ version, typeInfo.getFullQualifiedName().toString());
value = new GeospatialValueImpl(this.geoDeserializer.deserialize(reader, event.asStartElement(), geoType));
}
if (event.isCharacters() && !event.asCharacters().isWhiteSpace()
- && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) {
+ && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) {
value = new PrimitiveValueImpl(event.asCharacters().getData());
}
@@ -97,7 +106,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private Value fromComplexOrEnum(final XMLEventReader reader, final StartElement start)
- throws XMLStreamException {
+ throws XMLStreamException {
Value value = null;
@@ -108,8 +117,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
if (event.isStartElement()) {
if (value == null) {
value = version.compareTo(ODataServiceVersion.V40) < 0
- ? new ComplexValueImpl()
- : new LinkedComplexValueImpl();
+ ? new ComplexValueImpl()
+ : new LinkedComplexValueImpl();
}
if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
@@ -132,12 +141,12 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
if (link.getRel().startsWith(
- version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL))) {
+ version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL))) {
value.asLinkedComplex().getNavigationLinks().add(link);
inline(reader, event.asStartElement(), link);
} else if (link.getRel().startsWith(
- version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL))) {
+ version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL))) {
value.asLinkedComplex().getAssociationLinks().add(link);
}
@@ -159,13 +168,13 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private CollectionValue fromCollection(final XMLEventReader reader, final StartElement start,
- final EdmTypeInfo typeInfo) throws XMLStreamException {
+ final EdmTypeInfo typeInfo) throws XMLStreamException {
final CollectionValueImpl value = new CollectionValueImpl();
final EdmTypeInfo type = typeInfo == null
- ? null
- : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();
+ ? null
+ : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build();
boolean foundEndProperty = false;
while (reader.hasNext() && !foundEndProperty) {
@@ -173,16 +182,16 @@ public class AtomDeserializer extends AbstractAtomDealer {
if (event.isStartElement()) {
switch (guessPropertyType(reader, typeInfo)) {
- case COMPLEX:
- case ENUM:
- value.get().add(fromComplexOrEnum(reader, event.asStartElement()));
- break;
+ case COMPLEX:
+ case ENUM:
+ value.get().add(fromComplexOrEnum(reader, event.asStartElement()));
+ break;
- case PRIMITIVE:
- value.get().add(fromPrimitive(reader, event.asStartElement(), type));
- break;
+ case PRIMITIVE:
+ value.get().add(fromPrimitive(reader, event.asStartElement(), type));
+ break;
- default:
+ default:
// do not add null or empty values
}
}
@@ -196,7 +205,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private ODataPropertyType guessPropertyType(final XMLEventReader reader, final EdmTypeInfo typeInfo)
- throws XMLStreamException {
+ throws XMLStreamException {
XMLEvent child = null;
while (reader.hasNext() && child == null) {
@@ -211,8 +220,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
final ODataPropertyType type;
if (child == null) {
type = typeInfo == null || typeInfo.isPrimitiveType()
- ? ODataPropertyType.PRIMITIVE
- : ODataPropertyType.ENUM;
+ ? ODataPropertyType.PRIMITIVE
+ : ODataPropertyType.ENUM;
} else {
if (child.isStartElement()) {
if (Constants.NS_GML.equals(child.asStartElement().getName().getNamespaceURI())) {
@@ -224,8 +233,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
} else if (child.isCharacters()) {
type = typeInfo == null || typeInfo.isPrimitiveType()
- ? ODataPropertyType.PRIMITIVE
- : ODataPropertyType.ENUM;
+ ? ODataPropertyType.PRIMITIVE
+ : ODataPropertyType.ENUM;
} else {
type = ODataPropertyType.EMPTY;
}
@@ -234,10 +243,10 @@ public class AtomDeserializer extends AbstractAtomDealer {
return type;
}
- private AtomPropertyImpl property(final XMLEventReader reader, final StartElement start)
- throws XMLStreamException {
+ private Property property(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException {
- final AtomPropertyImpl property = new AtomPropertyImpl();
+ final PropertyImpl property = new PropertyImpl();
if (ODataServiceVersion.V40 == version && propertyValueQName.equals(start.getName())) {
// retrieve name from context
@@ -255,7 +264,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private void valuable(final Valuable valuable, final XMLEventReader reader, final StartElement start)
- throws XMLStreamException {
+ throws XMLStreamException {
final Attribute nullAttr = start.getAttributeByName(this.nullQName);
@@ -265,41 +274,41 @@ public class AtomDeserializer extends AbstractAtomDealer {
final String typeAttrValue = typeAttr == null ? null : typeAttr.getValue();
final EdmTypeInfo typeInfo = StringUtils.isBlank(typeAttrValue)
- ? null
- : new EdmTypeInfo.Builder().setTypeExpression(typeAttrValue).build();
+ ? null
+ : new EdmTypeInfo.Builder().setTypeExpression(typeAttrValue).build();
if (typeInfo != null) {
valuable.setType(typeInfo.internal());
}
final ODataPropertyType propType = typeInfo == null
- ? guessPropertyType(reader, typeInfo)
- : typeInfo.isCollection()
+ ? guessPropertyType(reader, typeInfo)
+ : typeInfo.isCollection()
? ODataPropertyType.COLLECTION
: typeInfo.isPrimitiveType()
- ? ODataPropertyType.PRIMITIVE
- : ODataPropertyType.COMPLEX;
+ ? ODataPropertyType.PRIMITIVE
+ : ODataPropertyType.COMPLEX;
switch (propType) {
- case COLLECTION:
- value = fromCollection(reader, start, typeInfo);
- break;
-
- case COMPLEX:
- value = fromComplexOrEnum(reader, start);
- break;
-
- case PRIMITIVE:
- // No type specified? Defaults to Edm.String
- if (typeInfo == null) {
- valuable.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName().toString());
- }
- value = fromPrimitive(reader, start, typeInfo);
- break;
+ case COLLECTION:
+ value = fromCollection(reader, start, typeInfo);
+ break;
+
+ case COMPLEX:
+ value = fromComplexOrEnum(reader, start);
+ break;
+
+ case PRIMITIVE:
+ // No type specified? Defaults to Edm.String
+ if (typeInfo == null) {
+ valuable.setType(EdmPrimitiveTypeKind.String.getFullQualifiedName().toString());
+ }
+ value = fromPrimitive(reader, start, typeInfo);
+ break;
- case EMPTY:
- default:
- value = new PrimitiveValueImpl(StringUtils.EMPTY);
+ case EMPTY:
+ default:
+ value = new PrimitiveValueImpl(StringUtils.EMPTY);
}
} else {
value = new NullValueImpl();
@@ -308,10 +317,15 @@ public class AtomDeserializer extends AbstractAtomDealer {
valuable.setValue(value);
}
- private ResWrap<AtomPropertyImpl> property(final InputStream input) throws XMLStreamException {
- final XMLEventReader reader = getReader(input);
- final StartElement start = skipBeforeFirstStartElement(reader);
- return getContainer(start, property(reader, start));
+ @Override
+ public ResWrap<Property> toProperty(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, property(reader, start));
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ }
}
private StartElement skipBeforeFirstStartElement(final XMLEventReader reader) throws XMLStreamException {
@@ -330,7 +344,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private void common(final XMLEventReader reader, final StartElement start,
- final AbstractODataObject object, final String key) throws XMLStreamException {
+ final AbstractODataObject object, final String key) throws XMLStreamException {
boolean foundEndElement = false;
while (reader.hasNext() && !foundEndElement) {
@@ -351,7 +365,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private void inline(final XMLEventReader reader, final StartElement start, final LinkImpl link)
- throws XMLStreamException {
+ throws XMLStreamException {
boolean foundEndElement = false;
while (reader.hasNext() && !foundEndElement) {
@@ -387,17 +401,17 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
}
- private ResWrap<AtomDeltaImpl> delta(final InputStream input) throws XMLStreamException {
+ public ResWrap<Delta> delta(final InputStream input) throws XMLStreamException {
final XMLEventReader reader = getReader(input);
final StartElement start = skipBeforeFirstStartElement(reader);
return getContainer(start, delta(reader, start));
}
- private AtomDeltaImpl delta(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ private Delta delta(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
return null;
}
- final AtomDeltaImpl delta = new AtomDeltaImpl();
+ final DeltaImpl delta = new DeltaImpl();
final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
if (xmlBase != null) {
delta.setBaseURI(xmlBase.getValue());
@@ -449,7 +463,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
delta.getDeletedEntities().add(deletedEntity);
} else if (linkQName.equals(event.asStartElement().getName())
- || deletedLinkQName.equals(event.asStartElement().getName())) {
+ || deletedLinkQName.equals(event.asStartElement().getName())) {
final DeltaLinkImpl link = new DeltaLinkImpl();
@@ -458,7 +472,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
link.setSource(URI.create(source.getValue()));
}
final Attribute relationship =
- event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_RELATIONSHIP));
+ event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_RELATIONSHIP));
if (relationship != null) {
link.setRelationship(relationship.getValue());
}
@@ -483,16 +497,16 @@ public class AtomDeserializer extends AbstractAtomDealer {
return delta;
}
- private ResWrap<XMLLinkCollectionImpl> linkCollection(final InputStream input) throws XMLStreamException {
+ public ResWrap<LinkCollection> linkCollection(final InputStream input) throws XMLStreamException {
final XMLEventReader reader = getReader(input);
final StartElement start = skipBeforeFirstStartElement(reader);
return getContainer(start, linkCollection(reader, start));
}
- private XMLLinkCollectionImpl linkCollection(final XMLEventReader reader, final StartElement start)
- throws XMLStreamException {
+ private LinkCollection linkCollection(final XMLEventReader reader, final StartElement start)
+ throws XMLStreamException {
- final XMLLinkCollectionImpl linkCollection = new XMLLinkCollectionImpl();
+ final LinkCollectionImpl linkCollection = new LinkCollectionImpl();
boolean isURI = false;
boolean isNext = false;
@@ -517,8 +531,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
return linkCollection;
}
- private void properties(final XMLEventReader reader, final StartElement start, final AtomEntityImpl entity)
- throws XMLStreamException {
+ private void properties(final XMLEventReader reader, final StartElement start, final EntityImpl entity)
+ throws XMLStreamException {
final Map<String, List<Annotation>> annotations = new HashMap<String, List<Annotation>>();
@@ -529,7 +543,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
if (event.isStartElement()) {
if (annotationQName.equals(event.asStartElement().getName())) {
final String target = event.asStartElement().
- getAttributeByName(QName.valueOf(Constants.ATTR_TARGET)).getValue();
+ getAttributeByName(QName.valueOf(Constants.ATTR_TARGET)).getValue();
if (!annotations.containsKey(target)) {
annotations.put(target, new ArrayList<Annotation>());
}
@@ -552,7 +566,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
private Annotation annotation(final XMLEventReader reader, final StartElement start)
- throws XMLStreamException {
+ throws XMLStreamException {
final Annotation annotation = new AnnotationImpl();
@@ -562,8 +576,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
return annotation;
}
- private AtomEntityImpl entityRef(final StartElement start) throws XMLStreamException {
- final AtomEntityImpl entity = new AtomEntityImpl();
+ private EntityImpl entityRef(final StartElement start) throws XMLStreamException {
+ final EntityImpl entity = new EntityImpl();
final Attribute entityRefId = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID);
if (entityRefId != null) {
@@ -573,12 +587,12 @@ public class AtomDeserializer extends AbstractAtomDealer {
return entity;
}
- private AtomEntityImpl entity(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
- final AtomEntityImpl entity;
+ private Entity entity(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ final EntityImpl entity;
if (entryRefQName.equals(start.getName())) {
entity = entityRef(start);
} else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
- entity = new AtomEntityImpl();
+ entity = new EntityImpl();
final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
if (xmlBase != null) {
entity.setBaseURI(xmlBase.getValue());
@@ -636,16 +650,16 @@ public class AtomDeserializer extends AbstractAtomDealer {
entity.setMediaETag(mediaETag.getValue());
}
} else if (link.getRel().startsWith(
- version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL))) {
-
+ version.getNamespaceMap().get(ODataServiceVersion.NAVIGATION_LINK_REL))) {
+
entity.getNavigationLinks().add(link);
inline(reader, event.asStartElement(), link);
} else if (link.getRel().startsWith(
- version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL))) {
+ version.getNamespaceMap().get(ODataServiceVersion.ASSOCIATION_LINK_REL))) {
entity.getAssociationLinks().add(link);
} else if (link.getRel().startsWith(
- version.getNamespaceMap().get(ODataServiceVersion.MEDIA_EDIT_LINK_REL))) {
+ version.getNamespaceMap().get(ODataServiceVersion.MEDIA_EDIT_LINK_REL))) {
final Attribute metag = event.asStartElement().getAttributeByName(etagQName);
if (metag != null) {
@@ -656,7 +670,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
} else if (actionQName.equals(event.asStartElement().getName())) {
final ODataOperation operation = new ODataOperation();
final Attribute metadata =
- event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_METADATA));
+ event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATTR_METADATA));
if (metadata != null) {
operation.setMetadataAnchor(metadata.getValue());
}
@@ -699,14 +713,19 @@ public class AtomDeserializer extends AbstractAtomDealer {
return entity;
}
- private ResWrap<AtomEntityImpl> entity(final InputStream input) throws XMLStreamException {
- final XMLEventReader reader = getReader(input);
- final StartElement start = skipBeforeFirstStartElement(reader);
- return getContainer(start, entity(reader, start));
+ @Override
+ public ResWrap<Entity> toEntity(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, entity(reader, start));
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ }
}
private void count(final XMLEventReader reader, final StartElement start, final EntitySet entitySet)
- throws XMLStreamException {
+ throws XMLStreamException {
boolean foundEndElement = false;
while (reader.hasNext() && !foundEndElement) {
@@ -722,11 +741,11 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
}
- private AtomEntitySetImpl entitySet(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ private EntitySet entitySet(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
if (!Constants.QNAME_ATOM_ELEM_FEED.equals(start.getName())) {
return null;
}
- final AtomEntitySetImpl entitySet = new AtomEntitySetImpl();
+ final EntitySetImpl entitySet = new EntitySetImpl();
final Attribute xmlBase = start.getAttributeByName(Constants.QNAME_ATTR_XML_BASE);
if (xmlBase != null) {
entitySet.setBaseURI(xmlBase.getValue());
@@ -779,14 +798,19 @@ public class AtomDeserializer extends AbstractAtomDealer {
return entitySet;
}
- private ResWrap<AtomEntitySetImpl> entitySet(final InputStream input) throws XMLStreamException {
- final XMLEventReader reader = getReader(input);
- final StartElement start = skipBeforeFirstStartElement(reader);
- return getContainer(start, entitySet(reader, start));
+ @Override
+ public ResWrap<EntitySet> toEntitySet(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return getContainer(start, entitySet(reader, start));
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ }
}
- private XMLODataErrorImpl error(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
- final XMLODataErrorImpl error = new XMLODataErrorImpl();
+ private ODataError error(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
+ final ODataErrorImpl error = new ODataErrorImpl();
boolean setCode = false;
boolean codeSet = false;
@@ -835,10 +859,15 @@ public class AtomDeserializer extends AbstractAtomDealer {
return error;
}
- private ResWrap<XMLODataErrorImpl> error(final InputStream input) throws XMLStreamException {
- final XMLEventReader reader = getReader(input);
- final StartElement start = skipBeforeFirstStartElement(reader);
- return getContainer(start, error(reader, start));
+ @Override
+ public ODataError toError(final InputStream input) throws ODataDeserializerException {
+ try {
+ final XMLEventReader reader = getReader(input);
+ final StartElement start = skipBeforeFirstStartElement(reader);
+ return error(reader, start);
+ } catch (XMLStreamException e) {
+ throw new ODataDeserializerException(e);
+ }
}
private <T> ResWrap<T> getContainer(final StartElement start, final T object) {
@@ -846,28 +875,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
final Attribute metadataETag = start.getAttributeByName(metadataEtagQName);
return new ResWrap<T>(
- context == null ? null : URI.create(context.getValue()),
- metadataETag == null ? null : metadataETag.getValue(),
- object);
- }
-
- @SuppressWarnings("unchecked")
- public <T, V extends T> ResWrap<T> read(final InputStream input, final Class<V> reference)
- throws XMLStreamException {
-
- if (XMLODataErrorImpl.class.equals(reference)) {
- return (ResWrap<T>) error(input);
- } else if (AtomEntitySetImpl.class.equals(reference)) {
- return (ResWrap<T>) entitySet(input);
- } else if (AtomEntityImpl.class.equals(reference)) {
- return (ResWrap<T>) entity(input);
- } else if (AtomPropertyImpl.class.equals(reference)) {
- return (ResWrap<T>) property(input);
- } else if (XMLLinkCollectionImpl.class.equals(reference)) {
- return (ResWrap<T>) linkCollection(input);
- } else if (AtomDeltaImpl.class.equals(reference)) {
- return (ResWrap<T>) delta(input);
- }
- return null;
+ context == null ? null : URI.create(context.getValue()),
+ metadataETag == null ? null : metadataETag.getValue(),
+ object);
}
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntityImpl.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntityImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntityImpl.java
deleted file mode 100644
index 6c7fb13..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntityImpl.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * 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.commons.core.data;
-
-public class AtomEntityImpl extends AbstractEntity {
-
- private static final long serialVersionUID = 6973729343868293279L;
-
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntitySetImpl.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntitySetImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntitySetImpl.java
deleted file mode 100644
index 397b18e..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomEntitySetImpl.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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.commons.core.data;
-
-/**
- * List of entries, represented via Atom.
- */
-public class AtomEntitySetImpl extends AbstractEntitySet {
-
- private static final long serialVersionUID = 5466590540021319153L;
-
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomPropertyImpl.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomPropertyImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomPropertyImpl.java
deleted file mode 100644
index e0018bc..0000000
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomPropertyImpl.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * 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.commons.core.data;
-
-public class AtomPropertyImpl extends AbstractProperty {
-
- private static final long serialVersionUID = 48748492242474814L;
-
-}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomSerializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomSerializer.java
index 200a4bf..3d4eae6 100644
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomSerializer.java
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomSerializer.java
@@ -18,31 +18,36 @@
*/
package org.apache.olingo.commons.core.data;
-import com.fasterxml.aalto.stax.OutputFactoryImpl;
import java.io.Writer;
import java.util.Collections;
import java.util.List;
+
import javax.xml.XMLConstants;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.data.Annotation;
import org.apache.olingo.commons.api.data.CollectionValue;
-import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntitySet;
import org.apache.olingo.commons.api.data.Link;
import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.data.Value;
import org.apache.olingo.commons.api.domain.ODataOperation;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ContentType;
+import org.apache.olingo.commons.api.op.ODataSerializer;
+import org.apache.olingo.commons.api.op.ODataSerializerException;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
-public class AtomSerializer extends AbstractAtomDealer {
+import com.fasterxml.aalto.stax.OutputFactoryImpl;
+
+public class AtomSerializer extends AbstractAtomDealer implements ODataSerializer {
private static final XMLOutputFactory FACTORY = new OutputFactoryImpl();
@@ -64,10 +69,10 @@ public class AtomSerializer extends AbstractAtomDealer {
for (Value item : value.get()) {
if (version.compareTo(ODataServiceVersion.V40) < 0) {
writer.writeStartElement(Constants.PREFIX_DATASERVICES, Constants.ELEM_ELEMENT,
- version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
} else {
writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ELEM_ELEMENT,
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
}
value(writer, item);
writer.writeEndElement();
@@ -91,14 +96,14 @@ public class AtomSerializer extends AbstractAtomDealer {
}
public void property(final XMLStreamWriter writer, final Property property, final boolean standalone)
- throws XMLStreamException {
+ throws XMLStreamException {
if (version.compareTo(ODataServiceVersion.V40) >= 0 && standalone) {
writer.writeStartElement(Constants.PREFIX_METADATA, Constants.VALUE,
- version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
} else {
writer.writeStartElement(Constants.PREFIX_DATASERVICES, property.getName(),
- version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
+ version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
}
if (standalone) {
@@ -109,13 +114,13 @@ public class AtomSerializer extends AbstractAtomDealer {
final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.ATTR_TYPE, typeInfo.external(version));
+ Constants.ATTR_TYPE, typeInfo.external(version));
}
}
if (property.getValue().isNull()) {
writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.ATTR_NULL, Boolean.TRUE.toString());
+ Constants.ATTR_NULL, Boolean.TRUE.toString());
} else {
value(writer, property.getValue());
if (property.getValue().isLinkedComplex()) {
@@ -174,7 +179,7 @@ public class AtomSerializer extends AbstractAtomDealer {
if (link.getInlineEntity() != null || link.getInlineEntitySet() != null) {
writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ATOM_ELEM_INLINE,
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
if (link.getInlineEntity() != null) {
writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
@@ -214,10 +219,10 @@ public class AtomSerializer extends AbstractAtomDealer {
}
private void annotation(final XMLStreamWriter writer, final Annotation annotation, final String target)
- throws XMLStreamException {
+ throws XMLStreamException {
writer.writeStartElement(Constants.PREFIX_METADATA, Constants.ANNOTATION,
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
writer.writeAttribute(Constants.ATOM_ATTR_TERM, annotation.getTerm());
@@ -229,13 +234,13 @@ public class AtomSerializer extends AbstractAtomDealer {
final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(annotation.getType()).build();
if (!EdmPrimitiveTypeKind.String.getFullQualifiedName().toString().equals(typeInfo.internal())) {
writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.ATTR_TYPE, typeInfo.external(version));
+ Constants.ATTR_TYPE, typeInfo.external(version));
}
}
if (annotation.getValue().isNull()) {
writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.ATTR_NULL, Boolean.TRUE.toString());
+ Constants.ATTR_NULL, Boolean.TRUE.toString());
} else {
value(writer, annotation.getValue());
}
@@ -250,8 +255,8 @@ public class AtomSerializer extends AbstractAtomDealer {
if (serverMode && StringUtils.isNotBlank(entity.getETag())) {
writer.writeAttribute(
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.ATOM_ATTR_ETAG, entity.getETag());
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATOM_ATTR_ETAG, entity.getETag());
}
if (entity.getId() != null) {
@@ -264,7 +269,7 @@ public class AtomSerializer extends AbstractAtomDealer {
writer.writeAttribute(Constants.ATOM_ATTR_SCHEME, version.getNamespaceMap().get(ODataServiceVersion.NS_SCHEME));
if (StringUtils.isNotBlank(entity.getType())) {
writer.writeAttribute(Constants.ATOM_ATTR_TERM,
- new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version));
+ new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version));
}
writer.writeEndElement();
@@ -289,7 +294,7 @@ public class AtomSerializer extends AbstractAtomDealer {
if (serverMode) {
for (ODataOperation operation : entity.getOperations()) {
writer.writeStartElement(
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATOM_ELEM_ACTION);
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATOM_ELEM_ACTION);
writer.writeAttribute(Constants.ATTR_METADATA, operation.getMetadataAnchor());
writer.writeAttribute(Constants.ATTR_TITLE, operation.getTitle());
writer.writeAttribute(Constants.ATTR_TARGET, operation.getTarget().toASCIIString());
@@ -384,7 +389,7 @@ public class AtomSerializer extends AbstractAtomDealer {
if (entitySet.getCount() != null) {
writer.writeStartElement(
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATOM_ELEM_COUNT);
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATOM_ELEM_COUNT);
writer.writeCharacters(Integer.toString(entitySet.getCount()));
writer.writeEndElement();
}
@@ -416,14 +421,14 @@ public class AtomSerializer extends AbstractAtomDealer {
next.setRel(Constants.NEXT_LINK_REL);
next.setHref(entitySet.getNext().toASCIIString());
- links(writer, Collections.<Link>singletonList(next));
+ links(writer, Collections.<Link> singletonList(next));
}
if (entitySet.getDeltaLink() != null) {
final LinkImpl next = new LinkImpl();
next.setRel(Constants.DELTA_LINK_REL);
next.setHref(entitySet.getDeltaLink().toASCIIString());
- links(writer, Collections.<Link>singletonList(next));
+ links(writer, Collections.<Link> singletonList(next));
}
}
}
@@ -472,56 +477,64 @@ public class AtomSerializer extends AbstractAtomDealer {
writer.flush();
}
- public <T> void write(final Writer writer, final T obj) throws XMLStreamException {
- if (obj instanceof EntitySet) {
- entitySet(writer, (EntitySet) obj);
- } else if (obj instanceof Entity) {
- entity(writer, (Entity) obj);
- } else if (obj instanceof Property) {
- property(writer, (Property) obj);
- } else if (obj instanceof Link) {
- link(writer, (Link) obj);
+ public <T> void write(final Writer writer, final T obj) throws ODataSerializerException {
+ try {
+ if (obj instanceof EntitySet) {
+ entitySet(writer, (EntitySet) obj);
+ } else if (obj instanceof Entity) {
+ entity(writer, (Entity) obj);
+ } else if (obj instanceof Property) {
+ property(writer, (Property) obj);
+ } else if (obj instanceof Link) {
+ link(writer, (Link) obj);
+ }
+ } catch (final XMLStreamException e) {
+ throw new ODataSerializerException(e);
}
}
@SuppressWarnings("unchecked")
- public <T> void write(final Writer writer, final ResWrap<T> container) throws XMLStreamException {
+ public <T> void write(final Writer writer, final ResWrap<T> container) throws ODataSerializerException {
final T obj = container == null ? null : container.getPayload();
- if (obj instanceof EntitySet) {
- this.entitySet(writer, (ResWrap<EntitySet>) container);
- } else if (obj instanceof Entity) {
- entity(writer, (ResWrap<Entity>) container);
- } else if (obj instanceof Property) {
- property(writer, (Property) obj);
- } else if (obj instanceof Link) {
- link(writer, (Link) obj);
+ try {
+ if (obj instanceof EntitySet) {
+ this.entitySet(writer, (ResWrap<EntitySet>) container);
+ } else if (obj instanceof Entity) {
+ entity(writer, (ResWrap<Entity>) container);
+ } else if (obj instanceof Property) {
+ property(writer, (Property) obj);
+ } else if (obj instanceof Link) {
+ link(writer, (Link) obj);
+ }
+ } catch (final XMLStreamException e) {
+ throw new ODataSerializerException(e);
}
}
private <T> void addContextInfo(
- final XMLStreamWriter writer, final ResWrap<T> container) throws XMLStreamException {
+ final XMLStreamWriter writer, final ResWrap<T> container) throws XMLStreamException {
if (container.getContextURL() != null) {
String base = container.getContextURL().getServiceRoot().toASCIIString();
- if (container.getPayload() instanceof AtomEntitySetImpl) {
- ((AtomEntitySetImpl) container.getPayload()).setBaseURI(base);
+ if (container.getPayload() instanceof EntitySet) {
+ ((EntitySetImpl) container.getPayload()).setBaseURI(base);
}
- if (container.getPayload() instanceof AtomEntityImpl) {
- ((AtomEntityImpl) container.getPayload()).setBaseURI(base);
+ if (container.getPayload() instanceof Entity) {
+ ((EntityImpl) container.getPayload()).setBaseURI(base);
}
writer.writeAttribute(
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.CONTEXT,
- container.getContextURL().getURI().toASCIIString());
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.CONTEXT,
+ container.getContextURL().getURI().toASCIIString());
}
if (StringUtils.isNotBlank(container.getMetadataETag())) {
writer.writeAttribute(
- version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
- Constants.ATOM_ATTR_METADATAETAG,
- container.getMetadataETag());
+ version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
+ Constants.ATOM_ATTR_METADATAETAG,
+ container.getMetadataETag());
}
}
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntityImpl.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntityImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntityImpl.java
new file mode 100755
index 0000000..28451a9
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntityImpl.java
@@ -0,0 +1,193 @@
+/*
+ * 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.commons.core.data;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.Link;
+import org.apache.olingo.commons.api.data.Property;
+import org.apache.olingo.commons.api.domain.ODataOperation;
+
+/**
+ * Class implementing an OData entity.
+ */
+public class EntityImpl extends AbstractODataObject implements Entity {
+
+ private static final long serialVersionUID = 2127764552600969783L;
+
+ private String eTag;
+
+ private String type;
+
+ private Link readLink;
+
+ private Link editLink;
+
+ private final List<Link> associationLinks = new ArrayList<Link>();
+
+ private final List<Link> navigationLinks = new ArrayList<Link>();
+
+ private final List<Link> mediaEditLinks = new ArrayList<Link>();
+
+ private final List<ODataOperation> operations = new ArrayList<ODataOperation>();
+
+ private final List<Property> properties = new ArrayList<Property>();
+
+ private URI mediaContentSource;
+
+ private String mediaContentType;
+
+ private String mediaETag;
+
+ @Override
+ public String getETag() {
+ return eTag;
+ }
+
+ public void setETag(final String eTag) {
+ this.eTag = eTag;
+ }
+
+ @Override
+ public String getType() {
+ return type;
+ }
+
+ @Override
+ public void setType(final String type) {
+ this.type = type;
+ }
+
+ @Override
+ public Link getSelfLink() {
+ return readLink;
+ }
+
+ @Override
+ public void setSelfLink(final Link readLink) {
+ this.readLink = readLink;
+ }
+
+ @Override
+ public Link getEditLink() {
+ return editLink;
+ }
+
+ @Override
+ public void setEditLink(final Link editLink) {
+ this.editLink = editLink;
+ }
+
+ private Link getOneByTitle(final String name, final List<Link> links) {
+ Link result = null;
+
+ for (Link link : links) {
+ if (name.equals(link.getTitle())) {
+ result = link;
+ }
+ }
+
+ return result;
+ }
+
+ @Override
+ public Link getAssociationLink(final String name) {
+ return getOneByTitle(name, associationLinks);
+ }
+
+ @Override
+ public List<Link> getAssociationLinks() {
+ return associationLinks;
+ }
+
+ @Override
+ public Link getNavigationLink(final String name) {
+ return getOneByTitle(name, navigationLinks);
+ }
+
+ @Override
+ public List<Link> getNavigationLinks() {
+ return navigationLinks;
+ }
+
+ @Override
+ public List<Link> getMediaEditLinks() {
+ return mediaEditLinks;
+ }
+
+ @Override
+ public List<ODataOperation> getOperations() {
+ return operations;
+ }
+
+ @Override
+ public List<Property> getProperties() {
+ return properties;
+ }
+
+ @Override
+ public Property getProperty(final String name) {
+ Property result = null;
+
+ for (Property property : properties) {
+ if (name.equals(property.getName())) {
+ result = property;
+ }
+ }
+
+ return result;
+ }
+
+ @Override
+ public String getMediaContentType() {
+ return this.mediaContentType;
+ }
+
+ @Override
+ public void setMediaContentType(final String mediaContentType) {
+ this.mediaContentType = mediaContentType;
+ }
+
+ @Override
+ public URI getMediaContentSource() {
+ return this.mediaContentSource;
+ }
+
+ @Override
+ public void setMediaContentSource(final URI mediaContentSource) {
+ this.mediaContentSource = mediaContentSource;
+ }
+
+ @Override
+ public String getMediaETag() {
+ return mediaETag;
+ }
+
+ @Override
+ public void setMediaETag(final String eTag) {
+ this.mediaETag = eTag;
+ }
+
+ @Override
+ public boolean isMediaEntity() {
+ return this.mediaContentSource != null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntitySetImpl.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntitySetImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntitySetImpl.java
new file mode 100755
index 0000000..1920809
--- /dev/null
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/EntitySetImpl.java
@@ -0,0 +1,74 @@
+/*
+ * 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.commons.core.data;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.olingo.commons.api.data.Entity;
+import org.apache.olingo.commons.api.data.EntitySet;
+
+public class EntitySetImpl extends AbstractODataObject implements EntitySet {
+
+ private static final long serialVersionUID = -9159884750819150969L;
+
+ private Integer count;
+
+ private final List<Entity> entities = new ArrayList<Entity>();
+
+ private URI next;
+
+ private URI deltaLink;
+
+ @Override
+ public void setCount(final Integer count) {
+ this.count = count;
+ }
+
+ @Override
+ public Integer getCount() {
+ return count;
+ }
+
+ @Override
+ public List<Entity> getEntities() {
+ return entities;
+ }
+
+ @Override
+ public void setNext(final URI next) {
+ this.next = next;
+ }
+
+ @Override
+ public URI getNext() {
+ return next;
+ }
+
+ @Override
+ public URI getDeltaLink() {
+ return deltaLink;
+ }
+
+ @Override
+ public void setDeltaLink(final URI deltaLink) {
+ this.deltaLink = deltaLink;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/46a34178/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/JSONDeltaDeserializer.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/JSONDeltaDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/JSONDeltaDeserializer.java
index f712e74..9ca82f1 100644
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/JSONDeltaDeserializer.java
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/JSONDeltaDeserializer.java
@@ -18,37 +18,40 @@
*/
package org.apache.olingo.commons.core.data;
-import org.apache.olingo.commons.core.data.v4.JSONDeltaImpl;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
import java.util.Iterator;
+
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.data.ContextURL;
+import org.apache.olingo.commons.api.data.Delta;
import org.apache.olingo.commons.api.data.ResWrap;
+import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
+import org.apache.olingo.commons.api.op.ODataDeserializerException;
+import org.apache.olingo.commons.core.data.v4.DeltaImpl;
-public class JSONDeltaDeserializer extends AbstractJsonDeserializer<JSONDeltaImpl> {
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
- @Override
- protected ResWrap<JSONDeltaImpl> doDeserialize(final JsonParser parser, final DeserializationContext ctxt)
- throws IOException, JsonProcessingException {
+public class JSONDeltaDeserializer extends JsonDeserializer {
+
+ public JSONDeltaDeserializer(final ODataServiceVersion version, final boolean serverMode) {
+ super(version, serverMode);
+ }
+
+ protected ResWrap<Delta> doDeserialize(final JsonParser parser) throws IOException {
final ObjectNode tree = parser.getCodec().readTree(parser);
- final JSONDeltaImpl delta = new JSONDeltaImpl();
+ final DeltaImpl delta = new DeltaImpl();
- final URI contextURL;
- if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
- contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
- } else {
- contextURL = null;
- }
+ final URI contextURL = tree.hasNonNull(Constants.JSON_CONTEXT) ?
+ URI.create(tree.get(Constants.JSON_CONTEXT).textValue()) : null;
if (contextURL != null) {
delta.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA));
}
@@ -64,17 +67,15 @@ public class JSONDeltaDeserializer extends AbstractJsonDeserializer<JSONDeltaImp
}
if (tree.hasNonNull(Constants.VALUE)) {
+ JSONEntityDeserializer entityDeserializer = new JSONEntityDeserializer(version, serverMode);
for (final Iterator<JsonNode> itor = tree.get(Constants.VALUE).iterator(); itor.hasNext();) {
final ObjectNode item = (ObjectNode) itor.next();
final ContextURL itemContextURL = item.hasNonNull(Constants.JSON_CONTEXT)
- ? ContextURL.getInstance(URI.create(item.get(Constants.JSON_CONTEXT).textValue())) : null;
+ ? ContextURL.getInstance(URI.create(item.get(Constants.JSON_CONTEXT).textValue())) : null;
item.remove(Constants.JSON_CONTEXT);
if (itemContextURL == null || itemContextURL.isEntity()) {
- final ResWrap<JSONEntityImpl> entity = item.traverse(parser.getCodec()).
- readValueAs(new TypeReference<JSONEntityImpl>() {
- });
- delta.getEntities().add(entity.getPayload());
+ delta.getEntities().add(entityDeserializer.doDeserialize(item.traverse(parser.getCodec())).getPayload());
} else if (itemContextURL.isDeltaDeletedEntity()) {
delta.getDeletedEntities().add(parser.getCodec().treeToValue(item, DeletedEntityImpl.class));
} else if (itemContextURL.isDeltaLink()) {
@@ -85,7 +86,15 @@ public class JSONDeltaDeserializer extends AbstractJsonDeserializer<JSONDeltaImp
}
}
- return new ResWrap<JSONDeltaImpl>(contextURL, null, delta);
+ return new ResWrap<Delta>(contextURL, null, delta);
}
+ public ResWrap<Delta> toDelta(InputStream input) throws ODataDeserializerException {
+ try {
+ JsonParser parser = new JsonFactory(new ObjectMapper()).createParser(input);
+ return doDeserialize(parser);
+ } catch (final IOException e) {
+ throw new ODataDeserializerException(e);
+ }
+ }
}