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

http://github.com/mongodb/mongo-java-driver · Java · 147 lines · 99 code · 12 blank · 36 comment · 7 complexity · de41d46be61c615b9e57532585df0828 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2016 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.MongoNamespace;
  19. import com.mongodb.WriteConcern;
  20. import com.mongodb.WriteConcernResult;
  21. import com.mongodb.async.SingleResultCallback;
  22. import com.mongodb.binding.AsyncWriteBinding;
  23. import com.mongodb.binding.WriteBinding;
  24. import com.mongodb.bulk.DeleteRequest;
  25. import com.mongodb.connection.AsyncConnection;
  26. import com.mongodb.connection.Connection;
  27. import com.mongodb.connection.ConnectionDescription;
  28. import org.bson.BsonDocument;
  29. import org.bson.BsonString;
  30. import static com.mongodb.assertions.Assertions.notNull;
  31. import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
  32. import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol;
  33. import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocolAsync;
  34. import static com.mongodb.operation.OperationHelper.AsyncCallableWithConnection;
  35. import static com.mongodb.operation.OperationHelper.CallableWithConnection;
  36. import static com.mongodb.operation.OperationHelper.LOGGER;
  37. import static com.mongodb.operation.OperationHelper.releasingCallback;
  38. import static com.mongodb.operation.OperationHelper.serverIsAtLeastVersionTwoDotSix;
  39. import static com.mongodb.operation.OperationHelper.withConnection;
  40. import static com.mongodb.operation.UserOperationHelper.translateUserCommandException;
  41. import static com.mongodb.operation.UserOperationHelper.userCommandCallback;
  42. import static com.mongodb.operation.WriteConcernHelper.appendWriteConcernToCommand;
  43. import static com.mongodb.operation.WriteConcernHelper.writeConcernErrorTransformer;
  44. import static java.util.Arrays.asList;
  45. /**
  46. * An operation to remove a user.
  47. *
  48. * @since 3.0
  49. */
  50. public class DropUserOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
  51. private final String databaseName;
  52. private final String userName;
  53. private final WriteConcern writeConcern;
  54. /**
  55. * Construct a new instance.
  56. *
  57. * @param databaseName the name of the database for the operation.
  58. * @param userName the name of the user to be dropped.
  59. * @deprecated Prefer {@link #DropUserOperation(String, String, WriteConcern)}
  60. */
  61. @Deprecated
  62. public DropUserOperation(final String databaseName, final String userName) {
  63. this(databaseName, userName, null);
  64. }
  65. /**
  66. * Construct a new instance.
  67. *
  68. * @param databaseName the name of the database for the operation.
  69. * @param userName the name of the user to be dropped.
  70. * @param writeConcern the write concern
  71. *
  72. * @since 3.4
  73. */
  74. public DropUserOperation(final String databaseName, final String userName, final WriteConcern writeConcern) {
  75. this.databaseName = notNull("databaseName", databaseName);
  76. this.userName = notNull("userName", userName);
  77. this.writeConcern = writeConcern;
  78. }
  79. @Override
  80. public Void execute(final WriteBinding binding) {
  81. return withConnection(binding, new CallableWithConnection<Void>() {
  82. @Override
  83. public Void call(final Connection connection) {
  84. if (serverIsAtLeastVersionTwoDotSix(connection.getDescription())) {
  85. try {
  86. executeWrappedCommandProtocol(binding, databaseName, getCommand(connection.getDescription()), connection,
  87. writeConcernErrorTransformer());
  88. } catch (MongoCommandException e) {
  89. translateUserCommandException(e);
  90. }
  91. } else {
  92. connection.delete(getNamespace(), true, WriteConcern.ACKNOWLEDGED, asList(getDeleteRequest()));
  93. }
  94. return null;
  95. }
  96. });
  97. }
  98. @Override
  99. public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<Void> callback) {
  100. withConnection(binding, new AsyncCallableWithConnection() {
  101. @Override
  102. public void call(final AsyncConnection connection, final Throwable t) {
  103. SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
  104. if (t != null) {
  105. errHandlingCallback.onResult(null, t);
  106. } else {
  107. final SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection);
  108. if (serverIsAtLeastVersionTwoDotSix(connection.getDescription())) {
  109. executeWrappedCommandProtocolAsync(binding, databaseName, getCommand(connection.getDescription()), connection,
  110. writeConcernErrorTransformer(), userCommandCallback(wrappedCallback));
  111. } else {
  112. connection.deleteAsync(getNamespace(), true, WriteConcern.ACKNOWLEDGED, asList(getDeleteRequest()),
  113. new SingleResultCallback<WriteConcernResult>() {
  114. @Override
  115. public void onResult(final WriteConcernResult result, final Throwable t) {
  116. wrappedCallback.onResult(null, t);
  117. }
  118. });
  119. }
  120. }
  121. }
  122. });
  123. }
  124. private MongoNamespace getNamespace() {
  125. return new MongoNamespace(databaseName, "system.users");
  126. }
  127. private DeleteRequest getDeleteRequest() {
  128. return new DeleteRequest(new BsonDocument("user", new BsonString(userName)));
  129. }
  130. private BsonDocument getCommand(final ConnectionDescription description) {
  131. BsonDocument commandDocument = new BsonDocument("dropUser", new BsonString(userName));
  132. appendWriteConcernToCommand(writeConcern, commandDocument, description);
  133. return commandDocument;
  134. }
  135. }