Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-10985: Ignore CORS configuraion in CoAP component #1665

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -33,12 +33,15 @@
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<>();

Expand Down Expand Up @@ -89,6 +92,10 @@ public Consumer createConsumer(CamelContext camelContext,
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) {
Expand All @@ -106,24 +113,16 @@ public Consumer createConsumer(CamelContext camelContext,
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;
}
Expand Down
@@ -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));
}
};
}
}