View Javadoc
1   /*
2    * #%L
3    * ReplyCommandCallback.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  
21  package com.allanbank.mongodb.client.callback;
22  
23  import java.util.List;
24  
25  import com.allanbank.mongodb.Callback;
26  import com.allanbank.mongodb.MongoDbException;
27  import com.allanbank.mongodb.bson.Document;
28  import com.allanbank.mongodb.bson.Element;
29  import com.allanbank.mongodb.client.message.Reply;
30  import com.allanbank.mongodb.error.ReplyException;
31  
32  /**
33   * Callback to expect and extract a single document from the reply.
34   * 
35   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
36   *         mutated in incompatible ways between any two releases of the driver.
37   * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
38   */
39  public class ReplyCommandCallback extends AbstractReplyCallback<Document> {
40  
41      /**
42       * Create a new ReplyDocumentCallback.
43       * 
44       * @param results
45       *            The callback to notify of the reply document.
46       */
47      public ReplyCommandCallback(final Callback<Document> results) {
48          super(results);
49      }
50  
51      /**
52       * {@inheritDoc}
53       * <p>
54       * Creates an exception if the {@link Reply} has less than or more than a
55       * single reply document.
56       * </p>
57       * 
58       * @param reply
59       *            The raw reply.
60       * @return The exception created.
61       */
62      @Override
63      protected MongoDbException asError(final Reply reply) {
64          MongoDbException error = super.asError(reply);
65          if (error == null) {
66              final List<Document> results = reply.getResults();
67              if (results.size() != 1) {
68                  error = new ReplyException(reply,
69                          "Should only be a single document in the reply.");
70              }
71          }
72          return error;
73      }
74  
75      /**
76       * {@inheritDoc}
77       * <p>
78       * Overridden to not throw an exception on a zero 'ok' value.
79       * </p>
80       * 
81       * @param reply
82       *            The raw reply.
83       * @param knownError
84       *            If true then the reply is assumed to be an error reply.
85       * @return The exception created.
86       */
87      @Override
88      protected MongoDbException asError(final Reply reply,
89              final boolean knownError) {
90          final List<Document> results = reply.getResults();
91          if (results.size() == 1) {
92              final Document doc = results.get(0);
93              final Element okElem = doc.get("ok");
94              final Element errorNumberElem = doc.get("code");
95              Element errorMessageElem = null;
96              for (int i = 0; (errorMessageElem == null)
97                      && (i < ERROR_MESSAGE_FIELDS.size()); ++i) {
98                  errorMessageElem = doc.get(ERROR_MESSAGE_FIELDS.get(i));
99              }
100 
101             if ((okElem == null) && knownError) {
102                 return asError(reply, -1, toInt(errorNumberElem),
103                         asString(errorMessageElem));
104 
105             }
106         }
107         return null;
108     }
109 
110     /**
111      * {@inheritDoc}
112      * <p>
113      * Overridden to return the reply document.
114      * </p>
115      * 
116      * @see AbstractReplyCallback#convert(Reply)
117      */
118     @Override
119     protected Document convert(final Reply reply) throws MongoDbException {
120         final List<Document> results = reply.getResults();
121         if (results.size() == 1) {
122             return results.get(0);
123         }
124 
125         return null;
126     }
127 }