You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2017/04/27 12:54:27 UTC
camel git commit: CAMEL-10985: Ignore CORS configuraion in CoAP
component
Repository: camel
Updated Branches:
refs/heads/master cc64dc47f -> 71229bcbf
CAMEL-10985: Ignore CORS configuraion in CoAP component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/71229bcb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/71229bcb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/71229bcb
Branch: refs/heads/master
Commit: 71229bcbf7cb0cb5d1f3f841f7e1714b2c32fccf
Parents: cc64dc4
Author: James Netherton <ja...@gmail.com>
Authored: Thu Apr 27 10:48:33 2017 +0100
Committer: Zoran Regvart <zr...@apache.org>
Committed: Thu Apr 27 14:54:02 2017 +0200
----------------------------------------------------------------------
.../org/apache/camel/coap/CoAPComponent.java | 17 +++---
.../org/apache/camel/coap/CoAPCORSTest.java | 57 ++++++++++++++++++++
2 files changed, 65 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/71229bcb/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java
index 801bf23..f24aa84 100644
--- a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java
+++ b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java
@@ -33,12 +33,15 @@ import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.network.config.NetworkConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Represents the component that manages {@link CoAPEndpoint}.
*/
public class CoAPComponent extends UriEndpointComponent implements RestConsumerFactory {
static final int DEFAULT_PORT = 5684;
+ private static final Logger LOG = LoggerFactory.getLogger(CoAPComponent.class);
final Map<Integer, CoapServer> servers = new ConcurrentHashMap<>();
@@ -89,6 +92,10 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF
config = getCamelContext().getRestConfiguration("coap", true);
}
+ if (config.isEnableCORS()) {
+ LOG.info("CORS configuration will be ignored as CORS is not supported by the CoAP component");
+ }
+
String host = config.getHost();
if (ObjectHelper.isEmpty(host)) {
if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
@@ -106,24 +113,16 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF
map.putAll(config.getEndpointProperties());
}
- // allow HTTP Options as we want to handle CORS in rest-dsl
- boolean cors = config.isEnableCORS();
-
String query = URISupport.createQueryString(map);
String url = (config.getScheme() == null ? "coap" : config.getScheme()) + "://" + host;
if (config.getPort() != -1) {
url += ":" + config.getPort();
}
- String restrict = verb.toUpperCase(Locale.US);
- if (cors) {
- restrict += ",OPTIONS";
- }
-
if (uriTemplate == null) {
uriTemplate = "";
}
- url += basePath + uriTemplate + "?coapMethod=" + restrict;
+ url += basePath + uriTemplate + "?coapMethod=" + verb.toUpperCase(Locale.US);
if (!query.isEmpty()) {
url += "&" + query;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/71229bcb/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPCORSTest.java
----------------------------------------------------------------------
diff --git a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPCORSTest.java b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPCORSTest.java
new file mode 100644
index 0000000..38812f3
--- /dev/null
+++ b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPCORSTest.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.coap;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.eclipse.californium.core.CoapClient;
+import org.eclipse.californium.core.CoapResponse;
+import org.eclipse.californium.core.network.config.NetworkConfig;
+import org.junit.Test;
+
+/**
+ * https://issues.apache.org/jira/browse/CAMEL-10985
+ *
+ * CORS configuration is ignored and REST endpoints function as per normal
+ */
+public class CoAPCORSTest extends CamelTestSupport {
+ private static final String COAP_RESPONSE = "{ \"foo\": \"bar\" }";
+ private static final int COAP_PORT = AvailablePortFinder.getNextAvailable();
+
+ @Test
+ public void testEnableCors() throws Exception {
+ NetworkConfig.createStandardWithoutFile();
+
+ CoapClient client = new CoapClient("coap://localhost:" + COAP_PORT + "/rest");
+ CoapResponse coapResponse = client.get();
+ assertEquals(COAP_RESPONSE, coapResponse.getResponseText());
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ restConfiguration().component("coap").port(COAP_PORT).enableCORS(true);
+
+ rest().get("/rest").route().setBody(constant(COAP_RESPONSE));
+ }
+ };
+ }
+}