Search in sources :

Example 6 with Attachment

use of org.summerb.microservices.articles.api.dto.Attachment in project summerb by skarpushin.

the class ArticleController method getAttachment.

@RequestMapping(method = RequestMethod.GET, value = "/articles-attachments/{id}/{proposedName}")
public ResponseEntity<InputStreamResource> getAttachment(Model model, @PathVariable("id") long id, HttpServletResponse response) throws AttachmentNotFoundException {
    try {
        Attachment attachment = attachmentService.findById(id);
        if (attachment == null) {
            throw new AttachmentNotFoundException(id);
        }
        long now = new Date().getTime();
        HttpHeaders headers = new HttpHeaders();
        headers.setCacheControl("private");
        headers.setExpires(now + DAY);
        headers.setContentType(MediaType.parseMediaType(mimeTypeResolver.resolveContentTypeByFileName(attachment.getName())));
        headers.setContentLength((int) attachment.getSize());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + attachment.getName() + "\"");
        // NOTE: Looks like there is a bug in the spring - it will add
        // Last-Modified twice to the response
        // responseHeaders.setLastModified(maxLastModified);
        response.setDateHeader("Last-Modified", now);
        InputStreamResource ret = new InputStreamResource(articleService.getAttachmnetContent(id));
        return new ResponseEntity<InputStreamResource>(ret, headers, HttpStatus.OK);
    } catch (AttachmentNotFoundException t) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        response.setHeader("Error", "File not found");
        return null;
    } catch (Throwable t) {
        log.warn("Failed to get article attachment", t);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        String msg = exceptionTranslator.buildUserMessage(t, CurrentRequestUtils.getLocale());
        response.setHeader("Error", "Failed to get article attachment -> " + msg);
        return null;
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Attachment(org.summerb.microservices.articles.api.dto.Attachment) Date(java.util.Date) InputStreamResource(org.springframework.core.io.InputStreamResource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with Attachment

use of org.summerb.microservices.articles.api.dto.Attachment in project summerb by skarpushin.

the class ArticlesAuthoringController method createAttachment.

// NOTE: File upload example:
// http://www.ioncannon.net/programming/975/spring-3-file-upload-example/
@RequestMapping(method = RequestMethod.POST, value = "/{articleId}/addAttachment")
public String createAttachment(@ModelAttribute(ATTR_ARTICLE_ATTACHMENT) ArticleAttachmentVm articleAttachmentVm, Model model, @PathVariable("articleId") long articleId) throws Exception {
    try {
        Preconditions.checkArgument(articleAttachmentVm.getFile() != null && articleAttachmentVm.getFile().getFileItem() != null, "File required");
        Article article = articleService.findById(articleId);
        Preconditions.checkArgument(article != null, "Article not found");
        Attachment att = articleAttachmentVm.getAttachment();
        att.setArticleId(articleId);
        FileItem fileItem = articleAttachmentVm.getFile().getFileItem();
        att.setSize(fileItem.getSize());
        String fileName = fileItem.getName();
        att.setName(fileName);
        att.setContents(articleAttachmentVm.getFile().getInputStream());
        articleService.addArticleAttachment(att);
        return Views.redirect(String.format("article-authoring/%s", article.getArticleKey()));
    } catch (Throwable t) {
        log.error("Failed to create attachment", t);
        String msg = exceptionTranslator.buildUserMessage(t, CurrentRequestUtils.getLocale());
        addPageMessage(model.asMap(), new PageMessage(msg, MessageSeverity.Danger));
        // TODO: Navigate to article authoring instead!
        return viewNameArticleAuthoring;
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) PageMessage(org.summerb.approaches.springmvc.model.PageMessage) Article(org.summerb.microservices.articles.api.dto.Article) Attachment(org.summerb.microservices.articles.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with Attachment

use of org.summerb.microservices.articles.api.dto.Attachment in project summerb by skarpushin.

the class ArticlesAuthoringController method getArticle.

@RequestMapping(method = RequestMethod.GET, value = "/{articleKey}")
public String getArticle(Model model, @PathVariable("articleKey") String articleKey, Locale locale) throws NotAuthorizedException {
    model.addAttribute(ATTR_ARTICLE_KEY, articleKey);
    Map<Locale, Article> options = articleService.findArticleLocalizations(articleKey);
    if (options.size() == 0) {
        throw new RuntimeException("Article not found: " + articleKey);
    }
    List<ArticleVm> contents = new LinkedList<ArticleVm>();
    for (Entry<Locale, Article> entry : options.entrySet()) {
        ArticleVm contentPm = new ArticleVm();
        contentPm.setDto(entry.getValue());
        contentPm.setAttachments(new ListPm<Attachment>(Arrays.asList(articleService.findArticleAttachments(entry.getValue().getId()))));
        contents.add(contentPm);
    }
    ArticlesVm articlesVm = new ArticlesVm(contents);
    model.addAttribute(ATTR_ARTICLES, articlesVm.getMap());
    model.addAttribute(ATTR_ARTICLE, contents.get(0));
    return viewNameArticleAuthoring;
}
Also used : Locale(java.util.Locale) ArticlesVm(org.summerb.microservices.articles.mvc.vm.ArticlesVm) ArticleVm(org.summerb.microservices.articles.mvc.vm.ArticleVm) Article(org.summerb.microservices.articles.api.dto.Article) Attachment(org.summerb.microservices.articles.api.dto.Attachment) LinkedList(java.util.LinkedList) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with Attachment

use of org.summerb.microservices.articles.api.dto.Attachment in project summerb by skarpushin.

the class AttachmentDaoExtFilesImpl method deleteByQuery.

@Override
public int deleteByQuery(Query query) {
    PaginatedList<Attachment> aa = query(PagerParams.ALL, query);
    int ret = 0;
    for (Attachment a : aa.getItems()) {
        ret += delete(a.getId());
    }
    return ret;
}
Also used : Attachment(org.summerb.microservices.articles.api.dto.Attachment)

Example 10 with Attachment

use of org.summerb.microservices.articles.api.dto.Attachment in project summerb by skarpushin.

the class AttachmentServiceImplTest method testAttachment_expectCanRetrieveInputStream.

@Test
public void testAttachment_expectCanRetrieveInputStream() throws Exception {
    Attachment a = new Attachment();
    a.setArticleId(createTestArticle());
    a.setName("test attachment");
    a.setSize(123);
    a.setContents(createValidContentsReader());
    Attachment result = attachmentService.create(a);
    InputStream b = attachmentService.getContentInputStream(result.getId());
    assertStreamsEquals(createValidContentsReader(), b);
}
Also used : InputStream(java.io.InputStream) Attachment(org.summerb.microservices.articles.api.dto.Attachment) Test(org.junit.Test)

Aggregations

Attachment (org.summerb.microservices.articles.api.dto.Attachment)11 Test (org.junit.Test)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Locale (java.util.Locale)3 Article (org.summerb.microservices.articles.api.dto.Article)3 RenderedArticle (org.summerb.microservices.articles.api.dto.consuming.RenderedArticle)2 EventBus (com.google.common.eventbus.EventBus)1 UrlBuilderTestImpl (integr.ru.skarpushin.services.articles.impl.UrlBuilderTestImpl)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 FileItem (org.apache.commons.fileupload.FileItem)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 PagerParams (org.summerb.approaches.jdbccrud.api.dto.PagerParams)1 Query (org.summerb.approaches.jdbccrud.api.query.Query)1 PageMessage (org.summerb.approaches.springmvc.model.PageMessage)1 ArticleAbsoluteUrlBuilder (org.summerb.microservices.articles.api.ArticleAbsoluteUrlBuilder)1