Example #1
0
  @Override
  protected String wrappedDoPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, HttpException, HumanVisibleException, RetryException {
    String address = request.getParameter("address");
    if (address != null) address = address.trim();
    if (address == null
        || address.length() == 0
        || address.indexOf('@') < 1
        || address.endsWith("example.com")) {
      throw new HumanVisibleException("You have to put in an email address")
          .setHtmlSuggestion("<a href=\"/\">Try again</a>");
    }

    try {
      wantsInSystem.addWantsIn(address);
    } catch (ValidationException e) {
      throw new HumanVisibleException(
              "Something was wrong with that email address (" + e.getMessage() + ")")
          .setHtmlSuggestion("<a href=\"/\">Try again</a>");
    }

    response.setContentType("text/html");
    OutputStream out = response.getOutputStream();
    out.write(
        ("<head><title>Saved your address</title></head>\n"
                + " <body><p>We saved your address; we'll let you know when we have room for more.</p><p>Thanks!</p></body>\n")
            .getBytes());
    out.flush();

    return null;
  }
  public void validateAll() {
    logger.info("Administrator kicked off validation of all ExternalAccount rows in the database");
    Query q =
        em.createQuery("SELECT ea.id, ea.accountType, ea.handle, ea.extra FROM ExternalAccount ea");
    List<Object[]> results = TypeUtils.castList(Object[].class, q.getResultList());

    logger.info("There are {} ExternalAccount rows to validate", results.size());

    List<Long> invalidHandles = new ArrayList<Long>();
    List<Long> invalidExtras = new ArrayList<Long>();

    for (Object[] row : results) {
      // logger.debug("row is: {}", Arrays.toString(row));
      Long id = (Long) row[0];
      ExternalAccountType accountType = (ExternalAccountType) row[1];
      String handle = (String) row[2];
      String extra = (String) row[3];
      if (accountType == null) {
        logger.info("Row has null accountType: {}", Arrays.toString(row));
      } else {
        try {
          String c = accountType.canonicalizeHandle(handle);
          if (handle != null && !c.equals(handle)) {
            logger.info(
                "Row is not canonicalized: {}: '{}' vs. '{}'",
                new Object[] {Arrays.toString(row), handle, c});
          }
        } catch (ValidationException e) {
          logger.info("Row had invalid 'handle': {}: {}", Arrays.toString(row), e.getMessage());
          invalidHandles.add(id);
        }
        try {
          String c = accountType.canonicalizeExtra(extra);
          if (extra != null && !c.equals(extra)) {
            logger.info(
                "Row is not canonicalized: {}: '{}' vs. '{}'",
                new Object[] {Arrays.toString(row), extra, c});
          }
        } catch (ValidationException e) {
          logger.info("Row had invalid 'extra': {}: {}", Arrays.toString(row), e.getMessage());
          invalidExtras.add(id);
        }
      }
    }

    if (!invalidHandles.isEmpty()) {
      StringBuilder sb = new StringBuilder();
      sb.append("UPDATE ExternalAccount SET handle = NULL WHERE id IN (");
      for (Long id : invalidHandles) {
        sb.append(id);
        sb.append(",");
      }
      sb.setLength(sb.length() - 1); // chop comma
      sb.append(");");
      logger.info("Possible query to null invalid handles: {}", sb);
    }

    if (!invalidExtras.isEmpty()) {
      StringBuilder sb = new StringBuilder();
      sb.append("UPDATE ExternalAccount SET extra = NULL WHERE id IN (");
      for (Long id : invalidHandles) {
        sb.append(id);
        sb.append(",");
      }
      sb.setLength(sb.length() - 1); // chop comma
      sb.append(");");
      logger.info("Possible query to null invalid extras: {}", sb);
    }

    logger.info(
        "ExternalAccount validation complete, {} invalid handles {} invalid extras",
        invalidHandles.size(),
        invalidExtras.size());
  }