public static DiscoverResponse internalServerError(String errorMessage) { return new DiscoverResponse(ResponseCode.INTERNAL_SERVER_ERROR, null, errorMessage); } }
@Override public boolean isSuccess() { return getCode() == ResponseCode.CONTENT; }
@Override public synchronized DiscoverResponse discover(ServerIdentity identity, DiscoverRequest request) { if (identity.isLwm2mBootstrapServer()) { // discover is not supported for bootstrap return DiscoverResponse.methodNotAllowed(); } if (id == LwM2mId.SECURITY) { return DiscoverResponse.notFound(); } return doDiscover(request); }
if (response.getCode().isError()) { exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage()); } else { exchange.respond(toCoapResponseCode(response.getCode()), Link.serialize(response.getObjectLinks()), MediaTypeRegistry.APPLICATION_LINK_FORMAT);
@Override public JsonElement serialize(LwM2mResponse src, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); element.addProperty("status", src.getCode().toString()); element.addProperty("valid", src.isValid()); element.addProperty("success", src.isSuccess()); element.addProperty("failure", src.isFailure()); if (typeOfSrc instanceof Class<?>) { if (ReadResponse.class.isAssignableFrom((Class<?>) typeOfSrc)) { element.add("content", context.serialize(((ReadResponse) src).getContent())); } else if (DiscoverResponse.class.isAssignableFrom((Class<?>) typeOfSrc)) { element.add("objectLinks", context.serialize(((DiscoverResponse) src).getObjectLinks())); } } return element; } }
protected DiscoverResponse doDiscover(DiscoverRequest request) { LwM2mPath path = request.getPath(); if (path.isObject()) { // Manage discover on object Link[] ObjectLinks = LinkFormatHelper.getObjectDescription(this, null); return DiscoverResponse.success(ObjectLinks); } else if (path.isObjectInstance()) { // Manage discover on instance if (!getAvailableInstanceIds().contains(path.getObjectInstanceId())) return DiscoverResponse.notFound(); Link[] instanceLink = LinkFormatHelper.getInstanceDescription(this, path.getObjectInstanceId(), null); return DiscoverResponse.success(instanceLink); } else if (path.isResource()) { // Manage discover on resource if (!getAvailableInstanceIds().contains(path.getObjectInstanceId())) return DiscoverResponse.notFound(); ResourceModel resourceModel = getObjectModel().resources.get(path.getResourceId()); if (resourceModel == null) return DiscoverResponse.notFound(); if (!getAvailableResourceIds(path.getObjectInstanceId()).contains(path.getResourceId())) return DiscoverResponse.notFound(); Link resourceLink = LinkFormatHelper.getResourceDescription(this, path.getObjectInstanceId(), path.getResourceId(), null); return DiscoverResponse.success(new Link[] { resourceLink }); } return DiscoverResponse.badRequest(null); }
public static JsonObject jSerialize(LwM2mResponse r) { final JsonObject o = Json.object(); o.add("code", r.getCode().toString()); if (r.isFailure()) { o.add("errorMessage", r.getErrorMessage()); return o; } if (r instanceof ReadResponse) { o.add("kind", "read"); o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent())); } else if (r instanceof ObserveResponse) { o.add("kind", "observe"); o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent())); } else if (r instanceof DiscoverResponse) { o.add("kind", "discover"); o.add("objectLinks", Link.serialize(((DiscoverResponse) r).getObjectLinks())); } else if (r instanceof DeleteResponse) { o.add("kind", "delete"); } else if (r instanceof ExecuteResponse) { o.add("kind", "execute"); } else if (r instanceof WriteResponse) { o.add("kind", "write"); } else if (r instanceof WriteAttributesResponse) { o.add("kind", "writeAttributes"); } else if (r instanceof CreateResponse) { o.add("kind", "create"); o.add("location", ((CreateResponse) r).getLocation()); } return o; }
public static DiscoverResponse notFound() { return new DiscoverResponse(ResponseCode.NOT_FOUND, null, null); }
public static JsonObject jSerialize(LwM2mResponse r) { final JsonObject o = Json.object(); o.add("code", r.getCode().toString()); if (r.isFailure()) { o.add("errorMessage", r.getErrorMessage()); return o; } if (r instanceof ReadResponse) { o.add("kind", "read"); o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent())); } else if (r instanceof ObserveResponse) { o.add("kind", "observe"); o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent())); } else if (r instanceof DiscoverResponse) { o.add("kind", "discover"); o.add("objectLinks", Link.serialize(((DiscoverResponse) r).getObjectLinks())); } else if (r instanceof DeleteResponse) { o.add("kind", "delete"); } else if (r instanceof ExecuteResponse) { o.add("kind", "execute"); } else if (r instanceof WriteResponse) { o.add("kind", "write"); } else if (r instanceof WriteAttributesResponse) { o.add("kind", "writeAttributes"); } else if (r instanceof CreateResponse) { o.add("kind", "create"); o.add("location", ((CreateResponse) r).getLocation()); } return o; }
public static DiscoverResponse methodNotAllowed() { return new DiscoverResponse(ResponseCode.METHOD_NOT_ALLOWED, null, null); }
public static DiscoverResponse success(Link[] links) { return new DiscoverResponse(ResponseCode.CONTENT, links, null); }
public static DiscoverResponse badRequest(String errorMessage) { return new DiscoverResponse(ResponseCode.BAD_REQUEST, null, errorMessage); }
public static DiscoverResponse unauthorized() { return new DiscoverResponse(ResponseCode.UNAUTHORIZED, null, null); }
@Override public void visit(DiscoverRequest request) { if (coapResponse.isError()) { // handle error response: lwM2mresponse = new DiscoverResponse(toLwM2mResponseCode(coapResponse.getCode()), null, coapResponse.getPayloadString(), coapResponse); } else if (coapResponse.getCode() == org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT) { // handle success response: Link[] links; if (MediaTypeRegistry.APPLICATION_LINK_FORMAT != coapResponse.getOptions().getContentFormat()) { LOG.debug("Expected LWM2M Client [{}] to return application/link-format [{}] content but got [{}]", registration.getEndpoint(), MediaTypeRegistry.APPLICATION_LINK_FORMAT, coapResponse.getOptions().getContentFormat()); links = new Link[] {}; // empty list } else { links = Link.parse(coapResponse.getPayload()); } lwM2mresponse = new DiscoverResponse(ResponseCode.CONTENT, links, null, coapResponse); } else { // handle unexpected response: handleUnexpectedResponseCode(registration.getEndpoint(), request, coapResponse); } }
@Override public void visit(final DiscoverRequest request) { switch (coapResponse.getCode()) { case CONTENT: LinkObject[] links = null; if (MediaTypeRegistry.APPLICATION_LINK_FORMAT != coapResponse.getOptions().getContentFormat()) { LOG.debug("Expected LWM2M Client [{}] to return application/link-format [{}] content but got [{}]", client.getEndpoint(), MediaTypeRegistry.APPLICATION_LINK_FORMAT, coapResponse.getOptions() .getContentFormat()); links = new LinkObject[] {}; // empty list } else { links = LinkObject.parse(coapResponse.getPayload()); } lwM2mresponse = new DiscoverResponse(fromCoapCode(coapResponse.getCode().value), links); break; case NOT_FOUND: case UNAUTHORIZED: case METHOD_NOT_ALLOWED: lwM2mresponse = new DiscoverResponse(fromCoapCode(coapResponse.getCode().value)); break; default: handleUnexpectedResponseCode(client.getEndpoint(), coapRequest, coapResponse); } }
case "discover": String objectLinks = o.getString("objectLinks", ""); return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage); case "create": { String location = o.getString("location", null);
case "discover": String objectLinks = o.getString("objectLinks", ""); return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage); case "create": { String location = o.getString("location", null);