Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
possibility to use other connection managers (besides HttpClient)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasily Karyaev committed Sep 24, 2012
1 parent d193b6f commit d2befa3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/ecwid/mailchimp/MailChimpAPIConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2012 Ecwid, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ecwid.mailchimp;

import java.io.Closeable;
import java.io.IOException;

/**
* Abstract connection manager to access MailChimp API service point.
*
* @author Vasily Karyaev <[email protected]>
*/
public interface MailChimpAPIConnection extends Closeable {

/**
* Make a POST request to MailChimp API service point.
*
* @param url URL to post data to
* @param payload data to post
*
* @return service endpoint response body if the response was successful (a 2xx status code)
*
* @throws IOException if an I/O error occurred during the communication,
* or if the service point response was unsuccessful (>= 300 status code)
*/
public String post(String url, String payload) throws IOException;

/**
* Release resources associated with the connection.
*/
@Override
public void close();
}
46 changes: 39 additions & 7 deletions src/main/java/com/ecwid/mailchimp/MailChimpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand All @@ -35,18 +36,48 @@
*
* @author Vasily Karyaev <[email protected]>
*/
public class MailChimpClient {
public class MailChimpClient implements Closeable {
private static final Logger log = Logger.getLogger(MailChimpClient.class.getName());

private final HttpClient http = new DefaultHttpClient();
private final MailChimpAPIConnection connection;

/**
* Construct a {@code MailChimpClient} object accessing MailChimp API service point
* through the specified connection manager.
*
* @param connection connection manager to be used to access the service point
*/
public MailChimpClient(MailChimpAPIConnection connection) {
this.connection = connection;
}

/**
* Construct a {@code MailChimpClient} object accessing MailChimp API service point
* through the default connection manager (based on Apache HttpClient library).
*/
public MailChimpClient() {
this(new MailChimpAPIConnection() {
final HttpClient http = new DefaultHttpClient();

@Override
public String post(String url, String payload) throws IOException {
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(URLEncoder.encode(payload, "UTF-8")));
return http.execute(post, new BasicResponseHandler());
}

@Override
public void close() {
http.getConnectionManager().shutdown();
}
});
}

private String execute(String url, String request) throws IOException {
if(log.isLoggable(Level.FINE)) {
log.fine("Post to "+url+" : "+request);
}
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(URLEncoder.encode(request, "UTF-8")));
String response = http.execute(post, new BasicResponseHandler());
String response = connection.post(url, request);
if(log.isLoggable(Level.FINE)) {
log.fine("Response: "+response);
}
Expand Down Expand Up @@ -99,9 +130,10 @@ private String buildUrl(MailChimpMethod<?> method) throws UnsupportedEncodingExc
}

/**
* Release resources.
* Release resources associated with the connection to MailChimp API service point.
*/
@Override
public void close() {
http.getConnectionManager().shutdown();
connection.close();
}
}

0 comments on commit d2befa3

Please sign in to comment.