/**
   * Setup the comment e-mail
   *
   * @param blog {@link Blog} information
   * @param entry {@link Entry}
   * @param email Email message
   * @throws EmailException If there is an error preparing the e-mail message
   */
  protected void setupEmail(Blog blog, Entry entry, Email email) throws EmailException {
    email.setCharset(BlojsomConstants.UTF8);

    // If we have a mail session for the environment, use that
    if (_session != null) {
      email.setMailSession(_session);
    } else {
      // Otherwise, if there is a username and password for the mail server, use that
      if (!BlojsomUtils.checkNullOrBlank(_mailServerUsername)
          && !BlojsomUtils.checkNullOrBlank(_mailServerPassword)) {
        email.setHostName(_mailServer);
        email.setAuthentication(_mailServerUsername, _mailServerPassword);
      } else {
        email.setHostName(_mailServer);
      }
    }

    email.setFrom(blog.getBlogOwnerEmail(), "Blojsom Comment");

    String author = entry.getAuthor();
    if (BlojsomUtils.checkNullOrBlank(author)) {
      author = blog.getBlogOwner();
    }

    String authorEmail = blog.getBlogOwnerEmail();

    if (author != null) {
      try {
        User user = _fetcher.loadUser(blog, author);

        if (user == null) {
          authorEmail = blog.getBlogOwnerEmail();
        } else {
          authorEmail = user.getUserEmail();
          if (BlojsomUtils.checkNullOrBlank(authorEmail)) {
            authorEmail = blog.getBlogOwnerEmail();
          }
        }
      } catch (FetcherException e) {
      }
    }

    email.addTo(authorEmail, author);
    email.setSentDate(new Date());
  }
  /**
   * Handle an event broadcast from another component
   *
   * @param event {@link Event} to be handled
   */
  public void handleEvent(Event event) {
    if (!BlojsomUtils.checkNullOrBlank(_twitterUpdateURL)) {
      if ((event instanceof EntryAddedEvent) || (event instanceof EntryUpdatedEvent)) {
        EntryEvent entryEvent = (EntryEvent) event;

        Blog blog = entryEvent.getBlog();
        String author = entryEvent.getEntry().getAuthor();
        User user;

        try {
          user = _fetcher.loadUser(blog, author);
        } catch (FetcherException e) {
          if (_logger.isErrorEnabled()) {
            _logger.error("Error loading User object to retrieve Twitter properties", e);
          }

          return;
        }

        if (!BlojsomUtils.checkNullOrBlank((String) blog.getProperty(TWITTER_SIGN_IN_IP))
            && !BlojsomUtils.checkNullOrBlank((String) blog.getProperty(TWITTER_PASSWORD_IP))) {

          String signIn = (String) blog.getProperty(TWITTER_SIGN_IN_IP);
          String password = (String) blog.getProperty(TWITTER_PASSWORD_IP);
          String updateText = (String) blog.getProperty(TWITTER_STATUS_UPDATE_TEXT_IP);

          if (BlojsomUtils.checkNullOrBlank(updateText)) {
            updateText = TWITTER_DEFAULT_STATUS_UPDATE_TEXT;
          }

          if (("true".equals(blog.getProperty(TWITTER_UPDATE_ON_ENTRY_ADDED_IP)))
              || ("true".equals(blog.getProperty(TWITTER_UPDATE_ON_ENTRY_UPDATED_IP)))) {
            String title = entryEvent.getEntry().getTitle();
            String twitterUpdate =
                BlojsomUtils.urlEncode(
                    BlojsomUtils.escapeString(
                        MessageFormat.format(updateText, new Object[] {title})));

            Authenticator.setDefault(new TwitterAuthenticator(signIn, password));

            try {
              URL url = new URL(_twitterUpdateURL);
              URLConnection urlConnection = url.openConnection();
              urlConnection.setUseCaches(false);
              urlConnection.setDoInput(true);
              urlConnection.setDoOutput(true);
              urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

              String twitterData = TWITTER_STATUS_PARAMETER + "=" + twitterUpdate;
              OutputStreamWriter twitterWriter =
                  new OutputStreamWriter(urlConnection.getOutputStream());
              twitterWriter.write(twitterData);
              twitterWriter.flush();
              twitterWriter.close();

              // Read all the text returned by the server
              BufferedReader twitterReader =
                  new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
              StringBuffer twitterReply = new StringBuffer();
              String input;

              while ((input = twitterReader.readLine()) != null) {
                twitterReply.append(input);
              }

              twitterReader.close();

              if (BlojsomUtils.checkNullOrBlank(twitterReply.toString())) {
                if (_logger.isErrorEnabled()) {
                  _logger.error("Error communicating update to Twitter");
                }
              } else {
                if (_logger.isDebugEnabled()) {
                  _logger.debug("Successfully sent update to Twitter");
                }
              }
            } catch (IOException e) {
              if (_logger.isErrorEnabled()) {
                _logger.error(e);
              }
            }
          } else {
            if (_logger.isDebugEnabled()) {
              _logger.debug(
                  "Twitter notification update not enabled for either add or update entry events");
            }
          }
        } else {
          if (_logger.isDebugEnabled()) {
            _logger.debug("Twitter sign in and/or password is null or blank");
          }
        }
      }
    }
  }