Пример #1
0
  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
      throws ServletException, IOException {

    final Session session = createAdminSession();
    try {

      String statusFilter = req.getParameter("status");

      if (statusFilter == null) {
        // By default, only approved posts are returned.
        statusFilter = RelatedUgcNodeIndexerExtension.FIELD_STATE_APPROVED;
      }

      final List<Post> mltPosts =
          RelatedSearchUtil.getRelatedPosts(
              getResourceResolver(session),
              req.getParameter("q"),
              statusFilter,
              req.getParameter("resource-type"),
              req.getParameter("component-path"),
              MLT_FIELDS,
              Integer.valueOf(req.getParameter("max-results")),
              MIN_TERM_FREQ,
              MIN_DOC_FREQ);

      if (mltPosts == null) {
        return;
      }

      final JSONObject json = new JSONObject();
      JSONObject match;
      final List<JSONObject> matches = new ArrayList<JSONObject>();

      for (final Post p : mltPosts) {
        match = new JSONObject();
        match.put("subject", xssProtectionService.protectFromXSS(p.getSubject()));
        match.put("replyCount", p.getRepliesCount());
        match.put("url", p.getUrl());
        match.put("id", p.getId());
        match.put("created", p.getCreated());
        match.put("createdBy", xssProtectionService.protectFromXSS(p.getCreatedBy().getFullName()));
        matches.add(match);
      }

      json.put("mlt", matches);

      resp.setContentType("application/json");
      resp.getWriter().write(json.toString());

    } catch (final Exception e) {
      throw new ServletException(e);
    } finally {
      if (session != null) {
        session.logout();
      }
    }
  }