Search in sources :

Example 1 with PagePreviewDTO

use of com.nixmash.blog.jsoup.dto.PagePreviewDTO in project nixmash-blog by mintster.

the class AdminPostsController method createLinkPost.

// endregion
// region Add Post POST
@RequestMapping(value = "/add/link", method = POST)
public String createLinkPost(@Valid PostDTO postDTO, BindingResult result, CurrentUser currentUser, RedirectAttributes attributes, Model model, HttpServletRequest request) throws DuplicatePostNameException {
    PagePreviewDTO pagePreview = (PagePreviewDTO) WebUtils.getSessionAttribute(request, "pagePreview");
    model.addAttribute("postheader", webUI.getMessage(ADD_LINK_HEADER));
    model.addAttribute("postFormType", "link");
    if (!isDuplicatePost(postDTO, null)) {
        if (result.hasErrors()) {
            model.addAttribute("hasLink", true);
            model.addAttribute("hasCarousel", true);
            model.addAttribute("pagePreview", pagePreview);
            if (result.hasFieldErrors("postTitle")) {
                postDTO.setPostTitle(pagePreview.getTitle());
            }
            model.addAttribute("postDTO", postDTO);
            return ADMIN_LINK_ADD_VIEW;
        } else {
            if (postDTO.getHasImages()) {
                if (postDTO.getDisplayType() != PostDisplayType.LINK) {
                    postDTO.setPostImage(pagePreview.getImages().get(postDTO.getImageIndex()).src);
                } else
                    postDTO.setPostImage(null);
            }
            postDTO.setPostSource(PostUtils.createPostSource(postDTO.getPostLink()));
            postDTO.setPostName(PostUtils.createSlug(postDTO.getPostTitle()));
            postDTO.setUserId(currentUser.getId());
            postDTO.setPostContent(cleanContentTailHtml(postDTO.getPostContent()));
            request.setAttribute("postTitle", postDTO.getPostTitle());
            Post post = postService.add(postDTO);
            postDTO.setPostId(post.getPostId());
            post.setPostMeta(jsoupService.createPostMeta(postDTO));
            // All links are saved as PUBLISHED so no _isPublished_ status check on new Links
            if (applicationSettings.getSolrEnabled())
                postDocService.addToIndex(post);
            // Links are included in Posts A-to-Z Listing
            fmService.createPostAtoZs();
            webUI.addFeedbackMessage(attributes, FEEDBACK_POST_LINK_ADDED);
            return "redirect:/admin/posts";
        }
    } else {
        result.reject("global.error.post.name.exists", new Object[] { postDTO.getPostTitle() }, "post name exists");
        model.addAttribute("hasLink", true);
        model.addAttribute("hasCarousel", true);
        model.addAttribute("pagePreview", pagePreview);
        return ADMIN_LINK_ADD_VIEW;
    }
}
Also used : Post(com.nixmash.blog.jpa.model.Post) PagePreviewDTO(com.nixmash.blog.jsoup.dto.PagePreviewDTO)

Example 2 with PagePreviewDTO

use of com.nixmash.blog.jsoup.dto.PagePreviewDTO in project nixmash-blog by mintster.

the class JsoupServiceImpl method getPagePreview.

@Override
public PagePreviewDTO getPagePreview(String url) {
    PagePreviewDTO pagePreviewDTO = null;
    Document doc;
    Boolean tryWithoutCertValidation = false;
    try {
        doc = getDocument(url, true);
        pagePreviewDTO = pagePreviewParser.parse(doc);
    } catch (IOException e) {
        logger.info(String.format("Jsoup IOException [validCert = TRUE]  url [%s] : %s", url, e.getMessage()));
        tryWithoutCertValidation = true;
    }
    if (tryWithoutCertValidation) {
        try {
            doc = getDocument(url, false);
            pagePreviewDTO = pagePreviewParser.parse(doc);
        } catch (IOException e) {
            logger.info(String.format("Jsoup IOException [validCert = FALSE]  url [%s] : %s", url, e.getMessage()));
            return null;
        }
    }
    return pagePreviewDTO;
}
Also used : PagePreviewDTO(com.nixmash.blog.jsoup.dto.PagePreviewDTO) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 3 with PagePreviewDTO

use of com.nixmash.blog.jsoup.dto.PagePreviewDTO in project nixmash-blog by mintster.

the class AdminPostsController method addLink.

@RequestMapping(value = "/add/link", params = { "isLink" }, method = GET)
public String addLink(@RequestParam(value = "isLink") Boolean isLink, @Valid PostLink postLink, BindingResult result, Model model, HttpServletRequest request) {
    model.addAttribute("postheader", webUI.getMessage(ADD_LINK_HEADER));
    if (StringUtils.isEmpty(postLink.getLink())) {
        result.rejectValue("link", "post.link.is.empty");
    } else {
        PagePreviewDTO pagePreview = jsoupService.getPagePreview(postLink.getLink());
        if (pagePreview == null) {
            result.rejectValue("link", "post.link.page.not.found");
            return ADMIN_LINK_ADD_VIEW;
        } else {
            model.addAttribute("categories", postService.getAdminSelectionCategories());
            model.addAttribute("hasLink", true);
            model.addAttribute("hasCarousel", true);
            WebUtils.setSessionAttribute(request, "pagePreview", pagePreview);
            model.addAttribute("pagePreview", pagePreview);
            model.addAttribute("postDTO", postDtoFromPagePreview(pagePreview, postLink.getLink()));
        }
    }
    return ADMIN_LINK_ADD_VIEW;
}
Also used : PagePreviewDTO(com.nixmash.blog.jsoup.dto.PagePreviewDTO)

Aggregations

PagePreviewDTO (com.nixmash.blog.jsoup.dto.PagePreviewDTO)3 Post (com.nixmash.blog.jpa.model.Post)1 IOException (java.io.IOException)1 Document (org.jsoup.nodes.Document)1