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());
}
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);
}
}
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();
}
}
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);
}
}
Aggregations