/** * @param exchange * @param context * @throws Exception */ @Override public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception { DBObject content = context.getContent(); // cannot PATCH with no data if (content == null) { ResponseHelper.endExchangeWithMessage( exchange, HttpStatus.SC_NOT_ACCEPTABLE, "data is empty"); return; } // cannot PATCH an array if (content instanceof BasicDBList) { ResponseHelper.endExchangeWithMessage( exchange, HttpStatus.SC_NOT_ACCEPTABLE, "data cannot be an array"); return; } Object id = context.getDocumentId(); if (content.get("_id") == null) { content.put("_id", id); } else if (!content.get("_id").equals(id)) { ResponseHelper.endExchangeWithMessage( exchange, HttpStatus.SC_NOT_ACCEPTABLE, "_id in json data cannot be different than id in URL"); return; } ObjectId requestEtag = RequestHelper.getWriteEtag(exchange); OperationResult result = documentDAO.upsertDocument( context.getDBName(), context.getCollectionName(), context.getDocumentId(), content, requestEtag, true); if (result.getEtag() != null) { exchange.getResponseHeaders().put(Headers.ETAG, result.getEtag().toString()); } else { ResponseHelper.endExchangeWithMessage( exchange, HttpStatus.SC_CONFLICT, "The document's ETag must be provided using the '" + Headers.IF_MATCH + "' header"); return; } // send the warnings if any (and in case no_content change the return code to ok if (context.getWarnings() != null && !context.getWarnings().isEmpty()) { sendWarnings(result.getHttpCode(), exchange, context); } else { exchange.setResponseCode(result.getHttpCode()); } exchange.endExchange(); }