Search in sources :

Example 1 with CoOpsConflictException

use of fi.foyt.coops.CoOpsConflictException 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 CoOpsConflictException

use of fi.foyt.coops.CoOpsConflictException in project muikku by otavanopisto.

the class CoOpsDmpDiffAlgorithm method unpatch.

@Override
public String unpatch(String data, String patch) throws CoOpsConflictException {
    DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
    LinkedList<Patch> patches = createUnpatch(new LinkedList<Patch>(diffMatchPatch.patch_fromText(patch)));
    Object[] patchResult = diffMatchPatch.patch_apply(patches, data);
    for (boolean applied : (boolean[]) patchResult[1]) {
        if (!applied) {
            throw new CoOpsConflictException();
        }
    }
    return (String) patchResult[0];
}
Also used : DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch)

Example 3 with CoOpsConflictException

use of fi.foyt.coops.CoOpsConflictException 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 CoOpsConflictException

use of fi.foyt.coops.CoOpsConflictException in project muikku by otavanopisto.

the class HtmlMaterialController method getRevisionHtml.

public String getRevisionHtml(HtmlMaterial htmlMaterial, long revision) throws CoOpsInternalErrorException {
    String result = htmlMaterial.getHtml();
    if (result == null) {
        result = "";
    }
    long baselineRevision = htmlMaterial.getRevisionNumber();
    CoOpsDiffAlgorithm algorithm = findAlgorithm("dmp");
    if (revision < baselineRevision) {
        List<HtmlMaterialRevision> revisions = htmlMaterialRevisionDAO.listByFileAndRevisionGeAndRevisonLtOrderedByRevision(htmlMaterial, revision, baselineRevision);
        for (int i = revisions.size() - 1; i >= 0; i--) {
            HtmlMaterialRevision patchingRevision = revisions.get(i);
            try {
                if (patchingRevision.getData() != null) {
                    result = algorithm.unpatch(result, patchingRevision.getData());
                }
            } catch (CoOpsConflictException e) {
                throw new CoOpsInternalErrorException("Patch failed when building material revision number " + revision);
            }
        }
    } else {
        List<HtmlMaterialRevision> revisions = htmlMaterialRevisionDAO.listByFileAndRevisionGtAndRevisonLeOrderedByRevision(htmlMaterial, baselineRevision, revision);
        for (HtmlMaterialRevision patchingRevision : revisions) {
            try {
                if (patchingRevision.getData() != null) {
                    result = algorithm.patch(result, patchingRevision.getData());
                }
            } catch (CoOpsConflictException e) {
                throw new CoOpsInternalErrorException("Patch failed when building material revision number " + revision);
            }
        }
    }
    return result;
}
Also used : HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) CoOpsDiffAlgorithm(fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm)

Example 5 with CoOpsConflictException

use of fi.foyt.coops.CoOpsConflictException in project muikku by otavanopisto.

the class CoOpsDmpDiffAlgorithm method patch.

@Override
public String patch(String data, String patch) throws CoOpsConflictException {
    DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
    LinkedList<Patch> patches = new LinkedList<Patch>(diffMatchPatch.patch_fromText(patch));
    Object[] patchResult = diffMatchPatch.patch_apply(patches, data);
    for (boolean applied : (boolean[]) patchResult[1]) {
        if (!applied) {
            throw new CoOpsConflictException();
        }
    }
    return (String) patchResult[0];
}
Also used : DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch) LinkedList(java.util.LinkedList)

Aggregations

CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)5 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)3 DiffMatchPatch (com.sksamuel.diffpatch.DiffMatchPatch)2 Patch (com.sksamuel.diffpatch.DiffMatchPatch.Patch)2 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)2 Patch (fi.foyt.coops.model.Patch)2 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)2 HtmlMaterialRevision (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision)2 IOException (java.io.IOException)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)1 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)1 ErrorMessage (fi.foyt.coops.extensions.websocket.ErrorMessage)1 PatchMessage (fi.foyt.coops.extensions.websocket.PatchMessage)1 CoOpsDiffAlgorithm (fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm)1 CoOpsPatchEvent (fi.otavanopisto.muikku.plugins.material.coops.event.CoOpsPatchEvent)1 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 CloseReason (javax.websocket.CloseReason)1