Changed functions name, added errors messages, and optimized the code
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/ea3a41cb Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/ea3a41cb Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/ea3a41cb Branch: refs/heads/master Commit: ea3a41cbd072b4073ad04d4d0ad59caf705df554 Parents: 89672bd Author: Dinesh <[email protected]> Authored: Wed Nov 29 14:13:44 2017 +0100 Committer: Dinesh <[email protected]> Committed: Wed Nov 29 14:13:44 2017 +0100 ---------------------------------------------------------------------- .../AddVisitorToMailChimpListAction.java | 55 +++++++ .../AddVisitorToMailChimpListsAction.java | 55 ------- .../RemoveVisitorFromMailChimpListAction.java | 54 +++++++ .../RemoveVisitorFromMailChimpListsAction.java | 54 ------- ...subscribeVisitorFromMailChimpListAction.java | 59 +++++++ ...ubscribeVisitorFromMailChimpListsAction.java | 59 ------- .../AddVisitorToMailChimpListAction.json | 20 +++ .../AddVisitorToMailChimpListsAction.json | 20 --- .../RemoveVisitorFromMailChimpListAction.json | 20 +++ .../RemoveVisitorFromMailChimpListsAction.json | 20 --- ...subscribeVisitorFromMailChimpListAction.json | 20 +++ ...ubscribeVisitorFromMailChimpListsAction.json | 20 --- .../resources/OSGI-INF/blueprint/blueprint.xml | 12 +- .../unomi/mailchimp/services/HttpUtils.java | 10 +- .../services/internal/MailChimpServiceImpl.java | 153 ++++++++----------- 15 files changed, 299 insertions(+), 332 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListAction.java b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListAction.java new file mode 100644 index 0000000..152e72b --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListAction.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.unomi.mailchimp.actions; + +import org.apache.unomi.api.Event; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.api.actions.ActionExecutor; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.mailchimp.services.MailChimpResult; +import org.apache.unomi.mailchimp.services.MailChimpService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class AddVisitorToMailChimpListAction implements ActionExecutor { + private static Logger logger = LoggerFactory.getLogger(AddVisitorToMailChimpListAction.class); + private MailChimpService mailChimpService; + + public void setMailChimpService(MailChimpService mailChimpService) { + this.mailChimpService = mailChimpService; + } + + @Override + public int execute(Action action, Event event) { + + MailChimpResult result = mailChimpService.addToMCList(event.getProfile(), action); + switch (result) { + + case UPDATED: + logger.info("The visitor has been successfully added in MailChimp list and subscribed"); + break; + case NO_CHANGE: + logger.info("The visitor is already in the MailChimp list and subscribed"); + break; + default: + break; + } + return EventService.NO_CHANGE; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListsAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListsAction.java b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListsAction.java deleted file mode 100644 index 3297887..0000000 --- a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListsAction.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.unomi.mailchimp.actions; - -import org.apache.unomi.api.Event; -import org.apache.unomi.api.actions.Action; -import org.apache.unomi.api.actions.ActionExecutor; -import org.apache.unomi.api.services.EventService; -import org.apache.unomi.mailchimp.services.MailChimpResult; -import org.apache.unomi.mailchimp.services.MailChimpService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class AddVisitorToMailChimpListsAction implements ActionExecutor { - private static Logger logger = LoggerFactory.getLogger(AddVisitorToMailChimpListsAction.class); - private MailChimpService mailChimpService; - - public void setMailChimpService(MailChimpService mailChimpService) { - this.mailChimpService = mailChimpService; - } - - @Override - public int execute(Action action, Event event) { - - MailChimpResult result = mailChimpService.addToMCList(event.getProfile(), action); - switch (result) { - - case UPDATED: - logger.info("The visitor has been successfully added in MailChimp list and subscribed"); - break; - case NO_CHANGE: - logger.info("The visitor is already in the MailChimp list and subscribed"); - break; - default: - break; - } - return EventService.NO_CHANGE; - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListAction.java b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListAction.java new file mode 100644 index 0000000..c83fbbb --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListAction.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.unomi.mailchimp.actions; + +import org.apache.unomi.api.Event; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.api.actions.ActionExecutor; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.mailchimp.services.MailChimpResult; +import org.apache.unomi.mailchimp.services.MailChimpService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RemoveVisitorFromMailChimpListAction implements ActionExecutor { + private static Logger logger = LoggerFactory.getLogger(RemoveVisitorFromMailChimpListAction.class); + private MailChimpService mailChimpService; + + public void setMailChimpService(MailChimpService mailChimpService) { + this.mailChimpService = mailChimpService; + } + + @Override + public int execute(Action action, Event event) { + MailChimpResult result = mailChimpService.removeFromMCList(event.getProfile(), action); + switch (result) { + + case REMOVED: + logger.info("The visitor has been successfully removed from MailChimp list"); + break; + case NO_CHANGE: + logger.info("Visitor was not part of the list"); + break; + default: + break; + } + return EventService.NO_CHANGE; + + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListsAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListsAction.java b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListsAction.java deleted file mode 100644 index 329aff7..0000000 --- a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListsAction.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.unomi.mailchimp.actions; - -import org.apache.unomi.api.Event; -import org.apache.unomi.api.actions.Action; -import org.apache.unomi.api.actions.ActionExecutor; -import org.apache.unomi.api.services.EventService; -import org.apache.unomi.mailchimp.services.MailChimpResult; -import org.apache.unomi.mailchimp.services.MailChimpService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class RemoveVisitorFromMailChimpListsAction implements ActionExecutor { - private static Logger logger = LoggerFactory.getLogger(RemoveVisitorFromMailChimpListsAction.class); - private MailChimpService mailChimpService; - - public void setMailChimpService(MailChimpService mailChimpService) { - this.mailChimpService = mailChimpService; - } - - @Override - public int execute(Action action, Event event) { - MailChimpResult result = mailChimpService.removeFromMCList(event.getProfile(), action); - switch (result) { - - case REMOVED: - logger.info("The visitor has been successfully removed from MailChimp list"); - break; - case NO_CHANGE: - logger.info("Visitor was not part of the list"); - break; - default: - break; - } - return EventService.NO_CHANGE; - - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListAction.java b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListAction.java new file mode 100644 index 0000000..078107e --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListAction.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.unomi.mailchimp.actions; + +import org.apache.unomi.api.Event; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.api.actions.ActionExecutor; +import org.apache.unomi.api.services.EventService; +import org.apache.unomi.mailchimp.services.MailChimpResult; +import org.apache.unomi.mailchimp.services.MailChimpService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UnsubscribeVisitorFromMailChimpListAction implements ActionExecutor { + private static Logger logger = LoggerFactory.getLogger(UnsubscribeVisitorFromMailChimpListAction.class); + private MailChimpService mailChimpService; + + public void setMailChimpService(MailChimpService mailChimpService) { + this.mailChimpService = mailChimpService; + } + + @Override + public int execute(Action action, Event event) { + MailChimpResult result = mailChimpService.unsubscribeFromMCList(event.getProfile(), action); + switch (result) { + + case UPDATED: + logger.info("The visitor has been successfully unsubscribed from MailChimp list"); + break; + case NO_CHANGE: + logger.info("Visitor is already unsubscribed"); + break; + default: + break; + } + return EventService.NO_CHANGE; + + } +} + + + + + http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListsAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListsAction.java b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListsAction.java deleted file mode 100644 index e6884e8..0000000 --- a/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListsAction.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.unomi.mailchimp.actions; - -import org.apache.unomi.api.Event; -import org.apache.unomi.api.actions.Action; -import org.apache.unomi.api.actions.ActionExecutor; -import org.apache.unomi.api.services.EventService; -import org.apache.unomi.mailchimp.services.MailChimpResult; -import org.apache.unomi.mailchimp.services.MailChimpService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class UnsubscribeVisitorFromMailChimpListsAction implements ActionExecutor { - private static Logger logger = LoggerFactory.getLogger(UnsubscribeVisitorFromMailChimpListsAction.class); - private MailChimpService mailChimpService; - - public void setMailChimpService(MailChimpService mailChimpService) { - this.mailChimpService = mailChimpService; - } - - @Override - public int execute(Action action, Event event) { - MailChimpResult result = mailChimpService.unsubscribeFromMCList(event.getProfile(), action); - switch (result) { - - case UPDATED: - logger.info("The visitor has been successfully unsubscribed from MailChimp list"); - break; - case NO_CHANGE: - logger.info("Visitor is already unsubscribed"); - break; - default: - break; - } - return EventService.NO_CHANGE; - - } -} - - - - - http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListAction.json b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListAction.json new file mode 100644 index 0000000..d1d6421 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListAction.json @@ -0,0 +1,20 @@ +{ + "metadata": { + "id": "addVisitorToMailChimpListAction", + "name": "Add the visitor to a MailChimp list", + "description": "This action add the visitor to a defined MailChimp list", + "systemTags": [ + "event", + "availableToEndUser" + ], + "readOnly": true + }, + "actionExecutor": "addVisitorToMailChimpList", + "parameters": [ + { + "id": "listIdentifier", + "type": "string", + "multivalued": false + } + ] +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListsAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListsAction.json b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListsAction.json deleted file mode 100644 index 9f7bc84..0000000 --- a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListsAction.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "metadata": { - "id": "addVisitorToMailChimpListsAction", - "name": "Add the visitor to a MailChimp list", - "description": "This action add the visitor to a defined MailChimp list", - "systemTags": [ - "event", - "availableToEndUser" - ], - "readOnly": true - }, - "actionExecutor": "addVisitorToMailChimpLists", - "parameters": [ - { - "id": "listIdentifier", - "type": "string", - "multivalued": false - } - ] -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListAction.json b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListAction.json new file mode 100644 index 0000000..7cbcf32 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListAction.json @@ -0,0 +1,20 @@ +{ + "metadata": { + "id": "removeVisitorFromMailChimpListAction", + "name": "Remove a visitor from a MailChimp List", + "description": "Remove the visitor from a defined MailChimp list", + "systemTags": [ + "event", + "availableToEndUser" + ], + "readOnly": true + }, + "actionExecutor": "removeVisitorFromMailChimpList", + "parameters": [ + { + "id": "listIdentifier", + "type": "string", + "multivalued": false + } + ] +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListsAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListsAction.json b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListsAction.json deleted file mode 100644 index 1644cd1..0000000 --- a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListsAction.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "metadata": { - "id": "removeVisitorFromMailChimpListsAction", - "name": "Remove a visitor from a MailChimp List", - "description": "Remove the visitor from a defined MailChimp list", - "systemTags": [ - "event", - "availableToEndUser" - ], - "readOnly": true - }, - "actionExecutor": "removeVisitorFromMailChimpLists", - "parameters": [ - { - "id": "listIdentifier", - "type": "string", - "multivalued": false - } - ] -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListAction.json b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListAction.json new file mode 100644 index 0000000..ea2dc95 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListAction.json @@ -0,0 +1,20 @@ +{ + "metadata": { + "id": "unsubscribeVisitorFromMailChimpListAction", + "name": "Unsubscribe a visitor from a MailChimp List", + "description": "Unsubscribe the visitor from a defined MailChimp list", + "systemTags": [ + "event", + "availableToEndUser" + ], + "readOnly": true + }, + "actionExecutor": "unsubscribeVisitorFromMailChimpList", + "parameters": [ + { + "id": "listIdentifier", + "type": "string", + "multivalued": false + } + ] +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListsAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListsAction.json b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListsAction.json deleted file mode 100644 index 0dfad79..0000000 --- a/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListsAction.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "metadata": { - "id": "unsubscribeVisitorFromMailChimpListsAction", - "name": "Unsubscribe a visitor from a MailChimp List", - "description": "Unsubscribe the visitor from a defined MailChimp list", - "systemTags": [ - "event", - "availableToEndUser" - ], - "readOnly": true - }, - "actionExecutor": "unsubscribeVisitorFromMailChimpLists", - "parameters": [ - { - "id": "listIdentifier", - "type": "string", - "multivalued": false - } - ] -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/actions/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/extensions/unomi-mailchimp/actions/src/main/resources/OSGI-INF/blueprint/blueprint.xml index d0a2cbe..b8b0f01 100644 --- a/extensions/unomi-mailchimp/actions/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/extensions/unomi-mailchimp/actions/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -25,27 +25,27 @@ <service auto-export="interfaces"> <service-properties> - <entry key="actionExecutorId" value="addVisitorToMailChimpLists"/> + <entry key="actionExecutorId" value="addVisitorToMailChimpList"/> </service-properties> - <bean class="org.apache.unomi.mailchimp.actions.AddVisitorToMailChimpListsAction"> + <bean class="org.apache.unomi.mailchimp.actions.AddVisitorToMailChimpListAction"> <property name="mailChimpService" ref="mailChimpService"/> </bean> </service> <service auto-export="interfaces"> <service-properties> - <entry key="actionExecutorId" value="removeVisitorFromMailChimpLists"/> + <entry key="actionExecutorId" value="removeVisitorFromMailChimpList"/> </service-properties> - <bean class="org.apache.unomi.mailchimp.actions.RemoveVisitorFromMailChimpListsAction"> + <bean class="org.apache.unomi.mailchimp.actions.RemoveVisitorFromMailChimpListAction"> <property name="mailChimpService" ref="mailChimpService"/> </bean> </service> <service auto-export="interfaces"> <service-properties> - <entry key="actionExecutorId" value="unsubscribeVisitorFromMailChimpLists"/> + <entry key="actionExecutorId" value="unsubscribeVisitorFromMailChimpList"/> </service-properties> - <bean class="org.apache.unomi.mailchimp.actions.UnsubscribeVisitorFromMailChimpListsAction"> + <bean class="org.apache.unomi.mailchimp.actions.UnsubscribeVisitorFromMailChimpListAction"> <property name="mailChimpService" ref="mailChimpService"/> </bean> </service> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/HttpUtils.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/HttpUtils.java b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/HttpUtils.java index 03c1e4a..d87fa5f 100644 --- a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/HttpUtils.java +++ b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/HttpUtils.java @@ -84,7 +84,7 @@ public class HttpUtils { return executeRequest(httpClient, request); } catch (UnsupportedEncodingException e) { - logger.error("The subDomain or the ApiKey were wrong {}", e.getMessage()); + logger.error("Error when executing request", e); return null; } } @@ -95,7 +95,7 @@ public class HttpUtils { return extractResponse(response); } catch (IOException e) { - logger.error("The subDomain or the ApiKey were wrong {}", e.getMessage()); + logger.error("Error when executing request", e); return null; } } @@ -114,7 +114,7 @@ public class HttpUtils { try { responseString = EntityUtils.toString(response.getEntity()); } catch (IOException e) { - logger.error("Error when retrieving entity response {}", e.getMessage()); + logger.error("Error when parsing entity response", e); return null; } } @@ -129,13 +129,13 @@ public class HttpUtils { try { response.close(); } catch (IOException e) { - logger.error("Error when trying to close response {}", e.getMessage()); + logger.error("Error when trying to close response", e); } } return jsonNode; } catch (IOException e) { - logger.error("Error when parsing response with ObjectMapper {}", e.getMessage()); + logger.error("Error when parsing response with ObjectMapper", e); return null; } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/ea3a41cb/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailChimpServiceImpl.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailChimpServiceImpl.java b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailChimpServiceImpl.java index 4da5b00..d2001ab 100644 --- a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailChimpServiceImpl.java +++ b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailChimpServiceImpl.java @@ -35,8 +35,24 @@ import java.util.HashMap; import java.util.List; public class MailChimpServiceImpl implements MailChimpService { + private static final String LISTS = "lists"; + private static final String ID = "id"; + private static final String NAME = "name"; + private static final String MERGE_FIELDS = "merge_fields"; + private static final String EMAIL_TYPE = "email_type"; + private static final String EMAIL_ADDRESS = "email_address"; + private static final String EMAIL = "email"; + private static final String ERRORS = "errors"; + private static final String FIRST_NAME = "firstName"; + private static final String LAST_NAME = "lastName"; + private static final String MEMBERS = "members"; + private static final String FNAME = "FNAME"; + private static final String LNAME = "LNAME"; + private static final String LIST_IDENTIFIER = "listIdentifier"; + private static final String STATUS = "status"; + private static final String SUBSCRIBED = "subscribed"; + private static final String UNSUBSCRIBED = "unsubscribed"; private static Logger logger = LoggerFactory.getLogger(MailChimpServiceImpl.class); - private String apiKey; private String urlSubDomain; private CloseableHttpClient httpClient; @@ -47,12 +63,12 @@ public class MailChimpServiceImpl implements MailChimpService { if (isMailChimpConnectorConfigured()) { JsonNode response = HttpUtils.executeGetRequest(httpClient, getBaseUrl() + "/lists", getHeaders()); if (response != null) { - if (response.has("lists") && response.get("lists").size() > 0) { - for (JsonNode list : response.get("lists")) { - if (list.has("id") && list.has("name")) { + if (response.has(LISTS) && response.get(LISTS).size() > 0) { + for (JsonNode list : response.get(LISTS)) { + if (list.has(ID) && list.has(NAME)) { HashMap<String, String> mcListInfo = new HashMap<>(); - mcListInfo.put("id", list.get("id").asText()); - mcListInfo.put("name", list.get("name").asText()); + mcListInfo.put(ID, list.get(ID).asText()); + mcListInfo.put(NAME, list.get(NAME).asText()); mcLists.add(mcListInfo); } else { logger.warn("Missing mandatory information for list, {}", list.asText()); @@ -72,54 +88,46 @@ public class MailChimpServiceImpl implements MailChimpService { return MailChimpResult.ERROR; } - String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + String listIdentifier = (String) action.getParameterValues().get(LIST_IDENTIFIER); if (StringUtils.isBlank(listIdentifier)) { logger.error("MailChimp list identifier not found"); return MailChimpResult.ERROR; } JsonNode member = isMemberOfMailChimpList(profile, action); - if (member != null) { - if (member.has("status")) { - if (member.get("status").asText().equals("unsubscribed")) { - logger.info("The visitor is already in the list, this status is unsubscribed"); - JSONObject body = new JSONObject(); - body.put("status", "subscribed"); - MailChimpResult response = updateSubscription(listIdentifier, body.toString(), member, true); - return updateSubscription(listIdentifier, body.toString(), member, true); - } - return MailChimpResult.NO_CHANGE; + if (member != null && member.has(STATUS)) { + if (member.get(STATUS).asText().equals(UNSUBSCRIBED)) { + logger.info("The visitor is already in the MailChimp list, his status is unsubscribed"); + JSONObject body = new JSONObject(); + body.put(STATUS, SUBSCRIBED); + return updateSubscription(listIdentifier, body.toString(), member, true); } + return MailChimpResult.NO_CHANGE; } JSONObject nameStruct = new JSONObject(); - nameStruct.put("FNAME", profile.getProperty("firstName").toString()); - nameStruct.put("LNAME", profile.getProperty("lastName").toString()); + nameStruct.put(FNAME, profile.getProperty(FIRST_NAME).toString()); + nameStruct.put(LNAME, profile.getProperty(LAST_NAME).toString()); JSONObject userData = new JSONObject(); - userData.put("merge_fields", nameStruct); - userData.put("email_type", "html"); - userData.put("email_address", profile.getProperty("email").toString()); - userData.put("status", "subscribed"); + userData.put(MERGE_FIELDS, nameStruct); + userData.put(EMAIL_TYPE, "html"); + userData.put(EMAIL_ADDRESS, profile.getProperty(EMAIL).toString()); + userData.put(STATUS, SUBSCRIBED); JSONArray dataMember = new JSONArray(); dataMember.put(userData); JSONObject body = new JSONObject(); - body.put("members", dataMember); + body.put(MEMBERS, dataMember); JsonNode response = HttpUtils.executePostRequest(httpClient, getBaseUrl() + "/lists/" + listIdentifier, getHeaders(), body.toString()); - if (response != null) { - if (response.has("errors") && response.get("errors").elements().hasNext() && response.get("errors") - .elements().next().has("error")) { - return MailChimpResult.NO_CHANGE; - } else { - return MailChimpResult.UPDATED; - } + if (response == null || (response.has(ERRORS) && response.get(ERRORS).size() > 0)) { + logger.error("Error when adding user to MailChimp list, list identifier was {} and response was {}", listIdentifier, response); + return MailChimpResult.ERROR; } - return MailChimpResult.ERROR; - + return MailChimpResult.UPDATED; } @Override @@ -128,7 +136,7 @@ public class MailChimpServiceImpl implements MailChimpService { return MailChimpResult.ERROR; } - String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + String listIdentifier = (String) action.getParameterValues().get(LIST_IDENTIFIER); if (StringUtils.isBlank(listIdentifier)) { logger.warn("Couldn't get the list identifier from Unomi"); return MailChimpResult.ERROR; @@ -136,16 +144,12 @@ public class MailChimpServiceImpl implements MailChimpService { JsonNode member = isMemberOfMailChimpList(profile, action); if (member == null) { - return MailChimpResult.ERROR; - } - if (!member.has("id")) { return MailChimpResult.NO_CHANGE; } - - JsonNode response = HttpUtils.executeDeleteRequest(httpClient, getBaseUrl() + "/lists/" + listIdentifier + "/members/" + member.get("id").asText(), getHeaders()); - if (response == null) { - logger.error("Couldn't remove the visitor from the list"); + JsonNode response = HttpUtils.executeDeleteRequest(httpClient, getBaseUrl() + "/lists/" + listIdentifier + "/members/" + member.get(ID).asText(), getHeaders()); + if (response == null || (response.has(ERRORS) && response.get(ERRORS).size() > 0)) { + logger.error("Couldn't remove the visitor from the MailChimp list, list identifier was {} and response was {}", listIdentifier, response); return MailChimpResult.ERROR; } return MailChimpResult.REMOVED; @@ -157,29 +161,20 @@ public class MailChimpServiceImpl implements MailChimpService { return MailChimpResult.ERROR; } - String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + String listIdentifier = (String) action.getParameterValues().get(LIST_IDENTIFIER); if (StringUtils.isBlank(listIdentifier)) { logger.warn("Couldn't get the list identifier from Unomi"); return MailChimpResult.ERROR; } JsonNode member = isMemberOfMailChimpList(profile, action); - if (member == null) { - logger.error("Visitor was not part of the list"); - return MailChimpResult.ERROR; - } - - if (member.get("status").asText().equals("unsubscribed")) { + if (member == null || member.get(STATUS).asText().equals(UNSUBSCRIBED)) { return MailChimpResult.NO_CHANGE; } JSONObject body = new JSONObject(); - body.put("status", "unsubscribed"); - - + body.put(STATUS, UNSUBSCRIBED); return updateSubscription(listIdentifier, body.toString(), member, false); - - } private void initHttpClient() { @@ -197,24 +192,10 @@ public class MailChimpServiceImpl implements MailChimpService { return false; } - //This method is not use yet, it can be used in the future - private boolean isMailChimpServerOnline() { - JsonNode response = HttpUtils.executeGetRequest(httpClient, getBaseUrl() + "/ping", getHeaders()); - if (response != null) { - if (response.has("health_status") && response.get("health_status").textValue().equals("Everything's Chimpy!")) { - return true; - } else { - logger.error("Error when communicating with MailChimp server, response was: {}", response.asText()); - return false; - } - } - return false; - } - private boolean visitorHasMandatoryProperties(Profile profile) { - if (profile.getProperty("firstName") == null - || profile.getProperty("lastName") == null - || profile.getProperty("email") == null) { + if (profile.getProperty(FIRST_NAME) == null + || profile.getProperty(LAST_NAME) == null + || profile.getProperty(EMAIL) == null) { logger.warn("Visitor mandatory properties are missing"); return false; } @@ -222,49 +203,35 @@ public class MailChimpServiceImpl implements MailChimpService { } private JsonNode isMemberOfMailChimpList(Profile profile, Action action) { - String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + String listIdentifier = (String) action.getParameterValues().get(LIST_IDENTIFIER); if (StringUtils.isBlank(listIdentifier)) { logger.warn("MailChimp list identifier not found"); return null; } - String email = profile.getProperty("email").toString(); + String email = profile.getProperty(EMAIL).toString(); JsonNode response = HttpUtils.executeGetRequest(httpClient, getBaseUrl() + "/lists/" + listIdentifier + "/members/", getHeaders()); - JsonNode member = null; if (response != null) { - if (response.has("members")) { - if (response.get("members").iterator().hasNext() - && response.get("members").iterator().next().has("email_address")) { - for (JsonNode m : response.get("members")) { - if (m.get("email_address").textValue().equals(email)) { - member = m; - break; - } - } - if (member == null) { - return response; - - } else { + if (response.has(MEMBERS)) { + for (JsonNode member : response.get(MEMBERS)) { + if (member.get(EMAIL_ADDRESS).asText().equals(email)) { return member; } } } - return response; } return null; } private MailChimpResult updateSubscription(String listIdentifier, String jsonData, JsonNode member, boolean toSubscribe) { - JsonNode response = HttpUtils.executePatchRequest(httpClient, getBaseUrl() + "/lists/" + listIdentifier + "/members/" + member.get("id").asText(), getHeaders(), jsonData); + JsonNode response = HttpUtils.executePatchRequest(httpClient, getBaseUrl() + "/lists/" + listIdentifier + "/members/" + member.get(ID).asText(), getHeaders(), jsonData); if (response != null) { - if (response.has("status")) { - String responseStatus = response.get("status").asText(); - if ((toSubscribe && responseStatus.equals("subscribed")) || (!toSubscribe && responseStatus.equals("unsubscribed"))) { + if (response.has(STATUS)) { + String responseStatus = response.get(STATUS).asText(); + if ((toSubscribe && responseStatus.equals(SUBSCRIBED)) || (!toSubscribe && responseStatus.equals(UNSUBSCRIBED))) { return MailChimpResult.UPDATED; } else { return MailChimpResult.NO_CHANGE; } - - } } logger.error("Couldn't update the subscription of the visitor");
