@Override public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { // body arrived, flush headers if (!responseSet) { response = responseBuilder.build(); responseSet = true; headersArrived.countDown(); } output.write(bodyPart.getBodyPartBytes()); return State.CONTINUE; }
@Override public ByteBuffer getResponseBodyAsByteBuffer() { int length = 0; for (HttpResponseBodyPart part : bodyParts) length += part.length(); ByteBuffer target = ByteBuffer.wrap(new byte[length]); for (HttpResponseBodyPart part : bodyParts) target.put(part.getBodyPartBytes()); target.flip(); return target; }
@Override public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { if (accumulateBody) { responseBuilder.accumulate(bodyPart); } State state = State.CONTINUE; try { resumableListener.onBytesReceived(bodyPart.getBodyByteBuffer()); } catch (IOException ex) { return AsyncHandler.State.ABORT; } if (decoratedAsyncHandler != null) { state = decoratedAsyncHandler.onBodyPartReceived(bodyPart); } byteTransferred.addAndGet(bodyPart.getBodyPartBytes().length); resumableProcessor.put(url, byteTransferred.get()); return state; }
@Override public State onBodyPartReceived(final HttpResponseBodyPart bodyPart) throws Exception { if (nonNull(bodyPart) && nonNull(bodyPart.getBodyByteBuffer())) { bytesSent += bodyPart.getBodyPartBytes().length; isLast = bodyPart.isLast(); context.getResponse().getOutputStream().write(bodyPart.getBodyPartBytes()); } return State.CONTINUE; }
public void onBinaryFragment(HttpResponseBodyPart part) { for (WebSocketListener listener : listeners) { if (listener instanceof WebSocketByteFragmentListener) WebSocketByteFragmentListener.class.cast(listener).onFragment(part); } if (interestedInByteMessages) { ChannelBuffer fragment = NettyResponseBodyPart.class.cast(part).getChannelBuffer(); if (part.isLast()) { if (bufferSize == 0) { notifyByteListeners(fragment); } else { bufferFragment(fragment); notifyByteListeners(wrappedBuffer(fragments().toArray(new ChannelBuffer[fragments().size()]))); } reset(); } else bufferFragment(fragment); } }
/** * @param bodyPart a body part (possibly empty, but will be filtered out) */ public void accumulate(HttpResponseBodyPart bodyPart) { if (bodyPart.length() > 0) bodyParts.add(bodyPart); }
private void fireReceived(HttpResponseBodyPart content) { int remaining = content.getBodyByteBuffer().remaining(); amount += remaining; if (listener != null) { listener.onBytesReceived(uri, amount, remaining, total); } }
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { // body arrived, flush headers if (!responseSet) { response = responseBuilder.build(); responseSet = true; headersArrived.countDown(); } bodyPart.writeTo(output); return State.CONTINUE; }
public void onTextFragment(HttpResponseBodyPart part) { for (WebSocketListener listener : listeners) { if (listener instanceof WebSocketTextFragmentListener) WebSocketTextFragmentListener.class.cast(listener).onFragment(part); } if (interestedInTextMessages) { ChannelBuffer fragment = NettyResponseBodyPart.class.cast(part).getChannelBuffer(); if (part.isLast()) { if (bufferSize == 0) { notifyTextListeners(fragment); } else { bufferFragment(fragment); notifyTextListeners(wrappedBuffer(fragments().toArray(new ChannelBuffer[fragments().size()]))); } reset(); } else bufferFragment(fragment); } }
@Override public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { length += content.length(); if (length > responseSizeLimit) { log.warn("Response from webhook is too big for {}. Skipping. Size : {}", email, length); return State.ABORT; } return super.onBodyPartReceived(content); }
@Override public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { byteReceived.addAndGet(content.getBodyByteBuffer().capacity()); return super.onBodyPartReceived(content); }
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { stream.write(bodyPart.getBodyPartBytes()); if (bodyPart.getBodyPartBytes().length == 0) { return State.ABORT; } return State.CONTINUE; }
public State onBodyPartReceived(HttpResponseBodyPart e) throws Exception { if (e.length() != 0) { String s = new String(e.getBodyPartBytes()); logger.info("got part: {}", s); queue.put(s); } return State.CONTINUE; }
@Test public void testOnBodyPartReceived() throws Exception { ResumableAsyncHandler handler = new ResumableAsyncHandler(); HttpResponseBodyPart bodyPart = mock(HttpResponseBodyPart.class); when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); ByteBuffer buffer = ByteBuffer.allocate(0); when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); State state = handler.onBodyPartReceived(bodyPart); assertEquals(state, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onBodyPartReceived"); }
public void onTextFragment(HttpResponseBodyPart part) { for (WebSocketListener listener : listeners) { if (listener instanceof WebSocketTextFragmentListener) WebSocketTextFragmentListener.class.cast(listener).onFragment(part); } if (interestedInTextMessages) { ChannelBuffer fragment = NettyResponseBodyPart.class.cast(part).getChannelBuffer(); if (part.isLast()) { if (bufferSize == 0) { notifyTextListeners(fragment); } else { bufferFragment(fragment); notifyTextListeners(wrappedBuffer(fragments().toArray(new ChannelBuffer[fragments().size()]))); } reset(); } else bufferFragment(fragment); } }
/** * @param bodyPart * a body part (possibly empty, but will be filtered out) * @return this */ public ResponseBuilder accumulate(HttpResponseBodyPart bodyPart) { if (bodyPart.length() > 0) bodyParts.add(bodyPart); return this; }
/** * {@inheritDoc} */ public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { fireReceived(content); if (omitBody) { return State.CONTINUE; } if (!accumulateBody && bodyConsumer != null) { bodyConsumer.consume(content.getBodyByteBuffer()); } else { return super.onBodyPartReceived(content); } return State.CONTINUE; }
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { stream.write(bodyPart.getBodyPartBytes()); return State.CONTINUE; }
@Override public ByteBuffer getResponseBodyAsByteBuffer() throws IOException { int length = 0; for (HttpResponseBodyPart part : bodyParts) length += part.length(); ByteBuffer target = ByteBuffer.wrap(new byte[length]); for (HttpResponseBodyPart part : bodyParts) target.put(part.getBodyPartBytes()); return target; }
@Test public void testOnBodyPartReceivedWithDecoratedAsyncHandler() throws Exception { HttpResponseBodyPart bodyPart = mock(HttpResponseBodyPart.class); when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); ByteBuffer buffer = ByteBuffer.allocate(0); when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class); when(decoratedAsyncHandler.onBodyPartReceived(bodyPart)).thenReturn(State.CONTINUE); // following is needed to set the url variable HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); when(mockResponseStatus.getStatusCode()).thenReturn(200); Uri uri = Uri.create("http://non.null"); when(mockResponseStatus.getUri()).thenReturn(uri); ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); handler.onStatusReceived(mockResponseStatus); State state = handler.onBodyPartReceived(bodyPart); assertEquals(state, State.CONTINUE, "State should be equal to the state returned from decoratedAsyncHandler"); }