use of org.eclipse.californium.core.CoapClient in project camel by apache.
the class CoAPRestVerbTest method testDelete.
@Test
public void testDelete() throws Exception {
NetworkConfig.createStandardWithoutFile();
MockEndpoint mock = getMockEndpoint("mock:delete");
mock.expectedHeaderReceived("id", "1");
CoapClient client = new CoapClient("coap://localhost:" + coapport + "/users/1");
client.setTimeout(1000000);
CoapResponse rsp = client.delete();
assertMockEndpointsSatisfied();
}
use of org.eclipse.californium.core.CoapClient in project camel by apache.
the class CoAPRestVerbTest method testPut.
@Test
public void testPut() throws Exception {
NetworkConfig.createStandardWithoutFile();
final String body = "{ \"id\":\"1\", \"name\":\"Scott\" }";
MockEndpoint mock = getMockEndpoint("mock:update");
mock.expectedBodiesReceived(body);
mock.expectedHeaderReceived("id", "1");
CoapClient client = new CoapClient("coap://localhost:" + coapport + "/users/1");
client.setTimeout(1000000);
CoapResponse rsp = client.put(body, MediaTypeRegistry.APPLICATION_JSON);
assertMockEndpointsSatisfied();
}
use of org.eclipse.californium.core.CoapClient in project camel by apache.
the class CoAPProducer method getClient.
private synchronized CoapClient getClient(Exchange exchange) {
if (client == null) {
URI uri = exchange.getIn().getHeader("coapUri", URI.class);
if (uri == null) {
uri = endpoint.getUri();
}
client = new CoapClient(uri);
}
return client;
}
use of org.eclipse.californium.core.CoapClient in project camel by apache.
the class CoAPProducer method process.
public void process(Exchange exchange) throws Exception {
CoapClient client = getClient(exchange);
String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
if (ct == null) {
//?default?
ct = "application/octet-stream";
}
String method = exchange.getIn().getHeader(Exchange.HTTP_METHOD, String.class);
if (method == null) {
method = endpoint.getCoapMethod();
}
if (method == null) {
Object body = exchange.getIn().getBody();
if (body == null) {
method = "GET";
} else {
method = "POST";
}
}
int mediaType = MediaTypeRegistry.parse(ct);
CoapResponse response = null;
boolean pingResponse = false;
switch(method) {
case "GET":
response = client.get();
break;
case "DELETE":
response = client.delete();
break;
case "POST":
byte[] bodyPost = exchange.getIn().getBody(byte[].class);
response = client.post(bodyPost, mediaType);
break;
case "PUT":
byte[] bodyPut = exchange.getIn().getBody(byte[].class);
response = client.put(bodyPut, mediaType);
break;
case "PING":
pingResponse = client.ping();
break;
default:
break;
}
if (response != null) {
Message resp = exchange.getOut();
String mt = MediaTypeRegistry.toString(response.getOptions().getContentFormat());
resp.setHeader(org.apache.camel.Exchange.CONTENT_TYPE, mt);
resp.setBody(response.getPayload());
}
if (method.equalsIgnoreCase("PING")) {
Message resp = exchange.getOut();
resp.setBody(pingResponse);
}
}
use of org.eclipse.californium.core.CoapClient in project camel by apache.
the class CoAPComponentTest method testCoAP.
@Test
public void testCoAP() throws Exception {
NetworkConfig.createStandardWithoutFile();
CoapClient client = new CoapClient("coap://localhost:" + PORT + "/TestResource");
CoapResponse rsp = client.get();
assertEquals("Hello ", rsp.getResponseText());
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
mock.expectedBodiesReceived("Hello");
sender.sendBody("Hello");
assertMockEndpointsSatisfied();
}
Aggregations