* the result of the operation
* @return the message
*/
protected Message buildKeyValueResponse (final KeyValueOperations operation, final CompletionToken token, final Object result)
{
Message message = null;
switch (operation) {
case SET :
case DELETE :
final boolean success = (Boolean) result;
if (success) {
final Ok.Builder okPayload = IdlCommon.Ok.newBuilder ();
okPayload.setToken (token);
message = new Message (KeyValueMessage.OK, okPayload.build ());
} else {
final NotOk.Builder nokPayload = IdlCommon.NotOk.newBuilder ();
nokPayload.setToken (token);
message = new Message (KeyValueMessage.NOK, nokPayload.build ());
}
break;
case LIST :
final ListReply.Builder listPayload = KeyValuePayloads.ListReply.newBuilder ();
listPayload.setToken (token);
@SuppressWarnings ("unchecked") final List<String> resList = (List<String>) result;
listPayload.addAllKeys (resList);
message = new Message (KeyValueMessage.LIST_REPLY, listPayload.build ());
break;
case GET :
final GetReply.Builder getPayload = KeyValuePayloads.GetReply.newBuilder ();
getPayload.setToken (token);
@SuppressWarnings ("unchecked") final Map<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage> resMap = (Map<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage>) result;
final List<KVEntry> getResults = new ArrayList<KVEntry> ();
for (final Map.Entry<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage> entry : resMap.entrySet ()) {
final KVEntry.Builder kvEntry = KeyValuePayloads.KVEntry.newBuilder ();
kvEntry.setKey (entry.getKey ());
final IdlCommon.Envelope.Builder envelope = IdlCommon.Envelope.newBuilder ();
if (null != entry.getValue ().getContentEncoding ())
envelope.setContentEncoding (entry.getValue ().getContentEncoding ());
else
envelope.setContentEncoding ("");
if (null != entry.getValue ().getContentType ())
envelope.setContentType (entry.getValue ().getContentType ());
else
envelope.setContentType ("");
kvEntry.setEnvelope (envelope.build ());
if (entry.getValue ().getData () == null) {
kvEntry.setValue (ByteString.EMPTY);
} else {
kvEntry.setValue (ByteString.copyFrom (entry.getValue ().getData ()));
}
getResults.add (kvEntry.build ());
}
getPayload.addAllResults (getResults);
message = new Message (KeyValueMessage.GET_REPLY, getPayload.build ());
break;
default:
break;
}
return message;