/**
* 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.Document;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.Parser;
import org.apache.abdera.parser.stax.FOMFeed;
import org.apache.abdera.util.Constants;
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 javax.xml.namespace.QName;
import java.io.StringReader;
public class ConstantContactClient {
public static final String END_EMAIL_TAG = "</Email>";
private Parser parser;
private RequestExecutor requestExecutor;
public ConstantContactClient(String apiKey, String username, String password) {
parser = new Abdera().getParser();
requestExecutor = new RequestExecutor(apiKey, username, password);
}
public String extractEmail(String response) {
if (response == null) {
return null;
}
int startIndex = response.indexOf("<Email xmlns=\"http://ws.constantcontact.com/ns/1.0/\" ");
int endIndex = response.indexOf(END_EMAIL_TAG) + END_EMAIL_TAG.length();
return response.substring(startIndex, endIndex);
}
public Feed populate(String response) {
Document<? extends Element> doc = parser.parse(new StringReader(response));
QName qName = doc.getRoot().getQName();
if (qName.equals(Constants.ENTRY)) {
FOMFeed feed = new FOMFeed();
feed.addEntry((Entry) doc.getRoot());
return feed;
} else if (qName.equals(Constants.FEED)) {
Feed feed = (Feed) doc.getRoot();
if (feed.getLink("next") != null) {
// TODO get more responses and merge them
// <link href="/ws/customers/joesflowers/contacts?next=23654&updatedsince=2009-12-01T20%3A18%3A25.503Z&
// listtype=active&ous=2009-12-01T01%3A00%3A00.000Z" rel="next" />
// <link href="/ws/customers/joesflowers/contacts?updatedsince=2009-12-01T01%3A00%3A00.000Z&listtype=active"
}
return feed;
} else {
throw new UnsupportedOperationException("Cannot parse response of type: " + qName);
}
}
public Feed doGetRequest(String url) throws ConstantContactException {
String response = requestExecutor.doGetRequest(url);
return populate(response);
}
public void doDeleteRequest(String url) throws ConstantContactException {
requestExecutor.doDeleteRequest(url);
}
public void doPostRequest(HttpPost httpPost) throws ConstantContactException {
requestExecutor.doPostRequest(httpPost);
}
public void doDeleteRequest(String url, int expectedHttpStatusCode) throws ConstantContactException {
requestExecutor.doDeleteRequest(url, expectedHttpStatusCode);
}
public Feed doGetRequest(HttpGet httpGet) throws ConstantContactException {
String response = requestExecutor.doGetRequest(httpGet);
return populate(response);
}
public void doPostRequest(String url) throws ConstantContactException {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
doPostRequest(httpPost);
}
public void doPutRequest(HttpPut httpPut) throws ConstantContactException {
requestExecutor.doPutRequest(httpPut);
}
}