Search in sources :

Example 21 with GridRestResponse

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

the class GridLogCommandHandlerTest method testHandleAsyncFromAndToNotSet.

/**
 * @throws Exception If failed.
 */
@Test
public void testHandleAsyncFromAndToNotSet() throws Exception {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteHome(igniteHome);
    GridTestKernalContext ctx = newContext(cfg);
    GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
    GridRestLogRequest req = new GridRestLogRequest();
    req.path(igniteHome + "/work/log/" + "test.log");
    IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
    assertNull(resp.result().getError());
    assertEquals(GridRestResponse.STATUS_SUCCESS, resp.result().getSuccessStatus());
    assertNotNull(resp.result().getResponse());
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridRestLogRequest(org.apache.ignite.internal.processors.rest.request.GridRestLogRequest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 22 with GridRestResponse

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

the class GridJettyRestHandler method processRequest.

/**
 * Process HTTP request.
 *
 * @param act Action.
 * @param req Http request.
 * @param res Http response.
 */
private void processRequest(String act, HttpServletRequest req, HttpServletResponse res) {
    res.setContentType("application/json");
    res.setCharacterEncoding("UTF-8");
    GridRestCommand cmd = command(req);
    if (cmd == null) {
        res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    if (!authChecker.apply(req.getHeader("X-Signature"))) {
        res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return;
    }
    GridRestResponse cmdRes;
    Map<String, String> params = parameters(req);
    try {
        GridRestRequest cmdReq = createRequest(cmd, params, req);
        if (log.isDebugEnabled())
            log.debug("Initialized command request: " + cmdReq);
        cmdRes = hnd.handle(cmdReq);
        if (cmdRes == null)
            throw new IllegalStateException("Received null result from handler: " + hnd);
        if (getAllAsArray && cmd == GridRestCommand.CACHE_GET_ALL) {
            List<Object> resKeyValue = new ArrayList<>();
            for (Map.Entry<Object, Object> me : ((Map<Object, Object>) cmdRes.getResponse()).entrySet()) resKeyValue.add(new IgniteBiTuple<>(me.getKey(), me.getValue()));
            cmdRes.setResponse(resKeyValue);
        }
        byte[] sesTok = cmdRes.sessionTokenBytes();
        if (sesTok != null)
            cmdRes.setSessionToken(U.byteArray2HexString(sesTok));
        res.setStatus(cmdRes.getSuccessStatus() == GridRestResponse.SERVICE_UNAVAILABLE ? HttpServletResponse.SC_SERVICE_UNAVAILABLE : HttpServletResponse.SC_OK);
    } catch (Throwable e) {
        res.setStatus(HttpServletResponse.SC_OK);
        U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e);
        if (e instanceof Error)
            throw (Error) e;
        cmdRes = new GridRestResponse(STATUS_FAILED, e.getMessage());
    }
    try (ServletOutputStream os = res.getOutputStream()) {
        try {
            // Try serialize.
            jsonMapper.writeValue(NULL_OUTPUT_STREAM, cmdRes);
            jsonMapper.writeValue(os, cmdRes);
        } catch (JsonProcessingException e) {
            U.error(log, "Failed to convert response to JSON: " + cmdRes, e);
            jsonMapper.writeValue(os, new GridRestResponse(STATUS_FAILED, e.getMessage()));
        }
        if (log.isDebugEnabled())
            log.debug("Processed HTTP request [action=" + act + ", jsonRes=" + cmdRes + ", req=" + req + ']');
    } catch (IOException e) {
        U.error(log, "Failed to send HTTP response: " + cmdRes, e);
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) BinaryObject(org.apache.ignite.binary.BinaryObject) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 23 with GridRestResponse

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

the class GridCacheMetadataCommandTest method cacheMetadataWithMultupleThreads.

/**
 * <p>Test for requesting the cache's metadata from multiple threads
 * in order to detect starvation or deadlock in the mngmt pool caused by calling other internal tasks within the
 * metadata task.</p>
 *
 * <p>Steps to reproduce:</p>
 * <ul>
 *  <li>Start a few server nodes with the small size of the mngmt pool.</li>
 *  <li>Call the metadata task by requesting REST API from multiple threads.</li>
 *  <li>Check all requests have finished successfully.</li>
 * </ul>
 *
 * @throws Exception If failed.
 */
@Test
public void cacheMetadataWithMultupleThreads() throws Exception {
    int servers = 2;
    int iterations = 1000;
    int threads = 10;
    startGrids(servers);
    ExecutorService ex = Executors.newFixedThreadPool(threads);
    try {
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < threads; i++) {
            futures.add(ex.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    GridRestCommandHandler hnd = new GridCacheCommandHandler((grid(0)).context());
                    GridRestCacheRequest req = new GridRestCacheRequest();
                    req.command(GridRestCommand.CACHE_METADATA);
                    req.cacheName(DEFAULT_CACHE_NAME);
                    for (int i = 0; i < iterations; i++) {
                        GridRestResponse resp = hnd.handleAsync(req).get();
                        assertEquals(GridRestResponse.STATUS_SUCCESS, resp.getSuccessStatus());
                    }
                    return null;
                }
            }));
        }
        for (Future<?> f : futures) f.get(1, TimeUnit.MINUTES);
    } finally {
        ex.shutdownNow();
    }
}
Also used : GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 24 with GridRestResponse

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

the class GridQueryCommandHandlerTest method testNullPageSize.

/**
 * @throws Exception If failed.
 */
@Test
public void testNullPageSize() throws Exception {
    grid().getOrCreateCache(getName());
    QueryCommandHandler cmdHnd = new QueryCommandHandler(grid().context());
    Integer arg1 = 1000;
    Object[] arr = new Object[] { arg1, arg1 };
    RestQueryRequest req = new RestQueryRequest();
    req.command(GridRestCommand.EXECUTE_SQL_QUERY);
    req.queryType(RestQueryRequest.QueryType.SCAN);
    req.typeName(Integer.class.getName());
    req.pageSize(null);
    req.sqlQuery("salary+>+%3F+and+salary+<%3D+%3F");
    req.arguments(arr);
    req.cacheName(getName());
    try {
        IgniteInternalFuture<GridRestResponse> resp = cmdHnd.handleAsync(req);
        resp.get();
        fail("Expected exception not thrown.");
    } catch (IgniteCheckedException e) {
        info("Got expected exception: " + e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RestQueryRequest(org.apache.ignite.internal.processors.rest.request.RestQueryRequest) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)24 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)11 Test (org.junit.Test)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 GridRestLogRequest (org.apache.ignite.internal.processors.rest.request.GridRestLogRequest)8 GridTestKernalContext (org.apache.ignite.testframework.junits.GridTestKernalContext)7 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)4 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 GridRestCommand (org.apache.ignite.internal.processors.rest.GridRestCommand)3 RestQueryRequest (org.apache.ignite.internal.processors.rest.request.RestQueryRequest)3 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 UUID (java.util.UUID)2 IgniteException (org.apache.ignite.IgniteException)2