+ /**
+ * Processes client requests containing a set of operations to be performed in bulk.
+ *
+ * <p>
+ * Method: POST
+ *
+ * @param operations - JSON structure enumerating the operations to be performed.
+ * @param request - Raw HTTP request.
+ * @param headers - HTTP headers.
+ * @return - A standard REST response structure.
+ */
+ public ResponseEntity<String> processPost(String operations, HttpServletRequest request, HttpHeaders headers,
+ DocumentStoreInterface documentStore) {
+ // Initialize the MDC Context for logging purposes.
+ ApiUtils.initMdcContext(request, headers);
+
+ // Set a default result code and entity string for the request.
+ int resultCode = 500;
+ String resultString;
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("SEARCH: Process Bulk Request - operations = [" + operations.replaceAll("\n", "") + " ]");
+ }
+
+ try {
+ // Validate that the request is correctly authenticated before going
+ // any further.
+ if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
+ ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Authentication failure.");
+
+ return buildResponse(HttpStatus.FORBIDDEN.value(), "Authentication failure.", request);
+ }
+ } catch (Exception e) {
+ // This is a catch all for any unexpected failure trying to perform
+ // the authentication.
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE,
+ "Unexpected authentication failure - cause: " + e.getMessage());
+ if (logger.isDebugEnabled()) {
+ logger.debug("Stack Trace:\n" + e.getStackTrace());
+ }
+
+ return buildResponse(HttpStatus.FORBIDDEN.value(), "Authentication failure - cause " + e.getMessage(),
+ request);
+ }
+
+ // We expect a payload containing a JSON structure enumerating the
+ // operations to be performed.
+ if (operations == null) {
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Missing operations list payload");
+ return buildResponse(resultCode, "Missing payload", request);
+ }
+
+ // Marshal the supplied json string into a Java object.
+ ObjectMapper mapper = new ObjectMapper();
+ BulkRequest[] requests = null;
+ try {
+ requests = mapper.readValue(operations, BulkRequest[].class);
+ } catch (IOException e) {
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Failed to marshal operations list: " + e.getMessage());
+ if (logger.isDebugEnabled()) {
+ logger.debug("Stack Trace:\n" + e.getStackTrace());
+ }
+ // Populate the result code and entity string for our HTTP response
+ // and return the response to the client..
+ return buildResponse(HttpStatus.BAD_REQUEST.value(), "Unable to marshal operations: " + e.getMessage(),
+ request);
+ }
+
+ // Verify that our parsed operations list actually contains some valid
+ // operations.
+ if (requests.length == 0) {
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Empty operations list in bulk request");
+
+
+ // Populate the result code and entity string for our HTTP response
+ // and return the response to the client..
+ return buildResponse(HttpStatus.BAD_REQUEST.value(), "Empty operations list in bulk request", request);
+ }
+ try {
+
+ // Now, forward the set of bulk operations to the DAO for processing.
+ OperationResult result = documentStore.performBulkOperations(requests);
+
+ // Populate the result code and entity string for our HTTP response.
+ resultCode = result.getResultCode();
+ resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
+
+ } catch (DocumentStoreOperationException e) {
+
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE,
+ "Unexpected failure communicating with document store: " + e.getMessage());
+ if (logger.isDebugEnabled()) {
+ logger.debug("Stack Trace:\n" + e.getStackTrace());
+ }
+
+ // Populate the result code and entity string for our HTTP response.
+ resultCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
+ resultString = "Unexpected failure processing bulk operations: " + e.getMessage();
+ }
+
+ // Build our HTTP response.
+ ResponseEntity<String> response =
+ ResponseEntity.status(resultCode).contentType(MediaType.APPLICATION_JSON).body(resultString);
+
+ // Log the result.
+ if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
+ logger.info(SearchDbMsgs.PROCESSED_BULK_OPERATIONS);
+ } else {
+ logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, response.getBody());
+ }
+
+ // Finally, return the HTTP response to the client.
+ return buildResponse(resultCode, resultString, request);