Skip to content

Commit

Permalink
Rename fromCoapCode>toLwM2mResponseCode fromLwM2mCode>toCoapResponseCode
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 10, 2017
1 parent 80543cb commit a018896
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*******************************************************************************/
package org.eclipse.leshan.client.californium.impl;

import static org.eclipse.leshan.core.californium.ResponseCodeUtil.fromLwM2mCode;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toCoapResponseCode;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.server.resources.CoapExchange;
Expand All @@ -41,7 +41,7 @@ public BootstrapResource(BootstrapHandler bootstrapHandler) {
public void handlePOST(CoapExchange exchange) {
ServerIdentity identity = ResourceUtil.extractServerIdentity(exchange, bootstrapHandler);
BootstrapFinishResponse response = bootstrapHandler.finished(identity, new BootstrapFinishRequest());
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.client.californium.impl;

import static org.eclipse.leshan.core.californium.ResponseCodeUtil.fromCoapCode;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toLwM2mResponseCode;

import org.eclipse.californium.core.coap.Response;
import org.eclipse.leshan.core.request.BootstrapRequest;
Expand Down Expand Up @@ -50,7 +50,7 @@ public void visit(RegisterRequest request) {
case BAD_REQUEST:
case FORBIDDEN:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new RegisterResponse(fromCoapCode(coapResponse.getCode().value), null,
lwM2mresponse = new RegisterResponse(toLwM2mResponseCode(coapResponse.getCode().value), null,
coapResponse.getPayloadString());
break;
default:
Expand All @@ -67,7 +67,7 @@ public void visit(DeregisterRequest request) {
case BAD_REQUEST:
case NOT_FOUND:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new DeregisterResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new DeregisterResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString());
break;
default:
Expand All @@ -84,7 +84,7 @@ public void visit(UpdateRequest request) {
case BAD_REQUEST:
case NOT_FOUND:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new UpdateResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new UpdateResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString());
break;
default:
Expand All @@ -100,7 +100,7 @@ public void visit(BootstrapRequest request) {
break;
case BAD_REQUEST:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new BootstrapResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new BootstrapResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString());
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.eclipse.leshan.client.californium.impl;

import static org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.fromLwM2mCode;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toCoapResponseCode;

import java.util.List;

Expand Down Expand Up @@ -119,9 +119,9 @@ public void handleGET(CoapExchange exchange) {
if (exchange.getRequestOptions().getAccept() == MediaTypeRegistry.APPLICATION_LINK_FORMAT) {
DiscoverResponse response = nodeEnabler.discover(identity, new DiscoverRequest(URI));
if (response.getCode().isError()) {
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
exchange.respond(fromLwM2mCode(response.getCode()), Link.serialize(response.getObjectLinks()),
exchange.respond(toCoapResponseCode(response.getCode()), Link.serialize(response.getObjectLinks()),
MediaTypeRegistry.APPLICATION_LINK_FORMAT);
}
} else {
Expand All @@ -146,7 +146,7 @@ public void handleGET(CoapExchange exchange) {
format.getCode());
return;
} else {
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
}
Expand All @@ -161,7 +161,7 @@ public void handleGET(CoapExchange exchange) {
format.getCode());
return;
} else {
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
}
Expand All @@ -184,7 +184,7 @@ public void handlePUT(CoapExchange coapExchange) {
if (spec != null) {
WriteAttributesResponse response = nodeEnabler.writeAttributes(identity,
new WriteAttributesRequest(URI, spec));
coapExchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
// Manage Write and Bootstrap Write Request (replace)
Expand All @@ -208,11 +208,11 @@ public void handlePUT(CoapExchange coapExchange) {
if (identity.isLwm2mBootstrapServer()) {
BootstrapWriteResponse response = nodeEnabler.write(identity,
new BootstrapWriteRequest(path, lwM2mNode, contentFormat));
coapExchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
WriteResponse response = nodeEnabler.write(identity,
new WriteRequest(Mode.REPLACE, contentFormat, URI, lwM2mNode));
coapExchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}

return;
Expand All @@ -237,7 +237,7 @@ public void handlePOST(CoapExchange exchange) {
byte[] payload = exchange.getRequestPayload();
ExecuteResponse response = nodeEnabler.execute(identity,
new ExecuteRequest(URI, payload != null ? new String(payload) : null));
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}

Expand All @@ -260,7 +260,7 @@ public void handlePOST(CoapExchange exchange) {
LwM2mNode lwM2mNode = decoder.decode(exchange.getRequestPayload(), contentFormat, path, model);
WriteResponse response = nodeEnabler.write(identity,
new WriteRequest(Mode.UPDATE, contentFormat, URI, lwM2mNode));
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} catch (CodecException e) {
LOG.warn("Unable to decode payload to write", e);
exchange.respond(ResponseCode.BAD_REQUEST);
Expand All @@ -287,10 +287,10 @@ public void handlePOST(CoapExchange exchange) {
CreateResponse response = nodeEnabler.create(identity, createRequest);
if (response.getCode() == org.eclipse.leshan.ResponseCode.CREATED) {
exchange.setLocationPath(response.getLocation());
exchange.respond(fromLwM2mCode(response.getCode()));
exchange.respond(toCoapResponseCode(response.getCode()));
return;
} else {
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
} catch (CodecException e) {
Expand All @@ -307,7 +307,7 @@ public void handleDELETE(CoapExchange coapExchange) {
ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);

DeleteResponse response = nodeEnabler.delete(identity, new DeleteRequest(URI));
coapExchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*******************************************************************************/
package org.eclipse.leshan.client.californium.impl;

import static org.eclipse.leshan.core.californium.ResponseCodeUtil.fromLwM2mCode;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toCoapResponseCode;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
Expand Down Expand Up @@ -47,6 +47,6 @@ public void handleDELETE(CoapExchange exchange) {

ServerIdentity identity = ResourceUtil.extractServerIdentity(exchange, bootstrapHandler);
BootstrapDeleteResponse response = bootstrapHandler.delete(identity, new BootstrapDeleteRequest());
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public static int toLwM2mCode(int coapCode) {
return codeClass * 100 + codeDetail;
}

public static ResponseCode fromCoapCode(int code) {
ResponseCode lwm2mResponseCode = ResponseCode.fromCode(toLwM2mCode(code));
public static ResponseCode toLwM2mResponseCode(int coapCode) {
ResponseCode lwm2mResponseCode = ResponseCode.fromCode(toLwM2mCode(coapCode));
if (lwm2mResponseCode == null)
throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + code);
throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + coapCode);

return lwm2mResponseCode;
}
Expand All @@ -49,12 +49,13 @@ public static int toCoapCode(int lwm2mCode) {
return codeClass << 5 | codeDetail;
}

public static org.eclipse.californium.core.coap.CoAP.ResponseCode fromLwM2mCode(ResponseCode code) {
Validate.notNull(code);
public static org.eclipse.californium.core.coap.CoAP.ResponseCode toCoapResponseCode(
ResponseCode Lwm2mResponseCode) {
Validate.notNull(Lwm2mResponseCode);
try {
return org.eclipse.californium.core.coap.CoAP.ResponseCode.valueOf(toCoapCode(code.getCode()));
return org.eclipse.californium.core.coap.CoAP.ResponseCode.valueOf(toCoapCode(Lwm2mResponseCode.getCode()));
} catch (MessageFormatException e) {
throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + code);
throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + Lwm2mResponseCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

import static org.eclipse.leshan.core.californium.ResponseCodeUtil.fromLwM2mCode;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toCoapResponseCode;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
Expand Down Expand Up @@ -81,9 +81,9 @@ public void handlePOST(CoapExchange exchange) {
// handle bootstrap request
BootstrapResponse response = bootstrapHandler.bootstrap(clientIdentity, new BootstrapRequest(endpoint));
if (response.isSuccess()) {
exchange.respond(fromLwM2mCode(response.getCode()));
exchange.respond(toCoapResponseCode(response.getCode()));
} else {
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

import static org.eclipse.leshan.core.californium.ResponseCodeUtil.fromCoapCode;
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toLwM2mResponseCode;

import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.Request;
Expand Down Expand Up @@ -96,7 +96,7 @@ public void visit(ReadRequest request) {
case METHOD_NOT_ALLOWED:
case NOT_ACCEPTABLE:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new ReadResponse(fromCoapCode(coapResponse.getCode().value), null,
lwM2mresponse = new ReadResponse(toLwM2mResponseCode(coapResponse.getCode().value), null,
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand Down Expand Up @@ -124,7 +124,7 @@ public void visit(DiscoverRequest request) {
case UNAUTHORIZED:
case METHOD_NOT_ALLOWED:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new DiscoverResponse(fromCoapCode(coapResponse.getCode().value), null,
lwM2mresponse = new DiscoverResponse(toLwM2mResponseCode(coapResponse.getCode().value), null,
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -144,7 +144,7 @@ public void visit(WriteRequest request) {
case METHOD_NOT_ALLOWED:
case UNSUPPORTED_CONTENT_FORMAT:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new WriteResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new WriteResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -163,7 +163,7 @@ public void visit(WriteAttributesRequest request) {
case UNAUTHORIZED:
case METHOD_NOT_ALLOWED:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new WriteAttributesResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new WriteAttributesResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -182,7 +182,7 @@ public void visit(ExecuteRequest request) {
case NOT_FOUND:
case METHOD_NOT_ALLOWED:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new ExecuteResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new ExecuteResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -204,7 +204,7 @@ public void visit(CreateRequest request) {
case METHOD_NOT_ALLOWED:
case UNSUPPORTED_CONTENT_FORMAT:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new CreateResponse(fromCoapCode(coapResponse.getCode().value), null,
lwM2mresponse = new CreateResponse(toLwM2mResponseCode(coapResponse.getCode().value), null,
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -223,7 +223,7 @@ public void visit(DeleteRequest request) {
case METHOD_NOT_ALLOWED:
case BAD_REQUEST:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new DeleteResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new DeleteResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand Down Expand Up @@ -260,7 +260,7 @@ public void visit(ObserveRequest request) {
case METHOD_NOT_ALLOWED:
case NOT_ACCEPTABLE:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new ObserveResponse(fromCoapCode(coapResponse.getCode().value), null, null, null,
lwM2mresponse = new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode().value), null, null, null,
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -277,7 +277,7 @@ public void visit(BootstrapWriteRequest request) {
case UNSUPPORTED_CONTENT_FORMAT:
case BAD_REQUEST:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new BootstrapWriteResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new BootstrapWriteResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -293,7 +293,7 @@ public void visit(BootstrapDeleteRequest request) {
break;
case BAD_REQUEST:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new BootstrapDeleteResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new BootstrapDeleteResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand All @@ -309,7 +309,7 @@ public void visit(BootstrapFinishRequest request) {
break;
case BAD_REQUEST:
case INTERNAL_SERVER_ERROR:
lwM2mresponse = new BootstrapFinishResponse(fromCoapCode(coapResponse.getCode().value),
lwM2mresponse = new BootstrapFinishResponse(toLwM2mResponseCode(coapResponse.getCode().value),
coapResponse.getPayloadString(), coapResponse);
break;
default:
Expand Down
Loading

0 comments on commit a018896

Please sign in to comment.