Search in sources :

Example 1 with CoapResponse

use of org.eclipse.californium.core.CoapResponse 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 CoapResponse

use of org.eclipse.californium.core.CoapResponse 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 CoapResponse

use of org.eclipse.californium.core.CoapResponse 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 4 with CoapResponse

use of org.eclipse.californium.core.CoapResponse 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)

Example 5 with CoapResponse

use of org.eclipse.californium.core.CoapResponse in project camel by apache.

the class CoAPRestComponentTest method testCoAP.

@Test
public void testCoAP() throws Exception {
    NetworkConfig.createStandardWithoutFile();
    CoapClient client;
    CoapResponse rsp;
    client = new CoapClient("coap://localhost:" + coapport + "/TestResource/Ducky");
    client.setTimeout(1000000);
    rsp = client.get();
    assertEquals("Hello Ducky", rsp.getResponseText());
    rsp = client.post("data", MediaTypeRegistry.TEXT_PLAIN);
    assertEquals("Hello Ducky: data", rsp.getResponseText());
    client = new CoapClient("coap://localhost:" + coapport + "/TestParms?id=Ducky");
    client.setTimeout(1000000);
    rsp = client.get();
    assertEquals("Hello Ducky", rsp.getResponseText());
    rsp = client.post("data", MediaTypeRegistry.TEXT_PLAIN);
    assertEquals("Hello Ducky: data", rsp.getResponseText());
    assertEquals(MediaTypeRegistry.TEXT_PLAIN, rsp.getOptions().getContentFormat());
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) CoapClient(org.eclipse.californium.core.CoapClient) Test(org.junit.Test)

Aggregations

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