Beispiel #1
0
 @Test
 public void testIsValid() {
   String ok = "<p>Test <b><a href='http://example.com/'>OK</a></b></p>";
   String nok1 = "<p><script></script>Not <b>OK</b></p>";
   String nok2 = "<p align=right>Test Not <b>OK</b></p>";
   assertTrue(Jsoup.isValid(ok, Whitelist.basic()));
   assertFalse(Jsoup.isValid(nok1, Whitelist.basic()));
   assertFalse(Jsoup.isValid(nok2, Whitelist.basic()));
 }
Beispiel #2
0
  /**
   * Gets the content of the article, and creates the final section for the generated report (html)
   *
   * @param aLink
   * @return
   */
  public static String getArticleContent(String aLink, ArticleBodyParser parser) {
    Document doc = null;
    String htmltext = "";
    if (Objects.nonNull(aLink) && !aLink.isEmpty()) {
      try {
        doc =
            Jsoup.connect(aLink)
                .header("Accept-Encoding", "gzip, deflate")
                .userAgent(userAgent)
                .timeout(6000)
                .followRedirects(true)
                .maxBodySize(0)
                .get();
      } catch (IOException e) {
        LOGGER.log(
            Level.WARNING, "Error connecting, while fetching the article with link " + aLink);
      }
      if (doc != null) {
        Element title = doc.getElementsByTag("title").first();
        Element mainArticle = parser.parseArticleFromDoc(doc);
        if (Objects.nonNull(title) && Objects.nonNull(mainArticle)) {
          htmltext =
              HtmlContentWriterUtil.generateArticleHtml(
                  title.html(), aLink, Jsoup.clean(mainArticle.html(), Whitelist.basic()));

        } else {
          LOGGER.warning("We could not fetch the title and main body for link: " + aLink);
        }
      }
    }
    return htmltext;
  }
Beispiel #3
0
  @Test
  public void basicBehaviourTest() {
    String h =
        "<div><p><a href='javascript:sendAllMoney()'>Dodgy</a> <A HREF='HTTP://nice.com'>Nice</p><blockquote>Hello</blockquote>";
    String cleanHtml = Jsoup.clean(h, Whitelist.basic());

    assertEquals(
        "<p><a rel=\"nofollow\">Dodgy</a> <a href=\"http://nice.com\" rel=\"nofollow\">Nice</a></p><blockquote>Hello</blockquote>",
        TextUtil.stripNewlines(cleanHtml));
  }
 /** Turns a HTML document back into a set of text, elements, annotations. */
 public static UnrenderedBlip unrender(final String content) {
   final StringBuilder sb = new StringBuilder();
   final Map<Integer, com.google.wave.api.Element> elements = Maps.newHashMap();
   final Annotations annotations = new Annotations();
   // Sanitized
   final String safe = Jsoup.clean(content, Whitelist.basic());
   final Document doc = Jsoup.parse(safe);
   unrender(doc.body(), sb, elements, annotations);
   return new UnrenderedBlip(sb.toString(), elements, annotations);
 }
  public static Pair<String, String> lyrics(String link) {
    try {
      // Get the lyrics
      final Document doc = Jsoup.connect(link).get();

      // Get the title
      final String safeTitle =
          Jsoup.clean(doc.title().replaceAll(" Lyrics \\| MetroLyrics", ""), Whitelist.basic());

      // Get the Lyrics
      final Elements rawLyrics = doc.select("#lyrics-body-text");
      final String safeLyrics = Jsoup.clean(rawLyrics.toString(), Whitelist.basic());

      return Pair.of(safeTitle, safeLyrics);

    } catch (IOException e) {
      // There's been some error, so return null
      return null;
    }
  }
Beispiel #6
0
  @RequestMapping(value = "getrecordslowmediumhigh", method = RequestMethod.GET)
  public ResponseEntity<?> getRecordslowMediumHigh(
      HttpServletRequest request, HttpServletResponse response)
        // SessionStatus status)
      {
    String simpleScenario = request.getParameter("simpleScenario");
    String safesimpleScenario = Jsoup.clean(simpleScenario, Whitelist.basic());

    String varR = request.getParameter("varR");
    String safevarR = Jsoup.clean(varR, Whitelist.basic());

    String season = request.getParameter("season");
    String safeseason = Jsoup.clean(season, Whitelist.basic());

    String year = request.getParameter("yearSimple");
    String safeyear = Jsoup.clean(year, Whitelist.basic());

    // PointModel model = ClimateRecordHandler.FindClimateRecordsHighMediumLow(safesimpleScenario,
    // safeyear, safevarR, safeseason);

    List<ClimateRecord> res =
        climateRecordService.findPointModellowMediumHigh(simpleScenario, safeyear, varR, season);
    ClimateRecordHandler ch = new ClimateRecordHandler();

    PointModel model = null;

    if (res.size() > 0) {
      model = ch.FindClimateRecordsHighMediumLow(simpleScenario, safeyear, varR, season, res);
    }

    if (model != null) {
      // status.setComplete();
      return new ResponseEntity<PointModel>(model, HttpStatus.OK);
    }

    return new ResponseEntity<String>(
        "Nothing was found for selected values", HttpStatus.BAD_REQUEST);
  }
Beispiel #7
0
 @Test
 public void allowsRelativeLinksIfConfiguredThusly() {
   String html = "<a href='/foo'>Link</a>";
   String clean = Jsoup.clean(html, Whitelist.basic().setUseAbsoluteURLs(false));
   assertEquals("<a href=\"/foo\" rel=\"nofollow\">Link</a>", clean);
 }
Beispiel #8
0
 @Test
 public void dropsUnresolvableRelativeLinks() {
   String html = "<a href='/foo'>Link</a>";
   String clean = Jsoup.clean(html, Whitelist.basic());
   assertEquals("<a rel=\"nofollow\">Link</a>", clean);
 }
Beispiel #9
0
 @Test
 public void resolvesRelativeLinks() {
   String html = "<a href='/foo'>Link</a>";
   String clean = Jsoup.clean(html, "http://example.com/", Whitelist.basic());
   assertEquals("<a href=\"http://example.com/foo\" rel=\"nofollow\">Link</a>", clean);
 }
Beispiel #10
0
 /**
  * Convert Markdown to HTML content (in an amazingly unoptimized way)
  *
  * @param content some Markdown content
  * @return that content in plain and sanitized HTML (XSS safe!)
  */
 public static String markdownToHtml(String content) {
   // optimization for the XML importer: don't use the slow
   // Markdown processor for content that's already HTML
   if (content.startsWith("<h3>")) return Jsoup.clean(content, Whitelist.basic());
   return Jsoup.clean(new PegDownProcessor().markdownToHtml(content), Whitelist.basic());
 }
Beispiel #11
0
  @RequestMapping(value = "getrecordsclm", method = RequestMethod.GET)
  public ResponseEntity<?> getRecordsClm(HttpServletRequest request, HttpServletResponse response)
        // , SessionStatus status)
      {

    String drivingdata = request.getParameter("drivingdata");
    String safedrivingdata = Jsoup.clean(drivingdata, Whitelist.basic());

    String varR = request.getParameter("varR");
    String safevarR = Jsoup.clean(varR, Whitelist.basic());

    String season = request.getParameter("seasonclm");
    String safeseason = Jsoup.clean(season, Whitelist.basic());

    String modelClm = request.getParameter("modelclm");
    String safemodelClm = Jsoup.clean(modelClm, Whitelist.basic());

    String mainScenario = request.getParameter("mainscenario");
    String safemainScenario = Jsoup.clean(mainScenario, Whitelist.basic());

    String start_future1 = request.getParameter("start_yr_future1");
    String safestart_future1 = Jsoup.clean(start_future1, Whitelist.basic());

    String start_future2 = request.getParameter("start_yr_future2");
    String safestart_future2 = Jsoup.clean(start_future2, Whitelist.basic());

    String start_past1 = request.getParameter("start_yr_past1");
    String safestart_past1 = Jsoup.clean(start_past1, Whitelist.basic());

    String start_past2 = request.getParameter("start_yr_past2");
    String safestart_past2 = Jsoup.clean(start_past2, Whitelist.basic());

    String end_future1 = request.getParameter("end_yr_Rest1");
    String safeend_future1 = Jsoup.clean(end_future1, Whitelist.basic());

    String end_future2 = request.getParameter("end_yr_Rest2");
    String safeend_future2 = Jsoup.clean(end_future2, Whitelist.basic());

    String end_past1 = request.getParameter("end_yr_past1");
    String safeend_past1 = Jsoup.clean(end_past1, Whitelist.basic());

    String end_past2 = request.getParameter("end_yr_past2");
    String safeend_past2 = Jsoup.clean(end_past2, Whitelist.basic());

    List<ClimateRecord> res =
        climateRecordService.findClimateRecordsFutureAndPastClm(
            safemainScenario,
            safedrivingdata,
            safemodelClm,
            safevarR,
            safeseason,
            safestart_future1,
            safestart_future2,
            safestart_past1,
            safestart_past2,
            safeend_future1,
            safeend_future2,
            safeend_past1,
            safeend_past2);
    PointModel model = null;
    if (res.size() > 0) {
      ClimateRecordHandler ch = new ClimateRecordHandler();
      model =
          ch.FindClimateRecordsFuturePastCLM(
              safemainScenario,
              drivingdata,
              modelClm,
              varR,
              season,
              start_future1,
              start_future2,
              start_past1,
              start_past2,
              end_future1,
              end_future2,
              end_past1,
              end_past2,
              res);
    }
    if (model != null) {
      // status.setComplete();
      return new ResponseEntity<PointModel>(model, HttpStatus.OK);
    }

    return new ResponseEntity<String>(
        "Nothing was found for selected values", HttpStatus.BAD_REQUEST);
  }
  @FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result addEvent(
      Context context,
      @Param("day") String day,
      @Param("title") String title,
      @Param("description") String description,
      @Param("redir-search") Integer redirSearchId,
      @Param("redir-target") Integer redirTargetId) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    Event event = new Event();
    event.setGroupId(group.getId());
    try {
      event.setDay(LocalDate.parse(day));
    } catch (Exception ex) {
    }

    if (event.getDay() == null) {
      flash.error("error.invalidDate");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    if (Validator.isEmpty(title)) {
      flash.error("error.invalidTitle");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    if (baseDB.event.find(group, event.getDay()) != null) {
      flash.error("google.group.alreadyEventForThisDate");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    event.setTitle(title);
    event.setDescription(Jsoup.clean(description == null ? "" : description, Whitelist.basic()));

    if (!baseDB.event.insert(event)) {
      flash.error("error.internalError");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    flash.success("google.group.eventInserted");
    if (redirSearchId != null) {
      return Results.redirect(
          router.getReverseRoute(
              GoogleSearchController.class,
              "search",
              "groupId",
              group.getId(),
              "searchId",
              redirSearchId));
    }
    if (redirTargetId != null) {
      return Results.redirect(
          router.getReverseRoute(
              GoogleTargetController.class,
              "target",
              "groupId",
              group.getId(),
              "targetId",
              redirTargetId));
    }

    return Results.redirect(
        router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
  }