|
| 1 | + |
| 2 | +package com.sparkpost.samples; |
| 3 | + |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | + |
| 9 | +import org.apache.commons.lang3.StringUtils; |
| 10 | +import org.apache.log4j.Level; |
| 11 | +import org.apache.log4j.Logger; |
| 12 | + |
| 13 | +import com.sparkpost.Client; |
| 14 | +import com.sparkpost.exception.SparkPostException; |
| 15 | +import com.sparkpost.model.SuppressionList; |
| 16 | +import com.sparkpost.model.SuppressionListEntry; |
| 17 | +import com.sparkpost.model.responses.Response; |
| 18 | +import com.sparkpost.resources.ResourceSuppressionList; |
| 19 | +import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp; |
| 20 | +import com.sparkpost.transport.RestConnection; |
| 21 | + |
| 22 | +/** |
| 23 | + * Lists all Sending Domains |
| 24 | + */ |
| 25 | +public class MandrillBlacklistImport extends SparkPostBaseApp { |
| 26 | + |
| 27 | + static final Logger logger = Logger.getLogger(MandrillBlacklistImport.class); |
| 28 | + |
| 29 | + private Client client; |
| 30 | + |
| 31 | + public static class Fields { |
| 32 | + |
| 33 | + public static final int EMAIL_COL = 0; |
| 34 | + public static final int REASON_COL = 1; |
| 35 | + public static final int DESCRIPTION_COL = 2; |
| 36 | + public static final int CREATED_AT_COL = 3; |
| 37 | + public static final int EXPIRES_AT_COL = 4; |
| 38 | + public static final int LAST_EVENT_AT_COL = 5; |
| 39 | + public static final int EXPIRES_AT2_COL = 6; |
| 40 | + public static final int SUBACCOUNT_COL = 7;// Unused |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + public static void main(String[] args) throws SparkPostException, IOException { |
| 45 | + Logger.getRootLogger().setLevel(Level.DEBUG); |
| 46 | + |
| 47 | + MandrillBlacklistImport sample = new MandrillBlacklistImport(); |
| 48 | + sample.runApp(); |
| 49 | + } |
| 50 | + |
| 51 | + /* |
| 52 | + * How to export Mandrill blacklist: https://mandrill.zendesk.com/hc/en-us/articles/205582997 |
| 53 | + * Beware some samples I've received from Mandrill are not 100% correctly formated. I've found it necessary to remove some corrupt lines. |
| 54 | + */ |
| 55 | + private void runApp() throws SparkPostException, IOException { |
| 56 | + |
| 57 | + this.client = this.newConfiguredClient(); |
| 58 | + RestConnection connection = new RestConnection(this.client, getEndPoint()); |
| 59 | + |
| 60 | + SuppressionList suppressionList = new SuppressionList(); |
| 61 | + suppressionList.setRecipients(new ArrayList<SuppressionListEntry>()); |
| 62 | + |
| 63 | + // Load sample Mandrill Blacklist file |
| 64 | + String csvFile = "samples/mandrillBlacklistExample.csv"; |
| 65 | + BufferedReader br = null; |
| 66 | + String line = ""; |
| 67 | + String cvsSplitBy = ","; |
| 68 | + |
| 69 | + int row = 0; |
| 70 | + try { |
| 71 | + br = new BufferedReader(new FileReader(csvFile)); |
| 72 | + while ((line = br.readLine()) != null) { |
| 73 | + row++; |
| 74 | + if (StringUtils.isEmpty(line)) { |
| 75 | + System.out.println("Warning: LN:" + row + " is empty"); |
| 76 | + continue; |
| 77 | + } |
| 78 | + String[] entryRow = line.split(cvsSplitBy); |
| 79 | + |
| 80 | + if (entryRow.length < Fields.EXPIRES_AT2_COL) { |
| 81 | + System.out.println("Error: LN:" + row + " too short (" + entryRow.length + "),\n \"" + line + "\"\n"); |
| 82 | + } |
| 83 | + |
| 84 | + String email = entryRow[Fields.EMAIL_COL]; |
| 85 | + System.out.println("Parse: " + line); |
| 86 | + |
| 87 | + if (StringUtils.isEmpty(email)) { |
| 88 | + // Ignore rows that do not have an email address since it is required. Should we fail instead? |
| 89 | + System.out.println("Ignore header row"); |
| 90 | + continue; |
| 91 | + } else if (email.equalsIgnoreCase("email")) { |
| 92 | + // First row is column names so skip it |
| 93 | + System.out.println("Warning: LN:" + row + " Ignore blank email row\""); |
| 94 | + continue; |
| 95 | + } else if (StringUtils.isEmpty(entryRow[Fields.REASON_COL]) || !entryRow[Fields.REASON_COL].equalsIgnoreCase("hard-bounce")) { |
| 96 | + // ignore empty reason or soft-bounces |
| 97 | + //System.out.println("Ignore reason: " + entryRow[Fields.REASON_COL]); |
| 98 | + System.out.println("LN:" + row + " Ignore reason: " + entryRow[Fields.REASON_COL]); |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + System.out.println("LN:" + row + " adding " + entryRow[Fields.EMAIL_COL]); |
| 103 | + |
| 104 | + SuppressionListEntry entry = new SuppressionListEntry(); |
| 105 | + // MBL: = Mandrill Blacklist |
| 106 | + entry.setDescription("MBL: " + line); |
| 107 | + entry.setEmail(entryRow[Fields.EMAIL_COL]); |
| 108 | + // Assumes Mandrill blacklist is only for non-transactional email |
| 109 | + entry.setTransactional(false); |
| 110 | + entry.setNonTransactional(true); |
| 111 | + |
| 112 | + // Leave off source so it is set to "Manually Added" |
| 113 | + // entry.setSource(null); |
| 114 | + |
| 115 | + // Maybe a Set is better to make sure addresses are unique |
| 116 | + suppressionList.getRecipients().add(entry); |
| 117 | + } |
| 118 | + |
| 119 | + } finally { |
| 120 | + System.out.println("Last row: " + row); |
| 121 | + System.out.println("Last Line: " + line); |
| 122 | + if (br != null) { |
| 123 | + try { |
| 124 | + br.close(); |
| 125 | + } catch (IOException e) { |
| 126 | + e.printStackTrace(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + System.out.println("Done"); |
| 132 | + |
| 133 | + if (suppressionList.getRecipients() == null || suppressionList.getRecipients().size() == 0) { |
| 134 | + System.out.println("There are no members of blacklist to add or update"); |
| 135 | + return; |
| 136 | + |
| 137 | + } |
| 138 | + Response result = ResourceSuppressionList.insertOrUpdateBulk(connection, suppressionList); |
| 139 | + System.out.println("Supression List Result: " + result); |
| 140 | + |
| 141 | + } |
| 142 | +} |
0 commit comments