@Override public void sendRequest(final Exchange exchange, final Request request) { observe(exchange); exchangeStore.registerOutboundRequest(exchange); if (LOGGER.isLoggable(Level.FINER)) { LOGGER.log( Level.FINER, "Tracking open request [MID: {0}, Token: {1}]", new Object[] { request.getMID(), request.getTokenString() }); } }
/** * Formats a {@link Request} into a readable String representation. * * @param r the Request * @return the pretty print */ public static String prettyPrint(Request r) { StringBuilder sb = new StringBuilder(); sb.append("==[ CoAP Request ]=============================================").append(System.lineSeparator()); sb.append(String.format("MID : %d", r.getMID())).append(System.lineSeparator()); sb.append(String.format("Token : %s", r.getTokenString())).append(System.lineSeparator()); sb.append(String.format("Type : %s", r.getType().toString())).append(System.lineSeparator()); sb.append(String.format("Method : %s", r.getCode().toString())).append(System.lineSeparator()); sb.append(String.format("Options: %s", r.getOptions().toString())).append(System.lineSeparator()); sb.append(String.format("Payload: %d Bytes", r.getPayloadSize())).append(System.lineSeparator()); if (r.getPayloadSize() > 0 && MediaTypeRegistry.isPrintable(r.getOptions().getContentFormat())) { sb.append("---------------------------------------------------------------").append(System.lineSeparator()); sb.append(r.getPayloadString()); sb.append(System.lineSeparator()); } sb.append("==============================================================="); return sb.toString(); }
@Override public void handleGET(CoapExchange exchange) { // get request to read out details Request request = exchange.advanced().getRequest(); StringBuilder payload = new StringBuilder(); payload.append(String.format("Type: %d (%s)\nCode: %d (%s)\nMID: %d\n", request.getType().value, request.getType(), request.getCode().value, request.getCode(), request.getMID() )); payload.append("?").append(request.getOptions().getUriQueryString()); if (payload.length()>64) { payload.delete(63, payload.length()); payload.append('ยป'); } // complete the request exchange.respond(CONTENT, payload.toString(), TEXT_PLAIN); } }
@Override public String toString() { String payload = getPayloadTracingString(); return String.format("%s-%-6s MID=%5d, Token=%s, OptionSet=%s, %s", getType(), getCode(), getMID(), getTokenString(), getOptions(), payload); }
@Override public void onCancel() { if (!exchange.isComplete()) { LOGGER.log(Level.FINE, "completing canceled request [MID={0}, token={1}]", new Object[]{ exchange.getRequest().getMID(), exchange.getRequest().getTokenString() }); exchange.setComplete(); } } }
@Override public String toString() { String payload = getPayloadTracingString(); return String.format("%s-%-6s MID=%5d, Token=%s, OptionSet=%s, %s", getType(), getCode(), getMID(), getTokenString(), getOptions(), payload); }
protected boolean checkResponse(Request request, Response response) { boolean success = true; success &= checkType(Type.ACK, response.getType()); success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code"); success &= checkInt(request.getMID(), response.getMID(), "MID"); return success; } }
@Override public void handleGET(CoapExchange exchange) { Request request = exchange.advanced().getRequest(); String payload = String.format("Long path resource\n" + "Type: %d (%s)\nCode: %d (%s)\nMID: %d", request.getType().value, request.getType(), request.getCode().value, request.getCode(), request.getMID() ); // complete the request exchange.respond(CONTENT, payload, TEXT_PLAIN); } }
protected boolean checkResponse(Request request, Response response) { boolean success = true; success &= checkType(Type.ACK, response.getType()); // Code = 65(2.01 Created) or 68 (2.04 changed) success &= checkInts(expectedResponseCodes, response.getCode().value, "code"); success &= checkInt(request.getMID(), response.getMID(), "MID"); return success; } }
public CoapMessage(Request request, boolean incoming) { this(incoming, request.getType(), request.getMID(), request.getTokenString(), request.getOptions(), request .getPayload()); this.code = request.getCode().toString(); }
protected boolean checkResponse(Request request, Response response) { boolean success = true; success &= checkType(Type.ACK, response.getType()); // Code = 68 (2.04 Changed) or 65 (2.01 Created) success &= checkInts(expectedResponseCodes, response.getCode().value, "code"); success &= checkInt(request.getMID(), response.getMID(), "MID"); return success; } }
@Override public void handleGET(CoapExchange exchange) { // promise the client that this request will be acted upon by sending an Acknowledgement exchange.accept(); // do the time-consuming computation try { Thread.sleep(1000); } catch (InterruptedException e) { } // get request to read out details Request request = exchange.advanced().getRequest(); String payload = String.format("Type: %d (%s)\nCode: %d (%s)\nMID: %d\n", request.getType().value, request.getType(), request.getCode().value, request.getCode(), request.getMID() ); // complete the request exchange.respond(CONTENT, payload, TEXT_PLAIN); } }
protected boolean checkResponse(Request request, Response response) { boolean success = true; success &= checkType(Type.ACK, response.getType()); success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code"); success &= checkInt(request.getMID(), response.getMID(), "MID"); success &= hasContentType(response); success &= hasNonEmptyPalyoad(response); return success; } }
protected final void appendRequestDetails(final Request request) { if (request.isCanceled()) { buffer.append("CANCELED "); } buffer.append(request.getType()).append(" [MID=").append(request.getMID()) .append(", T=").append(request.getTokenString()).append("], ") .append(request.getCode()).append(", /").append(request.getOptions().getUriPathString()); appendBlockOption(1, request.getOptions().getBlock1()); appendBlockOption(2, request.getOptions().getBlock2()); appendObserveOption(request.getOptions()); appendSize1(request.getOptions()); appendEtags(request.getOptions()); }
/** * Serializes a request to the wire format. * <p> * The main difference between this and the <em>serializeRequest</em> method is that this method * does <em>not</em> cache the byte array in the request's <em>bytes</em> property. * * @param request The request to serialize. * @return The encoded request. */ public final byte[] getByteArray(final Request request) { DatagramWriter writer = new DatagramWriter(); byte[] body = serializeOptionsAndPayload(request); MessageHeader header = new MessageHeader(CoAP.VERSION, request.getType(), request.getToken(), request.getRawCode(), request.getMID(), body.length); serializeHeader(writer, header); writer.writeBytes(body); return writer.toByteArray(); }
@Test public void testRegisterOutboundRequestAssignsMid() { Exchange exchange = newOutboundRequest(); // WHEN registering the outbound request store.registerOutboundRequest(exchange); // THEN the request gets assigned an MID and is put to the store assertNotNull(exchange.getCurrentRequest().getMID()); KeyMID key = KeyMID.fromOutboundMessage(exchange.getCurrentRequest()); assertThat(store.get(key), is(exchange)); }
@Test public void testRegisterOutboundRequestRejectsOtherRequestWithAlreadyUsedMid() { Exchange exchange = newOutboundRequest(); store.registerOutboundRequest(exchange); // WHEN registering another request with the same MID Exchange newExchange = newOutboundRequest(); newExchange.getCurrentRequest().setMID(exchange.getCurrentRequest().getMID()); try { store.registerOutboundRequest(newExchange); fail("should have thrown IllegalArgumentException"); } catch (IllegalArgumentException e) { // THEN the newExchange is not put to the store KeyMID key = KeyMID.fromOutboundMessage(exchange.getCurrentRequest()); Exchange exchangeFromStore = store.get(key); assertThat(exchangeFromStore, is(exchange)); assertThat(exchangeFromStore, is(not(newExchange))); } }
@Test public void testRequestParsing() { Request request = new Request(Code.POST); request.setType(Type.NON); request.setMID(expectedMid); request.setToken(new byte[] { 11, 82, -91, 77, 3 }); request.getOptions().addIfMatch(new byte[] { 34, -17 }).addIfMatch(new byte[] { 88, 12, -2, -99, 5 }) .setContentFormat(40).setAccept(40); RawData rawData = serializer.serializeRequest(request); // MessageHeader header = parser.parseHeader(rawData); // assertTrue(CoAP.isRequest(header.getCode())); // // Request result = parser.parseRequest(rawData); Request result = (Request) parser.parseMessage(rawData); assertEquals(request.getMID(), result.getMID()); assertArrayEquals(request.getToken(), result.getToken()); assertEquals(request.getOptions().asSortedList(), result.getOptions().asSortedList()); }
private static Response responseFor(final Request request) { Response response = new Response(ResponseCode.CONTENT); response.setMID(request.getMID()); response.setToken(request.getToken()); response.setBytes(new byte[]{}); response.setSource(request.getDestination()); response.setSourcePort(request.getDestinationPort()); response.setDestination(request.getSource()); response.setDestinationPort(request.getSourcePort()); return response; } }
private Response responseFor(final Request request) { Response response = new Response(ResponseCode.CONTENT); response.setMID(request.getMID()); response.setToken(request.getToken()); response.setBytes(new byte[]{}); response.setSource(request.getDestination()); response.setSourcePort(request.getDestinationPort()); response.setDestination(request.getSource()); response.setDestinationPort(request.getSourcePort()); return response; } }