/driver-core/src/main/com/mongodb/operation/UpdateUserOperation.java

https://github.com/mebigfatguy/mongo-java-driver · Java · 139 lines · 81 code · 12 blank · 46 comment · 3 complexity · a9762b12c28e48d774eae5cbd08b6642 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.operation;
  17. import com.mongodb.MongoCommandException;
  18. import com.mongodb.MongoCredential;
  19. import com.mongodb.WriteConcern;
  20. import com.mongodb.async.SingleResultCallback;
  21. import com.mongodb.binding.AsyncWriteBinding;
  22. import com.mongodb.binding.WriteBinding;
  23. import com.mongodb.connection.AsyncConnection;
  24. import com.mongodb.connection.Connection;
  25. import com.mongodb.connection.ConnectionDescription;
  26. import org.bson.BsonDocument;
  27. import static com.mongodb.assertions.Assertions.notNull;
  28. import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
  29. import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol;
  30. import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocolAsync;
  31. import static com.mongodb.operation.OperationHelper.AsyncCallableWithConnection;
  32. import static com.mongodb.operation.OperationHelper.CallableWithConnection;
  33. import static com.mongodb.operation.OperationHelper.LOGGER;
  34. import static com.mongodb.operation.OperationHelper.releasingCallback;
  35. import static com.mongodb.operation.OperationHelper.withConnection;
  36. import static com.mongodb.operation.UserOperationHelper.asCommandDocument;
  37. import static com.mongodb.operation.UserOperationHelper.translateUserCommandException;
  38. import static com.mongodb.operation.UserOperationHelper.userCommandCallback;
  39. import static com.mongodb.internal.operation.WriteConcernHelper.appendWriteConcernToCommand;
  40. import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorTransformer;
  41. /**
  42. * An operation that updates a user.
  43. *
  44. * @since 3.0
  45. * @deprecated use {@link CommandWriteOperation} directly or the mongod shell helpers.
  46. */
  47. @Deprecated
  48. public class UpdateUserOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
  49. private final MongoCredential credential;
  50. private final boolean readOnly;
  51. private final WriteConcern writeConcern;
  52. /**
  53. * Construct a new instance.
  54. *
  55. * @param credential the users credentials.
  56. * @param readOnly true if the user is a readOnly user.
  57. */
  58. public UpdateUserOperation(final MongoCredential credential, final boolean readOnly) {
  59. this(credential, readOnly, null);
  60. }
  61. /**
  62. * Construct a new instance.
  63. *
  64. * @param credential the users credentials.
  65. * @param readOnly true if the user is a readOnly user.
  66. * @param writeConcern the write concern
  67. *
  68. * @since 3.4
  69. */
  70. public UpdateUserOperation(final MongoCredential credential, final boolean readOnly, final WriteConcern writeConcern) {
  71. this.credential = notNull("credential", credential);
  72. this.readOnly = readOnly;
  73. this.writeConcern = writeConcern;
  74. }
  75. /**
  76. * Gets the users credentials.
  77. *
  78. * @return the users credentials.
  79. */
  80. public MongoCredential getCredential() {
  81. return credential;
  82. }
  83. /**
  84. * Returns true if the user is a readOnly user.
  85. *
  86. * @return true if the user is a readOnly user.
  87. */
  88. public boolean isReadOnly() {
  89. return readOnly;
  90. }
  91. @Override
  92. public Void execute(final WriteBinding binding) {
  93. return withConnection(binding, new CallableWithConnection<Void>() {
  94. @Override
  95. public Void call(final Connection connection) {
  96. try {
  97. executeWrappedCommandProtocol(binding, getCredential().getSource(), getCommand(connection.getDescription()),
  98. connection, writeConcernErrorTransformer());
  99. } catch (MongoCommandException e) {
  100. translateUserCommandException(e);
  101. }
  102. return null;
  103. }
  104. });
  105. }
  106. @Override
  107. public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<Void> callback) {
  108. withConnection(binding, new AsyncCallableWithConnection() {
  109. @Override
  110. public void call(final AsyncConnection connection, final Throwable t) {
  111. SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
  112. if (t != null) {
  113. errHandlingCallback.onResult(null, t);
  114. } else {
  115. final SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection);
  116. executeWrappedCommandProtocolAsync(binding, credential.getSource(), getCommand(connection.getDescription()),
  117. connection, writeConcernErrorTransformer(), userCommandCallback(wrappedCallback));
  118. }
  119. }
  120. });
  121. }
  122. private BsonDocument getCommand(final ConnectionDescription description) {
  123. BsonDocument commandDocument = asCommandDocument(credential, description, readOnly, "updateUser");
  124. appendWriteConcernToCommand(writeConcern, commandDocument, description);
  125. return commandDocument;
  126. }
  127. }