Search in sources :

Example 1 with UserEntityFile

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;
}
Also used : UserEntityFile(fi.otavanopisto.muikku.model.users.UserEntityFile) Date(java.util.Date)

Example 2 with 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();
}
Also used : EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserEntityFile(fi.otavanopisto.muikku.model.users.UserEntityFile) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 3 with UserEntityFile

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;
}
Also used : UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserEntityFile(fi.otavanopisto.muikku.model.users.UserEntityFile)

Example 4 with 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();
    }
}
Also used : IOException(java.io.IOException) UserEntityFile(fi.otavanopisto.muikku.model.users.UserEntityFile) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 5 with UserEntityFile

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));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) UserEntityFile(fi.otavanopisto.muikku.model.users.UserEntityFile)

Aggregations

UserEntityFile (fi.otavanopisto.muikku.model.users.UserEntityFile)5 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 Path (javax.ws.rs.Path)2 EnvironmentUser (fi.otavanopisto.muikku.model.users.EnvironmentUser)1 IOException (java.io.IOException)1 Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 CacheControl (javax.ws.rs.core.CacheControl)1 EntityTag (javax.ws.rs.core.EntityTag)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1