use of fi.otavanopisto.muikku.model.users.UserEntityFile in project muikku by otavanopisto.
the class UserEntityFileDAO method create.
public UserEntityFile create(UserEntity userEntity, String identifier, String name, String contentType, byte[] data, UserEntityFileVisibility visibility) {
UserEntityFile userEntityFile = new UserEntityFile();
userEntityFile.setUserEntity(userEntity);
userEntityFile.setIdentifier(identifier);
userEntityFile.setName(name);
userEntityFile.setContentType(contentType);
userEntityFile.setData(data);
userEntityFile.setVisibility(visibility);
userEntityFile.setLastModified(new Date());
getEntityManager().persist(userEntityFile);
return userEntityFile;
}
use of fi.otavanopisto.muikku.model.users.UserEntityFile in project muikku by otavanopisto.
the class UserEntityFileRESTService method getFileContent.
@GET
@Path("/user/{USERENTITYID}/identifier/{IDENTIFIER}")
@RESTPermit(handling = Handling.INLINE)
public Response getFileContent(@PathParam("USERENTITYID") Long userEntityId, @PathParam("IDENTIFIER") String identifier, @Context Request request) {
// Check if the file exists
UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
if (userEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
UserEntityFile userEntityFile = userEntityFileController.findByUserEntityAndIdentifier(userEntity, identifier);
if (userEntityFile == null) {
return Response.status(Status.NOT_FOUND).build();
}
if (userEntityFile.getVisibility() != UserEntityFileVisibility.PUBLIC) {
UserEntity loggedUserEntity = sessionController.getLoggedUserEntity();
if (loggedUserEntity == null) {
return Response.status(Status.NOT_FOUND).build();
} else if (!userEntityFile.getUserEntity().getId().equals(loggedUserEntity.getId())) {
if (userEntityFile.getVisibility() == UserEntityFileVisibility.STAFF) {
EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(loggedUserEntity);
if (environmentUser == null || environmentUser.getRole() == null || environmentUser.getRole().getArchetype() == EnvironmentRoleArchetype.STUDENT) {
return Response.status(Status.NOT_FOUND).build();
}
} else {
return Response.status(Status.NOT_FOUND).build();
}
}
}
// Serve the content
String tagIdentifier = String.format("%d-%s-%d", userEntityFile.getUserEntity().getId(), userEntityFile.getIdentifier(), userEntityFile.getLastModified().getTime());
EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(tagIdentifier)));
ResponseBuilder builder = request.evaluatePreconditions(tag);
if (builder != null) {
return builder.build();
}
CacheControl cacheControl = new CacheControl();
cacheControl.setMustRevalidate(true);
byte[] data = userEntityFile.getData();
return Response.ok(data).cacheControl(cacheControl).tag(tag).header("Content-Length", data.length).header("Content-Disposition", String.format("attachment; filename=\"%s\"", userEntityFile.getName())).type(userEntityFile.getContentType()).build();
}
use of fi.otavanopisto.muikku.model.users.UserEntityFile in project muikku by otavanopisto.
the class UserEntityFileController method storeUserEntityFile.
public UserEntityFile storeUserEntityFile(String identifier, String name, String contentType, byte[] data, UserEntityFileVisibility visibility) {
UserEntity userEntity = sessionController.getLoggedUserEntity();
UserEntityFile userEntityFile = userEntityFileDAO.findByUserEntityAndIdentifier(userEntity, identifier);
if (userEntityFile == null) {
userEntityFile = userEntityFileDAO.create(userEntity, identifier, name, contentType, data, visibility);
} else {
userEntityFile = userEntityFileDAO.updateData(userEntityFile, identifier, name, contentType, data, visibility);
}
return userEntityFile;
}
use of fi.otavanopisto.muikku.model.users.UserEntityFile in project muikku by otavanopisto.
the class UserEntityFileRESTService method createUserEntityFile.
@POST
@Path("/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createUserEntityFile(RestUserEntityFile entity) {
if (StringUtils.isBlank(entity.getContentType())) {
return Response.status(Status.BAD_REQUEST).entity("contentType is missing").build();
}
if (StringUtils.isBlank(entity.getFileId()) && StringUtils.isBlank(entity.getBase64Data())) {
return Response.status(Status.BAD_REQUEST).entity("fileId or base64Data is missing").build();
}
if (StringUtils.isBlank(entity.getIdentifier())) {
return Response.status(Status.BAD_REQUEST).entity("identifier is missing").build();
}
if (StringUtils.isBlank(entity.getName())) {
return Response.status(Status.BAD_REQUEST).entity("name is missing").build();
}
if (entity.getVisibility() == null) {
return Response.status(Status.BAD_REQUEST).entity("visibility is missing").build();
}
// File data (fileId)
byte[] content = null;
if (StringUtils.isNotBlank(entity.getFileId())) {
try {
content = TempFileUtils.getTempFileData(entity.getFileId());
} catch (IOException e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
} else if (StringUtils.isNotBlank(entity.getBase64Data())) {
String base64Image = entity.getBase64Data().split(",")[1];
content = DatatypeConverter.parseBase64Binary(base64Image);
}
if (content == null) {
return Response.status(Status.BAD_REQUEST).entity("fileId is invalid").build();
}
// Store file
UserEntityFile userEntityFile = userEntityFileController.storeUserEntityFile(entity.getIdentifier(), entity.getName(), entity.getContentType(), content, entity.getVisibility());
if (userEntityFile == null) {
return Response.status(Status.NOT_FOUND).build();
} else {
return Response.ok(createRestModel(userEntityFile)).build();
}
}
use of fi.otavanopisto.muikku.model.users.UserEntityFile in project muikku by otavanopisto.
the class UserEntityFileDAO method findByUserEntityAndIdentifier.
public UserEntityFile findByUserEntityAndIdentifier(UserEntity userEntity, String identifier) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserEntityFile> criteria = criteriaBuilder.createQuery(UserEntityFile.class);
Root<UserEntityFile> root = criteria.from(UserEntityFile.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(UserEntityFile_.userEntity), userEntity), criteriaBuilder.equal(root.get(UserEntityFile_.identifier), identifier)));
return getSingleResult(entityManager.createQuery(criteria));
}
Aggregations