Search in sources :

Example 6 with GridRestCommand

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

the class GridQueryCommandHandlerTest method testUnsupportedCommands.

/**
 * @throws Exception If failed.
 */
@Test
public void testUnsupportedCommands() throws Exception {
    GridTestKernalContext ctx = newContext(grid().configuration());
    ctx.add(new GridTimeoutProcessor(ctx));
    QueryCommandHandler cmdHnd = new QueryCommandHandler(ctx);
    Collection<GridRestCommand> commands = cmdHnd.supportedCommands();
    assertFalse(commands.contains(GridRestCommand.LOG));
}
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 7 with GridRestCommand

use of org.apache.ignite.internal.processors.rest.GridRestCommand 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)

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