From addbdaf48b355c4a76c272edd88c6e8966b2a7ef Mon Sep 17 00:00:00 2001 From: Vasily Karyaev Date: Mon, 24 Sep 2012 10:37:41 +0400 Subject: [PATCH] refactoring --- .../com/ecwid/mailchimp/MailChimpClient.java | 42 ++++++----------- .../connection/DefaultConnectionManager.java | 46 +++++++++++++++++++ .../MailChimpConnectionManager.java} | 4 +- 3 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/ecwid/mailchimp/connection/DefaultConnectionManager.java rename src/main/java/com/ecwid/mailchimp/{MailChimpAPIConnection.java => connection/MailChimpConnectionManager.java} (92%) diff --git a/src/main/java/com/ecwid/mailchimp/MailChimpClient.java b/src/main/java/com/ecwid/mailchimp/MailChimpClient.java index 4fc75b8..fcb6316 100644 --- a/src/main/java/com/ecwid/mailchimp/MailChimpClient.java +++ b/src/main/java/com/ecwid/mailchimp/MailChimpClient.java @@ -15,6 +15,8 @@ */ package com.ecwid.mailchimp; +import com.ecwid.mailchimp.connection.DefaultConnectionManager; +import com.ecwid.mailchimp.connection.MailChimpConnectionManager; import com.ecwid.mailchimp.internal.gson.MailChimpGsonFactory; import com.google.gson.Gson; import com.google.gson.JsonElement; @@ -25,11 +27,6 @@ import java.net.URLEncoder; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.DefaultHttpClient; /** * MailChimp API wrapper. @@ -39,38 +36,27 @@ public class MailChimpClient implements Closeable { private static final Logger log = Logger.getLogger(MailChimpClient.class.getName()); - private final MailChimpAPIConnection connection; + private final MailChimpConnectionManager 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 + * through the {@linkplain DefaultConnectionManager default connection manager}. */ - public MailChimpClient(MailChimpAPIConnection connection) { - this.connection = connection; + public MailChimpClient() { + this(new DefaultConnectionManager()); } /** * Construct a {@code MailChimpClient} object accessing MailChimp API service point - * through the default connection manager (based on Apache HttpClient library). + * through the specified connection manager. + *

+ * Use this constructor if the {@linkplain DefaultConnectionManager default connection manager} + * is not suitable (for instance, in GAE environment). + * + * @param connection connection manager to be used to access the service point */ - 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(); - } - }); + public MailChimpClient(MailChimpConnectionManager connection) { + this.connection = connection; } private String execute(String url, String request) throws IOException { diff --git a/src/main/java/com/ecwid/mailchimp/connection/DefaultConnectionManager.java b/src/main/java/com/ecwid/mailchimp/connection/DefaultConnectionManager.java new file mode 100644 index 0000000..20eb50f --- /dev/null +++ b/src/main/java/com/ecwid/mailchimp/connection/DefaultConnectionManager.java @@ -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.connection; + +import java.io.IOException; +import java.net.URLEncoder; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicResponseHandler; +import org.apache.http.impl.client.DefaultHttpClient; + +/** + * Default implementation of {@link MailChimpConnectionManager} + * which uses Apache HttpClient library to access MailChimp API service point. + * + * @author Vasily Karyaev + */ +public class DefaultConnectionManager implements MailChimpConnectionManager { + private 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(); + } +} diff --git a/src/main/java/com/ecwid/mailchimp/MailChimpAPIConnection.java b/src/main/java/com/ecwid/mailchimp/connection/MailChimpConnectionManager.java similarity index 92% rename from src/main/java/com/ecwid/mailchimp/MailChimpAPIConnection.java rename to src/main/java/com/ecwid/mailchimp/connection/MailChimpConnectionManager.java index 111cca4..005462b 100644 --- a/src/main/java/com/ecwid/mailchimp/MailChimpAPIConnection.java +++ b/src/main/java/com/ecwid/mailchimp/connection/MailChimpConnectionManager.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.ecwid.mailchimp; +package com.ecwid.mailchimp.connection; import java.io.Closeable; import java.io.IOException; @@ -23,7 +23,7 @@ * * @author Vasily Karyaev */ -public interface MailChimpAPIConnection extends Closeable { +public interface MailChimpConnectionManager extends Closeable { /** * Make a POST request to MailChimp API service point.