diff --git a/arangod/RestHandler/RestVocbaseBaseHandler.cpp b/arangod/RestHandler/RestVocbaseBaseHandler.cpp index c0ff9bc648..d15a37a4a1 100644 --- a/arangod/RestHandler/RestVocbaseBaseHandler.cpp +++ b/arangod/RestHandler/RestVocbaseBaseHandler.cpp @@ -287,6 +287,48 @@ void RestVocbaseBaseHandler::generateForbidden() { "operation forbidden"); } +//////////////////////////////////////////////////////////////////////////////// +/// @brief generates precondition failed +/// DEPRECATED +//////////////////////////////////////////////////////////////////////////////// + +void RestVocbaseBaseHandler::generatePreconditionFailed( + VPackSlice const& slice) { + TRI_ASSERT(slice.isObject()); + TRI_ASSERT(slice.hasKey(TRI_VOC_ATTRIBUTE_ID)); + TRI_ASSERT(slice.hasKey(TRI_VOC_ATTRIBUTE_REV)); + TRI_ASSERT(slice.hasKey(TRI_VOC_ATTRIBUTE_KEY)); + + createResponse(HttpResponse::PRECONDITION_FAILED); + _response->setContentType("application/json; charset=utf-8"); + std::string rev = VelocyPackHelper::getStringValue(slice, TRI_VOC_ATTRIBUTE_REV, ""); + _response->setHeader("etag", 4, "\"" + rev + "\""); + VPackBuilder builder; + { + VPackObjectBuilder guard(&builder); + // _id and _key are safe and do not need to be JSON-encoded + builder.add("error", VPackValue(true)); + builder.add( + "code", + VPackValue(static_cast(HttpResponse::PRECONDITION_FAILED))); + builder.add("errorNum", VPackValue(TRI_ERROR_ARANGO_CONFLICT)); + builder.add("errorMessage", VPackValue("precondition failed")); + builder.add(TRI_VOC_ATTRIBUTE_ID, slice.get(TRI_VOC_ATTRIBUTE_ID)); + builder.add(TRI_VOC_ATTRIBUTE_KEY, slice.get(TRI_VOC_ATTRIBUTE_KEY)); + builder.add(TRI_VOC_ATTRIBUTE_REV, slice.get(TRI_VOC_ATTRIBUTE_REV)); + } + + VPackStringBufferAdapter buffer(_response->body().stringBuffer()); + VPackDumper dumper(&buffer); + + try { + dumper.dump(builder.slice()); + } catch (...) { + generateError(HttpResponse::SERVER_ERROR, TRI_ERROR_INTERNAL, + "cannot generate output"); + } +} + //////////////////////////////////////////////////////////////////////////////// /// @brief generates precondition failed //////////////////////////////////////////////////////////////////////////////// @@ -315,6 +357,8 @@ void RestVocbaseBaseHandler::generatePreconditionFailed( .appendText("\"}"); } + + //////////////////////////////////////////////////////////////////////////////// /// @brief generates not modified //////////////////////////////////////////////////////////////////////////////// @@ -328,6 +372,7 @@ void RestVocbaseBaseHandler::generateNotModified(TRI_voc_rid_t rid) { //////////////////////////////////////////////////////////////////////////////// /// @brief generates next entry from a result set +/// DEPRECATED //////////////////////////////////////////////////////////////////////////////// void RestVocbaseBaseHandler::generateDocument( @@ -421,6 +466,52 @@ void RestVocbaseBaseHandler::generateDocument( } } +//////////////////////////////////////////////////////////////////////////////// +/// @brief generates next entry from a result set +/// DEPRECATED +//////////////////////////////////////////////////////////////////////////////// + +void RestVocbaseBaseHandler::generateDocument(VPackSlice const& document, + bool generateBody) { + TRI_ASSERT(document.isObject()); + TRI_ASSERT(document.hasKey(TRI_VOC_ATTRIBUTE_REV)); + + std::string rev = VelocyPackHelper::getStringValue(document, TRI_VOC_ATTRIBUTE_REV, ""); + + // and generate a response + createResponse(HttpResponse::OK); + _response->setContentType("application/json; charset=utf-8"); + _response->setHeader("etag", 4, "\"" + rev + "\""); + + if (generateBody) { + VPackStringBufferAdapter buffer(_response->body().stringBuffer()); + VPackDumper dumper(&buffer); + try { + dumper.dump(document); + } catch (...) { + generateError(HttpResponse::SERVER_ERROR, TRI_ERROR_INTERNAL, + "cannot generate output"); + } + } else { + // TODO can we optimize this? + // Just dump some where else to find real length + TRI_string_buffer_t tmpBuffer; + // convert object to string + TRI_InitStringBuffer(&tmpBuffer, TRI_UNKNOWN_MEM_ZONE); + + VPackStringBufferAdapter buffer(&tmpBuffer); + VPackDumper dumper(&buffer); + try { + dumper.dump(document); + } catch (...) { + generateError(HttpResponse::SERVER_ERROR, TRI_ERROR_INTERNAL, + "cannot generate output"); + } + _response->headResponse(TRI_LengthStringBuffer(&tmpBuffer)); + TRI_DestroyStringBuffer(&tmpBuffer); + } +} + //////////////////////////////////////////////////////////////////////////////// /// @brief generate an error message for a transaction error //////////////////////////////////////////////////////////////////////////////// diff --git a/arangod/RestHandler/RestVocbaseBaseHandler.h b/arangod/RestHandler/RestVocbaseBaseHandler.h index 520546a6c4..c069aedc04 100644 --- a/arangod/RestHandler/RestVocbaseBaseHandler.h +++ b/arangod/RestHandler/RestVocbaseBaseHandler.h @@ -282,11 +282,18 @@ class RestVocbaseBaseHandler : public RestBaseHandler { ////////////////////////////////////////////////////////////////////////////// /// @brief generates precondition failed, without transaction info + /// DEPRECATED ////////////////////////////////////////////////////////////////////////////// void generatePreconditionFailed(std::string const&, TRI_voc_key_t key, TRI_voc_rid_t rid); + ////////////////////////////////////////////////////////////////////////////// + /// @brief generates precondition failed, without transaction info + ////////////////////////////////////////////////////////////////////////////// + + void generatePreconditionFailed(arangodb::velocypack::Slice const& slice); + ////////////////////////////////////////////////////////////////////////////// /// @brief generates not modified ////////////////////////////////////////////////////////////////////////////// @@ -295,11 +302,18 @@ class RestVocbaseBaseHandler : public RestBaseHandler { ////////////////////////////////////////////////////////////////////////////// /// @brief generates first entry from a result set + /// DEPRECATED ////////////////////////////////////////////////////////////////////////////// void generateDocument(SingleCollectionReadOnlyTransaction& trx, TRI_voc_cid_t, TRI_doc_mptr_copy_t const&, VocShaper*, bool); + ////////////////////////////////////////////////////////////////////////////// + /// @brief generates first entry from a result set + ////////////////////////////////////////////////////////////////////////////// + + void generateDocument(arangodb::velocypack::Slice const&, bool); + ////////////////////////////////////////////////////////////////////////////// /// @brief generate an error message for a transaction error //////////////////////////////////////////////////////////////////////////////