@Override
    public void handle(final ClassicHttpRequest request,
                       final ClassicHttpResponse response,
                       final HttpContext context)
        throws HttpException, IOException {

        final String method = request.getMethod().toUpperCase(Locale.ROOT);
        if (!"GET".equals(method) &&
                !"HEAD".equals(method) &&
                !"POST".equals(method) &&
                !"PUT".equals(method)) {
            throw new MethodNotSupportedException(method + " not supported by " + getClass().getName());
        }

        HttpEntity entity = request.getEntity();

        // For some reason, just putting the incoming entity into
        // the response will not work. We have to buffer the message.
        final byte[] data;
        final ContentType contentType;
        if (entity == null) {
            data = new byte [0];
            contentType = null;
        } else {
            data = EntityUtils.toByteArray(entity);
            final String contentTypeStr = entity.getContentType();
            contentType = contentTypeStr == null ? null : ContentType.parse(contentTypeStr);
        }

        entity = new ByteArrayEntity(data, contentType);

        response.setCode(HttpStatus.SC_OK);
        response.setEntity(entity);
    }
Handles a request by echoing the incoming request entity.
If there is no request entity, an empty document is returned.

@param request   the request
@param response  the response
@param context   the context

@throws HttpException    in case of a problem
@throws IOException      in case of an IO problem
httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/EchoHandler.java