Java 类org.apache.http.nio.protocol.BasicAsyncRequestHandler 实例源码

项目:APICloud-Studio    文件:LocalWebServer.java   
private void startServer(final InetAddress host, final int port)
{
    updateState(State.STARTING);
    thread = new Thread()
    {
        @Override
        public void run()
        {
            runServer(new InetSocketAddress(host, port), new BasicAsyncRequestHandler(
                    new LocalWebServerHttpRequestHandler(LocalWebServer.this)));
        }
    };
    thread.setDaemon(true);
    thread.start();
    Thread.yield();
    long startTime = System.currentTimeMillis();
    while (thread.isAlive() && (System.currentTimeMillis() - startTime) < STARTUP_TIMEOUT)
    {
        if (reactor != null && reactor.getStatus() == IOReactorStatus.ACTIVE)
        {
            updateState(State.STARTED);
            break;
        }
        try
        {
            Thread.sleep(10);
        }
        catch (InterruptedException e)
        {
            updateState(State.UNKNOWN);
            break;
        }
    }
}
项目:LibOppo    文件:OppoMessenger.java   
/** Registers a handler to be called when a message arrives at the given endpoint. */
public void registerHandler(final String endPoint, final Function<CharSource, CharSource> handler) {
    mapper.register(endPoint, new BasicAsyncRequestHandler(new HttpRequestHandler() {
        @Override
        public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) throws IOException {
            if (request instanceof HttpEntityEnclosingRequest) {
                final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();

                if (entity==null) {
                    logger().level(Warn).message("Received empty request from device").log();
                    response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
                    return;
                }

                final String requestJson = URLDecoder.decode(EntityUtils.toString(entity, Charsets.UTF_8), Charsets.UTF_8.name());

                final CharSource responseJson = handler.apply(CharSource.wrap(requestJson));

                if (responseJson==null || responseJson.isEmpty()) {
                    response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
                    return;
                }

                response.setStatusCode(HttpStatus.SC_OK);

                EntityUtils.updateEntity(response,
                        new StringEntity(URLEncoder.encode(responseJson.read(), Charsets.UTF_8.name())));
            }
        }
    }));
}