如何在一个底层ReverseProxyClient中读取http主体

时间:2018-02-27 17:25:41

标签: java undertow

我需要在一个底层反向代理中读取内部服务器响应,但是我在私有String getBody(HttpServerExchange交换)中遇到了死锁

我需要阅读内部回复并添加标题,任何人都可以帮助我吗?

java.lang.IllegalStateException:UT000126:尝试从IO线程执行阻塞IO。这是禁止的,因为它可能导致死锁

public class SimpleReverseProxyClient implements ProxyClient {
    private static final ProxyTarget TARGET = new ProxyTarget() {};

    private final UndertowClient client;
    private final URI internalUri;

    public SimpleReverseProxyClient(URI internalUri) {
        this.client = UndertowClient.getInstance();
        this.internalUri = internalUri;
    }

    @Override
    public ProxyTarget findTarget(HttpServerExchange exchange) {
        return TARGET;
    }

    @Override
    public void getConnection(ProxyTarget target, HttpServerExchange exchange, ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit) {
        client.connect(
                new ConnectNotifier(callback, exchange),
                internalUri,
                exchange.getIoThread(),
                exchange.getConnection().getByteBufferPool(),
                OptionMap.EMPTY);
    }

    private final class ConnectNotifier implements ClientCallback<ClientConnection> {
        private final ProxyCallback<ProxyConnection> callback;
        private final HttpServerExchange exchange;

        private ConnectNotifier(ProxyCallback<ProxyConnection> callback, HttpServerExchange exchange) {
            this.callback = callback;
            this.exchange = exchange;
        }

        @Override
        public void completed(final ClientConnection connection) {
            final ServerConnection serverConnection = exchange.getConnection();
            serverConnection.addCloseListener(serverConnection1 -> IoUtils.safeClose(connection));
            String body = getBody(exchange);
            System.out.println(body);
            exchange.getResponseHeaders().add(new HttpString("token"), "test");
            callback.completed(exchange, new ProxyConnection(connection, "/"));
        }

        private String getBody(HttpServerExchange exchange) {
            BufferedReader reader = null;
            StringBuilder builder = new StringBuilder( );

            try {
                exchange.startBlocking( );
                reader = new BufferedReader( new InputStreamReader( exchange.getInputStream( ) ) );

                String line;
                while( ( line = reader.readLine( ) ) != null ) {
                    builder.append( line );
                }
            } catch( IOException e ) {
                e.printStackTrace( );
            } finally {
                if( reader != null ) {
                    try {
                        reader.close( );
                    } catch( IOException e ) {
                        e.printStackTrace( );
                    }
                }
            }
            String body = builder.toString( );
            return body;
        }

        @Override
        public void failed(IOException e) {
            callback.failed(exchange);
        }
    }
}


 Undertow server = Undertow.builder()
            .addHttpListener(9290, "localhost")
            .setHandler(ProxyHandler.builder()
                    .setProxyClient(new SimpleReverseProxyClient(
                            uri))
                    .build()
            ).build();
    server.start();

0 个答案:

没有答案