Skip to content

Commit cc202fb

Browse files
committed
feat(coap-core): define mapping from http status to coap code
1 parent 4a66f35 commit cc202fb

2 files changed

Lines changed: 127 additions & 5 deletions

File tree

coap-core/src/main/java/com/mbed/coap/packet/Code.java

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
2+
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2018 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,20 +19,21 @@
1919

2020
public enum Code {
2121
//RFC 7252
22-
//draft-ietf-core-http-mapping-08 - for HTTP mapping
22+
//RFC 8075
2323

2424
C201_CREATED(2, 01, 201),
2525
C202_DELETED(2, 02, 200),
2626
C203_VALID(2, 03, 200),
2727
C204_CHANGED(2, 04, 200),
28-
C205_CONTENT(2, 05, 200),
28+
C205_CONTENT(2, 05, 200), // No direct CoAP equivalent
2929
C400_BAD_REQUEST(4, 00, 400),
3030
C401_UNAUTHORIZED(4, 01, 403),
3131
C402_BAD_OPTION(4, 02, 400),
3232
C403_FORBIDDEN(4, 03, 403),
3333
C404_NOT_FOUND(4, 04, 404),
34-
C405_METHOD_NOT_ALLOWED(4, 05, 400),
34+
C405_METHOD_NOT_ALLOWED(4, 05, 405),
3535
C406_NOT_ACCEPTABLE(4, 06, 406),
36+
C429_TOO_MANY_REQUESTS(4, 29, 429), // RFC 8516
3637
C412_PRECONDITION_FAILED(4, 12, 412),
3738
C415_UNSUPPORTED_MEDIA_TYPE(4, 15, 415),
3839
C500_INTERNAL_SERVER_ERROR(5, 00, 500),
@@ -105,4 +106,86 @@ public boolean isSuccess() {
105106
return coapCode >>> 5 == 2;
106107
}
107108

109+
public static Code fromHttp(int httpStatus, Method method) {
110+
111+
switch (httpStatus) {
112+
// SUCCESS (2.xx)
113+
case 200:
114+
switch (method) {
115+
case PUT:
116+
case POST:
117+
case PATCH:
118+
case iPATCH:
119+
return C204_CHANGED;
120+
case DELETE:
121+
return C202_DELETED;
122+
default:
123+
return C205_CONTENT;
124+
}
125+
case 201:
126+
return C201_CREATED;
127+
case 204:
128+
if (method == Method.DELETE) {
129+
return C202_DELETED;
130+
}
131+
return C204_CHANGED;
132+
133+
134+
// REDIRECT (3.xx)
135+
case 304:
136+
return C203_VALID;
137+
138+
// CLIENT ERRORS (4.xx)
139+
case 400:
140+
return C400_BAD_REQUEST;
141+
case 401:
142+
return C401_UNAUTHORIZED;
143+
case 403:
144+
return C403_FORBIDDEN;
145+
case 404:
146+
return C404_NOT_FOUND;
147+
case 405:
148+
return C405_METHOD_NOT_ALLOWED;
149+
case 406:
150+
return C406_NOT_ACCEPTABLE;
151+
case 408:
152+
return C408_REQUEST_ENTITY_INCOMPLETE; // Closest semantic match
153+
case 409:
154+
return C409_CONFLICT;
155+
case 412:
156+
return C412_PRECONDITION_FAILED;
157+
case 413:
158+
return C413_REQUEST_ENTITY_TOO_LARGE;
159+
case 415:
160+
return C415_UNSUPPORTED_MEDIA_TYPE;
161+
case 429:
162+
return C429_TOO_MANY_REQUESTS;
163+
164+
// SERVER ERRORS (5.xx)
165+
case 500:
166+
return C500_INTERNAL_SERVER_ERROR;
167+
case 501:
168+
return C501_NOT_IMPLEMENTED;
169+
case 502:
170+
return C502_BAD_GATEWAY;
171+
case 503:
172+
return C503_SERVICE_UNAVAILABLE;
173+
case 504:
174+
return C504_GATEWAY_TIMEOUT;
175+
176+
default:
177+
return mapUnhandledHttpStatusToDefault(httpStatus);
178+
}
179+
}
180+
181+
private static Code mapUnhandledHttpStatusToDefault(int httpStatus) {
182+
if (httpStatus <= 299) {
183+
return C205_CONTENT;
184+
} else if (httpStatus <= 399) {
185+
return C502_BAD_GATEWAY;
186+
} else if (httpStatus <= 499) {
187+
return C400_BAD_REQUEST;
188+
}
189+
return C500_INTERNAL_SERVER_ERROR;
190+
}
108191
}

coap-core/src/test/java/com/mbed/coap/packet/CodeTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
2+
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,11 @@
1616
*/
1717
package com.mbed.coap.packet;
1818

19+
import static com.mbed.coap.packet.Code.fromHttp;
20+
import static com.mbed.coap.packet.Method.DELETE;
21+
import static com.mbed.coap.packet.Method.GET;
22+
import static com.mbed.coap.packet.Method.POST;
23+
import static com.mbed.coap.packet.Method.PUT;
1924
import static org.junit.jupiter.api.Assertions.assertEquals;
2025
import static org.junit.jupiter.api.Assertions.assertFalse;
2126
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -96,4 +101,38 @@ public void testIsSuccess() {
96101
assertFalse(Code.C500_INTERNAL_SERVER_ERROR.isSuccess());
97102
assertFalse(Code.C503_SERVICE_UNAVAILABLE.isSuccess());
98103
}
104+
105+
@Test
106+
public void shouldConvertHttpToCoapCode() {
107+
assertEquals(Code.C205_CONTENT, fromHttp(200, GET));
108+
assertEquals(Code.C204_CHANGED, fromHttp(200, PUT));
109+
assertEquals(Code.C202_DELETED, fromHttp(200, DELETE));
110+
assertEquals(Code.C201_CREATED, fromHttp(201, POST));
111+
assertEquals(Code.C205_CONTENT, fromHttp(202, GET));
112+
assertEquals(Code.C202_DELETED, fromHttp(204, DELETE));
113+
114+
assertEquals(Code.C203_VALID, fromHttp(304, GET));
115+
116+
assertEquals(Code.C400_BAD_REQUEST, fromHttp(400, GET));
117+
assertEquals(Code.C401_UNAUTHORIZED, fromHttp(401, GET));
118+
assertEquals(Code.C403_FORBIDDEN, fromHttp(403, GET));
119+
assertEquals(Code.C404_NOT_FOUND, fromHttp(404, GET));
120+
assertEquals(Code.C405_METHOD_NOT_ALLOWED, fromHttp(405, GET));
121+
assertEquals(Code.C406_NOT_ACCEPTABLE, fromHttp(406, GET));
122+
assertEquals(Code.C408_REQUEST_ENTITY_INCOMPLETE, fromHttp(408, GET));
123+
assertEquals(Code.C409_CONFLICT, fromHttp(409, GET));
124+
assertEquals(Code.C412_PRECONDITION_FAILED, fromHttp(412, GET));
125+
assertEquals(Code.C413_REQUEST_ENTITY_TOO_LARGE, fromHttp(413, GET));
126+
assertEquals(Code.C415_UNSUPPORTED_MEDIA_TYPE, fromHttp(415, GET));
127+
assertEquals(Code.C429_TOO_MANY_REQUESTS, fromHttp(429, GET));
128+
129+
assertEquals(Code.C500_INTERNAL_SERVER_ERROR, fromHttp(500, GET));
130+
assertEquals(Code.C501_NOT_IMPLEMENTED, fromHttp(501, GET));
131+
assertEquals(Code.C502_BAD_GATEWAY, fromHttp(502, GET));
132+
assertEquals(Code.C503_SERVICE_UNAVAILABLE, fromHttp(503, GET));
133+
assertEquals(Code.C504_GATEWAY_TIMEOUT, fromHttp(504, GET));
134+
135+
136+
137+
}
99138
}

0 commit comments

Comments
 (0)