/*
 * Copyright (c) 2008-2014 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.connection;

import com.mongodb.MongoCommandException;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoServerException;
import com.mongodb.async.SingleResultCallback;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import org.bson.codecs.BsonDocumentCodec;

import static com.mongodb.MongoNamespace.COMMAND_COLLECTION_NAME;

final class CommandHelper {
    static BsonDocument executeCommand(final String database, final BsonDocument command, final InternalConnection internalConnection) {
        return sendAndReceive(database, command, internalConnection);
    }

    static BsonDocument executeCommandWithoutCheckingForFailure(final String database, final BsonDocument command,
                                                                final InternalConnection internalConnection) {
        try {
            return sendAndReceive(database, command, internalConnection);
        } catch (MongoServerException e) {
            return new BsonDocument();
        }
    }

    static void executeCommandAsync(final String database, final BsonDocument command, final InternalConnection internalConnection,
                                    final SingleResultCallback<BsonDocument> callback) {
        final SimpleCommandMessage message =
                new SimpleCommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME).getFullName(), command, false,
                                                MessageSettings.builder().build());

        internalConnection.sendAndReceiveAsync(message, new SingleResultCallback<ResponseBuffers>() {
            @Override
            public void onResult(final ResponseBuffers result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else {
                    ReplyMessage<BsonDocument> replyMessage = new ReplyMessage<BsonDocument>(result, new BsonDocumentCodec(),
                                                                                                    message.getId());
                    BsonDocument reply = replyMessage.getDocuments().get(0);
                    callback.onResult(reply, null);
                }
            }
        });
    }

    static boolean isCommandOk(final BsonDocument response) {
        if (!response.containsKey("ok")) {
            return false;
        }
        BsonValue okValue = response.get("ok");
        if (okValue.isBoolean()) {
            return okValue.asBoolean().getValue();
        } else if (okValue.isNumber()) {
            return okValue.asNumber().intValue() == 1;
        } else {
            return false;
        }
    }

    private static BsonDocument sendAndReceive(final String database, final BsonDocument command,
                                               final InternalConnection internalConnection) {
        SimpleCommandMessage message = new SimpleCommandMessage(new MongoNamespace(database, COMMAND_COLLECTION_NAME).getFullName(),
                                                                       command, false, MessageSettings.builder().build());
        ResponseBuffers responseBuffers = internalConnection.sendAndReceive(message);
        try {
            return new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId()).getDocuments().get(0);
        } finally {
            responseBuffers.close();
        }
    }

    private static void receiveReplyAsync(final InternalConnection internalConnection, final SimpleCommandMessage message,
                                          final SingleResultCallback<ReplyMessage<BsonDocument>> callback) {
        internalConnection.receiveMessageAsync(message.getId(),
                                               new SingleResultCallback<ResponseBuffers>() {
                                                   @Override
                                                   public void onResult(final ResponseBuffers result, final Throwable t) {
                                                       try {
                                                           if (t != null) {
                                                               callback.onResult(null, t);
                                                           } else {
                                                               callback.onResult(new ReplyMessage<BsonDocument>(result,
                                                                                                                new BsonDocumentCodec(),
                                                                                                                message.getId()), null);
                                                           }
                                                       } finally {
                                                           if (result != null) {
                                                               result.close();
                                                           }
                                                       }
                                                   }
                                               });
    }

    private static MongoCommandException createCommandFailureException(final BsonDocument reply,
                                                                       final InternalConnection internalConnection) {
        return new MongoCommandException(reply, internalConnection.getDescription().getServerAddress());
    }

    private CommandHelper() {
    }
}

@@ -635,50 +635,8 @@
 n;%0A%0A
-import com.mongodb.MongoCommandException;%0A
 impo
@@ -3980,2115 +3980,8 @@
  %7D%0A%0A
-    private static void receiveReplyAsync(final InternalConnection internalConnection, final SimpleCommandMessage message,%0A                                          final SingleResultCallback%3CReplyMessage%3CBsonDocument%3E%3E callback) %7B%0A        internalConnection.receiveMessageAsync(message.getId(),%0A                                               new SingleResultCallback%3CResponseBuffers%3E() %7B%0A                                                   @Override%0A                                                   public void onResult(final ResponseBuffers result, final Throwable t) %7B%0A                                                       try %7B%0A                                                           if (t != null) %7B%0A                                                               callback.onResult(null, t);%0A                                                           %7D else %7B%0A                                                               callback.onResult(new ReplyMessage%3CBsonDocument%3E(result,%0A                                                                                                                new BsonDocumentCodec(),%0A                                                                                                                message.getId()), null);%0A                                                           %7D%0A                                                       %7D finally %7B%0A                                                           if (result != null) %7B%0A                                                               result.close();%0A                                                           %7D%0A                                                       %7D%0A                                                   %7D%0A                                               %7D);%0A    %7D%0A%0A    private static MongoCommandException createCommandFailureException(final BsonDocument reply,%0A                                                                       final InternalConnection internalConnection) %7B%0A        return new MongoCommandException(reply, internalConnection.getDescription().getServerAddress());%0A    %7D%0A%0A
     

Remove unused methods