Java 类org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper 实例源码

项目:ROKOS-OK-Bitcoin-Fullnode    文件:PeerServlet.java   
/**
 * Process HTTP POST request
 *
 * @param   req                 HTTP request
 * @param   resp                HTTP response
 * @throws  ServletException    Servlet processing error
 * @throws  IOException         I/O error
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    JSONStreamAware jsonResponse;
    //
    // Process the peer request
    //
    PeerImpl peer = Peers.findOrCreatePeer(req.getRemoteAddr());
    if (peer == null) {
        jsonResponse = UNKNOWN_PEER;
    } else {
        jsonResponse = process(peer, req.getReader());
    }
    //
    // Return the response
    //
    resp.setContentType("text/plain; charset=UTF-8");
    try (CountingOutputWriter writer = new CountingOutputWriter(resp.getWriter())) {
        JSON.writeJSONString(jsonResponse, writer);
        long byteCount;
        if (isGzipEnabled) {
            byteCount = ((Response) ((CompressedResponseWrapper) resp).getResponse()).getContentCount();
        } else {
            byteCount = writer.getCount();
        }
        if (peer != null) {
            peer.updateUploadedVolume(byteCount);
        }
    } catch (RuntimeException | IOException e) {
        if (peer != null) {
            if ((Peers.communicationLoggingMask & Peers.LOGGING_MASK_EXCEPTIONS) != 0) {
                if (e instanceof RuntimeException) {
                    Logger.logDebugMessage("Error sending response to peer " + peer.getHost(), e);
                } else {
                    Logger.logDebugMessage(String.format("Error sending response to peer %s: %s",
                        peer.getHost(), e.getMessage()!=null ? e.getMessage() : e.toString()));
                }
            }
            peer.blacklist(e);
        }
        throw e;
    }
}