Search in sources :

Example 1 with GridRestCommand

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

the class GridCacheCommandHandler method handleAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(final GridRestRequest req) {
    assert req instanceof GridRestCacheRequest : "Invalid command for topology handler: " + req;
    assert SUPPORTED_COMMANDS.contains(req.command());
    if (log.isDebugEnabled())
        log.debug("Handling cache REST request: " + req);
    GridRestCacheRequest req0 = (GridRestCacheRequest) req;
    final Object key = req0.key();
    final Set<GridClientCacheFlag> cacheFlags = GridClientCacheFlag.parseCacheFlags(req0.cacheFlags());
    try {
        GridRestCommand cmd = req0.command();
        if (req0.cacheName() == null && !CACHE_NAME_NOT_REQUIRED_REQUESTS.contains(cmd))
            throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("cacheName"));
        if (key == null && KEY_REQUIRED_REQUESTS.contains(cmd))
            throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("key"));
        final Long ttl = req0.ttl();
        IgniteInternalFuture<GridRestResponse> fut;
        switch(cmd) {
            case DESTROY_CACHE:
                {
                    // Do not check thread tx here since there can be active system cache txs.
                    fut = ((IgniteKernal) ctx.grid()).destroyCacheAsync(req0.cacheName(), false, false).chain(new CX1<IgniteInternalFuture<?>, GridRestResponse>() {

                        @Override
                        public GridRestResponse applyx(IgniteInternalFuture<?> f) throws IgniteCheckedException {
                            f.get();
                            return new GridRestResponse(null);
                        }
                    });
                    break;
                }
            case GET_OR_CREATE_CACHE:
                {
                    String templateName = req0.templateName();
                    if (F.isEmpty(templateName))
                        templateName = TEMPLATE_PARTITIONED;
                    CacheConfigurationOverride cfgOverride = req0.configuration();
                    boolean dfltPartTemplate = F.isEmpty(templateName) || TEMPLATE_PARTITIONED.equalsIgnoreCase(templateName);
                    boolean dfltReplTemplate = TEMPLATE_REPLICATED.equalsIgnoreCase(templateName);
                    if (dfltPartTemplate || dfltReplTemplate) {
                        if (cfgOverride == null)
                            cfgOverride = new CacheConfigurationOverride();
                        cfgOverride.mode(dfltPartTemplate ? PARTITIONED : REPLICATED);
                        if (cfgOverride.writeSynchronizationMode() == null)
                            cfgOverride.writeSynchronizationMode(FULL_SYNC);
                    }
                    // Do not check thread tx here since there can be active system cache txs.
                    fut = ((IgniteKernal) ctx.grid()).getOrCreateCacheAsync(req0.cacheName(), templateName, cfgOverride, false).chain(new CX1<IgniteInternalFuture<?>, GridRestResponse>() {

                        @Override
                        public GridRestResponse applyx(IgniteInternalFuture<?> f) throws IgniteCheckedException {
                            f.get();
                            return new GridRestResponse(null);
                        }
                    });
                    break;
                }
            case CACHE_METADATA:
                {
                    fut = ctx.task().execute(MetadataTask.class, req0.cacheName());
                    break;
                }
            case CACHE_CONTAINS_KEYS:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new ContainsKeysCommand(getKeys(req0)));
                    break;
                }
            case CACHE_CONTAINS_KEY:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new ContainsKeyCommand(key));
                    break;
                }
            case CACHE_GET:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new GetCommand(key));
                    break;
                }
            case CACHE_GET_AND_PUT:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new GetAndPutCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_GET_AND_REPLACE:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new GetAndReplaceCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_GET_AND_PUT_IF_ABSENT:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new GetAndPutIfAbsentCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_PUT_IF_ABSENT:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new PutIfAbsentCommand(key, ttl, getValue(req0)));
                    break;
                }
            case CACHE_GET_ALL:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new GetAllCommand(getKeys(req0)));
                    break;
                }
            case CACHE_PUT:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new PutCommand(key, ttl, getValue(req0)));
                    break;
                }
            case CACHE_ADD:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new AddCommand(key, ttl, getValue(req0)));
                    break;
                }
            case CACHE_PUT_ALL:
                {
                    Map<Object, Object> map = req0.values();
                    if (F.isEmpty(map))
                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("values"));
                    for (Map.Entry<Object, Object> e : map.entrySet()) {
                        if (e.getKey() == null)
                            throw new IgniteCheckedException("Failing putAll operation (null keys are not allowed).");
                        if (e.getValue() == null)
                            throw new IgniteCheckedException("Failing putAll operation (null values are not allowed).");
                    }
                    // HashMap wrapping for correct serialization
                    map = new HashMap<>(map);
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new PutAllCommand(map));
                    break;
                }
            case CACHE_REMOVE:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new RemoveCommand(key));
                    break;
                }
            case CACHE_REMOVE_VALUE:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new RemoveValueCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_REPLACE_VALUE:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new ReplaceValueCommand(key, getValue(req0), req0.value2()));
                    break;
                }
            case CACHE_GET_AND_REMOVE:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new GetAndRemoveCommand(key));
                    break;
                }
            case CACHE_REMOVE_ALL:
                {
                    Map<Object, Object> map = req0.values();
                    // HashSet wrapping for correct serialization
                    Set<Object> keys = map == null ? null : new HashSet<>(map.keySet());
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new RemoveAllCommand(keys));
                    break;
                }
            case CACHE_CLEAR:
                {
                    Set<Object> cacheNames = getKeys(req0);
                    GridCompoundFuture compFut = new GridCompoundFuture();
                    for (Object cName : cacheNames) compFut.add(executeCommand(req.destinationId(), (String) cName, cacheFlags, key, new RemoveAllCommand(null)));
                    compFut.markInitialized();
                    fut = compFut.chain(new CX1<GridCompoundFuture<GridCacheRestResponse, ?>, GridRestResponse>() {

                        @Override
                        public GridRestResponse applyx(GridCompoundFuture<GridCacheRestResponse, ?> cf) throws IgniteCheckedException {
                            boolean success = true;
                            for (IgniteInternalFuture<GridCacheRestResponse> f : cf.futures()) if (!((Boolean) f.get().getResponse()))
                                success = false;
                            GridCacheRestResponse resp = new GridCacheRestResponse();
                            if (success)
                                resp.setResponse(true);
                            else
                                resp.setResponse(false);
                            return resp;
                        }
                    });
                    break;
                }
            case CACHE_REPLACE:
                {
                    final Object val = req0.value();
                    if (val == null)
                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new ReplaceCommand(key, ttl, val));
                    break;
                }
            case CACHE_CAS:
                {
                    final Object val1 = req0.value();
                    final Object val2 = req0.value2();
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new CasCommand(val2, val1, key));
                    break;
                }
            case CACHE_APPEND:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new AppendCommand(key, req0));
                    break;
                }
            case CACHE_PREPEND:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), cacheFlags, key, new PrependCommand(key, req0));
                    break;
                }
            case CACHE_METRICS:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), key, new MetricsCommand());
                    break;
                }
            case CACHE_SIZE:
                {
                    fut = executeCommand(req.destinationId(), req0.cacheName(), key, new SizeCommand());
                    break;
                }
            case CACHE_UPDATE_TLL:
                {
                    if (ttl == null)
                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("ttl"));
                    fut = executeCommand(req.destinationId(), req0.cacheName(), key, new UpdateTllCommand(key, ttl));
                    break;
                }
            default:
                throw new IllegalArgumentException("Invalid command for cache handler: " + req);
        }
        return fut;
    } catch (IgniteException | IgniteCheckedException e) {
        U.error(log, "Failed to execute cache command: " + req, e);
        return new GridFinishedFuture<>(e);
    } finally {
        if (log.isDebugEnabled())
            log.debug("Handled cache REST request: " + req);
    }
}
Also used : HashMap(java.util.HashMap) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridClientCacheFlag(org.apache.ignite.internal.client.GridClientCacheFlag) IgniteException(org.apache.ignite.IgniteException) CacheConfigurationOverride(org.apache.ignite.internal.processors.cache.CacheConfigurationOverride) HashSet(java.util.HashSet) GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) Map(java.util.Map) HashMap(java.util.HashMap) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) MutableEntry(javax.cache.processor.MutableEntry) CacheInvokeEntry(org.apache.ignite.internal.processors.cache.CacheInvokeEntry) CacheConfigurationOverride(org.apache.ignite.internal.processors.cache.CacheConfigurationOverride) CacheConfigurationOverride(org.apache.ignite.internal.processors.cache.CacheConfigurationOverride)

Example 2 with GridRestCommand

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

the class GridTcpMemcachedNioListener method command.

/**
 * Gets command and command attributes from operation code.
 *
 * @param opCode Operation code.
 * @return Command.
 */
@Nullable
private GridTuple3<GridRestCommand, Boolean, Boolean> command(int opCode) {
    GridRestCommand cmd;
    boolean quiet = false;
    boolean retKey = false;
    switch(opCode) {
        case 0x00:
            cmd = CACHE_GET;
            break;
        case 0x01:
            cmd = CACHE_PUT;
            break;
        case 0x02:
            cmd = CACHE_ADD;
            break;
        case 0x03:
            cmd = CACHE_REPLACE;
            break;
        case 0x04:
            cmd = CACHE_REMOVE;
            break;
        case 0x05:
            cmd = ATOMIC_INCREMENT;
            break;
        case 0x06:
            cmd = ATOMIC_DECREMENT;
            break;
        case 0x07:
            cmd = QUIT;
            break;
        case 0x08:
            cmd = CACHE_REMOVE_ALL;
            break;
        case 0x09:
            cmd = CACHE_GET;
            break;
        case 0x0A:
            cmd = NOOP;
            break;
        case 0x0B:
            cmd = VERSION;
            break;
        case 0x0C:
            cmd = CACHE_GET;
            retKey = true;
            break;
        case 0x0D:
            cmd = CACHE_GET;
            retKey = true;
            break;
        case 0x0E:
            cmd = CACHE_APPEND;
            break;
        case 0x0F:
            cmd = CACHE_PREPEND;
            break;
        case 0x10:
            cmd = CACHE_METRICS;
            break;
        case 0x11:
            cmd = CACHE_PUT;
            quiet = true;
            break;
        case 0x12:
            cmd = CACHE_ADD;
            quiet = true;
            break;
        case 0x13:
            cmd = CACHE_REPLACE;
            quiet = true;
            break;
        case 0x14:
            cmd = CACHE_REMOVE;
            quiet = true;
            break;
        case 0x15:
            cmd = ATOMIC_INCREMENT;
            quiet = true;
            break;
        case 0x16:
            cmd = ATOMIC_DECREMENT;
            quiet = true;
            break;
        case 0x17:
            cmd = QUIT;
            quiet = true;
            break;
        case 0x18:
            cmd = CACHE_REMOVE_ALL;
            quiet = true;
            break;
        case 0x19:
            cmd = CACHE_APPEND;
            quiet = true;
            break;
        case 0x1A:
            cmd = CACHE_PREPEND;
            quiet = true;
            break;
        default:
            return null;
    }
    return new GridTuple3<>(cmd, quiet, retKey);
}
Also used : GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) GridTuple3(org.apache.ignite.internal.util.lang.GridTuple3) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GridRestCommand

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

the class UserActionCommandHandler method handleAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
    assert req != null;
    if (log.isDebugEnabled())
        log.debug("Handling topology REST request: " + req);
    RestUserActionRequest req0 = (RestUserActionRequest) req;
    try {
        GridRestCommand cmd = req.command();
        IgniteSecurity security = ctx.security();
        switch(cmd) {
            case ADD_USER:
                security.createUser(req0.user(), req0.password().toCharArray());
                break;
            case REMOVE_USER:
                security.dropUser(req0.user());
                break;
            case UPDATE_USER:
                security.alterUser(req0.user(), req0.password().toCharArray());
                break;
        }
        if (log.isDebugEnabled())
            log.debug("Handled topology REST request [req=" + req + ']');
        return new GridFinishedFuture<>(new GridRestResponse(true));
    } catch (Throwable e) {
        log.error("Failed to handle REST request [req=" + req + ']', e);
        return new GridFinishedFuture<>(e);
    }
}
Also used : RestUserActionRequest(org.apache.ignite.internal.processors.rest.request.RestUserActionRequest) GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) IgniteSecurity(org.apache.ignite.internal.processors.security.IgniteSecurity) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 4 with GridRestCommand

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

the class GridQueryCommandHandlerTest method testSupportedCommands.

/**
 * @throws Exception If failed.
 */
@Test
public void testSupportedCommands() throws Exception {
    GridTestKernalContext ctx = newContext(grid().configuration());
    ctx.add(new GridTimeoutProcessor(ctx));
    QueryCommandHandler cmdHnd = new QueryCommandHandler(ctx);
    Collection<GridRestCommand> commands = cmdHnd.supportedCommands();
    assertEquals(5, commands.size());
    assertTrue(commands.contains(GridRestCommand.EXECUTE_SQL_QUERY));
    assertTrue(commands.contains(GridRestCommand.EXECUTE_SQL_FIELDS_QUERY));
    assertTrue(commands.contains(GridRestCommand.EXECUTE_SCAN_QUERY));
    assertTrue(commands.contains(GridRestCommand.FETCH_SQL_QUERY));
    assertTrue(commands.contains(GridRestCommand.CLOSE_SQL_QUERY));
}
Also used : GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) GridTimeoutProcessor(org.apache.ignite.internal.processors.timeout.GridTimeoutProcessor) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with GridRestCommand

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

the class UserModifyingApplication method run.

/**
 * {@inheritDoc}
 */
@Override
public void run(final JsonNode jsonNode) throws IgniteCheckedException {
    String restKey = jsonNode.get("rest_key").asText();
    String name = jsonNode.get("username").asText();
    String pwd = jsonNode.get("password").asText();
    markInitialized();
    GridRestCommand cmd = GridRestCommand.fromKey(restKey);
    switch(cmd) {
        case ADD_USER:
            client.query(new SqlFieldsQuery(String.format("CREATE USER \"%s\" WITH PASSWORD '%s';", name, pwd))).getAll();
            break;
        case UPDATE_USER:
            client.query(new SqlFieldsQuery(String.format("ALTER USER \"%s\" WITH PASSWORD '%s';", name, pwd))).getAll();
            break;
        case REMOVE_USER:
            client.query(new SqlFieldsQuery(String.format("DROP USER \"%s\";", name))).getAll();
            break;
        default:
            throw new IgniteCheckedException("Unknown operation: " + cmd + ".");
    }
    markFinished();
}
Also used : GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Aggregations

GridRestCommand (org.apache.ignite.internal.processors.rest.GridRestCommand)7 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)3 Map (java.util.Map)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridTimeoutProcessor (org.apache.ignite.internal.processors.timeout.GridTimeoutProcessor)2 GridTestKernalContext (org.apache.ignite.testframework.junits.GridTestKernalContext)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 MutableEntry (javax.cache.processor.MutableEntry)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1