@Test public void testReceiveResponseCancelsRequestForExcessiveResponseBody() { NetworkConfig config = NetworkConfig.createStandardWithoutFile() .setInt(Keys.MAX_MESSAGE_SIZE, 128) .setInt(Keys.MAX_RESOURCE_BODY_SIZE, 200); MessageObserver requestObserver = mock(MessageObserver.class); BlockwiseLayer blockwiseLayer = new BlockwiseLayer(config); Request req = Request.newGet(); req.setURI("coap: req.addMessageObserver(requestObserver); Response response = receiveResponseFor(req); response.getOptions().setSize2(256).setBlock2(BlockOption.size2Szx(64), true, 0); Exchange exchange = new Exchange(req, Origin.LOCAL, MatcherTestUtils.TEST_EXCHANGE_EXECUTOR); exchange.setRequest(req); blockwiseLayer.receiveResponse(exchange, response); verify(requestObserver).onCancel(); }
@Override public void receiveResponse(final Exchange exchange, final Response response) { if (isTransparentBlockwiseHandlingEnabled() && !exchange.getRequest().isMulticast()) { if (response.isError()) { switch(response.getCode()) { case REQUEST_ENTITY_INCOMPLETE: case REQUEST_ENTITY_TOO_LARGE: if (handleEntityTooLarge(exchange, response)) { return; } KeyUri key = getKey(exchange, exchange.getCurrentRequest()); Block1BlockwiseStatus status = getBlock1Status(key); if (status != null) { clearBlock1Status(key, status); } default: } if (exchange.getRequest() != exchange.getCurrentRequest()) { Response resp = new Response(response.getCode()); resp.setToken(exchange.getRequest().getToken()); if (exchange.getRequest().getType() == Type.CON) { resp.setType(Type.ACK); resp.setMID(exchange.getRequest().getMID()); } else { resp.setType(Type.NON); } resp.setSourceContext(response.getSourceContext()); resp.setPayload(response.getPayload()); resp.setOptions(response.getOptions()); resp.setRTT(exchange.calculateRTT()); exchange.setResponse(resp); upper().receiveResponse(exchange, resp); } else { upper().receiveResponse(exchange, response); } return; } if (response.getMaxResourceBodySize() == 0) { response.setMaxResourceBodySize(exchange.getRequest().getMaxResourceBodySize()); } KeyUri key = getKey(exchange, response); Block2BlockwiseStatus status = getBlock2Status(key); if (discardBlock2(key, status, exchange, response)) { return; } if (!response.hasBlockOption()) { exchange.setResponse(response); upper().receiveResponse(exchange, response); } else { if (response.getOptions().hasBlock1()) { handleBlock1Response(exchange, response); } if (response.getOptions().hasBlock2()) { handleBlock2Response(exchange, response); } } } else { exchange.setResponse(response); upper().receiveResponse(exchange, response); } }
BlockwiseLayer extends AbstractLayer { @Override public void receiveResponse(final Exchange exchange, final Response response) { if (isTransparentBlockwiseHandlingEnabled() && !exchange.getRequest().isMulticast()) { if (response.isError()) { switch(response.getCode()) { case REQUEST_ENTITY_INCOMPLETE: case REQUEST_ENTITY_TOO_LARGE: if (handleEntityTooLarge(exchange, response)) { return; } KeyUri key = getKey(exchange, exchange.getCurrentRequest()); Block1BlockwiseStatus status = getBlock1Status(key); if (status != null) { clearBlock1Status(key, status); } default: } if (exchange.getRequest() != exchange.getCurrentRequest()) { Response resp = new Response(response.getCode()); resp.setToken(exchange.getRequest().getToken()); if (exchange.getRequest().getType() == Type.CON) { resp.setType(Type.ACK); resp.setMID(exchange.getRequest().getMID()); } else { resp.setType(Type.NON); } resp.setSourceContext(response.getSourceContext()); resp.setPayload(response.getPayload()); resp.setOptions(response.getOptions()); resp.setRTT(exchange.calculateRTT()); exchange.setResponse(resp); upper().receiveResponse(exchange, resp); } else { upper().receiveResponse(exchange, response); } return; } if (response.getMaxResourceBodySize() == 0) { response.setMaxResourceBodySize(exchange.getRequest().getMaxResourceBodySize()); } KeyUri key = getKey(exchange, response); Block2BlockwiseStatus status = getBlock2Status(key); if (discardBlock2(key, status, exchange, response)) { return; } if (!response.hasBlockOption()) { exchange.setResponse(response); upper().receiveResponse(exchange, response); } else { if (response.getOptions().hasBlock1()) { handleBlock1Response(exchange, response); } if (response.getOptions().hasBlock2()) { handleBlock2Response(exchange, response); } } } else { exchange.setResponse(response); upper().receiveResponse(exchange, response); } } }
BlockwiseLayer extends AbstractLayer { @Override public void receiveResponse(final Exchange exchange, final Response response) { if (isTransparentBlockwiseHandlingEnabled() && !exchange.getRequest().isMulticast()) { if (response.isError()) { switch(response.getCode()) { case REQUEST_ENTITY_INCOMPLETE: case REQUEST_ENTITY_TOO_LARGE: if (handleEntityTooLarge(exchange, response)) { return; } KeyUri key = getKey(exchange, exchange.getCurrentRequest()); Block1BlockwiseStatus status = getBlock1Status(key); if (status != null) { clearBlock1Status(key, status); } default: } if (exchange.getRequest() != exchange.getCurrentRequest()) { Response resp = new Response(response.getCode()); resp.setToken(exchange.getRequest().getToken()); if (exchange.getRequest().getType() == Type.CON) { resp.setType(Type.ACK); resp.setMID(exchange.getRequest().getMID()); } else { resp.setType(Type.NON); } resp.setSourceContext(response.getSourceContext()); resp.setPayload(response.getPayload()); resp.setOptions(response.getOptions()); resp.setRTT(exchange.calculateRTT()); exchange.setResponse(resp); upper().receiveResponse(exchange, resp); } else { upper().receiveResponse(exchange, response); } return; } if (response.getMaxResourceBodySize() == 0) { response.setMaxResourceBodySize(exchange.getRequest().getMaxResourceBodySize()); } KeyUri key = getKey(exchange, response); Block2BlockwiseStatus status = getBlock2Status(key); if (discardBlock2(key, status, exchange, response)) { return; } if (!response.hasBlockOption()) { exchange.setResponse(response); upper().receiveResponse(exchange, response); } else { if (response.getOptions().hasBlock1()) { handleBlock1Response(exchange, response); } if (response.getOptions().hasBlock2()) { handleBlock2Response(exchange, response); } } } else { exchange.setResponse(response); upper().receiveResponse(exchange, response); } } BlockwiseLayer(final NetworkConfig config); }
BlockwiseLayer extends AbstractLayer { @Override public void receiveResponse(final Exchange exchange, final Response response) { if (isTransparentBlockwiseHandlingEnabled() && !exchange.getRequest().isMulticast()) { if (response.isError()) { switch(response.getCode()) { case REQUEST_ENTITY_INCOMPLETE: case REQUEST_ENTITY_TOO_LARGE: if (handleEntityTooLarge(exchange, response)) { return; } KeyUri key = getKey(exchange, exchange.getCurrentRequest()); Block1BlockwiseStatus status = getBlock1Status(key); if (status != null) { clearBlock1Status(key, status); } default: } if (exchange.getRequest() != exchange.getCurrentRequest()) { Response resp = new Response(response.getCode()); resp.setToken(exchange.getRequest().getToken()); if (exchange.getRequest().getType() == Type.CON) { resp.setType(Type.ACK); resp.setMID(exchange.getRequest().getMID()); } else { resp.setType(Type.NON); } resp.setSourceContext(response.getSourceContext()); resp.setPayload(response.getPayload()); resp.setOptions(response.getOptions()); resp.setRTT(exchange.calculateRTT()); exchange.setResponse(resp); upper().receiveResponse(exchange, resp); } else { upper().receiveResponse(exchange, response); } return; } if (response.getMaxResourceBodySize() == 0) { response.setMaxResourceBodySize(exchange.getRequest().getMaxResourceBodySize()); } KeyUri key = getKey(exchange, response); Block2BlockwiseStatus status = getBlock2Status(key); if (discardBlock2(key, status, exchange, response)) { return; } if (!response.hasBlockOption()) { exchange.setResponse(response); upper().receiveResponse(exchange, response); } else { if (response.getOptions().hasBlock1()) { handleBlock1Response(exchange, response); } if (response.getOptions().hasBlock2()) { handleBlock2Response(exchange, response); } } } else { exchange.setResponse(response); upper().receiveResponse(exchange, response); } } BlockwiseLayer(final NetworkConfig config); @Override void start(); @Override void destroy(); @Override void sendRequest(final Exchange exchange, final Request request); @Override void receiveRequest(final Exchange exchange, final Request request); @Override void sendResponse(final Exchange exchange, final Response response); @Override void receiveResponse(final Exchange exchange, final Response response); boolean isEmpty(); }
BlockwiseLayer extends AbstractLayer { @Override public void receiveResponse(final Exchange exchange, final Response response) { if (isTransparentBlockwiseHandlingEnabled() && !exchange.getRequest().isMulticast()) { if (response.isError()) { switch(response.getCode()) { case REQUEST_ENTITY_INCOMPLETE: case REQUEST_ENTITY_TOO_LARGE: if (handleEntityTooLarge(exchange, response)) { return; } KeyUri key = getKey(exchange, exchange.getCurrentRequest()); Block1BlockwiseStatus status = getBlock1Status(key); if (status != null) { clearBlock1Status(key, status); } default: } if (exchange.getRequest() != exchange.getCurrentRequest()) { Response resp = new Response(response.getCode()); resp.setToken(exchange.getRequest().getToken()); if (exchange.getRequest().getType() == Type.CON) { resp.setType(Type.ACK); resp.setMID(exchange.getRequest().getMID()); } else { resp.setType(Type.NON); } resp.setSourceContext(response.getSourceContext()); resp.setPayload(response.getPayload()); resp.setOptions(response.getOptions()); resp.setRTT(exchange.calculateRTT()); exchange.setResponse(resp); upper().receiveResponse(exchange, resp); } else { upper().receiveResponse(exchange, response); } return; } if (response.getMaxResourceBodySize() == 0) { response.setMaxResourceBodySize(exchange.getRequest().getMaxResourceBodySize()); } KeyUri key = getKey(exchange, response); Block2BlockwiseStatus status = getBlock2Status(key); if (discardBlock2(key, status, exchange, response)) { return; } if (!response.hasBlockOption()) { exchange.setResponse(response); upper().receiveResponse(exchange, response); } else { if (response.getOptions().hasBlock1()) { handleBlock1Response(exchange, response); } if (response.getOptions().hasBlock2()) { handleBlock2Response(exchange, response); } } } else { exchange.setResponse(response); upper().receiveResponse(exchange, response); } } BlockwiseLayer(final NetworkConfig config); @Override void start(); @Override void destroy(); @Override void sendRequest(final Exchange exchange, final Request request); @Override void receiveRequest(final Exchange exchange, final Request request); @Override void sendResponse(final Exchange exchange, final Response response); @Override void receiveResponse(final Exchange exchange, final Response response); boolean isEmpty();  }