1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
37
38
39 public class ReplyCommandCallback extends AbstractReplyCallback<Document> {
40
41
42
43
44
45
46
47 public ReplyCommandCallback(final Callback<Document> results) {
48 super(results);
49 }
50
51
52
53
54
55
56
57
58
59
60
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
77
78
79
80
81
82
83
84
85
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
112
113
114
115
116
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 }