Search in sources :

Example 1 with Patch

use of fi.foyt.coops.model.Patch in project muikku by otavanopisto.

the class CoOpsApiImpl method filePatch.

public void filePatch(String fileId, String sessionId, Long revisionNumber, String patch, Map<String, String> properties, Map<String, Object> extensions) throws CoOpsInternalErrorException, CoOpsUsageException, CoOpsNotFoundException, CoOpsConflictException, CoOpsForbiddenException {
    CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
    if (session == null) {
        throw new CoOpsUsageException("Invalid session id");
    }
    CoOpsDiffAlgorithm algorithm = htmlMaterialController.findAlgorithm(session.getAlgorithm());
    if (algorithm == null) {
        throw new CoOpsUsageException("Algorithm is not supported by this server");
    }
    HtmlMaterial htmlMaterial = findFile(fileId);
    Long maxRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
    if (!maxRevision.equals(revisionNumber)) {
        throw new CoOpsConflictException();
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String checksum = null;
    if (StringUtils.isNotBlank(patch)) {
        String data = htmlMaterialController.getRevisionHtml(htmlMaterial, maxRevision);
        if (data == null) {
            data = "";
        }
        String patched = algorithm.patch(data, patch);
        checksum = DigestUtils.md5Hex(patched);
    }
    Long patchRevisionNumber = maxRevision + 1;
    HtmlMaterialRevision htmlMaterialRevision = htmlMaterialController.createRevision(htmlMaterial, sessionId, patchRevisionNumber, new Date(), patch, checksum);
    if (properties != null) {
        for (String key : properties.keySet()) {
            String value = properties.get(key);
            htmlMaterialController.createRevisionProperty(htmlMaterialRevision, key, value);
        }
    }
    if (extensions != null) {
        for (String key : extensions.keySet()) {
            String value;
            try {
                value = objectMapper.writeValueAsString(extensions.get(key));
            } catch (IOException e) {
                throw new CoOpsInternalErrorException(e);
            }
            htmlMaterialController.createRevisionExtensionProperty(htmlMaterialRevision, key, value);
        }
    }
    patchEvent.fire(new CoOpsPatchEvent(fileId, new Patch(sessionId, patchRevisionNumber, checksum, patch, properties, extensions)));
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) IOException(java.io.IOException) Date(java.util.Date) CoOpsPatchEvent(fi.otavanopisto.muikku.plugins.material.coops.event.CoOpsPatchEvent) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Patch(fi.foyt.coops.model.Patch) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with Patch

use of fi.foyt.coops.model.Patch in project muikku by otavanopisto.

the class CoOpsApiImpl method fileUpdate.

public List<Patch> fileUpdate(String fileId, String sessionId, Long revisionNumber) throws CoOpsNotFoundException, CoOpsInternalErrorException, CoOpsUsageException, CoOpsForbiddenException {
    CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
    if (session == null) {
        throw new CoOpsUsageException("Invalid session id");
    }
    if (revisionNumber == null) {
        throw new CoOpsUsageException("revisionNumber parameter is missing");
    }
    HtmlMaterial htmlMaterial = findFile(fileId);
    if (htmlMaterial == null) {
        throw new CoOpsNotFoundException();
    }
    List<Patch> updateResults = new ArrayList<>();
    List<HtmlMaterialRevision> htmlMaterialRevisions = htmlMaterialController.listRevisionsAfter(htmlMaterial, revisionNumber);
    if (!htmlMaterialRevisions.isEmpty()) {
        for (HtmlMaterialRevision htmlMaterialRevision : htmlMaterialRevisions) {
            String patch = htmlMaterialRevision.getData();
            Map<String, String> properties = null;
            Map<String, Object> extensions = null;
            List<HtmlMaterialRevisionProperty> revisionProperties = htmlMaterialController.listRevisionProperties(htmlMaterialRevision);
            if (revisionProperties.size() > 0) {
                properties = new HashMap<>();
                for (HtmlMaterialRevisionProperty revisionProperty : revisionProperties) {
                    properties.put(revisionProperty.getKey(), revisionProperty.getValue());
                }
            }
            List<HtmlMaterialRevisionExtensionProperty> revisionExtensionProperties = htmlMaterialController.listRevisionExtensionProperties(htmlMaterialRevision);
            if (revisionExtensionProperties.size() > 0) {
                extensions = new HashMap<>();
                for (HtmlMaterialRevisionExtensionProperty revisionExtensionProperty : revisionExtensionProperties) {
                    extensions.put(revisionExtensionProperty.getKey(), revisionExtensionProperty.getValue());
                }
            }
            if (patch != null) {
                updateResults.add(new Patch(htmlMaterialRevision.getSessionId(), htmlMaterialRevision.getRevision(), htmlMaterialRevision.getChecksum(), patch, properties, extensions));
            } else {
                updateResults.add(new Patch(htmlMaterialRevision.getSessionId(), htmlMaterialRevision.getRevision(), null, null, properties, extensions));
            }
        }
    }
    return updateResults;
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) ArrayList(java.util.ArrayList) HtmlMaterialRevisionProperty(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionProperty) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) HtmlMaterialRevisionExtensionProperty(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionExtensionProperty) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Patch(fi.foyt.coops.model.Patch)

Example 3 with Patch

use of fi.foyt.coops.model.Patch 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)

Example 4 with Patch

use of fi.foyt.coops.model.Patch in project muikku by otavanopisto.

the class CoOpsDocumentWebSocket method onOpen.

@OnOpen
public void onOpen(final Session client, EndpointConfig endpointConfig, @PathParam("HTMLMATERIALID") String htmlMaterialId, @PathParam("SESSIONID") String sessionId) throws IOException {
    synchronized (this) {
        // 
        // TODO: RequestScope is not available on the websockets, switch to ticket system
        // 
        // if (!sessionController.isLoggedIn()) {
        // client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Permission denied"));
        // }
        // 
        // UserEntity userEntity = sessionController.getLoggedUserEntity();
        // 
        // EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
        // 
        // if (environmentUser.getRole() == null || environmentUser.getRole().getArchetype() == EnvironmentRoleArchetype.STUDENT) {
        // client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Permission denied"));
        // }
        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(htmlMaterialId))) {
            client.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Session is associated with another fileId"));
            return;
        }
        Map<String, Session> sessions = fileClients.get(htmlMaterialId);
        if (sessions == null) {
            fileClients.put(htmlMaterialId, new HashMap<String, Session>());
        }
        fileClients.get(htmlMaterialId).put(client.getId(), client);
        coOpsSessionController.updateSessionType(session, CoOpsSessionType.WS);
        HtmlMaterial htmlMaterial = session.getHtmlMaterial();
        Long currentRevisionNumber = htmlMaterial.getRevisionNumber();
        if (session.getJoinRevision() < currentRevisionNumber) {
            ObjectMapper objectMapper = new ObjectMapper();
            List<Patch> patches;
            try {
                patches = coOpsApi.fileUpdate(session.getHtmlMaterial().getId().toString(), session.getSessionId(), session.getJoinRevision());
                for (Patch patch : patches) {
                    sendPatch(client, patch);
                }
            } 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 (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) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) CloseReason(javax.websocket.CloseReason) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) ErrorMessage(fi.foyt.coops.extensions.websocket.ErrorMessage) Patch(fi.foyt.coops.model.Patch) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) Session(javax.websocket.Session) OnOpen(javax.websocket.OnOpen)

Aggregations

CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)4 Patch (fi.foyt.coops.model.Patch)4 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)4 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)3 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)3 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)2 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)2 ErrorMessage (fi.foyt.coops.extensions.websocket.ErrorMessage)2 HtmlMaterialRevision (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision)2 IOException (java.io.IOException)2 CloseReason (javax.websocket.CloseReason)2 PatchMessage (fi.foyt.coops.extensions.websocket.PatchMessage)1 CoOpsPatchEvent (fi.otavanopisto.muikku.plugins.material.coops.event.CoOpsPatchEvent)1 HtmlMaterialRevisionExtensionProperty (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionExtensionProperty)1 HtmlMaterialRevisionProperty (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionProperty)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 OnMessage (javax.websocket.OnMessage)1