Search in sources :

Example 11 with GridRestCacheRequest

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;
}
Also used : GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)

Example 12 with GridRestCacheRequest

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;
    }
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) HashMap(java.util.HashMap) DataStructuresRequest(org.apache.ignite.internal.processors.rest.request.DataStructuresRequest)

Example 13 with GridRestCacheRequest

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;
}
Also used : GridClientAuthenticationRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientAuthenticationRequest) GridRestTopologyRequest(org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRestChangeStateRequest(org.apache.ignite.internal.processors.rest.request.GridRestChangeStateRequest) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) GridClientStateRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientStateRequest) GridClientTopologyRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientTopologyRequest) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest) GridClientCacheRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) GridClientTaskRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskRequest) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with GridRestCacheRequest

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;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)

Example 15 with GridRestCacheRequest

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;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Aggregations

GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)20 GridRedisGenericException (org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)9 HashMap (java.util.HashMap)4 Map (java.util.Map)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 DataStructuresRequest (org.apache.ignite.internal.processors.rest.request.DataStructuresRequest)3 GridRestTaskRequest (org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest)3 EnumMap (java.util.EnumMap)2 List (java.util.List)2 IgniteException (org.apache.ignite.IgniteException)2 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)2 GridRestCommandHandler (org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler)2 GridRestRequest (org.apache.ignite.internal.processors.rest.request.GridRestRequest)2 GridRestTopologyRequest (org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1