private void handleResponse(ClientHttpResponse response) throws IOException { if (response.getStatusCode() == HttpStatus.GONE) { close(); return; } if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE) { logger.warn("Remote application responded with service unavailable. Did " + "you forget to start it with remote debugging enabled?"); close(); return; } if (response.getStatusCode() == HttpStatus.OK) { HttpTunnelPayload payload = HttpTunnelPayload.get(response); if (payload != null) { this.forwarder.forward(payload); } } if (response.getStatusCode() != HttpStatus.TOO_MANY_REQUESTS) { openNewConnection(null); } }
@Override public int write(ByteBuffer src) throws IOException { int size = src.remaining(); if (size > 0) { openNewConnection( new HttpTunnelPayload(this.requestSeq.incrementAndGet(), src)); } return size; }
private void sendAndReceive(HttpTunnelPayload payload) throws IOException { ClientHttpRequest request = createRequest(payload != null); if (payload != null) { payload.logIncoming(); payload.assignTo(request); } handleResponse(request.execute()); }
private void readAndForwardTargetServerData() throws IOException { while (this.targetServer.isOpen()) { closeStaleHttpConnections(); ByteBuffer data = HttpTunnelPayload.getPayloadData(this.targetServer); synchronized (this.httpConnections) { if (data != null) { HttpTunnelPayload payload = new HttpTunnelPayload( this.responseSeq.incrementAndGet(), data); payload.logIncoming(); HttpConnection connection = getOrWaitForHttpConnection(); connection.respond(payload); } } } }
private void forwardToTargetServer(HttpConnection httpConnection) throws IOException { if (httpConnection.isDisconnectRequest()) { this.targetServer.close(); interrupt(); } ServerHttpRequest request = httpConnection.getRequest(); HttpTunnelPayload payload = HttpTunnelPayload.get(request); if (payload != null) { this.payloadForwarder.forward(payload); } }
/** * Send a payload response. * @param payload the payload to send * @throws IOException in case of I/O errors */ public void respond(HttpTunnelPayload payload) throws IOException { Assert.notNull(payload, "Payload must not be null"); this.response.setStatusCode(HttpStatus.OK); payload.assignTo(this.response); complete(); }
@Test public void httpConnectionRespondWithPayload() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); connection.waitForResponse(); connection.respond(new HttpTunnelPayload(1, ByteBuffer.wrap("hello".getBytes()))); assertThat(this.servletResponse.getStatus()).isEqualTo(200); assertThat(this.servletResponse.getContentAsString()).isEqualTo("hello"); assertThat(this.servletResponse.getHeader(SEQ_HEADER)).isEqualTo("1"); }
private void handleResponse(ClientHttpResponse response) throws IOException { if (response.getStatusCode() == HttpStatus.GONE) { close(); return; } if (response.getStatusCode() == HttpStatus.OK) { HttpTunnelPayload payload = HttpTunnelPayload.get(response); if (payload != null) { this.forwarder.forward(payload); } } if (response.getStatusCode() != HttpStatus.TOO_MANY_REQUESTS) { openNewConnection(null); } }
@Test public void httpConnectionRespondWithPayload() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); connection.waitForResponse(); connection.respond(new HttpTunnelPayload(1, ByteBuffer.wrap("hello".getBytes()))); assertThat(this.servletResponse.getStatus(), equalTo(200)); assertThat(this.servletResponse.getContentAsString(), equalTo("hello")); assertThat(this.servletResponse.getHeader(SEQ_HEADER), equalTo("1")); }