public WriteResponse execute(PutRequest request) {
CouchHttpResponse couchHttpResponse;
if (request instanceof PutEntityRequest) {
// Special handling for Entity
PutEntityRequest putEntityRequest = (PutEntityRequest) request;
EntityMeta entityMeta = metaRepository.getOrCreateEntityMeta(putEntityRequest.getEntityClass());
if (putEntityRequest.getDocumentId() == null && putEntityRequest.getDocumentRevision() == null) {
// Request does not define documentId or revision so look to the EntityMeta
if (entityMeta.hasId()) {
// Read the id and revision
Object entity = putEntityRequest.getEntity();
String id = entityMeta.readDocumentId(entity);
String revision = entityMeta.readDocumentRevision(entity);
String entityType = entityMeta.getEntityType();
// Create the PutEntityRequest
putEntityRequest = couch.put().entity(id, entityType, entity, revision);
} else {
throw CouchException.internalServerError("Cannot build PutEntityRequest for entity " + putEntityRequest.getEntityClass() + ", @CouchId is not specified.");