Search in sources :

Example 6 with GridRestRequest

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

GridRestRequest (org.apache.ignite.internal.processors.rest.request.GridRestRequest)6 Map (java.util.Map)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)3 GridRestChangeStateRequest (org.apache.ignite.internal.processors.rest.request.GridRestChangeStateRequest)3 GridRestTaskRequest (org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest)3 GridRestTopologyRequest (org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest)3 Nullable (org.jetbrains.annotations.Nullable)3 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 CacheConfigurationOverride (org.apache.ignite.internal.processors.cache.CacheConfigurationOverride)2 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)2 EnumMap (java.util.EnumMap)1