private void assertCorrectVersion(int version) { if (version != CoAP.VERSION) { throw new MessageFormatException("Message has invalid version: " + version); } } }
/** * Parses a byte array into a CoAP Message. * * @param msg the byte array to parse. * @return the message. * @throws MessageFormatException if the array cannot be parsed into a message. */ public final Message parseMessage(final byte[] msg) { String message = "illegal message code"; DatagramReader reader = new DatagramReader(msg); MessageHeader header = parseHeader(reader); try { if (CoAP.isRequest(header.getCode())) { return parseMessage(reader, header, new Request(CoAP.Code.valueOf(header.getCode()))); } else if (CoAP.isResponse(header.getCode())) { return parseMessage(reader, header, new Response(CoAP.ResponseCode.valueOf(header.getCode()))); } else if (CoAP.isEmptyMessage(header.getCode())) { return parseMessage(reader, header, new EmptyMessage(header.getType())); } } catch (MessageFormatException e) { /** use message to add CoAP message specific information */ message = e.getMessage(); } throw new CoAPMessageFormatException(message, header.getMID(), header.getCode(), CoAP.Type.CON == header.getType()); }
/** * Asserts that the length of the token as read from the encoded message complies * with the CoAP spec. * * @param tokenLength the length value read from the message header. * @throws MessageFormatException if the value is greater than eight. */ protected static final void assertValidTokenLength(int tokenLength) { if (tokenLength > 8) { // must be treated as a message format error according to CoAP spec // https://tools.ietf.org/html/rfc7252#section-3 throw new MessageFormatException("Message has invalid token length (> 8)" + tokenLength); } }
/** * Converts the specified integer value to a request code. * * @param value the integer value * @return the request code * @throws MessageFormatException if the integer value does not represent a valid request code. */ public static CodeClass valueOf(final int value) { switch (value) { case 0: return REQUEST; case 2: return SUCCESS_RESPONSE; case 4: return ERROR_RESPONSE; case 5: return SERVER_ERROR_RESPONSE; case 7: return SIGNAL; default: throw new MessageFormatException(String.format("Unknown CoAP class code: %d", value)); } } }
/** * Converts the specified integer value to a request code. * * @param value the integer value * @return the request code * @throws MessageFormatException if the integer value does not represent a valid request code. */ public static Code valueOf(final int value) { int classCode = getCodeClass(value); int detailCode = getCodeDetail(value); if (classCode > 0) { throw new MessageFormatException(String.format("Not a CoAP request code: %s", formatCode(classCode, detailCode))); } switch (detailCode) { case 1: return GET; case 2: return POST; case 3: return PUT; case 4: return DELETE; default: throw new MessageFormatException(String.format("Unknown CoAP request code: %s", formatCode(classCode, detailCode))); } } }
/** * Converts the specified integer value to a response code. * * @param value the value * @return the response code * @throws MessageFormatException if the value does not represent a valid response code. */ public static ResponseCode valueOf(final int value) { int codeClass = getCodeClass(value); int codeDetail = getCodeDetail(value); switch (codeClass) { case 2: return valueOfSuccessCode(codeDetail); case 4: return valueOfClientErrorCode(codeDetail); case 5: return valueOfServerErrorCode(codeDetail); default: throw new MessageFormatException(String.format("Not a CoAP response code: %s", formatCode(codeClass, codeDetail))); } }