Search in sources :

Example 1 with Attachment

use of org.summerb.minicms.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.minicms.api.dto.Attachment)

Example 2 with Attachment

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

the class ArticleRendererCachingImplTest method testRenderArticle_expectArticleCacheInvalidatedOnAttachmentChange.

@Test
public void testRenderArticle_expectArticleCacheInvalidatedOnAttachmentChange() throws Exception {
    ArticleRendererCachingImpl fixture = new ArticleRendererCachingImpl();
    fixture.setEventBus(mock(EventBus.class));
    ArticleRenderer renderer = mock(ArticleRenderer.class);
    fixture.setArticleRenderer(renderer);
    fixture.afterPropertiesSet();
    Locale locale = new Locale("ru");
    mockTest(renderer, locale);
    // force cache
    fixture.renderArticle("a1", locale);
    fixture.renderArticle("a2", locale);
    fixture.renderArticle("a3", locale);
    RenderedArticle ra2v2 = new RenderedArticle();
    ra2v2.setId(1L);
    ra2v2.setLang("ru");
    ra2v2.setTitle("t2v2");
    ra2v2.setArticleKey("a2");
    ra2v2.setContent("c");
    ra2v2.setArticleReferences(Arrays.asList(3L));
    when(renderer.renderArticle("a2", locale)).thenReturn(ra2v2);
    Attachment att3 = new Attachment();
    att3.setArticleId(3L);
    fixture.onArticleAttachmentChanged(EntityChangedEvent.removedObject(att3));
    ra2v2 = fixture.renderArticle("a2", locale);
    assertEquals("t2v2", ra2v2.getTitle());
}
Also used : Locale(java.util.Locale) ArticleRenderer(org.summerb.minicms.api.ArticleRenderer) Attachment(org.summerb.minicms.api.dto.Attachment) EventBus(com.google.common.eventbus.EventBus) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) Test(org.junit.Test)

Example 3 with Attachment

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

the class ArticleController method getAttachment2.

@RequestMapping(method = RequestMethod.GET, value = ArticleAbsoluteUrlBuilderImpl.DEFAULT_PATH_ARTICLES_ATTACHMENTS + "/{id}")
public ModelAndView getAttachment2(Model model, @PathVariable("id") long id, HttpServletResponse response) {
    try {
        Attachment attachment = attachmentService.findById(id);
        response.setContentType(mimeTypeResolver.resolveContentTypeByFileName(attachment.getName()));
        response.setContentLength((int) attachment.getSize());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + attachment.getName() + "\"");
        FileCopyUtils.copy(attachmentService.getContentInputStream(id), response.getOutputStream());
        return null;
    } catch (Throwable t) {
        log.debug("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 : Attachment(org.summerb.minicms.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Attachment

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

the class ArticleController method ajaxList.

@RequestMapping(method = RequestMethod.POST, value = "/rest/articles-attachments/ajaxList")
@ResponseBody
public Map<String, ? extends Object> ajaxList(@RequestBody EasyCrudQueryParams filteringParams, HttpServletResponse response) throws NotAuthorizedException {
    Query query = filteringParamsToQueryConverter.convert(filteringParams.getFilterParams(), attachmentService.getDtoClass());
    PaginatedList<Attachment> ret = attachmentService.query(filteringParams.getPagerParams(), query, filteringParams.getOrderBy());
    return Collections.singletonMap(EasyCrudControllerBase.ATTR_LIST, ret);
}
Also used : Query(org.summerb.easycrud.api.query.Query) Attachment(org.summerb.minicms.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with Attachment

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

the class ArticleController method getAttachment.

@RequestMapping(method = RequestMethod.GET, value = ArticleAbsoluteUrlBuilderImpl.DEFAULT_PATH_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(attachmentService.getContentInputStream(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.minicms.api.dto.Attachment) Date(java.util.Date) InputStreamResource(org.springframework.core.io.InputStreamResource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Attachment (org.summerb.minicms.api.dto.Attachment)12 Test (org.junit.Test)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Locale (java.util.Locale)3 Article (org.summerb.minicms.api.dto.Article)3 InputStream (java.io.InputStream)2 RenderedArticle (org.summerb.minicms.api.dto.consuming.RenderedArticle)2 EventBus (com.google.common.eventbus.EventBus)1 UrlBuilderTestImpl (integr.org.summerb.minicms.impl.UrlBuilderTestImpl)1 NotSerializableException (java.io.NotSerializableException)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.easycrud.api.dto.PagerParams)1 Query (org.summerb.easycrud.api.query.Query)1 ArticleAbsoluteUrlBuilder (org.summerb.minicms.api.ArticleAbsoluteUrlBuilder)1