Search in sources :

Example 36 with ReadResponse

use of org.eclipse.leshan.core.response.ReadResponse in project leshan by eclipse.

the class ClientServlet method doGet.

/**
 * {@inheritDoc}
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // all registered clients
    if (req.getPathInfo() == null) {
        Collection<Registration> registrations = new ArrayList<>();
        for (Iterator<Registration> iterator = server.getRegistrationService().getAllRegistrations(); iterator.hasNext(); ) {
            registrations.add(iterator.next());
        }
        String json = this.gson.toJson(registrations.toArray(new Registration[] {}));
        resp.setContentType("application/json");
        resp.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        resp.setStatus(HttpServletResponse.SC_OK);
        return;
    }
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    if (path.length < 1) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
        return;
    }
    String clientEndpoint = path[0];
    // /endPoint : get client
    if (path.length == 1) {
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            resp.setContentType("application/json");
            resp.getOutputStream().write(this.gson.toJson(registration).getBytes(StandardCharsets.UTF_8));
            resp.setStatus(HttpServletResponse.SC_OK);
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
        }
        return;
    }
    // /clients/endPoint/LWRequest/discover : do LightWeight M2M discover request on a given client.
    if (path.length >= 3 && "discover".equals(path[path.length - 1])) {
        String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/discover");
        try {
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                // create & process request
                DiscoverRequest request = new DiscoverRequest(target);
                DiscoverResponse cResponse = server.send(registration, request, TIMEOUT);
                processDeviceResponse(req, resp, cResponse);
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("No registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException | InterruptedException e) {
            handleException(e, resp);
        }
        return;
    }
    // /clients/endPoint/LWRequest : do LightWeight M2M read request on a given client.
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            // get content format
            String contentFormatParam = req.getParameter(FORMAT_PARAM);
            ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
            // create & process request
            ReadRequest request = new ReadRequest(contentFormat, target);
            ReadResponse cResponse = server.send(registration, request, TIMEOUT);
            processDeviceResponse(req, resp, cResponse);
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().format("No registered client with id '%s'", clientEndpoint).flush();
        }
    } catch (RuntimeException | InterruptedException e) {
        handleException(e, resp);
    }
}
Also used : ContentFormat(org.eclipse.leshan.core.request.ContentFormat) ArrayList(java.util.ArrayList) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) Registration(org.eclipse.leshan.server.registration.Registration) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 37 with ReadResponse

use of org.eclipse.leshan.core.response.ReadResponse in project leshan by eclipse.

the class ResponseSerDes method deserialize.

public static LwM2mResponse deserialize(JsonObject o) {
    String sCode = o.getString("code", null);
    if (sCode == null)
        throw new IllegalStateException("Invalid response missing code attribute");
    ResponseCode code = ResponseCode.fromName(sCode);
    String errorMessage = o.getString("errorMessage", null);
    String kind = o.getString("kind", null);
    switch(kind) {
        case "observe":
            {
                // TODO ser Observation
                LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
                return new ObserveResponse(code, content, null, null, errorMessage);
            }
        case "delete":
            return new DeleteResponse(code, errorMessage);
        case "discover":
            String objectLinks = o.getString("objectLinks", "");
            return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage);
        case "create":
            {
                String location = o.getString("location", null);
                return new CreateResponse(code, location, errorMessage);
            }
        case "execute":
            return new ExecuteResponse(code, errorMessage);
        case "writeAttributes":
            {
                return new WriteAttributesResponse(code, errorMessage);
            }
        case "write":
            {
                return new WriteResponse(code, errorMessage);
            }
        case "read":
            {
                LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
                return new ReadResponse(code, content, errorMessage);
            }
        default:
            throw new IllegalStateException("Invalid response missing kind attribute");
    }
}
Also used : ResponseCode(org.eclipse.leshan.ResponseCode) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse)

Aggregations

ReadResponse (org.eclipse.leshan.core.response.ReadResponse)37 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)34 Test (org.junit.Test)25 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)14 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)14 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)11 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)7 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)5 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)5 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)4 TimeoutException (org.eclipse.leshan.core.request.exception.TimeoutException)4 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)4 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)3 JsonObject (com.eclipsesource.json.JsonObject)2 ArrayList (java.util.ArrayList)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 LwM2mSingleResource (org.eclipse.leshan.core.node.LwM2mSingleResource)2 Observation (org.eclipse.leshan.core.observation.Observation)2