Search in sources :

Example 16 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridRedisExistsCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 2)
        throw new GridRedisGenericException("Wrong number of arguments");
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.command(CACHE_GET_ALL);
    restReq.cacheName(CACHE_NAME);
    List<String> keys = msg.auxMKeys();
    Map<Object, Object> mget = U.newHashMap(keys.size());
    for (String key : keys) mget.put(key, null);
    restReq.values(mget);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Example 17 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridRedisAppendCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 3)
        throw new GridRedisGenericException("Wrong syntax");
    GridRestCacheRequest appendReq = new GridRestCacheRequest();
    GridRestCacheRequest getReq = new GridRestCacheRequest();
    String val = msg.aux(VAL_POS);
    appendReq.clientId(msg.clientId());
    appendReq.key(msg.key());
    appendReq.value(val);
    appendReq.command(CACHE_APPEND);
    appendReq.cacheName(CACHE_NAME);
    Object resp = hnd.handle(appendReq).getResponse();
    if (resp != null && !(boolean) resp) {
        // append on non-existing key.
        GridRestCacheRequest setReq = new GridRestCacheRequest();
        setReq.clientId(msg.clientId());
        setReq.key(msg.key());
        setReq.value(val);
        setReq.command(CACHE_PUT);
        setReq.cacheName(CACHE_NAME);
        hnd.handle(setReq);
    }
    getReq.clientId(msg.clientId());
    getReq.key(msg.key());
    getReq.command(CACHE_GET);
    getReq.cacheName(CACHE_NAME);
    return getReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Example 18 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridRedisGetCommandHandler 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_GET);
    restReq.cacheName(CACHE_NAME);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)

Example 19 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridRedisGetRangeCommandHandler 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");
    GridRestCacheRequest getReq = new GridRestCacheRequest();
    getReq.clientId(msg.clientId());
    getReq.key(msg.key());
    getReq.command(CACHE_GET);
    getReq.cacheName(CACHE_NAME);
    return getReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Example 20 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridJettyRestHandler method createRequest.

/**
     * Creates REST request.
     *
     * @param cmd Command.
     * @param params Parameters.
     * @param req Servlet request.
     * @return REST request.
     * @throws IgniteCheckedException If creation failed.
     */
@Nullable
private GridRestRequest createRequest(GridRestCommand cmd, Map<String, Object> params, HttpServletRequest req) throws IgniteCheckedException {
    GridRestRequest restReq;
    switch(cmd) {
        case GET_OR_CREATE_CACHE:
        case DESTROY_CACHE:
            {
                GridRestCacheRequest restReq0 = new GridRestCacheRequest();
                restReq0.cacheName((String) params.get("cacheName"));
                restReq = restReq0;
                break;
            }
        case ATOMIC_DECREMENT:
        case ATOMIC_INCREMENT:
            {
                DataStructuresRequest restReq0 = new DataStructuresRequest();
                restReq0.key(params.get("key"));
                restReq0.initial(longValue("init", params, null));
                restReq0.delta(longValue("delta", params, null));
                restReq = restReq0;
                break;
            }
        case CACHE_CONTAINS_KEY:
        case CACHE_CONTAINS_KEYS:
        case CACHE_GET:
        case CACHE_GET_ALL:
        case CACHE_GET_AND_PUT:
        case CACHE_GET_AND_REPLACE:
        case CACHE_PUT_IF_ABSENT:
        case CACHE_GET_AND_PUT_IF_ABSENT:
        case CACHE_PUT:
        case CACHE_PUT_ALL:
        case CACHE_REMOVE:
        case CACHE_REMOVE_VALUE:
        case CACHE_REPLACE_VALUE:
        case CACHE_GET_AND_REMOVE:
        case CACHE_REMOVE_ALL:
        case CACHE_ADD:
        case CACHE_CAS:
        case CACHE_METRICS:
        case CACHE_SIZE:
        case CACHE_METADATA:
        case CACHE_REPLACE:
        case CACHE_APPEND:
        case CACHE_PREPEND:
            {
                GridRestCacheRequest restReq0 = new GridRestCacheRequest();
                String cacheName = (String) params.get("cacheName");
                restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName);
                restReq0.key(params.get("key"));
                restReq0.value(params.get("val"));
                restReq0.value2(params.get("val2"));
                Object val1 = params.get("val1");
                if (val1 != null)
                    restReq0.value(val1);
                restReq0.cacheFlags(intValue("cacheFlags", params, 0));
                restReq0.ttl(longValue("exp", params, null));
                if (cmd == CACHE_GET_ALL || cmd == CACHE_PUT_ALL || cmd == CACHE_REMOVE_ALL || cmd == CACHE_CONTAINS_KEYS) {
                    List<Object> keys = values("k", params);
                    List<Object> vals = values("v", params);
                    if (keys.size() < vals.size())
                        throw new IgniteCheckedException("Number of keys must be greater or equals to number of values.");
                    Map<Object, Object> map = U.newHashMap(keys.size());
                    Iterator<Object> keyIt = keys.iterator();
                    Iterator<Object> valIt = vals.iterator();
                    while (keyIt.hasNext()) map.put(keyIt.next(), valIt.hasNext() ? valIt.next() : null);
                    restReq0.values(map);
                }
                restReq = restReq0;
                break;
            }
        case TOPOLOGY:
        case NODE:
            {
                GridRestTopologyRequest restReq0 = new GridRestTopologyRequest();
                restReq0.includeMetrics(Boolean.parseBoolean((String) params.get("mtr")));
                restReq0.includeAttributes(Boolean.parseBoolean((String) params.get("attr")));
                restReq0.nodeIp((String) params.get("ip"));
                restReq0.nodeId(uuidValue("id", params));
                restReq = restReq0;
                break;
            }
        case EXE:
        case RESULT:
        case NOOP:
            {
                GridRestTaskRequest restReq0 = new GridRestTaskRequest();
                restReq0.taskId((String) params.get("id"));
                restReq0.taskName((String) params.get("name"));
                restReq0.params(values("p", params));
                restReq0.async(Boolean.parseBoolean((String) params.get("async")));
                restReq0.timeout(longValue("timeout", params, 0L));
                restReq = restReq0;
                break;
            }
        case LOG:
            {
                GridRestLogRequest restReq0 = new GridRestLogRequest();
                restReq0.path((String) params.get("path"));
                restReq0.from(intValue("from", params, -1));
                restReq0.to(intValue("to", params, -1));
                restReq = restReq0;
                break;
            }
        case NAME:
        case VERSION:
            {
                restReq = new GridRestRequest();
                break;
            }
        case EXECUTE_SQL_QUERY:
        case EXECUTE_SQL_FIELDS_QUERY:
            {
                RestQueryRequest restReq0 = new RestQueryRequest();
                restReq0.sqlQuery((String) params.get("qry"));
                restReq0.arguments(values("arg", params).toArray());
                restReq0.typeName((String) params.get("type"));
                String pageSize = (String) params.get("pageSize");
                if (pageSize != null)
                    restReq0.pageSize(Integer.parseInt(pageSize));
                String distributedJoins = (String) params.get("distributedJoins");
                if (distributedJoins != null)
                    restReq0.distributedJoins(Boolean.parseBoolean(distributedJoins));
                restReq0.cacheName((String) params.get("cacheName"));
                if (cmd == EXECUTE_SQL_QUERY)
                    restReq0.queryType(RestQueryRequest.QueryType.SQL);
                else
                    restReq0.queryType(RestQueryRequest.QueryType.SQL_FIELDS);
                restReq = restReq0;
                break;
            }
        case EXECUTE_SCAN_QUERY:
            {
                RestQueryRequest restReq0 = new RestQueryRequest();
                restReq0.sqlQuery((String) params.get("qry"));
                String pageSize = (String) params.get("pageSize");
                if (pageSize != null)
                    restReq0.pageSize(Integer.parseInt(pageSize));
                restReq0.cacheName((String) params.get("cacheName"));
                restReq0.className((String) params.get("className"));
                restReq0.queryType(RestQueryRequest.QueryType.SCAN);
                restReq = restReq0;
                break;
            }
        case FETCH_SQL_QUERY:
            {
                RestQueryRequest restReq0 = new RestQueryRequest();
                String qryId = (String) params.get("qryId");
                if (qryId != null)
                    restReq0.queryId(Long.parseLong(qryId));
                String pageSize = (String) params.get("pageSize");
                if (pageSize != null)
                    restReq0.pageSize(Integer.parseInt(pageSize));
                restReq0.cacheName((String) params.get("cacheName"));
                restReq = restReq0;
                break;
            }
        case CLOSE_SQL_QUERY:
            {
                RestQueryRequest restReq0 = new RestQueryRequest();
                String qryId = (String) params.get("qryId");
                if (qryId != null)
                    restReq0.queryId(Long.parseLong(qryId));
                restReq0.cacheName((String) params.get("cacheName"));
                restReq = restReq0;
                break;
            }
        default:
            throw new IgniteCheckedException("Invalid command: " + cmd);
    }
    restReq.address(new InetSocketAddress(req.getRemoteAddr(), req.getRemotePort()));
    restReq.command(cmd);
    if (params.containsKey("ignite.login") || params.containsKey("ignite.password")) {
        SecurityCredentials cred = new SecurityCredentials((String) params.get("ignite.login"), (String) params.get("ignite.password"));
        restReq.credentials(cred);
    }
    String clientId = (String) params.get("clientId");
    try {
        if (clientId != null)
            restReq.clientId(UUID.fromString(clientId));
    } catch (Exception ignored) {
    // Ignore invalid client id. Rest handler will process this logic.
    }
    String destId = (String) params.get("destId");
    try {
        if (destId != null)
            restReq.destinationId(UUID.fromString(destId));
    } catch (IllegalArgumentException ignored) {
    // Don't fail - try to execute locally.
    }
    String sesTokStr = (String) params.get("sessionToken");
    try {
        if (sesTokStr != null)
            restReq.sessionToken(U.hexString2ByteArray(sesTokStr));
    } catch (IllegalArgumentException ignored) {
    // Ignore invalid session token.
    }
    return restReq;
}
Also used : GridRestTopologyRequest(org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest) InetSocketAddress(java.net.InetSocketAddress) GridRestLogRequest(org.apache.ignite.internal.processors.rest.request.GridRestLogRequest) ServletException(javax.servlet.ServletException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) RestQueryRequest(org.apache.ignite.internal.processors.rest.request.RestQueryRequest) Iterator(java.util.Iterator) DataStructuresRequest(org.apache.ignite.internal.processors.rest.request.DataStructuresRequest) LinkedList(java.util.LinkedList) List(java.util.List) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

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