Example usage for org.apache.commons.lang3.exception ContextedException ContextedException

List of usage examples for org.apache.commons.lang3.exception ContextedException ContextedException

Introduction

In this page you can find the example usage for org.apache.commons.lang3.exception ContextedException ContextedException.

Prototype

public ContextedException(final Throwable cause) 

Source Link

Document

Instantiates ContextedException with cause, but without message.

Usage

From source file:com.rodaxsoft.mailgun.MailingListMemberManager.java

/**
 * Converts the <code>vars</code> object into an instance of the varsType and 
  * returns the members of the mailing list
 * @param varsType The target type for the <i>vars</i> object. 
 *        <i>Note: If no registered {@link Converter} is found for the varsType, 
 *        a {@link ContextedRuntimeException} will be thrown.</i>
 * @return A list of list member objects
 * @throws ContextedException If no registered {@link Converter} is found for the varsType
 * @see ConvertUtils#register(Converter, Class)
 * @since 0.2/* w  w w. j  ava 2 s.  c o m*/
 * 
 */
List<ListMember> getListMembers(Class<?> varsType) throws ContextedException {

    List<ListMember> members = null;

    if (varsType != null) {

        if (ConvertUtils.lookup(varsType) != null) {
            listMemberConverter.setVarClass(varsType);
        }

        else {
            ContextedException cre;
            cre = new ContextedException("No registered Converter for varType");
            cre.addContextValue("varType", varsType.getName());
            throw cre;
        }
    }

    final String path = "/lists/" + listAddress + "/members";
    RESTResponse response = invokeGet(path);

    if (response.success()) {
        JSONObject jsonObj = response.toJSONObject();
        JSONArray jsonArray = jsonObj.getJSONArray("items");
        @SuppressWarnings("unchecked")
        Iterator<JSONObject> iter = jsonArray.iterator();

        while (iter.hasNext()) {

            JSONObject obj = iter.next();

            if (null == members) {
                members = new ArrayList<>();
            }

            ListMember lm = (ListMember) ConvertUtils.convert(obj, ListMember.class);
            members.add(lm);
        }

    }

    return members;

}

From source file:com.rodaxsoft.mailgun.message.tools.MailgunSender.java

/**
 * Sets the default from email address or <code>sDefaultFromEmail</code> from the 
 * properties file if no <code>-f</code> option exists.
 * @param cmd Command line arguments//from   ww w.  ja va 2 s . co  m
 */
private static void setDefaultFromEmail(CommandLine cmd) throws ContextedException {
    if (!cmd.hasOption(FROM_EMAIL_ADDRESS_OPT)) {

        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<FileBasedConfiguration> builder;
        final Class<PropertiesConfiguration> propConfigClass = PropertiesConfiguration.class;
        builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(propConfigClass);
        builder.configure(params.properties().setFileName("config/mailgun-sender.properties"));

        try {
            FileBasedConfiguration config = builder.getConfiguration();
            sDefaultFromEmail = config.getString(DEFAULT_FROM_EMAIL_PROP_KEY);
            if (null == sDefaultFromEmail) {
                throw new ContextedException("Missing " + DEFAULT_FROM_EMAIL_PROP_KEY + " key")
                        .addContextValue(DEFAULT_FROM_EMAIL_PROP_KEY, sDefaultFromEmail).addContextValue("help",
                                "Must set from email address with option -f or in the `mailgun-sender.properties` file");
            }

            LOG.info("Configured mailgun-sender.properties");

        } catch (ConfigurationException e) {
            throw new ContextedException("Error loading `mailgun-sender.properties`", e);
        }
    }
}

From source file:com.rodaxsoft.mailgun.message.tools.MailgunSender.java

/**
 * Validates mailing list address//w  w  w .  j  a  va2  s  .  co  m
 * @param address The address to validate
 * @throws ContextedException if it's invalid or a processing error occurs
 */
private static void validateMailingListAddress(final String address) throws ContextedException {

    Collection<ListInfo> lists = MailgunManager.getMailingLists();

    Predicate<ListInfo> predicate = new Predicate<ListInfo>() {
        @Override
        public boolean evaluate(ListInfo info) {
            return info.getAddress().equals(address);
        }
    };

    if (null == IterableUtils.find(lists, predicate)) {
        throw new ContextedException("Invalid mailing address").addContextValue("address", address);
    }
}