@Override public <T> T attr(String name, T defaultValue) { return withDefault(attrs().get(name), defaultValue); }
private void parseRequestBody() { if (U.isEmpty(body())) return; String contentTypeHeader = header("Content-Type", "application/json"); if (contentTypeHeader.startsWith("application/json")) { parseRequestBodyUsing(custom().jsonRequestBodyParser()); } else if (contentTypeHeader.startsWith("application/xml")) { parseRequestBodyUsing(custom().xmlRequestBodyParser()); } else { throw U.rte("Couldn't parse the request body - unsupported content type: " + contentTypeHeader); } }
private void serveCached(ReqImpl req, CachedResp resp) { Channel channel = req.channel(); req.cached(true); HttpIO.INSTANCE.respond(HttpUtils.req(req), channel, -1, -1, resp.statusCode, req.isKeepAlive(), resp.contentType, new RespBodyBuffer(resp.body.duplicate()), resp.headers, null); channel.send().closeIf(!req.isKeepAlive()); }
void done(Req req) { ReqImpl reqq = (ReqImpl) req; reqq.doneProcessing(); Channel channel = reqq.channel(); channel.send(); channel.closeIf(!reqq.isKeepAlive()); }
@Override public void resume(AsyncLogic asyncLogic) { req.channel().resume(req.connectionId(), req.handle(), asyncLogic); }
void closeHeaders(MaybeReq req, Buf out) { // finishing the headers out.append(CR_LF); ReqImpl reqq = (ReqImpl) req.getReqOrNull(); if (reqq != null) { U.must(reqq.channel().output() == out); reqq.onHeadersCompleted(); } }
@Override public String sessionId() { String sessionId = cookie(SESSION_COOKIE, null); if (U.isEmpty(sessionId)) { sessionId = UUID.randomUUID().toString(); synchronized (cookies) { if (cookie(SESSION_COOKIE, null) == null) { cookies.put(SESSION_COOKIE, sessionId); response().cookie(SESSION_COOKIE, sessionId, "HttpOnly"); } } } return sessionId; }
private boolean serveFromCache(ReqImpl req) { // if the HTTP request is not cacheable, the cache key will be null HTTPCacheKey cacheKey = req.cacheKey(); Route route = req.route(); if (route != null) { Cache<HTTPCacheKey, CachedResp> cache = route.cache(); if (cache != null) { if (cacheKey != null) { CachedResp resp = cache.getIfExists(cacheKey); if (resp != null) { serveCached(req, resp); return true; } } else { cache.bypass(); // notify it's not cacheable } } } return false; }
handle = req.handle(); } else { handle = channel.handle(); connId = req.connectionId(); } else { connId = channel.connId();
void terminatingChunk() { resume(() -> { Buf out = req.channel().output(); out.append("0\r\n\r\n"); return true; }); }
@SuppressWarnings("unchecked") @Override public <T> T attr(String name) { return (T) U.notNull(attrs().get(name), "ATTRS[%s]", name); }
private void parseRequestBodyUsing(HttpRequestBodyParser parser) { if (U.notEmpty(body())) { Map<String, ?> bodyData = null; try { bodyData = parser.parseRequestBody(this, body); } catch (Exception e) { Log.error("Couldn't parse the request body! Please make sure the correct content type is specified in the request header!", e); } if (bodyData != null) { posted.putAll(bodyData); } } }
@Override public boolean hasSession() { return cookie(SESSION_COOKIE, null) != null; }
private void serveCached(ReqImpl req, CachedResp resp) { Channel channel = req.channel(); req.cached(true); HttpIO.INSTANCE.respond(HttpUtils.req(req), channel, -1, -1, resp.statusCode, req.isKeepAlive(), resp.contentType, new RespBodyBuffer(resp.body.duplicate()), resp.headers, null); channel.send().closeIf(!req.isKeepAlive()); }
void done(Req req) { ReqImpl reqq = (ReqImpl) req; reqq.doneProcessing(); Channel channel = reqq.channel(); channel.send(); channel.closeIf(!reqq.isKeepAlive()); }