Commit 04d1c4a8 authored by Björn Butzin's avatar Björn Butzin
Browse files

Added missing response code (Not_Acceptable_406) to CoapResponseCode.java

Removed toString() in CoapResponseCode.java and CoapRequestCode.java as they did the same as the super implementation
Added further JavaDoc comments
parent 010c0afe
......@@ -16,40 +16,58 @@
package org.ws4d.coap;
/**
* Defines some CoAP related constants.
* @author Christian Lerche <christian.lerche@uni-rostock.de>
*/
public final class Constants {
/**
* The minimal possible ID of a message.
* A CoAP Message ID must always be higher or equal.
* The minimal possible ID of a CoAP message. <br>
* A CoAP Message ID must always be higher or equal this value. <br>
* The Message ID is a 16-bit unsigned integer. <br>
* See rfc7252 - 3. "Message Format" for further details.
*/
public final static int MESSAGE_ID_MIN = 0;
/**
* The maximal possible id of a message.
* A CoAP Message ID must always be lower or equal.
* The maximal possible id of a CoAP message. <br>
* A CoAP Message ID must always be lower or equal. <br>
* The Message ID is a 16-bit unsigned integer. <br>
* See rfc7252 - 3. "Message Format" for further details.
*/
public final static int MESSAGE_ID_MAX = 65535;
/**
* The maximal size of a CoAP Message in ?Bytes?
* The maximal size of a CoAP message in bytes <br>
* See rfc7252 - 4.6. "Message Size" for further details.
*/
// TODO: which unit?
public final static int COAP_MESSAGE_SIZE_MAX = 1152;
/**
* The default port used by CoAP connections.
* The maximal size of a CoAP message payload in bytes <br>
* See rfc7252 - 4.6. "Message Size" for further details.
*/
public final static int COAP_PAYLOAD_SIZE_MAX = 1024;
/**
* The default port used by CoAP connections. <br>
* See rfc7252 - 6.1. "coap URI Scheme" for further details.
*/
public final static int COAP_DEFAULT_PORT = 5683;
/**
*
* This is the default value (in seconds) for the Max-Age Option. <br>
* The Max-Age Option indicates the maximum time (in seconds) a response may
* be cached before it is considered not fresh. <br>
* See rfc7252 - 5.10.5. "Max-Age" for further details.
*/
public final static int COAP_DEFAULT_MAX_AGE_S = 60;
/**
*
* This is the default value (in milliseconds) for the Max-Age Option. <br>
* The Max-Age Option indicates the maximum time (in milliseconds) a
* response may be cached before it is considered not fresh. <br>
* See rfc7252 - 5.10.5. "Max-Age" for further details.
*/
public final static int COAP_DEFAULT_MAX_AGE_MS = COAP_DEFAULT_MAX_AGE_S * 1000;
}
......@@ -24,18 +24,24 @@ import org.ws4d.coap.messages.CoapPacketType;
* @author Christian Lerche <christian.lerche@uni-rostock.de>
*/
public interface CoapMessage {
/**
* The number of milliseconds before a timeout is indicated
* The number of milliseconds before a timeout for an ACK is indicated <br>
* See rfc7252 - 4.8. "Transmission Parameters" for further details.
*/
public static final int RESPONSE_TIMEOUT_MS = 2000;
/**
*
* For a new confirmable message, the initial timeout is set to a random
* duration (often not an integral number of seconds) between ACK_TIMEOUT
* and (ACK_TIMEOUT * ACK_RANDOM_FACTOR)
* See rfc7252 - 4.8. "Transmission Parameters" for further details.
*/
public static final double RESPONSE_RANDOM_FACTOR = 1.5;
/**
* The maximum number of retransmits
* See rfc7252 - 4.8. "Transmission Parameters" for further details.
*/
public static final int MAX_RETRANSMIT = 4;
......@@ -46,7 +52,8 @@ public interface CoapMessage {
public static final int ACK_RST_RETRANS_TIMEOUT_MS = 120000;
/**
* @return Value of the internal message code.<br>-1, in case of an error.
* @return Value of the internal message code.<br>
* -1, in case of an error.
*/
public int getMessageCodeValue();
......@@ -57,7 +64,8 @@ public interface CoapMessage {
/**
*
* @param msgID - The ID of the Message to be set.
* @param msgID
* - The ID of the Message to be set.
*/
public void setMessageID(int msgID);
......@@ -133,8 +141,8 @@ public interface CoapMessage {
public byte[] getToken();
// public URI getRequestUri();
// public void setRequestUri(URI uri);
//TODO:allow this method only for Clients, Define Token Type
// public void setRequestUri(URI uri);
// TODO:allow this method only for Clients, Define Token Type
/**
*
......@@ -183,6 +191,7 @@ public interface CoapMessage {
*
* @return
*/
@Override
public String toString();
/**
......@@ -238,6 +247,7 @@ public interface CoapMessage {
*
* @return
*/
@Override
public int hashCode();
/**
......@@ -245,6 +255,7 @@ public interface CoapMessage {
* @param obj
* @return
*/
@Override
public boolean equals(Object obj);
}
......@@ -16,35 +16,55 @@
package org.ws4d.coap.messages;
/**
* This ENUM defines a subset of Internet media types to be used in CoAP. <br>
* See rfc7252 - 12.3. "CoAP Content-Formats Registry" for further details.
*
* @author Christian Lerche <christian.lerche@uni-rostock.de>
*/
public enum CoapMediaType {
text_plain (0), //text/plain; charset=utf-8
link_format (40), //application/link-format
xml(41), //application/xml
octet_stream (42), //application/octet-stream
exi(47), //application/exi
json(50), //application/json
UNKNOWN (-1);
int mediaType;
private CoapMediaType(int mediaType){
this.mediaType = mediaType;
}
public static CoapMediaType parse(int mediaType){
switch(mediaType){
case 0: return text_plain;
case 40:return link_format;
case 41:return xml;
case 42:return octet_stream;
case 47:return exi;
case 50:return json;
default: return UNKNOWN;
}
}
public int getValue(){
return mediaType;
}
text_plain(0), // text/plain; charset=utf-8
link_format(40), // application/link-format
xml(41), // application/xml
octet_stream(42), // application/octet-stream
exi(47), // application/exi
json(50), // application/json
UNKNOWN(-1);
int mediaType;
private CoapMediaType(int mediaType) {
this.mediaType = mediaType;
}
/**
* @param mediaType
* The media type code of the media type.
* @return The ENUM element matching the media type code. <br>
* UNKNOWN, if the media type code is not known.
*/
public static CoapMediaType parse(int mediaType) {
switch (mediaType) {
case 0:
return text_plain;
case 40:
return link_format;
case 41:
return xml;
case 42:
return octet_stream;
case 47:
return exi;
case 50:
return json;
default:
return UNKNOWN;
}
}
/**
* @return The media type code of the ENUM element.
*/
public int getValue() {
return mediaType;
}
}
\ No newline at end of file
......@@ -23,36 +23,49 @@ package org.ws4d.coap.messages;
* @author Christian Lerche <christian.lerche@uni-rostock.de>
*/
public enum CoapPacketType {
CON(0x00),
NON(0x01),
ACK(0x02),
RST(0x03);
CON(0x00), NON(0x01), ACK(0x02), RST(0x03);
private int packetType;
private int packetType;
CoapPacketType(int packetType) {
if (packetType >= 0x00 && packetType <= 0x03){
this.packetType = packetType;
} else {
throw new IllegalStateException("Unknown CoAP Packet Type");
/**
*
* @param packetType
* @throws IllegalStateException
* If packet type is out of range.
*/
CoapPacketType(int packetType) {
if (packetType >= 0x00 && packetType <= 0x03) {
this.packetType = packetType;
} else {
throw new IllegalStateException("Unknown CoAP Packet Type");
}
}
public static CoapPacketType getPacketType(int packetType) {
if (packetType == 0x00)
return CON;
else if (packetType == 0x01)
return NON;
else if (packetType == 0x02)
return ACK;
else if (packetType == 0x03)
return RST;
else
throw new IllegalStateException("Unknown CoAP Packet Type");
}
public int getValue() {
return packetType;
}
}
/**
* @param packetType
* the code for the packet type.
* @return The ENUM element matching the packetType. <br>
* @throws IllegalStateException
* If packetType is out of range.
*/
public static CoapPacketType getPacketType(int packetType) {
if (packetType == 0x00)
return CON;
else if (packetType == 0x01)
return NON;
else if (packetType == 0x02)
return ACK;
else if (packetType == 0x03)
return RST;
else
throw new IllegalStateException("Unknown CoAP Packet Type");
}
/**
* @return The packetType of the ENUM element.
*/
public int getValue() {
return packetType;
}
}
......@@ -16,48 +16,58 @@
package org.ws4d.coap.messages;
/**
* This Enumeration contains all request codes available for CoAP.
* See rfc7252 - 5.8. "Method Definitions" and 12.1.1. "Method Codes" for further details.
* @author Christian Lerche <christian.lerche@uni-rostock.de>
*/
public enum CoapRequestCode {
GET(1), POST(2), PUT(3), DELETE(4);
GET(1),
POST(2),
PUT(3),
DELETE(4);
private int code;
private CoapRequestCode(int code) {
this.code = code;
}
/**
* @return The method code of the ENUM element.
*/
public int getValue() {
return code;
}
/**
* @param codeValue the method code for the request code.
* @return The ENUM element matching the codeValue. <br>
* @throws IllegalArgumentException if codeValue is out of range.
*/
public static CoapRequestCode parseRequestCode(int codeValue) {
switch (codeValue) {
case 1:
return GET;
case 2:
return POST;
case 3:
return PUT;
case 4:
return DELETE;
case 1: return GET;
case 2: return POST;
case 3: return PUT;
case 4: return DELETE;
default:
throw new IllegalArgumentException("Invalid Request Code");
}
}
public int getValue() {
return code;
}
@Override
public String toString() {
switch (this) {
case GET:
return "GET";
case POST:
return "POST";
case PUT:
return "PUT";
case DELETE:
return "DELETE";
}
return null;
}
//TODO can be removed as it is the same as the super implementation
// @Override
// public String toString() {
// switch (this) {
// case GET:
// return "GET";
// case POST:
// return "POST";
// case PUT:
// return "PUT";
// case DELETE:
// return "DELETE";
// }
// return null;
// }
}
......@@ -16,56 +16,88 @@
package org.ws4d.coap.messages;
/**
* This Enumeration contains all response codes available for CoAP. <br>
* See rfc7252 - 5.9. "Response Code Definitions" for further details.
* @author Christian Lerche <christian.lerche@uni-rostock.de>
* @author Bjrn Butzin <bjoern.butzin@uni-rostock.de>
*/
public enum CoapResponseCode {
//Success 2.xx
Created_201(65),
Deleted_202(66),
Valid_203(67),
Changed_204(68),
Content_205(69),
//FIXME: Where is it defined? NOT part of rfc7252! Missing in parse & toString function
Continue_231(95),
//Client Error 4.xx
Bad_Request_400(128),
Unauthorized_401(129),
Bad_Option_402(130),
Forbidden_403(131),
Not_Found_404(132),
Method_Not_Allowed_405(133),
Method_Not_Allowed_405(133),
Not_Acceptable_406(134),
Precondition_Failed_412(140),
Request_Entity_To_Large_413(141),
Unsupported_Media_Type_415(143),
//Server Error 5.xx
Internal_Server_Error_500(160),
Not_Implemented_501(161),
Bad_Gateway_502(162),
Service_Unavailable_503(163),
Gateway_Timeout_504(164),
Proxying_Not_Supported_505(165),
Proxying_Not_Supported_505(165),
//additional, NOT part of rfc7252
UNKNOWN(-1);
private int code;
private CoapResponseCode(int code) {
this.code = code;
}
/**
* @return The codeValue of the ENUM element.
*/
public int getValue() {
return code;
}
/**
* @param codeValue the code for the response code.
* @return The ENUM element matching the codeValue. <br>
* UNKNOWN, if the codeValue doesn't match any ENUM element.
* @throws IllegalArgumentException if codeValue is out of range.
*/
public static CoapResponseCode parseResponseCode(int codeValue) {
switch (codeValue) {
/* 32..63: reserved */
// case 64: return OK_200; //not used anymore
////Success 2.xx
case 65: return Created_201;
case 66: return Deleted_202;
case 67: return Valid_203;
case 68: return Changed_204;
case 69: return Content_205;
//Client Error 4.xx
case 128: return Bad_Request_400;
case 129: return Unauthorized_401;
case 130: return Bad_Option_402;
case 131: return Forbidden_403;
case 132: return Not_Found_404;
case 133: return Method_Not_Allowed_405;
case 134: return Not_Acceptable_406;
case 140: return Precondition_Failed_412;
case 141: return Request_Entity_To_Large_413;
case 143: return Unsupported_Media_Type_415;
//Server Error 5.xx
case 160: return Internal_Server_Error_500;
case 161: return Not_Implemented_501;
case 162: return Bad_Gateway_502;
......@@ -81,55 +113,38 @@ public enum CoapResponseCode {
}
}
public int getValue() {
return code;
}
@Override
public String toString() {
switch (this) {
case Created_201:
return "Created_201";
case Deleted_202:
return "Deleted_202";
case Valid_203:
return "Valid_203";
case Changed_204:
return "Changed_204";
case Content_205:
return "Content_205";
case Bad_Request_400:
return "Bad_Request_400";
case Unauthorized_401:
return "Unauthorized_401";
case Bad_Option_402:
return "Bad_Option_402";
case Forbidden_403:
return "Forbidden_403";
case Not_Found_404:
return "Not_Found_404";
case Method_Not_Allowed_405:
return "Method_Not_Allowed_405";
case Precondition_Failed_412:
return "Precondition_Failed_412";
case Request_Entity_To_Large_413:
return "Request_Entity_To_Large_413";
case Unsupported_Media_Type_415:
return "Unsupported_Media_Type_415";
case Internal_Server_Error_500:
return "Internal_Server_Error_500";
case Not_Implemented_501:
return "Not_Implemented_501";
case Bad_Gateway_502:
return "Bad_Gateway_502";
case Service_Unavailable_503:
return "Service_Unavailable_503";
case Gateway_Timeout_504:
return "Gateway_Timeout_504";
case Proxying_Not_Supported_505:
return "Proxying_Not_Supported_505";
default:
return "Unknown_Response_Code";
}
}
//TODO can be removed as it is the same as the super implementation
// @Override
// public String toString() {
// switch (this) {
//
// //Success 2.xx
// case Created_201: return "Created_201";
// case Deleted_202: return "Deleted_202";
// case Valid_203: return "Valid_203";
// case Changed_204: return "Changed_204";
// case Content_205: return "Content_205";
//
// //Client Error 4.xx
// case Bad_Request_400: return "Bad_Request_400";
// case Unauthorized_401: return "Unauthorized_401";
// case Bad_Option_402: return "Bad_Option_402";
// case Forbidden_403: return "Forbidden_403";
// case Not_Found_404: return "Not_Found_404";
// case Method_Not_Allowed_405: return "Method_Not_Allowed_405";
// case Precondition_Failed_412: return "Precondition_Failed_412";
// case Not_Acceptable_406: return "Not_Acceptable_406";
// case Request_Entity_To_Large_413: return "Request_Entity_To_Large_413";
// case Unsupported_Media_Type_415: return "Unsupported_Media_Type_415";
//
// //Server Error 5.xx
// case Internal_Server_Error_500:return "Internal_Server_Error_500";
// case Not_Implemented_501: return "Not_Implemented_501";
// case Bad_Gateway_502: return "Bad_Gateway_502";
// case Service_Unavailable_503: return "Service_Unavailable_503";
// case Gateway_Timeout_504: return "Gateway_Timeout_504";
// case Proxying_Not_Supported_505: return "Proxying_Not_Supported_505";
// default: return "Unknown_Response_Code";
// }
// }
}
......@@ -22,15 +22,12 @@ import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.ws4d.coap.Constants;
import org.ws4d.coap.connection.BasicCoapChannelManager;
import org.ws4d.coap.interfaces.CoapChannelManager;
import org.ws4d.coap.interfaces.CoapClient;
import org.ws4d.coap.interfaces.CoapClientChannel;
import org.ws4d.coap.interfaces.CoapResponse;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.rest.BasicCoapResource;
import org.ws4d.coap.rest.CoapResourceServer;
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment