Example #1
0
  /**
   * Send a password change email to an email address
   *
   * @param to the address to send to
   * @param url the URL to embed in the email
   * @param webProperties properties such as the from address
   * @throws Exception if there is a problem creating the email
   */
  public static void emailPasswordToken(String to, String url, final Map webProperties)
      throws Exception {

    String projectTitle = (String) webProperties.get("project.title");
    String baseSubject = (String) webProperties.get("mail.passwordSubject");
    String mailSubject = MessageFormat.format(baseSubject, new Object[] {projectTitle});
    String mailText = (String) webProperties.get("mail.passwordText");
    String mailTextWithUrl = MessageFormat.format(mailText, new Object[] {url});
    email(to, mailSubject, mailTextWithUrl, webProperties);
  }
Example #2
0
  /**
   * Method called when user has finished updating a constraint.
   *
   * @param mapping The ActionMapping used to select this instance
   * @param form The optional ActionForm bean for this request (if any)
   * @param request The HTTP request we are processing
   * @param response The HTTP response we are creating
   * @return an ActionForward object defining where control goes next
   * @exception Exception if the application business logic throws an exception
   */
  @Override
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    ProfileManager pm = im.getProfileManager();
    String username = ((CreateAccountForm) form).getUsername();
    String password = ((CreateAccountForm) form).getPassword();
    pm.createNewProfile(username, password);
    Properties webProperties = SessionMethods.getWebProperties(session.getServletContext());
    try {
      MailUtils.email(username, webProperties);
      if (((CreateAccountForm) form).getMailinglist()
          && webProperties.getProperty("mail.mailing-list") != null
          && webProperties.getProperty("mail.mailing-list").length() > 0) {
        MailUtils.subscribe(username, webProperties);
      }
      SessionMethods.recordMessage(
          "You have successfully created an account, and logged in.", session);
    } catch (Exception e) {
      SessionMethods.recordError("Failed to send confirmation email", session);
    }

    /*
     * This code generates an MD5 key for the given username which is then
     * encoded in Hexadecimal. This could later be used for account
     * activation.
     *
     * try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[]
     * buffer = username.getBytes(); md5.update(buffer); byte[] array =
     * md5.digest(); String encoded = HexBin.encode(array); } catch
     * (NoSuchAlgorithmException e) { }
     */
    doLogin(request, username, password);
    return new ActionForward("/begin.do");
  }
Example #3
0
 /**
  * Send a welcoming email to an email address
  *
  * @param to the address to send to
  * @param webProperties properties such as the from address
  * @throws Exception if there is a problem creating the email
  */
 public static void welcome(String to, final Map webProperties) throws MessagingException {
   String subject = (String) webProperties.get("mail.subject");
   String text = (String) webProperties.get("mail.text");
   email(to, subject, text, webProperties);
 }
Example #4
0
 /**
  * Subscribe the given email address to the mailing list specified in the mine config file
  *
  * @param email the email to subscribe
  * @param webProperties the web properties
  * @throws Exception when somethign goes wrong
  */
 public static void subscribe(String email, final Map webProperties) throws MessagingException {
   String to = (String) webProperties.get("mail.mailing-list");
   String subject = "";
   String body = "";
   email(to, subject, body, email, webProperties);
 }
Example #5
0
 /**
  * @param to the address to send to
  * @param subject The Subject of the email
  * @param body The content of the email
  * @param webProperties Common properties for all emails (such as from, authentication)
  * @throws MessagingException if there is a problem creating the email
  */
 public static void email(String to, String subject, String body, final Map webProperties)
     throws MessagingException {
   String from = (String) webProperties.get("mail.from");
   email(to, subject, body, from, webProperties);
 }
Example #6
0
  /**
   * Send a post request to the registry service, if one has been set up. As Registrar extends
   * Thread, the request will be executed in a separate Thread.
   */
  @Override
  public void run() {
    // Check to see whether we should run at all
    String registryAddress = props.getProperty("registration.address");
    if (registryAddress == null || registryAddress.isEmpty()) {
      LOG.info("No registry address supplied: " + "will not attempt to register.");
      return;
    }
    String authToken = props.getProperty("registration.authToken");
    if (authToken == null || authToken.isEmpty()) {
      LOG.info("No registration authentication supplied: " + "will not attempt to register.");
      return;
    }

    // Get the data to send to the server
    Map<String, String> params = new HashMap<String, String>();

    params.put("name", props.getProperty("project.title"));
    params.put("description", props.getProperty("project.subtitle"));
    params.put("authToken", authToken);
    params.put("format", "json");
    params.put("url", props.getProperty("webapp.baseurl") + "/" + props.getProperty("webapp.path"));
    populateParams(params);

    try {
      Thread.sleep(60000);
      URL registry = new URL(registryAddress);
      String data = getQueryString(params);

      // Send data
      URLConnection conn = registry.openConnection();
      conn.setDoOutput(true);
      Writer out = new OutputStreamWriter(conn.getOutputStream());
      out.write(data);
      out.flush();
      out.close();

      // Get the response
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      StringBuilder sb = new StringBuilder();
      while ((line = rd.readLine()) != null) {
        sb.append(line);
      }
      rd.close();

      String response = sb.toString();

      // Handle the response
      LOG.debug("Registration response: " + response);
      JSONObject jo = new JSONObject(response);
      String status = jo.getString("status");
      if (CREATED.equals(status)) {
        String receivedToken = jo.getString("authToken");
        if (!authToken.equals(receivedToken)) {
          handleProblem(
              "Mine registered incorrectly: "
                  + "authToken does not match the one supplied. "
                  + "I gave "
                  + authToken
                  + ", but got back "
                  + receivedToken);
        } else {
          LOG.info("Mine successfully registered");
        }
      } else if (UPDATED.equals(status)) {
        LOG.info("Mine registration details successfully updated");
      } else {
        handleProblem("Problem registering mine: " + jo.getString("text"));
      }

    } catch (InterruptedException e) {
      handleProblem("Registration was interrupted" + e);
    } catch (UnsupportedEncodingException e) {
      handleProblem("Could not encode query string for parameters: " + params);
    } catch (MalformedURLException e) {
      handleProblem("Could not connect to registry - bad address: " + registryAddress);
    } catch (JSONException e) {
      handleProblem("Problem registering mine - server returned bad response: " + e);
    } catch (IOException e) {
      handleProblem("Problem connecting to registry: " + e);
    } catch (Exception e) {
      handleProblem("Unanticipated problem encountered registering mine: " + e);
    }
    if (!emailContent.isEmpty()) {
      String feedbackEmail = props.getProperty("registration.emailto");
      if (feedbackEmail == null) {
        feedbackEmail = props.getProperty("superuser.account");
      }
      try {
        MailUtils.email(feedbackEmail, subject, emailIntro + emailContent, props);
      } catch (MessagingException e) {
        LOG.error("Problem emailing information about problems to " + feedbackEmail);
      }
    }
  }