Search in sources :

Example 1 with Attachment

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

the class ArticleController method getAttachment2.

@RequestMapping(method = RequestMethod.GET, value = "/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(articleService.getAttachmnetContent(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.microservices.articles.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Attachment

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

the class AttachmentServiceImplTest method testAttachment_expectMultipleResults.

@Test
public void testAttachment_expectMultipleResults() throws Exception {
    Attachment a = new Attachment();
    a.setArticleId(createTestArticle());
    a.setName("test attachment");
    a.setSize(123);
    a.setContents(createValidContentsReader());
    attachmentService.create(a);
    a.setContents(createValidContentsReader());
    a.setName("test attachment 2");
    attachmentService.create(a);
    PaginatedList<Attachment> result = attachmentService.query(new PagerParams(0, 100), Query.n().eq(Attachment.FN_ARTICLE_ID, a.getArticleId()));
    assertEquals(2, result.getItems().size());
}
Also used : PagerParams(org.summerb.approaches.jdbccrud.api.dto.PagerParams) Attachment(org.summerb.microservices.articles.api.dto.Attachment) Test(org.junit.Test)

Example 3 with Attachment

use of org.summerb.microservices.articles.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.microservices.articles.api.ArticleRenderer) Attachment(org.summerb.microservices.articles.api.dto.Attachment) EventBus(com.google.common.eventbus.EventBus) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Test(org.junit.Test)

Example 4 with Attachment

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

the class ArticleRenderingServiceImplTest method testRenderArticle_smokeTest.

@Test
public void testRenderArticle_smokeTest() throws Exception {
    ArticleAbsoluteUrlBuilder articleAbsoluteUrlBuilder = new UrlBuilderTestImpl();
    ArticleService articleService = Mockito.mock(ArticleService.class);
    Article a1 = new Article();
    a1.setId(1L);
    a1.setArticleKey("a1");
    a1.setAnnotation("Article annotation. Read other article first: ${article['a2']}");
    a1.setContent("Article text. See: ${article['a2']}. And now - here is the screenshot: ${img['screenshot1.jpg']}");
    when(articleService.findArticleByKeyAndLocale(eq("a1"), any(Locale.class))).thenReturn(a1);
    Attachment att1 = new Attachment();
    att1.setId(1L);
    att1.setArticleId(1L);
    att1.setName("screenshot1.jpg");
    when(articleService.findArticleAttachments(1L)).thenReturn(new Attachment[] { att1 });
    Article a2 = new Article();
    a2.setId(1L);
    a2.setArticleKey("a2");
    a2.setTitle("Title of article 2");
    a2.setAnnotation("Article annotation. Read other article first: ${article['a2']}");
    a2.setContent("Article text. See: ${article['a2']}. And now - here is the screenshot: ${img['screenshot1.jpg']}");
    when(articleService.findArticleByKeyAndLocale(eq("a2"), any(Locale.class))).thenReturn(a2);
    ArticleRendererImpl fixture = new ArticleRendererImpl();
    fixture.setArticleAbsoluteUrlBuilder(articleAbsoluteUrlBuilder);
    fixture.setArticleService(articleService);
    fixture.setStringTemplateCompiler(new StringTemplateCompilerlImpl());
    RenderedArticle result = fixture.renderArticle("a1", Locale.ENGLISH);
    assertNotNull(result);
    String a2href = "<a href=\"url-article:a2\" title=\"Title of article 2\">Title of article 2</a>";
    assertEquals("Article annotation. Read other article first: " + a2href, result.getAnnotation());
    assertEquals("Article text. See: " + a2href + ". And now - here is the screenshot: <img class=\"article-image\" src=\"url-att:screenshot1.jpg\" alt=\"screenshot1.jpg\" />", result.getContent());
}
Also used : Locale(java.util.Locale) ArticleService(org.summerb.microservices.articles.api.ArticleService) StringTemplateCompilerlImpl(org.summerb.utils.stringtemplate.impl.StringTemplateCompilerlImpl) ArticleAbsoluteUrlBuilder(org.summerb.microservices.articles.api.ArticleAbsoluteUrlBuilder) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Article(org.summerb.microservices.articles.api.dto.Article) ArticleRendererImpl(org.summerb.microservices.articles.impl.ArticleRendererImpl) Attachment(org.summerb.microservices.articles.api.dto.Attachment) UrlBuilderTestImpl(integr.ru.skarpushin.services.articles.impl.UrlBuilderTestImpl) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Test(org.junit.Test)

Example 5 with Attachment

use of org.summerb.microservices.articles.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.approaches.jdbccrud.api.query.Query) Attachment(org.summerb.microservices.articles.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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