Search in sources :

Example 1 with PatchMessage

use of fi.foyt.coops.extensions.websocket.PatchMessage in project muikku by otavanopisto.

the class CoOpsDocumentWebSocket method onMessage.

@OnMessage
public void onMessage(Reader messageReader, Session client, @PathParam("HTMLMATERIALID") String fileId, @PathParam("SESSIONID") String sessionId) throws IOException {
    CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
    if (session == null) {
        client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Not Found"));
        return;
    }
    if (!session.getHtmlMaterial().getId().equals(NumberUtils.createLong(fileId))) {
        client.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Session is associated with another fileId"));
        return;
    }
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        PatchMessage patchMessage;
        try {
            patchMessage = objectMapper.readValue(messageReader, PatchMessage.class);
        } catch (IOException e) {
            throw new CoOpsInternalErrorException(e);
        }
        if (patchMessage == null) {
            throw new CoOpsInternalErrorException("Could not parse message");
        }
        if (!patchMessage.getType().equals("patch")) {
            throw new CoOpsInternalErrorException("Unknown message type: " + patchMessage.getType());
        }
        Patch patch = patchMessage.getData();
        coOpsApi.filePatch(fileId, patch.getSessionId(), patch.getRevisionNumber(), patch.getPatch(), patch.getProperties(), patch.getExtensions());
    } catch (CoOpsInternalErrorException e) {
        client.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Internal Error"));
    } catch (CoOpsUsageException e) {
        client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 400, e.getMessage())));
    } catch (CoOpsNotFoundException e) {
        client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 404, e.getMessage())));
    } catch (CoOpsConflictException e) {
        client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchRejected", 409, "Conflict")));
    } catch (CoOpsForbiddenException e) {
        client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 500, e.getMessage())));
    }
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) CloseReason(javax.websocket.CloseReason) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) PatchMessage(fi.foyt.coops.extensions.websocket.PatchMessage) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) IOException(java.io.IOException) ErrorMessage(fi.foyt.coops.extensions.websocket.ErrorMessage) Patch(fi.foyt.coops.model.Patch) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) OnMessage(javax.websocket.OnMessage)

Aggregations

CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)1 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)1 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)1 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)1 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)1 ErrorMessage (fi.foyt.coops.extensions.websocket.ErrorMessage)1 PatchMessage (fi.foyt.coops.extensions.websocket.PatchMessage)1 Patch (fi.foyt.coops.model.Patch)1 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)1 IOException (java.io.IOException)1 CloseReason (javax.websocket.CloseReason)1 OnMessage (javax.websocket.OnMessage)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1