Search in sources :

Example 11 with BinaryMaterial

use of fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial in project muikku by otavanopisto.

the class BinaryMaterialRESTService method createMaterial.

@POST
@Path("/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createMaterial(BinaryRestMaterial entity) {
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIALS)) {
        return Response.status(Status.FORBIDDEN).entity("Permission denied").build();
    }
    if (StringUtils.isBlank(entity.getContentType())) {
        return Response.status(Status.BAD_REQUEST).entity("contentType is missing").build();
    }
    if (StringUtils.isBlank(entity.getTitle())) {
        return Response.status(Status.BAD_REQUEST).entity("title is missing").build();
    }
    if (StringUtils.isBlank(entity.getFileId())) {
        return Response.status(Status.BAD_REQUEST).entity("fileId is missing").build();
    }
    byte[] content = null;
    try {
        content = TempFileUtils.getTempFileData(entity.getFileId());
    } catch (IOException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
    if (content == null) {
        return Response.status(Status.BAD_REQUEST).entity("fileId is invalid").build();
    }
    BinaryMaterial material = binaryMaterialController.createBinaryMaterial(entity.getTitle(), entity.getContentType(), content, entity.getLicense());
    if (material == null) {
        return Response.status(Status.NOT_FOUND).build();
    } else {
        return Response.ok(createRestModel(material)).build();
    }
}
Also used : IOException(java.io.IOException) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 12 with BinaryMaterial

use of fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial in project muikku by otavanopisto.

the class BinaryMaterialRESTService method getMaterialContent.

@GET
@Path("/{id}/content")
@RESTPermitUnimplemented
public Response getMaterialContent(@PathParam("id") Long id, @Context Request request) {
    BinaryMaterial material = binaryMaterialController.findBinaryMaterialById(id);
    if (material == null) {
        return Response.status(Status.NOT_FOUND).build();
    } else {
        EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(material.getVersion())));
        ResponseBuilder builder = request.evaluatePreconditions(tag);
        if (builder != null) {
            return builder.build();
        }
        CacheControl cacheControl = new CacheControl();
        cacheControl.setMustRevalidate(true);
        return Response.ok(material.getContent()).cacheControl(cacheControl).tag(tag).type(material.getContentType()).build();
    }
}
Also used : EntityTag(javax.ws.rs.core.EntityTag) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Aggregations

BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)12 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)5 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)3 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)2 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Path (javax.ws.rs.Path)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 BinaryMaterialCreateEvent (fi.otavanopisto.muikku.plugins.material.events.BinaryMaterialCreateEvent)1 BinaryMaterialUpdateEvent (fi.otavanopisto.muikku.plugins.material.events.BinaryMaterialUpdateEvent)1 Material (fi.otavanopisto.muikku.plugins.material.model.Material)1 MaterialMeta (fi.otavanopisto.muikku.plugins.material.model.MaterialMeta)1 MaterialProducer (fi.otavanopisto.muikku.plugins.material.model.MaterialProducer)1 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)1 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)1