Search in sources :

Example 6 with File

use of org.survey.model.file.File in project survey by markoniemi.

the class FileServiceImpl method downloadFile.

@Override
public // @Produces("application/pdf")
Response downloadFile(Long id) {
    File file = fileRepository.findOne(id);
    // java.io.File file = new java.io.File("test.pdf");
    ResponseBuilder response = Response.ok((Object) file, file.getMimeType());
    response.header("Content-Disposition", "attachment; filename=" + file.getFilename());
    return response.build();
}
Also used : ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) File(org.survey.model.file.File)

Example 7 with File

use of org.survey.model.file.File in project survey by markoniemi.

the class FileServiceImpl method uploadFile.

@Override
public // HttpServletRequest request) {
void uploadFile(List<Attachment> attachments) {
    for (Attachment attachment : attachments) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            MultivaluedMap<String, String> map = attachment.getHeaders();
            String fileName = getFileName(map);
            log.debug("uploadFile: {}", fileName);
            if (StringUtils.isNotEmpty(fileName)) {
                DataHandler handler = attachment.getDataHandler();
                InputStream inputStream = handler.getInputStream();
                IOUtils.copy(inputStream, outputStream);
                File file = createFile(fileName, "mimeType", "owner", outputStream.toByteArray());
                // TODO move to finally block
                inputStream.close();
                outputStream.flush();
                outputStream.close();
                // TODO for some reason there is an extra file with 0 size
                if (file.getContent().length > 0) {
                    fileRepository.save(file);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
// return Response.ok("file uploaded").build();
}
Also used : InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataHandler(javax.activation.DataHandler) File(org.survey.model.file.File)

Example 8 with File

use of org.survey.model.file.File in project survey by markoniemi.

the class FileServiceImpl method createFile.

private File createFile(String filename, String mimeType, String owner, byte[] fileContent) {
    File file = new File();
    file.setFilename(filename);
    file.setMimeType(mimeType);
    file.setContent(fileContent);
    // file.setOwner(owner);
    file.setCreateTime(System.currentTimeMillis());
    // TODO change files rest to files/:user/:filename
    file.setUrl("/survey-web/api/rest/files/");
    return file;
}
Also used : File(org.survey.model.file.File)

Example 9 with File

use of org.survey.model.file.File in project survey by markoniemi.

the class FileServiceTestBase method update.

@Test
public void update() {
    create();
    for (int i = 0; i < ENTITY_COUNT; i++) {
        File foundEntity = entityService.findOne(savedEntities.get(i).getId());
        File updatedEntity = entityFactory.getUpdatedEntity(foundEntity);
        updatedEntity.setId(foundEntity.getId());
        entityService.update(updatedEntity);
        foundEntity = entityService.findOne(savedEntities.get(i).getId());
        assertEntity(updatedEntity, foundEntity);
    }
}
Also used : File(org.survey.model.file.File) Test(org.junit.Test)

Example 10 with File

use of org.survey.model.file.File in project survey by markoniemi.

the class FileRepositoryTest method findAllByOwner.

@Test
public void findAllByOwner() {
    save();
    File originalFile = orginalEntities.get(0);
    @SuppressWarnings("unchecked") List<File> files = IteratorUtils.toList(fileRepository.findAllByOwner(originalFile.getOwner()).iterator());
    Assert.assertEquals(ENTITY_COUNT, files.size());
    Assert.assertTrue(files.containsAll(orginalEntities));
    int index = files.indexOf(originalFile);
    Assert.assertEquals(0, entityComparator.compare(originalFile, files.get(index)));
}
Also used : File(org.survey.model.file.File) Test(org.junit.Test) CrudRepositoryTest(org.survey.repository.CrudRepositoryTest)

Aggregations

File (org.survey.model.file.File)21 Test (org.junit.Test)10 ModelAndView (org.springframework.web.servlet.ModelAndView)4 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)3 ResultActions (org.springframework.test.web.servlet.ResultActions)3 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 CrudRepositoryTest (org.survey.repository.CrudRepositoryTest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DataHandler (javax.activation.DataHandler)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)1 After (org.junit.After)1 DefaultStreamedContent (org.primefaces.model.DefaultStreamedContent)1 User (org.survey.model.user.User)1