[
https://issues.apache.org/jira/browse/CAMEL-12029?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16265070#comment-16265070
]
ASF GitHub Bot commented on CAMEL-12029:
----------------------------------------
davsclaus closed pull request #2112: CAMEL-12029: Return 4.05 response if no
consumers were found for the request method
URL: https://github.com/apache/camel/pull/2112
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/components/camel-coap/src/main/java/org/apache/camel/coap/CamelCoapResource.java
b/components/camel-coap/src/main/java/org/apache/camel/coap/CamelCoapResource.java
index 7c709b61cc1..017fd4f6020 100644
---
a/components/camel-coap/src/main/java/org/apache/camel/coap/CamelCoapResource.java
+++
b/components/camel-coap/src/main/java/org/apache/camel/coap/CamelCoapResource.java
@@ -84,7 +84,12 @@ public void handleRequest(Exchange exchange) {
if (consumer == null) {
consumer = consumers.get("*");
}
-
+
+ if (consumer == null) {
+ cexchange.respond(ResponseCode.METHOD_NOT_ALLOWED);
+ return;
+ }
+
camelExchange = consumer.getEndpoint().createExchange();
consumer.createUoW(camelExchange);
@@ -125,8 +130,8 @@ public void handleRequest(Exchange exchange) {
byte bytes[] = exchange.getCurrentRequest().getPayload();
camelExchange.getIn().setBody(bytes);
-
- consumer.getProcessor().process(camelExchange);
+
+ consumer.getProcessor().process(camelExchange);
Message target = camelExchange.hasOut() ? camelExchange.getOut() :
camelExchange.getIn();
int format =
MediaTypeRegistry.parse(target.getHeader(org.apache.camel.Exchange.CONTENT_TYPE,
String.class));
diff --git
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java
index e35f98f6fb2..12cfd834abb 100644
---
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java
+++
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java
@@ -23,6 +23,7 @@
import org.apache.camel.test.junit4.CamelTestSupport;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
+import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.junit.Test;
@@ -39,19 +40,41 @@ public void testCoAP() throws Exception {
client = new CoapClient("coap://localhost:" + coapport +
"/TestResource/Ducky");
client.setTimeout(1000000);
rsp = client.get();
+ assertEquals(ResponseCode.CONTENT, rsp.getCode());
assertEquals("Hello Ducky", rsp.getResponseText());
rsp = client.post("data", MediaTypeRegistry.TEXT_PLAIN);
+ assertEquals(ResponseCode.CONTENT, rsp.getCode());
assertEquals("Hello Ducky: data", rsp.getResponseText());
client = new CoapClient("coap://localhost:" + coapport +
"/TestParams?id=Ducky");
client.setTimeout(1000000);
rsp = client.get();
+ assertEquals(ResponseCode.CONTENT, rsp.getCode());
assertEquals("Hello Ducky", rsp.getResponseText());
rsp = client.post("data", MediaTypeRegistry.TEXT_PLAIN);
+ assertEquals(ResponseCode.CONTENT, rsp.getCode());
assertEquals("Hello Ducky: data", rsp.getResponseText());
assertEquals(MediaTypeRegistry.TEXT_PLAIN,
rsp.getOptions().getContentFormat());
}
+ @Test
+ public void testCoAPMethodNotAllowedResponse() throws Exception {
+ NetworkConfig.createStandardWithoutFile();
+ CoapClient client = new CoapClient("coap://localhost:" + coapport +
"/TestResource/Ducky");
+ client.setTimeout(1000000);
+ CoapResponse rsp = client.delete();
+ assertEquals(ResponseCode.METHOD_NOT_ALLOWED, rsp.getCode());
+ }
+
+ @Test
+ public void testCoAPNotFoundResponse() throws Exception {
+ NetworkConfig.createStandardWithoutFile();
+ CoapClient client = new CoapClient("coap://localhost:" + coapport +
"/foo/bar/cheese");
+ client.setTimeout(1000000);
+ CoapResponse rsp = client.get();
+ assertEquals(ResponseCode.NOT_FOUND, rsp.getCode());
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> CoAP component should handle method not allowed
> -----------------------------------------------
>
> Key: CAMEL-12029
> URL: https://issues.apache.org/jira/browse/CAMEL-12029
> Project: Camel
> Issue Type: Improvement
> Components: camel-coap
> Reporter: James Netherton
> Assignee: James Netherton
> Priority: Minor
> Fix For: 2.19.5, 2.20.2, 2.21.0
>
>
> If you hit a CoAP consumer endpoint using a method that it does not support,
> an NPE is thrown within CamelCoapResource and it returns an 'internal server
> error' response to the client.
> We should detect this scenario and as per RFC 7252, and return a response
> like '4.05 Method Not Allowed'.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)