}
@Override
public ODataResponse writeLink(final EdmEntitySet entitySet, final Map<String, Object> data,
final EntityProviderWriteProperties properties) throws EntityProviderException {
CircleStreamBuffer csb = new CircleStreamBuffer();
try {
OutputStream outStream = csb.getOutputStream();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
XmlLinkEntityProducer entity = new XmlLinkEntityProducer(properties);
final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
entity.append(writer, entityInfo, data, true);
writer.flush();
csb.closeWrite();
return ODataResponse.entity(csb.getInputStream()).build();
} catch (EntityProviderException e) {
csb.close();
throw e;
} catch (Exception e) {
csb.close();
throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass()
.getSimpleName()), e);
}
}