Search in sources :

Example 1 with PageMessage

use of org.summerb.approaches.springmvc.model.PageMessage in project summerb by skarpushin.

the class ControllerExceptionHandlerStrategyLegacyImpl method buildHtmlError.

protected ModelAndView buildHtmlError(Throwable ex) {
    if (securityContextResolver != null && (ex instanceof AccessDeniedException && !securityContextResolver.hasRole(Roles.ROLE_USER))) {
        throw new IllegalArgumentException("Exception will not be handled by default exception handler: " + ex);
    }
    log.error("Exception occured", ex);
    ModelAndView ret = new ModelAndView(Views.ERROR_UNEXPECTED_CLARIFIED);
    String msg = exceptionTranslator.buildUserMessage(ex, LocaleContextHolder.getLocale());
    ControllerBase.addPageMessage(ret.getModel(), new PageMessage(msg, MessageSeverity.Danger));
    ret.getModel().put(ControllerBase.ATTR_EXCEPTION, ex);
    ret.getModel().put(ControllerBase.ATTR_EXCEPTION_STACKTRACE, ExceptionUtils.getThrowableStackTraceAsString(ex));
    return ret;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) PageMessage(org.summerb.approaches.springmvc.model.PageMessage) ModelAndView(org.springframework.web.servlet.ModelAndView)

Example 2 with PageMessage

use of org.summerb.approaches.springmvc.model.PageMessage in project summerb by skarpushin.

the class ArticleController method get.

@RequestMapping(method = RequestMethod.GET, value = "/articles/{articleKey}")
public String get(Model model, @PathVariable("articleKey") String articleKey, HttpServletResponse respons, Locale locale) {
    try {
        RenderedArticle article = articleRenderer.renderArticle(articleKey, locale);
        model.addAttribute(ATTR_ARTICLE, article);
    } catch (Throwable t) {
        log.debug("Failed to get article", t);
        String msg = exceptionTranslator.buildUserMessage(t, CurrentRequestUtils.getLocale());
        addPageMessage(model.asMap(), new PageMessage(msg, MessageSeverity.Danger));
    }
    return viewNameArticle;
}
Also used : PageMessage(org.summerb.approaches.springmvc.model.PageMessage) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with PageMessage

use of org.summerb.approaches.springmvc.model.PageMessage 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)

Aggregations

PageMessage (org.summerb.approaches.springmvc.model.PageMessage)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 FileItem (org.apache.commons.fileupload.FileItem)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 Article (org.summerb.microservices.articles.api.dto.Article)1 Attachment (org.summerb.microservices.articles.api.dto.Attachment)1 RenderedArticle (org.summerb.microservices.articles.api.dto.consuming.RenderedArticle)1