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();
}
}
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();
}
}
Aggregations