Search in sources :

Example 1 with CoOpsNotImplementedException

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

the class CoOpsApiImpl method fileJoin.

public Join fileJoin(String fileId, List<String> algorithms, String protocolVersion) throws CoOpsNotFoundException, CoOpsNotImplementedException, CoOpsInternalErrorException, CoOpsForbiddenException, CoOpsUsageException {
    HtmlMaterial htmlMaterial = findFile(fileId);
    if (!COOPS_PROTOCOL_VERSION.equals(protocolVersion)) {
        throw new CoOpsNotImplementedException("Protocol version mismatch. Client is using " + protocolVersion + " and server " + COOPS_PROTOCOL_VERSION);
    }
    if (algorithms == null || algorithms.isEmpty()) {
        throw new CoOpsInternalErrorException("Invalid request");
    }
    CoOpsDiffAlgorithm algorithm = htmlMaterialController.findAlgorithm(algorithms);
    if (algorithm == null) {
        throw new CoOpsNotImplementedException("Server and client do not have a commonly supported algorithm.");
    }
    Long currentRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
    String data = htmlMaterialController.getRevisionHtml(htmlMaterial, currentRevision);
    if (data == null) {
        data = "";
    }
    Map<String, String> properties = htmlMaterialController.getRevisionProperties(htmlMaterial, currentRevision);
    // TODO: Extension properties...
    Map<String, Object> extensions = new HashMap<>();
    String sessionId = UUID.randomUUID().toString();
    CoOpsSession coOpsSession = coOpsSessionController.createSession(htmlMaterial, sessionController.getLoggedUserEntity(), sessionId, currentRevision, algorithm.getName());
    addSessionEventsExtension(htmlMaterial, extensions);
    addWebSocketExtension(htmlMaterial, extensions, coOpsSession);
    return new Join(coOpsSession.getSessionId(), coOpsSession.getAlgorithm(), coOpsSession.getJoinRevision(), data, htmlMaterial.getContentType(), properties, extensions);
}
Also used : CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) HashMap(java.util.HashMap) Join(fi.foyt.coops.model.Join) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 2 with CoOpsNotImplementedException

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

use of fi.foyt.coops.CoOpsNotImplementedException 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 4 with CoOpsNotImplementedException

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

CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)4 CoOpsNotImplementedException (fi.foyt.coops.CoOpsNotImplementedException)4 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)4 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)3 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)3 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)3 File (fi.foyt.coops.model.File)3 Path (javax.ws.rs.Path)3 WorkspaceMaterialContainsAnswersExeption (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 Join (fi.foyt.coops.model.Join)1 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 CacheControl (javax.ws.rs.core.CacheControl)1 EntityTag (javax.ws.rs.core.EntityTag)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1