This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
possibility to use other connection managers (besides HttpClient)
- Loading branch information
Vasily Karyaev
committed
Sep 24, 2012
1 parent
d193b6f
commit d2befa3
Showing
2 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/com/ecwid/mailchimp/MailChimpAPIConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
@@ -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(); | ||
} | ||
} |