use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.
the class GridCacheCommandHandlerSelfTest method testAppend.
/**
* Test cache handler append/prepend commands with specified environment.
*
* @param curVal Current value in cache.
* @param newVal New value to append/prepend.
* @param append Append or prepend flag.
* @param <T> Cache value type.
* @return Resulting value in cache.
* @throws IgniteCheckedException In case of any grid exception.
*/
private <T> T testAppend(T curVal, T newVal, boolean append) throws IgniteCheckedException, EntryProcessorException {
GridRestCommandHandler hnd = new GridCacheCommandHandler(((IgniteKernal) grid()).context());
String key = UUID.randomUUID().toString();
GridRestCacheRequest req = new GridRestCacheRequest();
req.cacheName(DEFAULT_CACHE_NAME);
req.command(append ? GridRestCommand.CACHE_APPEND : GridRestCommand.CACHE_PREPEND);
req.key(key);
req.value(newVal);
assertFalse("Expects failure due to no value in cache.", (Boolean) hnd.handleAsync(req).get().getResponse());
T res;
try {
// Change cache state.
jcache().put(key, curVal);
// Validate behavior for initialized cache (has current value).
assertTrue((Boolean) hnd.handleAsync(req).get().getResponse());
} finally {
res = (T) jcache().getAndRemove(key);
}
return res;
}
use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.
the class GridTcpMemcachedNioListener method createRestRequest.
/**
* Creates REST request from the protocol request.
*
* @param req Request.
* @param cmd Command.
* @return REST request.
*/
@SuppressWarnings("unchecked")
private GridRestRequest createRestRequest(GridMemcachedMessage req, GridRestCommand cmd) {
assert req != null;
if (cmd == ATOMIC_INCREMENT || cmd == ATOMIC_DECREMENT) {
DataStructuresRequest restReq = new DataStructuresRequest();
restReq.command(cmd);
restReq.key(req.key());
restReq.delta(req.delta());
restReq.initial(req.initial());
return restReq;
} else {
GridRestCacheRequest restReq = new GridRestCacheRequest();
restReq.command(cmd);
restReq.clientId(req.clientId());
restReq.ttl(req.expiration());
restReq.cacheName(req.cacheName() == null ? CACHE_NAME : req.cacheName());
restReq.key(req.key());
if (cmd == CACHE_REMOVE_ALL) {
Object[] keys = (Object[]) req.value();
if (keys != null) {
Map<Object, Object> map = new HashMap<>();
for (Object key : keys) {
map.put(key, null);
}
restReq.values(map);
}
} else {
if (req.value() != null)
restReq.value(req.value());
}
return restReq;
}
}
use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.
the class GridTcpRestNioListener method createRestRequest.
/**
* Creates a REST request object from client TCP binary packet.
*
* @param ses NIO session.
* @param msg Request message.
* @return REST request object.
*/
@Nullable
private GridRestRequest createRestRequest(GridNioSession ses, GridClientMessage msg) {
GridRestRequest restReq = null;
if (msg instanceof GridClientAuthenticationRequest) {
GridClientAuthenticationRequest req = (GridClientAuthenticationRequest) msg;
restReq = new GridRestTaskRequest();
restReq.command(NOOP);
restReq.credentials(req.credentials());
} else if (msg instanceof GridClientCacheRequest) {
GridClientCacheRequest req = (GridClientCacheRequest) msg;
GridRestCacheRequest restCacheReq = new GridRestCacheRequest();
restCacheReq.cacheName(req.cacheName());
restCacheReq.cacheFlags(req.cacheFlagsOn());
restCacheReq.key(req.key());
restCacheReq.value(req.value());
restCacheReq.value2(req.value2());
Map vals = req.values();
if (vals != null)
restCacheReq.values(new HashMap<Object, Object>(vals));
restCacheReq.command(cacheCmdMap.get(req.operation()));
restReq = restCacheReq;
} else if (msg instanceof GridClientTaskRequest) {
GridClientTaskRequest req = (GridClientTaskRequest) msg;
GridRestTaskRequest restTaskReq = new GridRestTaskRequest();
restTaskReq.command(EXE);
restTaskReq.taskName(req.taskName());
restTaskReq.params(Arrays.asList(req.argument()));
restReq = restTaskReq;
} else if (msg instanceof GridClientTopologyRequest) {
GridClientTopologyRequest req = (GridClientTopologyRequest) msg;
GridRestTopologyRequest restTopReq = new GridRestTopologyRequest();
restTopReq.includeMetrics(req.includeMetrics());
restTopReq.includeAttributes(req.includeAttributes());
if (req.nodeId() != null) {
restTopReq.command(NODE);
restTopReq.nodeId(req.nodeId());
} else if (req.nodeIp() != null) {
restTopReq.command(NODE);
restTopReq.nodeIp(req.nodeIp());
} else
restTopReq.command(TOPOLOGY);
restReq = restTopReq;
} else if (msg instanceof GridClientStateRequest) {
GridClientStateRequest req = (GridClientStateRequest) msg;
GridRestChangeStateRequest restChangeReq = new GridRestChangeStateRequest();
if (req.isReqCurrentState()) {
restChangeReq.reqCurrentState();
restChangeReq.command(CLUSTER_CURRENT_STATE);
} else {
restChangeReq.active(req.active());
restChangeReq.command(req.active() ? CLUSTER_ACTIVE : CLUSTER_INACTIVE);
}
restReq = restChangeReq;
}
if (restReq != null) {
restReq.destinationId(msg.destinationId());
restReq.clientId(msg.clientId());
restReq.sessionToken(msg.sessionToken());
restReq.address(ses.remoteAddress());
}
return restReq;
}
use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.
the class GridRedisMSetCommandHandler method asRestRequest.
/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
assert msg != null;
GridRestCacheRequest restReq = new GridRestCacheRequest();
restReq.clientId(msg.clientId());
restReq.key(msg.key());
restReq.command(CACHE_PUT_ALL);
restReq.cacheName(CACHE_NAME);
List<String> els = msg.auxMKeys();
Map<Object, Object> mset = U.newHashMap(els.size() / 2);
Iterator<String> msetIt = els.iterator();
while (msetIt.hasNext()) mset.put(msetIt.next(), msetIt.hasNext() ? msetIt.next() : null);
restReq.values(mset);
return restReq;
}
use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.
the class GridRedisSetRangeCommandHandler method asRestRequest.
/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
assert msg != null;
if (msg.messageSize() < 4)
throw new GridRedisGenericException("Wrong number of arguments");
int off;
try {
off = Integer.parseInt(msg.aux(OFFSET_POS));
} catch (NumberFormatException e) {
U.error(log, "Erroneous offset", e);
throw new GridRedisGenericException("Offset is not an integer");
}
String val = String.valueOf(msg.aux(VAL_POS));
GridRestCacheRequest getReq = new GridRestCacheRequest();
getReq.clientId(msg.clientId());
getReq.key(msg.key());
getReq.command(CACHE_GET);
getReq.cacheName(CACHE_NAME);
if (val.isEmpty())
return getReq;
Object resp = hnd.handle(getReq).getResponse();
int totalLen = off + val.length();
if (off < 0 || totalLen > MAX_OFFSET)
throw new GridRedisGenericException("Offset is out of range");
GridRestCacheRequest putReq = new GridRestCacheRequest();
putReq.clientId(msg.clientId());
putReq.key(msg.key());
putReq.command(CACHE_PUT);
putReq.cacheName(CACHE_NAME);
if (resp == null) {
byte[] dst = new byte[totalLen];
System.arraycopy(val.getBytes(), 0, dst, off, val.length());
putReq.value(new String(dst));
} else {
if (!(resp instanceof String))
return getReq;
String cacheVal = String.valueOf(resp);
cacheVal = cacheVal.substring(0, off) + val;
putReq.value(cacheVal);
}
hnd.handle(putReq);
return getReq;
}
Aggregations