/** * Mule Constant Contact Connector * * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ package org.mule.modules.constantcontact; import org.apache.abdera.Abdera; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Feed; import org.apache.abdera.parser.stax.FOMElement; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class Resources { public static final String API_BASE = "https://api.constantcontact.com/ws/customers/%s"; public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern()); private static final String ACCOUNT_EMAIL_ADDRESS_PATH = "/settings/emailaddresses"; private static final String CAMPAIGNS_PATH = "/campaigns"; private static final String ACTIVITIES_PATH = "/activities"; private static final String CONTACTS_PATH = "/contacts"; private static final String EVENT_PATH = "/events"; private static final String REGISTRANTS_PATH = "/registrants"; private static final String GUESTS_PATH = "/guests"; private static final String ATTENDANCE_STATUS_PATH = "/attendancestatus"; private static final String PAYMENT_STATUS_PATH = "/paymentstatus"; private static final String MAILING_LIST_PATH = "/lists"; private static final String MEMBERS_PATH = "/members"; private static final String ENCODING = "UTF-8"; private String baseUrl; public Resources(String username) { this.baseUrl = String.format(API_BASE, username); } public String activitiesCollection() { return baseUrl + ACTIVITIES_PATH; } public String activityResource(String activityIdOrLink) { return activitiesCollection() + '/' + getOrExtractId(activityIdOrLink); } public String accountEmailAddressCollection() { return baseUrl + ACCOUNT_EMAIL_ADDRESS_PATH; } public String accountEmailAddressResource(String emailIdOrLink) { return accountEmailAddressCollection() + "/" + getOrExtractId(emailIdOrLink); } public String campaignsCollection() { return baseUrl + CAMPAIGNS_PATH; } public String campaignsCollection(CampaignStatus campaignStatus) { String campaignsResourceUrl = campaignsCollection(); if (campaignStatus != null) { campaignsResourceUrl += "?status=" + campaignStatus; } return campaignsResourceUrl; } public String campaignDetails(String campaignIdOrLink) { return baseUrl + CAMPAIGNS_PATH + '/' + getOrExtractId(campaignIdOrLink); } public String campaignEvents(String campaignIdOrLink) { return baseUrl + CAMPAIGNS_PATH + '/' + getOrExtractId(campaignIdOrLink) + EVENT_PATH; } public String contactDetails(String contactIdOrLink) { return contacts() + '/' + getOrExtractId(contactIdOrLink); } public String contacts() { return baseUrl + CONTACTS_PATH; } public String contactDetails(List<String> emailAddresses) throws UnsupportedEncodingException { StringBuilder contactDetailsUrlBuilder = new StringBuilder(50); contactDetailsUrlBuilder.append(baseUrl).append(CONTACTS_PATH).append('?'); for (String emailAddress : emailAddresses) { contactDetailsUrlBuilder.append("email=").append(URLEncoder.encode(emailAddress, ENCODING)); } return contactDetailsUrlBuilder.toString(); } public String contactsByListType(Date updatedSince, ListType listType) { return new StringBuilder(50). append(contacts()). append("?updatedsince=").append(DATE_FORMAT.format(updatedSince)). append("&listtype=").append(listType). toString(); } public String contactsByListId(Date updatedSince, String listId) { return new StringBuilder(50). append(contacts()). append("?updatedsince=").append(DATE_FORMAT.format(updatedSince)). append("&listid=").append(getOrExtractId(listId)). toString(); } public String events() { return baseUrl + EVENT_PATH; } public String eventDetails(String eventIdOrLink) { return events() + '/' + getOrExtractId(eventIdOrLink); } public String eventRegistrants(String eventIdOrLink) { return eventDetails(eventIdOrLink) + REGISTRANTS_PATH; } public HttpGet eventRegistrantDetails(String eventId, String registrantId) { HttpGet httpGet = new HttpGet(events() + '/' + getOrExtractId(eventId) + REGISTRANTS_PATH + '/' + getOrExtractId(registrantId)); httpGet.setHeader(HttpHeaders.ACCEPT, "application/atom+xml"); return httpGet; } public String eventRegistrantGuests(String eventId, String registrantId) { return new StringBuilder(50). append(eventRegistrantDetails(eventId, registrantId).getURI()). append(GUESTS_PATH). toString(); } public String guestDetails(String eventId, String registrantId, String guestId) { return baseUrl + EVENT_PATH + '/' + getOrExtractId(eventId) + REGISTRANTS_PATH + '/' + getOrExtractId(registrantId) + '/' + GUESTS_PATH + '/' + getOrExtractId(guestId); } public String attendanceStatus(String eventId, String registrantId) { return baseUrl + EVENT_PATH + '/' + getOrExtractId(eventId) + REGISTRANTS_PATH + '/' + getOrExtractId(registrantId) + '/' + ATTENDANCE_STATUS_PATH; } public String paymentStatus(String eventId, String registrantId) { return baseUrl + EVENT_PATH + '/' + getOrExtractId(eventId) + REGISTRANTS_PATH + '/' + getOrExtractId(registrantId) + '/' + PAYMENT_STATUS_PATH; } private static String getOrExtractId(String idOrLink) { if (idOrLink.contains("/")) { return idOrLink.substring(idOrLink.lastIndexOf('/')); } else { return idOrLink; } } public String mailingLists() { return baseUrl + MAILING_LIST_PATH; } public String mailingListDetails(String mailingListId) { return mailingLists() + '/' + getOrExtractId(mailingListId); } public String mailingListMembers(String mailingListId) { return mailingListDetails(mailingListId) + MEMBERS_PATH; } public HttpPost createContact(String createContactRequest) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(baseUrl + CONTACTS_PATH); httpPost.setHeader(HttpHeaders.ACCEPT, "application/atom+xml"); httpPost.setEntity(new StringEntity(createContactRequest, "application/atom+xml", ENCODING)); return httpPost; } public HttpPost mailingList(String createMailingListRequest) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(baseUrl + MAILING_LIST_PATH); httpPost.setHeader(HttpHeaders.ACCEPT, "application/atom+xml"); httpPost.setEntity(new StringEntity(createMailingListRequest, "application/atom+xml", ENCODING)); return httpPost; } public HttpPost createCampaign(String createCamapignRequest) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(baseUrl + CAMPAIGNS_PATH); httpPost.setHeader(HttpHeaders.ACCEPT, "application/atom+xml"); httpPost.setEntity(new StringEntity(createCamapignRequest, "application/atom+xml", ENCODING)); return httpPost; } public HttpPut updateFeed(Feed feed) throws ConstantContactException { Entry entry = feed.getEntries().get(0); HttpPut httpPut = new HttpPut(getEntryUri(entry)); httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/atom+xml"); try { httpPut.setEntity(new StringEntity(entry.toString(), ENCODING)); return httpPut; } catch (UnsupportedEncodingException e) { throw new ConstantContactException(e); } } public String exportContactsActivity(String listUri, FileType fileType, Boolean exportOptDate, Boolean exportOptSource, Boolean exportListName, SortBy sortBy, List<Column> columns) throws ConstantContactException { try { StringBuilder exportContactsActivity = new StringBuilder(50). append(activitiesCollection()). append("?activityType=").append(urlEncode(ActivityType.EXPORT_CONTACTS)). append("&listId=").append(urlEncode(listUri)). append("&fileType=").append(urlEncode(fileType)). append("&exportOptDate=").append(urlEncode(exportOptDate)). append("&exportOptSource=").append(urlEncode(exportOptSource)). append("&exportListName=").append(urlEncode(exportListName)). append("&sortBy=").append(urlEncode(sortBy)); if (columns != null) { for (Column column : columns) { exportContactsActivity.append("&columns=").append(urlEncode(column)); } } return exportContactsActivity.toString(); } catch (UnsupportedEncodingException e) { throw new ConstantContactException(e); } } private String urlEncode(Object valueToEncode) throws UnsupportedEncodingException { return URLEncoder.encode(valueToEncode.toString(), ENCODING); } public HttpPut updateEventRegistrantPaymentStatus(Feed feed, PaymentStatus paymentStatus) throws ConstantContactException { // TODO validate this Entry entry = feed.getEntries().get(0); HttpPut httpPut = new HttpPut(getEntryUri(entry)); httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/atom+xml"); Object evaluate = new Abdera().getXPath().evaluate("//PaymentStatus/Status", feed); ((FOMElement) evaluate).setText(paymentStatus.toString()); try { httpPut.setEntity(new StringEntity(entry.toString(), ENCODING)); return httpPut; } catch (UnsupportedEncodingException e) { throw new ConstantContactException(e); } } private String getEntryUri(Entry entry) { return entry.getId().toASCIIString().replace("http://", "https://"); } public HttpPut updateContact(Feed updatedContact) throws ConstantContactException { Entry entry = updatedContact.getEntries().get(0); HttpPut httpPut = new HttpPut(getEntryUri(entry)); httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/atom+xml"); try { httpPut.setEntity(new StringEntity(entry.toString(), ENCODING)); return httpPut; } catch (UnsupportedEncodingException e) { throw new ConstantContactException(e); } } }