Search in sources :

Example 1 with CoOpsForbiddenException

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

the class HtmlMaterialRESTService method publishMaterial.

@POST
@Path("/{id}/publish/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response publishMaterial(@PathParam("id") Long id, HtmlRestMaterialPublish entity) {
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIALS)) {
        return Response.status(Status.FORBIDDEN).entity("Permission denied").build();
    }
    HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
    if (htmlMaterial == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!htmlMaterial.getRevisionNumber().equals(entity.getFromRevision())) {
        return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONCURRENT_MODIFICATIONS)).build();
    }
    try {
        File fileRevision = coOpsApi.fileGet(id.toString(), entity.getToRevision());
        if (fileRevision == null) {
            return Response.status(Status.NOT_FOUND).build();
        }
        htmlMaterialController.updateHtmlMaterialToRevision(htmlMaterial, fileRevision.getContent(), entity.getToRevision(), false, entity.getRemoveAnswers() != null ? entity.getRemoveAnswers() : false);
    } catch (WorkspaceMaterialContainsAnswersExeption e) {
        return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONTAINS_ANSWERS)).build();
    } catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    return Response.noContent().build();
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) File(fi.foyt.coops.model.File) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 2 with CoOpsForbiddenException

use of fi.foyt.coops.CoOpsForbiddenException 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 3 with CoOpsForbiddenException

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

Example 4 with CoOpsForbiddenException

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

the class HtmlMaterialRESTService method revertMaterial.

@PUT
@Path("/{id}/revert/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response revertMaterial(@PathParam("id") Long id, HtmlRestMaterialRevert entity) {
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIALS)) {
        return Response.status(Status.FORBIDDEN).entity("Permission denied").build();
    }
    HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
    if (htmlMaterial == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    Long currentRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
    if (!currentRevision.equals(entity.getFromRevision())) {
        return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONCURRENT_MODIFICATIONS)).build();
    }
    try {
        File fileRevision = coOpsApi.fileGet(id.toString(), entity.getToRevision());
        if (fileRevision == null) {
            return Response.status(Status.NOT_FOUND).entity("Specified revision could not be found").build();
        }
        htmlMaterialController.updateHtmlMaterialToRevision(htmlMaterial, fileRevision.getContent(), entity.getToRevision(), true, entity.getRemoveAnswers() != null ? entity.getRemoveAnswers() : false);
    } catch (WorkspaceMaterialContainsAnswersExeption e) {
        return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONTAINS_ANSWERS)).build();
    } catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    return Response.noContent().build();
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) File(fi.foyt.coops.model.File) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) PUT(javax.ws.rs.PUT)

Example 5 with CoOpsForbiddenException

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

the class HtmlMaterialRESTService method findMaterial.

@GET
@Path("/{id}")
@RESTPermitUnimplemented
public Response findMaterial(@PathParam("id") Long id, @QueryParam("revision") Long revision, @Context Request request) {
    HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
    if (htmlMaterial == null) {
        return Response.status(Status.NOT_FOUND).build();
    } else {
        EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(revision == null ? htmlMaterial.getRevisionNumber() : revision)));
        ResponseBuilder builder = request.evaluatePreconditions(tag);
        if (builder != null) {
            return builder.build();
        }
        CacheControl cacheControl = new CacheControl();
        cacheControl.setMustRevalidate(true);
        if (revision == null) {
            return Response.ok(createRestModel(htmlMaterial)).build();
        } else {
            File fileRevision;
            try {
                fileRevision = coOpsApi.fileGet(id.toString(), revision);
            } catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
            }
            if (fileRevision == null) {
                return Response.status(Status.NOT_FOUND).build();
            }
            return Response.ok(createRestModel(htmlMaterial, fileRevision)).build();
        }
    }
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) EntityTag(javax.ws.rs.core.EntityTag) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) File(fi.foyt.coops.model.File) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Aggregations

CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)5 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)5 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)5 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)5 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)4 CoOpsNotImplementedException (fi.foyt.coops.CoOpsNotImplementedException)3 File (fi.foyt.coops.model.File)3 Path (javax.ws.rs.Path)3 ErrorMessage (fi.foyt.coops.extensions.websocket.ErrorMessage)2 Patch (fi.foyt.coops.model.Patch)2 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)2 WorkspaceMaterialContainsAnswersExeption (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 CloseReason (javax.websocket.CloseReason)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)1 PatchMessage (fi.foyt.coops.extensions.websocket.PatchMessage)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 IOException (java.io.IOException)1 OnMessage (javax.websocket.OnMessage)1