Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReplyCommandCallback |
|
| 3.5;3.5 |
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 | 40 | 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 | 52 | super(results); |
49 | 52 | } |
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 | 42 | MongoDbException error = super.asError(reply); |
65 | 42 | if (error == null) { |
66 | 42 | final List<Document> results = reply.getResults(); |
67 | 42 | if (results.size() != 1) { |
68 | 1 | error = new ReplyException(reply, |
69 | "Should only be a single document in the reply."); | |
70 | } | |
71 | } | |
72 | 42 | 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 | 47 | final List<Document> results = reply.getResults(); |
91 | 47 | if (results.size() == 1) { |
92 | 46 | final Document doc = results.get(0); |
93 | 46 | final Element okElem = doc.get("ok"); |
94 | 46 | final Element errorNumberElem = doc.get("code"); |
95 | 46 | Element errorMessageElem = null; |
96 | 46 | for (int i = 0; (errorMessageElem == null) |
97 | 500 | && (i < ERROR_MESSAGE_FIELDS.size()); ++i) { |
98 | 227 | errorMessageElem = doc.get(ERROR_MESSAGE_FIELDS.get(i)); |
99 | } | |
100 | ||
101 | 46 | if ((okElem == null) && knownError) { |
102 | 3 | return asError(reply, -1, toInt(errorNumberElem), |
103 | asString(errorMessageElem)); | |
104 | ||
105 | } | |
106 | } | |
107 | 44 | 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 | 42 | final List<Document> results = reply.getResults(); |
121 | 42 | if (results.size() == 1) { |
122 | 41 | return results.get(0); |
123 | } | |
124 | ||
125 | 1 | return null; |
126 | } | |
127 | } |