public void process(Exchange exchange) throws Exception {
    MongoDbOperation operation = endpoint.getOperation();
    Object header = exchange.getIn().getHeader(MongoDbConstants.OPERATION_HEADER);
    if (header != null) {
      LOG.debug("Overriding default operation with operation specified on header: {}", header);
      try {
        if (header instanceof MongoDbOperation) {
          operation = ObjectHelper.cast(MongoDbOperation.class, header);
        } else {
          // evaluate as a String
          operation =
              MongoDbOperation.valueOf(
                  exchange.getIn().getHeader(MongoDbConstants.OPERATION_HEADER, String.class));
        }
      } catch (Exception e) {
        throw new CamelMongoDbException(
            "Operation specified on header is not supported. Value: " + header, e);
      }
    }

    try {
      invokeOperation(operation, exchange);
    } catch (Exception e) {
      throw MongoDbComponent.wrapInCamelMongoDbException(e);
    }
  }
 private Message prepareResponseMessage(Exchange exchange, MongoDbOperation operation) {
   Message answer = exchange.getOut();
   MessageHelper.copyHeaders(exchange.getIn(), answer, false);
   if (isWriteOperation(operation) && endpoint.isWriteResultAsHeader()) {
     answer.setBody(exchange.getIn().getBody());
   }
   return answer;
 }
  private DBCollection calculateCollection(Exchange exchange) throws Exception {
    // dynamic calculation is an option. In most cases it won't be used and we should not penalise
    // all users with running this
    // resolution logic on every Exchange if they won't be using this functionality at all
    if (!endpoint.isDynamicity()) {
      return endpoint.getDbCollection();
    }

    String dynamicDB = exchange.getIn().getHeader(MongoDbConstants.DATABASE, String.class);
    String dynamicCollection =
        exchange.getIn().getHeader(MongoDbConstants.COLLECTION, String.class);

    @SuppressWarnings("unchecked")
    List<DBObject> dynamicIndex =
        exchange.getIn().getHeader(MongoDbConstants.COLLECTION_INDEX, List.class);

    DBCollection dbCol = null;

    if (dynamicDB == null && dynamicCollection == null) {
      dbCol = endpoint.getDbCollection();
    } else {
      DB db = null;

      if (dynamicDB == null) {
        db = endpoint.getDb();
      } else {
        db = endpoint.getMongoConnection().getDB(dynamicDB);
      }

      if (dynamicCollection == null) {
        dbCol = db.getCollection(endpoint.getCollection());
      } else {
        dbCol = db.getCollection(dynamicCollection);

        // on the fly add index
        if (dynamicIndex == null) {
          endpoint.ensureIndex(dbCol, endpoint.createIndex());
        } else {
          endpoint.ensureIndex(dbCol, dynamicIndex);
        }
      }
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug(
          "Dynamic database and/or collection selected: {}->{}",
          dbCol.getDB().getName(),
          dbCol.getName());
    }
    return dbCol;
  }
  private void processAndTransferWriteResult(WriteResult result, Exchange exchange) {
    // if invokeGetLastError is set, or a WriteConcern is set which implicitly calls getLastError,
    // then we have the chance to populate
    // the MONGODB_LAST_ERROR header, as well as setting an exception on the Exchange if one
    // occurred at the MongoDB server
    if (endpoint.isInvokeGetLastError()
        || (endpoint.getWriteConcern() != null
            ? endpoint.getWriteConcern().callGetLastError()
            : false)) {
      CommandResult cr =
          result.getCachedLastError() == null ? result.getLastError() : result.getCachedLastError();
      exchange.getOut().setHeader(MongoDbConstants.LAST_ERROR, cr);
      if (!cr.ok()) {
        exchange.setException(MongoDbComponent.wrapInCamelMongoDbException(cr.getException()));
      }
    }

    // determine where to set the WriteResult: as the OUT body or as an IN message header
    if (endpoint.isWriteResultAsHeader()) {
      exchange.getOut().setHeader(MongoDbConstants.WRITERESULT, result);
    } else {
      exchange.getOut().setBody(result);
    }
  }