[OLINGO-640] Minor adjustments to tests and deleted logger

This commit is contained in:
Christian Amend 2015-08-04 16:23:42 +02:00
parent 366597070f
commit db0b9d39de
3 changed files with 1386 additions and 1303 deletions

View File

@ -38,7 +38,7 @@ import org.apache.olingo.fit.util.StringHelper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public final class NavigationITCase extends AbstractBaseTestITCase { public class NavigationITCase extends AbstractBaseTestITCase {
private final ODataClient client = getClient(); private final ODataClient client = getClient();
@ -60,7 +60,7 @@ public final class NavigationITCase extends AbstractBaseTestITCase {
appendKeySegment(32767).build()).rawExecute(); appendKeySegment(32767).build()).rawExecute();
String zeroLevelResponseBody = StringHelper.asString(zeroLevelResponse); String zeroLevelResponseBody = StringHelper.asString(zeroLevelResponse);
assertTrue(zeroLevelResponseBody.contains("\"@odata.context\":\"$metadata#ESAllPrim/$entity\"")); assertTrue(zeroLevelResponseBody.contains("\"$metadata#ESAllPrim/$entity\""));
// one navigation // one navigation
final InputStream oneLevelResponse = client.getRetrieveRequestFactory().getEntityRequest( final InputStream oneLevelResponse = client.getRetrieveRequestFactory().getEntityRequest(
@ -70,7 +70,7 @@ public final class NavigationITCase extends AbstractBaseTestITCase {
.rawExecute(); .rawExecute();
String oneLevelResponseBody = StringHelper.asString(oneLevelResponse); String oneLevelResponseBody = StringHelper.asString(oneLevelResponse);
assertTrue(oneLevelResponseBody.contains("\"@odata.context\":\"../$metadata#ESTwoPrim/$entity\"")); assertTrue(oneLevelResponseBody.contains("\"../$metadata#ESTwoPrim/$entity\""));
// two navigation // two navigation
final InputStream twoLevelResponse = client.getRetrieveRequestFactory().getEntityRequest( final InputStream twoLevelResponse = client.getRetrieveRequestFactory().getEntityRequest(
@ -81,7 +81,7 @@ public final class NavigationITCase extends AbstractBaseTestITCase {
.rawExecute(); .rawExecute();
String twoLevelResponseBody = StringHelper.asString(twoLevelResponse); String twoLevelResponseBody = StringHelper.asString(twoLevelResponse);
assertTrue(twoLevelResponseBody.contains("\"@odata.context\":\"../../$metadata#ESTwoPrim/$entity\"")); assertTrue(twoLevelResponseBody.contains("\"../../$metadata#ESTwoPrim/$entity\""));
} }
@Test @Test

View File

@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the * to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance * "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at * with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@ -67,8 +67,6 @@ import org.apache.olingo.server.core.serializer.SerializerResultImpl;
import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer; import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
import org.apache.olingo.server.core.serializer.utils.ContextURLBuilder; import org.apache.olingo.server.core.serializer.utils.ContextURLBuilder;
import org.apache.olingo.server.core.serializer.utils.ExpandSelectHelper; import org.apache.olingo.server.core.serializer.utils.ExpandSelectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ODataXmlSerializer implements ODataSerializer { public class ODataXmlSerializer implements ODataSerializer {
private static final String DATA = "d"; private static final String DATA = "d";
@ -81,18 +79,16 @@ public class ODataXmlSerializer implements ODataSerializer {
private static final String NS_METADATA = "http://docs.oasis-open.org/odata/ns/metadata"; private static final String NS_METADATA = "http://docs.oasis-open.org/odata/ns/metadata";
private static final String NS_DATA = "http://docs.oasis-open.org/odata/ns/data"; private static final String NS_DATA = "http://docs.oasis-open.org/odata/ns/data";
private static final String NS_SCHEMA = "http://docs.oasis-open.org/odata/ns/scheme"; private static final String NS_SCHEMA = "http://docs.oasis-open.org/odata/ns/scheme";
private static final Logger log = LoggerFactory.getLogger(ODataXmlSerializer.class);
@Override @Override
public SerializerResult serviceDocument(final ServiceMetadata metadata, final String serviceRoot) public SerializerResult serviceDocument(final ServiceMetadata metadata, final String serviceRoot)
throws SerializerException { throws SerializerException {
CircleStreamBuffer buffer; CircleStreamBuffer buffer;
XMLStreamWriter xmlStreamWriter = null; XMLStreamWriter xmlStreamWriter = null;
SerializerException cachedException = null;
try { try {
buffer = new CircleStreamBuffer(); buffer = new CircleStreamBuffer();
xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
DEFAULT_CHARSET); DEFAULT_CHARSET);
ServiceDocumentXmlSerializer serializer = new ServiceDocumentXmlSerializer(metadata, serviceRoot); ServiceDocumentXmlSerializer serializer = new ServiceDocumentXmlSerializer(metadata, serviceRoot);
serializer.writeServiceDocument(xmlStreamWriter); serializer.writeServiceDocument(xmlStreamWriter);
@ -101,16 +97,20 @@ public class ODataXmlSerializer implements ODataSerializer {
return SerializerResultImpl.with().content(buffer.getInputStream()).build(); return SerializerResultImpl.with().content(buffer.getInputStream()).build();
} catch (final XMLStreamException e) { } catch (final XMLStreamException e) {
log.error(e.getMessage(), e); cachedException =
throw new SerializerException("An I/O exception occurred.", e, new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
SerializerException.MessageKeys.IO_EXCEPTION); throw cachedException;
} finally { } finally {
if (xmlStreamWriter != null) { if (xmlStreamWriter != null) {
try { try {
xmlStreamWriter.close(); xmlStreamWriter.close();
} catch (XMLStreamException e) { } catch (XMLStreamException e) {
throw new SerializerException("An I/O exception occurred.", e, if (cachedException != null) {
SerializerException.MessageKeys.IO_EXCEPTION); throw cachedException;
} else {
throw new SerializerException("An I/O exception occurred.", e,
SerializerException.MessageKeys.IO_EXCEPTION);
}
} }
} }
} }
@ -120,10 +120,10 @@ public class ODataXmlSerializer implements ODataSerializer {
public SerializerResult metadataDocument(final ServiceMetadata serviceMetadata) throws SerializerException { public SerializerResult metadataDocument(final ServiceMetadata serviceMetadata) throws SerializerException {
CircleStreamBuffer buffer; CircleStreamBuffer buffer;
XMLStreamWriter xmlStreamWriter = null; XMLStreamWriter xmlStreamWriter = null;
SerializerException cachedException = null;
try { try {
buffer = new CircleStreamBuffer(); buffer = new CircleStreamBuffer();
xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
DEFAULT_CHARSET); DEFAULT_CHARSET);
MetadataDocumentXmlSerializer serializer = new MetadataDocumentXmlSerializer(serviceMetadata); MetadataDocumentXmlSerializer serializer = new MetadataDocumentXmlSerializer(serviceMetadata);
serializer.writeMetadataDocument(xmlStreamWriter); serializer.writeMetadataDocument(xmlStreamWriter);
@ -132,16 +132,20 @@ public class ODataXmlSerializer implements ODataSerializer {
return SerializerResultImpl.with().content(buffer.getInputStream()).build(); return SerializerResultImpl.with().content(buffer.getInputStream()).build();
} catch (final XMLStreamException e) { } catch (final XMLStreamException e) {
log.error(e.getMessage(), e); cachedException =
throw new SerializerException("An I/O exception occurred.", e, new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
SerializerException.MessageKeys.IO_EXCEPTION); throw cachedException;
} finally { } finally {
if (xmlStreamWriter != null) { if (xmlStreamWriter != null) {
try { try {
xmlStreamWriter.close(); xmlStreamWriter.close();
} catch (XMLStreamException e) { } catch (XMLStreamException e) {
throw new SerializerException("An I/O exception occurred.", e, if (cachedException != null) {
SerializerException.MessageKeys.IO_EXCEPTION); throw cachedException;
} else {
throw new SerializerException("An I/O exception occurred.", e,
SerializerException.MessageKeys.IO_EXCEPTION);
}
} }
} }
} }
@ -151,31 +155,31 @@ public class ODataXmlSerializer implements ODataSerializer {
public SerializerResult error(final ODataServerError error) throws SerializerException { public SerializerResult error(final ODataServerError error) throws SerializerException {
CircleStreamBuffer buffer; CircleStreamBuffer buffer;
XMLStreamWriter writer = null; XMLStreamWriter writer = null;
if (error == null) { if (error == null) {
throw new SerializerException("ODataError object MUST NOT be null!", throw new SerializerException("ODataError object MUST NOT be null!",
SerializerException.MessageKeys.NULL_INPUT); SerializerException.MessageKeys.NULL_INPUT);
} }
try { try {
buffer = new CircleStreamBuffer(); buffer = new CircleStreamBuffer();
writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
DEFAULT_CHARSET); DEFAULT_CHARSET);
writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0"); writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
writer.writeStartElement("error"); writer.writeStartElement("error");
writer.writeDefaultNamespace(NS_METADATA); writer.writeDefaultNamespace(NS_METADATA);
writeErrorDetails(String.valueOf(error.getStatusCode()), error.getMessage(), error.getTarget(), writer); writeErrorDetails(String.valueOf(error.getStatusCode()), error.getMessage(), error.getTarget(), writer);
if (error.getDetails() != null && !error.getDetails().isEmpty()) { if (error.getDetails() != null && !error.getDetails().isEmpty()) {
writer.writeStartElement("details"); writer.writeStartElement("details");
for (ODataErrorDetail inner:error.getDetails()) { for (ODataErrorDetail inner : error.getDetails()) {
writeErrorDetails(inner.getCode(), inner.getMessage(), inner.getTarget(), writer); writeErrorDetails(inner.getCode(), inner.getMessage(), inner.getTarget(), writer);
} }
writer.writeEndElement(); writer.writeEndElement();
} }
writer.writeEndElement(); writer.writeEndElement();
writer.writeEndDocument(); writer.writeEndDocument();
writer.flush(); writer.flush();
writer.close(); writer.close();
return SerializerResultImpl.with().content(buffer.getInputStream()).build(); return SerializerResultImpl.with().content(buffer.getInputStream()).build();
@ -192,7 +196,7 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeCharacters(String.valueOf(code)); writer.writeCharacters(String.valueOf(code));
writer.writeEndElement(); writer.writeEndElement();
} }
writer.writeStartElement("message"); writer.writeStartElement("message");
writer.writeCharacters(message); writer.writeCharacters(message);
writer.writeEndElement(); writer.writeEndElement();
@ -200,22 +204,22 @@ public class ODataXmlSerializer implements ODataSerializer {
if (target != null) { if (target != null) {
writer.writeStartElement("target"); writer.writeStartElement("target");
writer.writeCharacters(target); writer.writeCharacters(target);
writer.writeEndElement(); writer.writeEndElement();
} }
} }
@Override @Override
public SerializerResult entityCollection(final ServiceMetadata metadata, public SerializerResult entityCollection(final ServiceMetadata metadata,
final EdmEntityType entityType, final EntityCollection entitySet, final EdmEntityType entityType, final EntityCollection entitySet,
final EntityCollectionSerializerOptions options) throws SerializerException { final EntityCollectionSerializerOptions options) throws SerializerException {
final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL()); final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
if (options.onlyReferences()) { if (options.onlyReferences()) {
ReferenceCollectionSerializerOptions rso = ReferenceCollectionSerializerOptions.with() ReferenceCollectionSerializerOptions rso = ReferenceCollectionSerializerOptions.with()
.contextURL(contextURL).build(); .contextURL(contextURL).build();
return entityReferenceCollection(metadata, entityType, entitySet, rso); return entityReferenceCollection(metadata, entityType, entitySet, rso);
} }
CircleStreamBuffer buffer; CircleStreamBuffer buffer;
XMLStreamWriter writer = null; XMLStreamWriter writer = null;
try { try {
@ -226,11 +230,11 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeNamespace(ATOM, NS_ATOM); writer.writeNamespace(ATOM, NS_ATOM);
writer.writeNamespace(METADATA, NS_METADATA); writer.writeNamespace(METADATA, NS_METADATA);
writer.writeNamespace(DATA, NS_DATA); writer.writeNamespace(DATA, NS_DATA);
writer.writeAttribute(METADATA, NS_METADATA, "context", writer.writeAttribute(METADATA, NS_METADATA, "context",
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
writeMetadataETag(metadata, writer); writeMetadataETag(metadata, writer);
writer.writeStartElement(ATOM, "id", NS_ATOM); writer.writeStartElement(ATOM, "id", NS_ATOM);
writer.writeCharacters(options.getId()); writer.writeCharacters(options.getId());
writer.writeEndElement(); writer.writeEndElement();
@ -242,7 +246,7 @@ public class ODataXmlSerializer implements ODataSerializer {
if (entitySet.getNext() != null) { if (entitySet.getNext() != null) {
writeNextLink(entitySet, writer); writeNextLink(entitySet, writer);
} }
writeEntitySet(metadata, entityType, entitySet, writeEntitySet(metadata, entityType, entitySet,
options.getExpand(), options.getSelect(), options.onlyReferences(), writer); options.getExpand(), options.getSelect(), options.onlyReferences(), writer);
@ -261,13 +265,13 @@ public class ODataXmlSerializer implements ODataSerializer {
public SerializerResult entity(final ServiceMetadata metadata, final EdmEntityType entityType, public SerializerResult entity(final ServiceMetadata metadata, final EdmEntityType entityType,
final Entity entity, final EntitySerializerOptions options) throws SerializerException { final Entity entity, final EntitySerializerOptions options) throws SerializerException {
final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL()); final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
if (options.onlyReferences()) { if (options.onlyReferences()) {
ReferenceSerializerOptions rso = ReferenceSerializerOptions.with() ReferenceSerializerOptions rso = ReferenceSerializerOptions.with()
.contextURL(contextURL).build(); .contextURL(contextURL).build();
return entityReference(metadata, entityType, entity, rso); return entityReference(metadata, entityType, entity, rso);
} }
CircleStreamBuffer buffer; CircleStreamBuffer buffer;
XMLStreamWriter writer = null; XMLStreamWriter writer = null;
try { try {
@ -295,12 +299,12 @@ public class ODataXmlSerializer implements ODataSerializer {
return contextURL; return contextURL;
} }
private void writeMetadataETag(final ServiceMetadata metadata, XMLStreamWriter writer) private void writeMetadataETag(final ServiceMetadata metadata, XMLStreamWriter writer)
throws XMLStreamException { throws XMLStreamException {
if (metadata != null if (metadata != null
&& metadata.getServiceMetadataETagSupport() != null && metadata.getServiceMetadataETagSupport() != null
&& metadata.getServiceMetadataETagSupport().getMetadataETag() != null) { && metadata.getServiceMetadataETagSupport().getMetadataETag() != null) {
writer.writeAttribute(METADATA, NS_METADATA, "metadata-etag", writer.writeAttribute(METADATA, NS_METADATA, "metadata-etag",
metadata.getServiceMetadataETagSupport().getMetadataETag()); metadata.getServiceMetadataETagSupport().getMetadataETag());
} }
} }
@ -316,7 +320,7 @@ public class ODataXmlSerializer implements ODataSerializer {
protected void writeEntity(final ServiceMetadata metadata, final EdmEntityType entityType, protected void writeEntity(final ServiceMetadata metadata, final EdmEntityType entityType,
final Entity entity, final ContextURL contextURL, final ExpandOption expand, final Entity entity, final ContextURL contextURL, final ExpandOption expand,
final SelectOption select, final boolean onlyReference, final XMLStreamWriter writer, final SelectOption select, final boolean onlyReference, final XMLStreamWriter writer,
final boolean top) throws XMLStreamException, SerializerException { final boolean top) throws XMLStreamException, SerializerException {
writer.writeStartElement(ATOM, "entry", NS_ATOM); writer.writeStartElement(ATOM, "entry", NS_ATOM);
@ -324,24 +328,24 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeNamespace(ATOM, NS_ATOM); writer.writeNamespace(ATOM, NS_ATOM);
writer.writeNamespace(METADATA, NS_METADATA); writer.writeNamespace(METADATA, NS_METADATA);
writer.writeNamespace(DATA, NS_DATA); writer.writeNamespace(DATA, NS_DATA);
if (contextURL != null) { // top-level entity if (contextURL != null) { // top-level entity
writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, writer.writeAttribute(METADATA, NS_METADATA, CONTEXT,
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
writeMetadataETag(metadata, writer); writeMetadataETag(metadata, writer);
} }
} }
if (entity.getETag() != null) { if (entity.getETag() != null) {
writer.writeAttribute(METADATA, NS_METADATA, "etag", entity.getETag()); writer.writeAttribute(METADATA, NS_METADATA, "etag", entity.getETag());
} }
writer.writeStartElement(NS_ATOM, "id"); writer.writeStartElement(NS_ATOM, "id");
writer.writeCharacters(entity.getId().toASCIIString()); writer.writeCharacters(entity.getId().toASCIIString());
writer.writeEndElement(); writer.writeEndElement();
writerAuthorInfo(entity.getTitle(), writer); writerAuthorInfo(entity.getTitle(), writer);
writer.writeStartElement(NS_ATOM, "link"); writer.writeStartElement(NS_ATOM, "link");
writer.writeAttribute("rel", "edit"); writer.writeAttribute("rel", "edit");
writer.writeAttribute("href", entity.getId().toASCIIString()); writer.writeAttribute("href", entity.getId().toASCIIString());
@ -355,41 +359,41 @@ public class ODataXmlSerializer implements ODataSerializer {
} else { } else {
String id = entity.getId().toASCIIString(); String id = entity.getId().toASCIIString();
if (id.endsWith("/")) { if (id.endsWith("/")) {
writer.writeAttribute("src", id+"$value"); writer.writeAttribute("src", id + "$value");
} else { } else {
writer.writeAttribute("src", id+"/$value"); writer.writeAttribute("src", id + "/$value");
} }
} }
writer.writeEndElement(); writer.writeEndElement();
} }
// write media links // write media links
for (Link link:entity.getMediaEditLinks()) { for (Link link : entity.getMediaEditLinks()) {
writeLink(writer, link); writeLink(writer, link);
} }
EdmEntityType resolvedType = resolveEntityType(metadata, entityType, entity.getType()); EdmEntityType resolvedType = resolveEntityType(metadata, entityType, entity.getType());
writeNavigationProperties(metadata, resolvedType, entity, expand, writer); writeNavigationProperties(metadata, resolvedType, entity, expand, writer);
writer.writeStartElement(ATOM, "category", NS_ATOM); writer.writeStartElement(ATOM, "category", NS_ATOM);
writer.writeAttribute("scheme", NS_SCHEMA); writer.writeAttribute("scheme", NS_SCHEMA);
writer.writeAttribute("term", "#"+resolvedType.getFullQualifiedName().getFullQualifiedNameAsString()); writer.writeAttribute("term", "#" + resolvedType.getFullQualifiedName().getFullQualifiedNameAsString());
writer.writeEndElement(); writer.writeEndElement();
// In the case media, content is sibiling // In the case media, content is sibiling
if (!entityType.hasStream()) { if (!entityType.hasStream()) {
writer.writeStartElement(NS_ATOM, "content"); writer.writeStartElement(NS_ATOM, "content");
writer.writeAttribute("type", "application/xml"); writer.writeAttribute("type", "application/xml");
} }
writer.writeStartElement(METADATA, "properties", NS_METADATA); writer.writeStartElement(METADATA, "properties", NS_METADATA);
writeProperties(metadata, resolvedType, entity.getProperties(), select, writer); writeProperties(metadata, resolvedType, entity.getProperties(), select, writer);
writer.writeEndElement(); // properties writer.writeEndElement(); // properties
if (!entityType.hasStream()) { // content if (!entityType.hasStream()) { // content
writer.writeEndElement(); writer.writeEndElement();
} }
writer.writeEndElement(); //entry writer.writeEndElement(); // entry
} }
private void writerAuthorInfo(final String title, final XMLStreamWriter writer) throws XMLStreamException { private void writerAuthorInfo(final String title, final XMLStreamWriter writer) throws XMLStreamException {
@ -400,12 +404,12 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeEndElement(); writer.writeEndElement();
writer.writeStartElement(NS_ATOM, "summary"); writer.writeStartElement(NS_ATOM, "summary");
writer.writeEndElement(); writer.writeEndElement();
writer.writeStartElement(NS_ATOM, "updated"); writer.writeStartElement(NS_ATOM, "updated");
writer.writeCharacters(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") writer.writeCharacters(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.format(new Date(System.currentTimeMillis()))); .format(new Date(System.currentTimeMillis())));
writer.writeEndElement(); writer.writeEndElement();
writer.writeStartElement(NS_ATOM, "author"); writer.writeStartElement(NS_ATOM, "author");
writer.writeStartElement(NS_ATOM, "name"); writer.writeStartElement(NS_ATOM, "name");
writer.writeEndElement(); writer.writeEndElement();
@ -460,9 +464,9 @@ public class ODataXmlSerializer implements ODataSerializer {
.getFullQualifiedName().getFullQualifiedNameAsString()); .getFullQualifiedName().getFullQualifiedNameAsString());
} }
protected void writeProperties(final ServiceMetadata metadata, final EdmStructuredType type, protected void writeProperties(final ServiceMetadata metadata, final EdmStructuredType type,
final List<Property> properties, final SelectOption select, final XMLStreamWriter writer) final List<Property> properties, final SelectOption select, final XMLStreamWriter writer)
throws XMLStreamException, SerializerException { throws XMLStreamException, SerializerException {
final boolean all = ExpandSelectHelper.isAll(select); final boolean all = ExpandSelectHelper.isAll(select);
final Set<String> selected = all ? null : final Set<String> selected = all ? null :
ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems()); ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems());
@ -486,7 +490,7 @@ public class ODataXmlSerializer implements ODataSerializer {
ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems()); ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems());
for (final String propertyName : type.getNavigationPropertyNames()) { for (final String propertyName : type.getNavigationPropertyNames()) {
final EdmNavigationProperty property = type.getNavigationProperty(propertyName); final EdmNavigationProperty property = type.getNavigationProperty(propertyName);
final Link navigationLink = getOrCreateLink(linked, propertyName); final Link navigationLink = getOrCreateLink(linked, propertyName);
if (expandAll || expanded.contains(propertyName)) { if (expandAll || expanded.contains(propertyName)) {
final ExpandItem innerOptions = expandAll ? null : final ExpandItem innerOptions = expandAll ? null :
ExpandSelectHelper.getExpandItem(expand.getExpandItems(), propertyName); ExpandSelectHelper.getExpandItem(expand.getExpandItems(), propertyName);
@ -495,14 +499,14 @@ public class ODataXmlSerializer implements ODataSerializer {
SerializerException.MessageKeys.NOT_IMPLEMENTED); SerializerException.MessageKeys.NOT_IMPLEMENTED);
} }
if (navigationLink != null) { if (navigationLink != null) {
writeLink(writer, navigationLink, false); writeLink(writer, navigationLink, false);
writer.writeStartElement(METADATA, "inline", NS_METADATA); writer.writeStartElement(METADATA, "inline", NS_METADATA);
writeExpandedNavigationProperty(metadata, property, navigationLink, writeExpandedNavigationProperty(metadata, property, navigationLink,
innerOptions == null ? null : innerOptions.getExpandOption(), innerOptions == null ? null : innerOptions.getExpandOption(),
innerOptions == null ? null : innerOptions.getSelectOption(), innerOptions == null ? null : innerOptions.getSelectOption(),
innerOptions == null ? false: innerOptions.isRef(), innerOptions == null ? false : innerOptions.isRef(),
writer); writer);
writer.writeEndElement(); writer.writeEndElement();
writer.writeEndElement(); writer.writeEndElement();
} }
} else { } else {
@ -512,33 +516,34 @@ public class ODataXmlSerializer implements ODataSerializer {
} else { } else {
for (final String propertyName : type.getNavigationPropertyNames()) { for (final String propertyName : type.getNavigationPropertyNames()) {
writeLink(writer, getOrCreateLink(linked, propertyName)); writeLink(writer, getOrCreateLink(linked, propertyName));
} }
} }
for (Link link:linked.getAssociationLinks()) { for (Link link : linked.getAssociationLinks()) {
writeLink(writer, link); writeLink(writer, link);
} }
} }
protected Link getOrCreateLink(final Linked linked, final String navigationPropertyName) protected Link getOrCreateLink(final Linked linked, final String navigationPropertyName)
throws XMLStreamException { throws XMLStreamException {
Link link = linked.getNavigationLink(navigationPropertyName); Link link = linked.getNavigationLink(navigationPropertyName);
if (link == null) { if (link == null) {
link = new Link(); link = new Link();
link.setRel("http://docs.oasis-open.org/odata/ns/related/"+navigationPropertyName); link.setRel("http://docs.oasis-open.org/odata/ns/related/" + navigationPropertyName);
link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE); link.setType(Constants.ENTITY_SET_NAVIGATION_LINK_TYPE);
link.setTitle(navigationPropertyName); link.setTitle(navigationPropertyName);
EntityCollection target = new EntityCollection(); EntityCollection target = new EntityCollection();
link.setInlineEntitySet(target); link.setInlineEntitySet(target);
link.setHref(linked.getId().toASCIIString()+"/"+navigationPropertyName); link.setHref(linked.getId().toASCIIString() + "/" + navigationPropertyName);
} }
return link; return link;
} }
private void writeLink(final XMLStreamWriter writer, final Link link) throws XMLStreamException { private void writeLink(final XMLStreamWriter writer, final Link link) throws XMLStreamException {
writeLink(writer, link, true); writeLink(writer, link, true);
} }
private void writeLink(final XMLStreamWriter writer, final Link link, final boolean close) private void writeLink(final XMLStreamWriter writer, final Link link, final boolean close)
throws XMLStreamException { throws XMLStreamException {
writer.writeStartElement(ATOM, "link", NS_ATOM); writer.writeStartElement(ATOM, "link", NS_ATOM);
writer.writeAttribute("rel", link.getRel()); writer.writeAttribute("rel", link.getRel());
if (link.getType() != null) { if (link.getType() != null) {
@ -550,12 +555,12 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeAttribute("href", link.getHref()); writer.writeAttribute("href", link.getHref());
if (close) { if (close) {
writer.writeEndElement(); writer.writeEndElement();
} }
} }
protected void writeExpandedNavigationProperty(final ServiceMetadata metadata, protected void writeExpandedNavigationProperty(final ServiceMetadata metadata,
final EdmNavigationProperty property, final Link navigationLink, final EdmNavigationProperty property, final Link navigationLink,
final ExpandOption innerExpand, final SelectOption innerSelect, boolean onlyReference, final ExpandOption innerExpand, final SelectOption innerSelect, boolean onlyReference,
final XMLStreamWriter writer) final XMLStreamWriter writer)
throws XMLStreamException, SerializerException { throws XMLStreamException, SerializerException {
if (property.isCollection()) { if (property.isCollection()) {
@ -577,7 +582,7 @@ public class ODataXmlSerializer implements ODataSerializer {
} }
} }
protected void writeProperty(final ServiceMetadata metadata, final EdmProperty edmProperty, protected void writeProperty(final ServiceMetadata metadata, final EdmProperty edmProperty,
final Property property, final Property property,
final Set<List<String>> selectedPaths, final XMLStreamWriter writer) throws XMLStreamException, final Set<List<String>> selectedPaths, final XMLStreamWriter writer) throws XMLStreamException,
SerializerException { SerializerException {
@ -594,25 +599,25 @@ public class ODataXmlSerializer implements ODataSerializer {
} }
writer.writeEndElement(); writer.writeEndElement();
} }
private String collectionType(EdmType type) { private String collectionType(EdmType type) {
return "#Collection("+type.getFullQualifiedName().getFullQualifiedNameAsString()+")"; return "#Collection(" + type.getFullQualifiedName().getFullQualifiedNameAsString() + ")";
} }
private String complexType(ServiceMetadata metadata, EdmComplexType baseType, String definedType) private String complexType(ServiceMetadata metadata, EdmComplexType baseType, String definedType)
throws SerializerException { throws SerializerException {
EdmComplexType type = resolveComplexType(metadata, baseType, definedType); EdmComplexType type = resolveComplexType(metadata, baseType, definedType);
return type.getFullQualifiedName().getFullQualifiedNameAsString(); return type.getFullQualifiedName().getFullQualifiedNameAsString();
} }
private String derivedComplexType(ServiceMetadata metadata, EdmComplexType baseType, String definedType) private String derivedComplexType(ServiceMetadata metadata, EdmComplexType baseType, String definedType)
throws SerializerException { throws SerializerException {
String derived = baseType.getFullQualifiedName().getFullQualifiedNameAsString(); String derived = baseType.getFullQualifiedName().getFullQualifiedNameAsString();
if (derived.equals(definedType)) { if (derived.equals(definedType)) {
return null; return null;
} }
return definedType; return definedType;
} }
private void writePropertyValue(final ServiceMetadata metadata, final EdmProperty edmProperty, private void writePropertyValue(final ServiceMetadata metadata, final EdmProperty edmProperty,
final Property property, final Set<List<String>> selectedPaths, final Property property, final Set<List<String>> selectedPaths,
@ -620,7 +625,7 @@ public class ODataXmlSerializer implements ODataSerializer {
try { try {
if (edmProperty.isPrimitive()) { if (edmProperty.isPrimitive()) {
if (edmProperty.isCollection()) { if (edmProperty.isCollection()) {
writer.writeAttribute(METADATA, NS_METADATA, "type", "#Collection("+edmProperty.getType().getName()+")"); writer.writeAttribute(METADATA, NS_METADATA, "type", "#Collection(" + edmProperty.getType().getName() + ")");
writePrimitiveCollection((EdmPrimitiveType) edmProperty.getType(), property, writePrimitiveCollection((EdmPrimitiveType) edmProperty.getType(), property,
edmProperty.isNullable(), edmProperty.getMaxLength(), edmProperty.isNullable(), edmProperty.getMaxLength(),
edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(),
@ -635,8 +640,8 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeAttribute(METADATA, NS_METADATA, "type", collectionType(edmProperty.getType())); writer.writeAttribute(METADATA, NS_METADATA, "type", collectionType(edmProperty.getType()));
writeComplexCollection(metadata, (EdmComplexType) edmProperty.getType(), property, selectedPaths, writer); writeComplexCollection(metadata, (EdmComplexType) edmProperty.getType(), property, selectedPaths, writer);
} else if (property.isComplex()) { } else if (property.isComplex()) {
writer.writeAttribute(METADATA, NS_METADATA, "type", writer.writeAttribute(METADATA, NS_METADATA, "type",
"#"+complexType(metadata, (EdmComplexType) edmProperty.getType(), property.getType())); "#" + complexType(metadata, (EdmComplexType) edmProperty.getType(), property.getType()));
writeComplexValue(metadata, (EdmComplexType) edmProperty.getType(), property.asComplex().getValue(), writeComplexValue(metadata, (EdmComplexType) edmProperty.getType(), property.asComplex().getValue(),
selectedPaths, writer); selectedPaths, writer);
} else if (property.isEnum()) { } else if (property.isEnum()) {
@ -677,7 +682,7 @@ public class ODataXmlSerializer implements ODataSerializer {
} }
} }
private void writeComplexCollection(final ServiceMetadata metadata, final EdmComplexType type, private void writeComplexCollection(final ServiceMetadata metadata, final EdmComplexType type,
final Property property, final Set<List<String>> selectedPaths, final XMLStreamWriter writer) final Property property, final Set<List<String>> selectedPaths, final XMLStreamWriter writer)
throws XMLStreamException, EdmPrimitiveTypeException, SerializerException { throws XMLStreamException, EdmPrimitiveTypeException, SerializerException {
for (Object value : property.asCollection()) { for (Object value : property.asCollection()) {
@ -711,7 +716,7 @@ public class ODataXmlSerializer implements ODataSerializer {
throw new SerializerException("Property type not yet supported!", throw new SerializerException("Property type not yet supported!",
SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName()); SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
} else if (property.isEnum()) { } else if (property.isEnum()) {
writer.writeAttribute(METADATA, NS_METADATA, "type", "#"+type.getName()); writer.writeAttribute(METADATA, NS_METADATA, "type", "#" + type.getName());
writePrimitiveValue(type, property.asEnum(), writePrimitiveValue(type, property.asEnum(),
isNullable, maxLength, precision, scale, isUnicode, writer); isNullable, maxLength, precision, scale, isUnicode, writer);
} else { } else {
@ -728,8 +733,8 @@ public class ODataXmlSerializer implements ODataSerializer {
isNullable, maxLength, precision, scale, isUnicode); isNullable, maxLength, precision, scale, isUnicode);
if (value == null) { if (value == null) {
writer.writeAttribute(DATA, NS_DATA, "null", "true"); writer.writeAttribute(DATA, NS_DATA, "null", "true");
} else if(type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean)) { } else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean)) {
writer.writeCharacters(value); writer.writeCharacters(value);
} else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Byte) } else if (type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Byte)
|| type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Decimal) || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Decimal)
|| type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Double) || type == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Double)
@ -744,7 +749,7 @@ public class ODataXmlSerializer implements ODataSerializer {
} }
} }
protected void writeComplexValue(final ServiceMetadata metadata, final EdmComplexType type, protected void writeComplexValue(final ServiceMetadata metadata, final EdmComplexType type,
final List<Property> properties, final Set<List<String>> selectedPaths, final XMLStreamWriter writer) final List<Property> properties, final Set<List<String>> selectedPaths, final XMLStreamWriter writer)
throws XMLStreamException, EdmPrimitiveTypeException, SerializerException { throws XMLStreamException, EdmPrimitiveTypeException, SerializerException {
for (final String propertyName : type.getPropertyNames()) { for (final String propertyName : type.getPropertyNames()) {
@ -781,11 +786,11 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeStartElement(METADATA, "value", NS_METADATA); writer.writeStartElement(METADATA, "value", NS_METADATA);
writer.writeNamespace(METADATA, NS_METADATA); writer.writeNamespace(METADATA, NS_METADATA);
if (contextURL != null) { if (contextURL != null) {
writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, writer.writeAttribute(METADATA, NS_METADATA, CONTEXT,
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
} }
writeMetadataETag(metadata, writer); writeMetadataETag(metadata, writer);
if(property.isNull()) { if (property.isNull()) {
writer.writeAttribute(METADATA, NS_METADATA, "null", "true"); writer.writeAttribute(METADATA, NS_METADATA, "null", "true");
} else { } else {
writePrimitive(type, property, writePrimitive(type, property,
@ -797,7 +802,7 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeEndDocument(); writer.writeEndDocument();
writer.flush(); writer.flush();
writer.close(); writer.close();
return SerializerResultImpl.with().content(buffer.getInputStream()).build(); return SerializerResultImpl.with().content(buffer.getInputStream()).build();
} catch (final XMLStreamException e) { } catch (final XMLStreamException e) {
throw new SerializerException("An I/O exception occurred.", e, throw new SerializerException("An I/O exception occurred.", e,
@ -816,7 +821,7 @@ public class ODataXmlSerializer implements ODataSerializer {
property.getName(), property.getValue().toString()); property.getName(), property.getValue().toString());
} }
} }
} }
} }
@Override @Override
@ -835,7 +840,7 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeNamespace(DATA, NS_DATA); writer.writeNamespace(DATA, NS_DATA);
writer.writeAttribute(METADATA, NS_METADATA, "type", "#" writer.writeAttribute(METADATA, NS_METADATA, "type", "#"
+ resolvedType.getFullQualifiedName().getFullQualifiedNameAsString()); + resolvedType.getFullQualifiedName().getFullQualifiedNameAsString());
writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, writer.writeAttribute(METADATA, NS_METADATA, CONTEXT,
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
writeMetadataETag(metadata, writer); writeMetadataETag(metadata, writer);
if (property.isNull()) { if (property.isNull()) {
@ -869,11 +874,11 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeStartElement(METADATA, "value", NS_METADATA); writer.writeStartElement(METADATA, "value", NS_METADATA);
writer.writeNamespace(METADATA, NS_METADATA); writer.writeNamespace(METADATA, NS_METADATA);
if (contextURL != null) { if (contextURL != null) {
writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, writer.writeAttribute(METADATA, NS_METADATA, CONTEXT,
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
} }
writeMetadataETag(metadata, writer); writeMetadataETag(metadata, writer);
writer.writeAttribute(METADATA, NS_METADATA, "type", "#Collection("+type.getName()+")"); writer.writeAttribute(METADATA, NS_METADATA, "type", "#Collection(" + type.getName() + ")");
writePrimitiveCollection(type, property, writePrimitiveCollection(type, property,
options.isNullable(), options.getMaxLength(), options.getPrecision(), options.getScale(), options.isNullable(), options.getMaxLength(), options.getPrecision(), options.getScale(),
options.isUnicode(), options.isUnicode(),
@ -882,7 +887,7 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeEndDocument(); writer.writeEndDocument();
writer.flush(); writer.flush();
writer.close(); writer.close();
return SerializerResultImpl.with().content(buffer.getInputStream()).build(); return SerializerResultImpl.with().content(buffer.getInputStream()).build();
} catch (final XMLStreamException e) { } catch (final XMLStreamException e) {
throw new SerializerException("An I/O exception occurred.", e, throw new SerializerException("An I/O exception occurred.", e,
@ -901,7 +906,7 @@ public class ODataXmlSerializer implements ODataSerializer {
property.getName(), property.getValue().toString()); property.getName(), property.getValue().toString());
} }
} }
} }
} }
@Override @Override
@ -918,8 +923,8 @@ public class ODataXmlSerializer implements ODataSerializer {
writer.writeStartElement(METADATA, "value", NS_METADATA); writer.writeStartElement(METADATA, "value", NS_METADATA);
writer.writeNamespace(METADATA, NS_METADATA); writer.writeNamespace(METADATA, NS_METADATA);
writer.writeNamespace(DATA, NS_DATA); writer.writeNamespace(DATA, NS_DATA);
writer.writeAttribute(METADATA, NS_METADATA, "type", collectionType(type)); writer.writeAttribute(METADATA, NS_METADATA, "type", collectionType(type));
writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, writer.writeAttribute(METADATA, NS_METADATA, CONTEXT,
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
writeMetadataETag(metadata, writer); writeMetadataETag(metadata, writer);
writeComplexCollection(metadata, type, property, null, writer); writeComplexCollection(metadata, type, property, null, writer);
@ -939,11 +944,11 @@ public class ODataXmlSerializer implements ODataSerializer {
} }
@Override @Override
public SerializerResult reference(final ServiceMetadata metadata, final EdmEntitySet edmEntitySet, public SerializerResult reference(final ServiceMetadata metadata, final EdmEntitySet edmEntitySet,
final Entity entity, final ReferenceSerializerOptions options) throws SerializerException { final Entity entity, final ReferenceSerializerOptions options) throws SerializerException {
return entityReference(metadata, edmEntitySet.getEntityType(), entity, options); return entityReference(metadata, edmEntitySet.getEntityType(), entity, options);
} }
protected SerializerResult entityReference(final ServiceMetadata metadata, final EdmEntityType entityType, protected SerializerResult entityReference(final ServiceMetadata metadata, final EdmEntityType entityType,
final Entity entity, ReferenceSerializerOptions options) throws SerializerException { final Entity entity, ReferenceSerializerOptions options) throws SerializerException {
CircleStreamBuffer buffer; CircleStreamBuffer buffer;
@ -962,14 +967,14 @@ public class ODataXmlSerializer implements ODataSerializer {
SerializerException.MessageKeys.IO_EXCEPTION); SerializerException.MessageKeys.IO_EXCEPTION);
} }
} }
private void writeReference(ServiceMetadata metadata, EdmEntityType entityType, private void writeReference(ServiceMetadata metadata, EdmEntityType entityType,
Entity entity, ContextURL contextURL, XMLStreamWriter writer, boolean top) throws XMLStreamException { Entity entity, ContextURL contextURL, XMLStreamWriter writer, boolean top) throws XMLStreamException {
writer.writeStartElement(METADATA, "ref", NS_METADATA); writer.writeStartElement(METADATA, "ref", NS_METADATA);
if (top) { if (top) {
writer.writeNamespace(METADATA, NS_METADATA); writer.writeNamespace(METADATA, NS_METADATA);
if (contextURL != null) { // top-level entity if (contextURL != null) { // top-level entity
writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, writer.writeAttribute(METADATA, NS_METADATA, CONTEXT,
ContextURLBuilder.create(contextURL).toASCIIString()); ContextURLBuilder.create(contextURL).toASCIIString());
} }
} }
@ -978,12 +983,12 @@ public class ODataXmlSerializer implements ODataSerializer {
} }
@Override @Override
public SerializerResult referenceCollection(final ServiceMetadata metadata, final EdmEntitySet edmEntitySet, public SerializerResult referenceCollection(final ServiceMetadata metadata, final EdmEntitySet edmEntitySet,
final EntityCollection entityCollection, ReferenceCollectionSerializerOptions options) final EntityCollection entityCollection, ReferenceCollectionSerializerOptions options)
throws SerializerException { throws SerializerException {
return entityReferenceCollection(metadata, edmEntitySet.getEntityType(), entityCollection, options); return entityReferenceCollection(metadata, edmEntitySet.getEntityType(), entityCollection, options);
} }
protected SerializerResult entityReferenceCollection(final ServiceMetadata metadata, protected SerializerResult entityReferenceCollection(final ServiceMetadata metadata,
final EdmEntityType entityType, final EntityCollection entitySet, final EdmEntityType entityType, final EntityCollection entitySet,
ReferenceCollectionSerializerOptions options) throws SerializerException { ReferenceCollectionSerializerOptions options) throws SerializerException {
@ -1019,16 +1024,16 @@ public class ODataXmlSerializer implements ODataSerializer {
throw new SerializerException("An I/O exception occurred.", e, throw new SerializerException("An I/O exception occurred.", e,
SerializerException.MessageKeys.IO_EXCEPTION); SerializerException.MessageKeys.IO_EXCEPTION);
} }
} }
private void writeCount(final EntityCollection entitySet, XMLStreamWriter writer) private void writeCount(final EntityCollection entitySet, XMLStreamWriter writer)
throws XMLStreamException { throws XMLStreamException {
writer.writeStartElement(METADATA, "count", NS_METADATA); writer.writeStartElement(METADATA, "count", NS_METADATA);
writer.writeCharacters(String.valueOf(entitySet.getCount())); writer.writeCharacters(String.valueOf(entitySet.getCount()));
writer.writeEndElement(); writer.writeEndElement();
} }
private void writeNextLink(final EntityCollection entitySet, XMLStreamWriter writer) private void writeNextLink(final EntityCollection entitySet, XMLStreamWriter writer)
throws XMLStreamException { throws XMLStreamException {
writer.writeStartElement(ATOM, "link", NS_ATOM); writer.writeStartElement(ATOM, "link", NS_ATOM);
writer.writeAttribute("rel", "next"); writer.writeAttribute("rel", "next");