Search in sources :

Example 41 with CommandResponse

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.

the class AbstractUpdate method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, ParseException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    String field = json.optString(GNSProtocol.FIELD.toString(), null);
    String value = json.optString(GNSProtocol.VALUE.toString(), null);
    String oldValue = json.optString(GNSProtocol.OLD_VALUE.toString(), null);
    int index = json.optInt(GNSProtocol.N.toString(), -1);
    JSONObject userJSON = json.has(GNSProtocol.USER_JSON.toString()) ? new JSONObject(json.getString(GNSProtocol.USER_JSON.toString())) : null;
    // writer might be unspecified so we use the guid
    String writer = json.optString(GNSProtocol.WRITER.toString(), guid);
    String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
    String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
    Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : // can be null on older client
    null;
    if (json.has("originalBase64")) {
        ClientCommandProcessorConfig.getLogger().log(Level.WARNING, "||||||||||||||||||||||||||| message:{0} original{1}", new Object[] { message, new String(Base64.decode(json.getString("originalBase64"))) });
    }
    ResponseCode responseCode;
    if (field == null) {
        responseCode = FieldAccess.updateUserJSON(header, commandPacket, guid, userJSON, writer, signature, message, timestamp, handler);
        if (!responseCode.isExceptionOrError()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode());
        }
    } else // single field update
    {
        if (!(responseCode = FieldAccess.update(header, commandPacket, guid, field, // special case for the ops which do not need a value
        value != null ? new ResultValue(Arrays.asList(value)) : new ResultValue(), oldValue != null ? new ResultValue(Arrays.asList(oldValue)) : null, index, getUpdateOperation(), writer, signature, message, timestamp, handler)).isExceptionOrError()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode());
        }
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) ResultValue(edu.umass.cs.gnsserver.utils.ResultValue) Date(java.util.Date)

Aggregations

CommandResponse (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse)41 JSONObject (org.json.JSONObject)41 Date (java.util.Date)25 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)16 AccountInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)13 GuidInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo)11 MetaDataTypeName (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.MetaDataTypeName)9 JSONArray (org.json.JSONArray)8 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)7 IOException (java.io.IOException)7 ResultValue (edu.umass.cs.gnsserver.utils.ResultValue)5 JSONException (org.json.JSONException)5 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)4 CommandType (edu.umass.cs.gnscommon.CommandType)1 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)1 AbstractCommand (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commands.AbstractCommand)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1