Search in sources :

Example 1 with CoapClient

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();
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CoapClient(org.eclipse.californium.core.CoapClient) Test(org.junit.Test)

Example 2 with CoapClient

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();
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CoapClient(org.eclipse.californium.core.CoapClient) Test(org.junit.Test)

Example 3 with CoapClient

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;
}
Also used : URI(java.net.URI) CoapClient(org.eclipse.californium.core.CoapClient)

Example 4 with CoapClient

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);
    }
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) Message(org.apache.camel.Message) CoapClient(org.eclipse.californium.core.CoapClient)

Example 5 with CoapClient

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();
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CoapClient(org.eclipse.californium.core.CoapClient) Test(org.junit.Test)

Aggregations

CoapClient (org.eclipse.californium.core.CoapClient)9 CoapResponse (org.eclipse.californium.core.CoapResponse)8 Test (org.junit.Test)7 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4 URI (java.net.URI)1 Message (org.apache.camel.Message)1