use of edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket in project GNS by MobilityFirst.
the class FieldAccess method selectQuery.
/**
* Sends a select request to the server to retrieve all the guid matching the query.
*
* @param header
* @param commandPacket
* @param reader
* @param query
* @param projection
* @param signature
* @param message
* @param handler
* @return a command response
* @throws InternalRequestException
*/
public static CommandResponse selectQuery(InternalRequestHeader header, CommandPacket commandPacket, String reader, String query, List<String> projection, String signature, String message, ClientRequestHandlerInterface handler) throws InternalRequestException {
if (Select.queryContainsEvil(query)) {
return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Bad query operators in " + query);
}
JSONArray result;
try {
SelectRequestPacket packet = SelectRequestPacket.MakeQueryRequest(-1, reader, query, projection);
result = executeSelectHelper(header, commandPacket, packet, reader, signature, message, handler.getApp());
if (result != null) {
return new CommandResponse(ResponseCode.NO_ERROR, result.toString());
}
} catch (IOException | JSONException | FailedDBOperationException e) {
// FIXME: why silently fail?
}
return new CommandResponse(ResponseCode.NO_ERROR, EMPTY_JSON_ARRAY_STRING);
}
use of edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket in project GNS by MobilityFirst.
the class FieldAccess method selectGroupLookupQuery.
/**
* Sends a select request to the server to retrieve the members of a context aware group guid.
*
* @param header
* @param commandPacket
* @param reader
* @param accountGuid - the guid (which should have been previously initialized using <code>selectGroupSetupQuery</code>
* @param signature
* @param message
* @param handler
* @return a command response
* @throws InternalRequestException
*/
public static CommandResponse selectGroupLookupQuery(InternalRequestHeader header, CommandPacket commandPacket, String reader, String accountGuid, String signature, String message, ClientRequestHandlerInterface handler) throws InternalRequestException {
JSONArray result;
try {
SelectRequestPacket packet = SelectRequestPacket.MakeGroupLookupRequest(-1, reader, accountGuid);
result = executeSelectHelper(header, commandPacket, packet, reader, signature, message, handler.getApp());
if (result != null) {
return new CommandResponse(ResponseCode.NO_ERROR, result.toString());
}
} catch (IOException | JSONException | FailedDBOperationException e) {
// FIXME: why silently fail?
}
return new CommandResponse(ResponseCode.NO_ERROR, EMPTY_JSON_ARRAY_STRING);
}
use of edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket in project GNS by MobilityFirst.
the class GNSApp method execute.
/**
*
* @param request
* @param doNotReplyToClient
* @return true if the command is successfully executed
*/
@SuppressWarnings("unchecked")
// we explicitly check type
@Override
public boolean execute(Request request, boolean doNotReplyToClient) {
boolean executed = false;
if (executeNoop(request)) {
return true;
}
try {
Packet.PacketType packetType = request.getRequestType() instanceof Packet.PacketType ? (Packet.PacketType) request.getRequestType() : null;
GNSConfig.getLogger().log(Level.FINE, "{0} starting execute({1}) doNotReplyToClient={2}", new Object[] { this, request.getSummary(), doNotReplyToClient });
Request prev = null;
// arun: enqueue request, dequeue before returning
if (request instanceof RequestIdentifier) {
if (enqueueCommand()) {
prev = this.outstanding.putIfAbsent(((RequestIdentifier) request).getRequestID(), request);
}
} else {
assert (false) : this + " should not be getting requests that do not implement " + RequestIdentifier.class;
}
switch(packetType) {
case SELECT_REQUEST:
Select.handleSelectRequest((SelectRequestPacket) request, this);
break;
case SELECT_RESPONSE:
Select.handleSelectResponse((SelectResponsePacket) request, this);
break;
case COMMAND:
CommandHandler.handleCommandPacket((CommandPacket) request, doNotReplyToClient, this);
break;
case ADMIN_COMMAND:
CommandHandler.handleCommandPacket((AdminCommandPacket) request, doNotReplyToClient, this);
break;
default:
assert (false) : (this + " should not be getting packets of type " + packetType + "; exiting");
GNSConfig.getLogger().log(Level.SEVERE, " Packet type not found: {0}", request.getSummary());
return false;
}
executed = true;
// arun: always clean up all created state upon exiting
if (request instanceof RequestIdentifier && prev == null) {
GNSConfig.getLogger().log(Level.FINE, "{0} finished execute({1}) -> {2}", new Object[] { this, request.getSummary(), request instanceof ClientRequest && ((ClientRequest) request).getResponse() != null ? ((ClientRequest) request).getResponse().getSummary() : null });
this.outstanding.remove(((RequestIdentifier) request).getRequestID());
}
} catch (JSONException | IOException | ClientException | InternalRequestException e) {
e.printStackTrace();
} catch (FailedDBOperationException e) {
// all database operations throw this exception, therefore we keep
// throwing this exception upwards and catch this
// here.
// A database operation error would imply that the application
// hasn't been able to successfully execute
// the request. therefore, this method returns 'false', hoping that
// whoever calls handleDecision would retry
// the request.
GNSConfig.getLogger().log(Level.SEVERE, "Error handling request: {0}", request.toString());
e.printStackTrace();
}
return executed;
}
use of edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket in project GNS by MobilityFirst.
the class FieldAccess method selectGroupSetupQuery.
/**
* Sends a select request to the server to setup a context aware group guid and retrieve all the guids matching the query.
*
* @param header
* @param commandPacket
* @param reader
* @param accountGuid
* @param query
* @param publicKey
* @param interval - the refresh interval (queries made more quickly than this will get a cached value)
* @param signature
* @param message
* @param handler
* @return a command response
* @throws InternalRequestException
*/
public static CommandResponse selectGroupSetupQuery(InternalRequestHeader header, CommandPacket commandPacket, String reader, String accountGuid, String query, String publicKey, int interval, String signature, String message, ClientRequestHandlerInterface handler) throws InternalRequestException {
String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
// Check to see if the guid doesn't exists and if so create it...
if (AccountAccess.lookupGuidInfoAnywhere(header, guid, handler) == null) {
// This code is similar to the code in AddGuid command except that we're not checking signatures... yet.
// FIXME: This should probably include authentication
GuidInfo accountGuidInfo;
if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, accountGuid, handler)) == null) {
return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + accountGuid);
}
AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler);
if (accountInfo == null) {
return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + accountGuid);
}
if (!accountInfo.isVerified()) {
return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
} else if (accountInfo.getGuids().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_SUBGUIDS)) {
return new CommandResponse(ResponseCode.TOO_MANY_GUIDS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_GUIDS.toString());
} else {
// The alias (HRN) of the new guid is a hash of the query.
String name = Base64.encodeToString(ShaOneHashFunction.getInstance().hash(query), false);
CommandResponse groupGuidCreateresult = AccountAccess.addGuid(header, commandPacket, accountInfo, accountGuidInfo, name, guid, publicKey, handler);
// If there was a problem adding return that error response.
if (!groupGuidCreateresult.getExceptionOrErrorCode().isOKResult()) {
return groupGuidCreateresult;
}
}
}
JSONArray result;
try {
SelectRequestPacket packet = SelectRequestPacket.MakeGroupSetupRequest(-1, reader, query, null, guid, interval);
result = executeSelectHelper(header, commandPacket, packet, reader, signature, message, handler.getApp());
if (result != null) {
return new CommandResponse(ResponseCode.NO_ERROR, result.toString());
}
} catch (IOException | JSONException | FailedDBOperationException e) {
LOGGER.log(Level.FINE, "Silently failing select for query: {0} field: {1} reader: {2} due to {3}", new Object[] { guid, query, reader, e.getMessage() });
}
return new CommandResponse(ResponseCode.NO_ERROR, EMPTY_JSON_ARRAY_STRING);
}
Aggregations