Added 3 features, add a visitor to a MailChimp list, remove a visitor from MailChimp list and unsubscribe a visitor from MailChimp list
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/b2b1b312 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/b2b1b312 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/b2b1b312 Branch: refs/heads/master Commit: b2b1b312ef02e5f8bb8214202ea0166a57610637 Parents: 13fb40e Author: Dinesh <[email protected]> Authored: Thu Nov 23 16:36:27 2017 +0100 Committer: Dinesh <[email protected]> Committed: Thu Nov 23 16:36:27 2017 +0100 ---------------------------------------------------------------------- extensions/pom.xml | 1 - extensions/unomi-mailchimp/README.md | 52 ++++ extensions/unomi-mailchimp/actions/pom.xml | 49 +++ .../AddVisitorToMailChimpListsAction.java | 55 ++++ .../RemoveVisitorFromMailChimpListsAction.java | 54 ++++ ...ubscribeVisitorFromMailChimpListsAction.java | 59 ++++ .../AddVisitorToMailChimpListsAction.json | 20 ++ .../RemoveVisitorFromMailChimpListsAction.json | 20 ++ ...ubscribeVisitorFromMailChimpListsAction.json | 20 ++ .../resources/OSGI-INF/blueprint/blueprint.xml | 53 ++++ extensions/unomi-mailchimp/core/pom.xml | 50 --- .../actions/AddToListsMailchimpAction.java | 40 --- .../cxs/actions/AddToListsMailchimpAction.json | 20 -- .../resources/OSGI-INF/blueprint/blueprint.xml | 35 --- extensions/unomi-mailchimp/karaf-kar/pom.xml | 8 +- .../karaf-kar/src/main/feature/feature.xml | 6 +- extensions/unomi-mailchimp/pom.xml | 6 +- extensions/unomi-mailchimp/rest/pom.xml | 6 +- .../apache/unomi/mailchimp/rest/MCEndPoint.java | 58 ---- .../unomi/mailchimp/rest/MailChimpEndPoint.java | 61 ++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 10 +- extensions/unomi-mailchimp/services/pom.xml | 15 +- .../unomi/mailchimp/services/HttpUtils.java | 166 +++++----- .../mailchimp/services/MailChimpResult.java | 27 ++ .../mailchimp/services/MailChimpService.java | 62 ++++ .../mailchimp/services/MailchimpService.java | 30 -- .../services/internal/MailChimpServiceImpl.java | 293 ++++++++++++++++++ .../services/internal/MailchimpServiceImpl.java | 230 -------------- .../resources/OSGI-INF/blueprint/blueprint.xml | 11 +- .../org.apache.unomi.mailchimpconnector.cfg | 3 +- extensions/weather-update/.gitignore | 8 - extensions/weather-update/README.md | 42 --- extensions/weather-update/core/pom.xml | 99 ------ .../actions/WeatherUpdateAction.java | 305 ------------------- .../cxs/actions/UpdateWeatherAction.json | 15 - .../sessions/weather/weatherLike.json | 14 - .../sessions/weather/weatherTemp.json | 14 - .../sessions/weather/weatherWindDirection.json | 14 - .../sessions/weather/weatherWindSpeed.json | 14 - .../sessions/weatherSessionProperties.json | 5 - .../resources/OSGI-INF/blueprint/blueprint.xml | 44 --- .../src/main/resources/messages_en.properties | 18 -- .../org.apache.unomi.weatherUpdate.cfg | 19 -- extensions/weather-update/karaf-kar/pom.xml | 81 ----- .../karaf-kar/src/main/feature/feature.xml | 28 -- extensions/weather-update/pom.xml | 36 --- 46 files changed, 943 insertions(+), 1333 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/pom.xml b/extensions/pom.xml index 89b6c14..abac863 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -36,7 +36,6 @@ <module>geonames</module> <module>router</module> <module>salesforce-connector</module> - <module>weather-update</module> <module>unomi-mailchimp</module> </modules> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/README.md ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/README.md b/extensions/unomi-mailchimp/README.md new file mode 100644 index 0000000..3ecfa15 --- /dev/null +++ b/extensions/unomi-mailchimp/README.md @@ -0,0 +1,52 @@ +<!-- + ~ 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. + --> + + +Apache Unomi :: Extensions :: MailChimp Beta Connector +================================= + +This extension have 3 actions. + Add a visitor into a defined Mailchimp list. + Remove a visitor from a defined Mailchimp list. + Unsubscribe a visitor from a defined Mailchimp list. + +## Configuration required + + Marketing Factory version : 1.8.0 + Unomi version : 1.3.0 + Digital Experience : 7.2.1.1 + +## Getting started + + +1. Create a new MailChimp account : + + https://login.mailchimp.com/signup/ + +2. Generate a new API Key, or get the default + + https://usX.admin.mailchimp.com/account/api/ + +3. Configure the MailChimp Beta Connector. In the etc/org.apache.unomi.mailchimpconnector.cfg file change the following settings: + + mailChimpConnector.apiKey=YOUR_APIKEY + mailChimpConnector.url.subDomain=YOUR_SUB_DOMAIN (exemple: https://usX.admin.mailchimp.com/account/api/, the X is the SUB_DOMAIN) + +4. Deploy into Apache Unomi using the following commands from the Apache Karaf shell: + + feature:repo-add mvn:org.apache.unomi/unomi-mailchimp-connector-karaf-kar/${project.version}/xml/features + feature:install unomi-mailchimp-connector-karaf-kar http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/actions/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/actions/pom.xml b/extensions/unomi-mailchimp/actions/pom.xml new file mode 100644 index 0000000..d7e901f --- /dev/null +++ b/extensions/unomi-mailchimp/actions/pom.xml @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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 + --> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.unomi</groupId> + <artifactId>unomi-mailchimp-connector</artifactId> + <version>1.3.0-incubating-SNAPSHOT</version> + </parent> + + <artifactId>unomi-mailchimp-connector-actions</artifactId> + <name>Apache Unomi :: Extensions :: MailChimp Connector :: Actions</name> + <description>A connector between Apache Unomi and MailChimp API</description> + <packaging>bundle</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.unomi</groupId> + <artifactId>unomi-api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.unomi</groupId> + <artifactId>unomi-mailchimp-connector-services</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/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 new file mode 100644 index 0000000..3297887 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/AddVisitorToMailChimpListsAction.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 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/b2b1b312/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 new file mode 100644 index 0000000..329aff7 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/RemoveVisitorFromMailChimpListsAction.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 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/b2b1b312/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 new file mode 100644 index 0000000..e6884e8 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/java/org/apache/unomi/mailchimp/actions/UnsubscribeVisitorFromMailChimpListsAction.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 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/b2b1b312/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 new file mode 100644 index 0000000..9f7bc84 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/AddVisitorToMailChimpListsAction.json @@ -0,0 +1,20 @@ +{ + "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/b2b1b312/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 new file mode 100644 index 0000000..1644cd1 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/RemoveVisitorFromMailChimpListsAction.json @@ -0,0 +1,20 @@ +{ + "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/b2b1b312/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 new file mode 100644 index 0000000..0dfad79 --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/META-INF/cxs/actions/UnsubscribeVisitorFromMailChimpListsAction.json @@ -0,0 +1,20 @@ +{ + "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/b2b1b312/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 new file mode 100644 index 0000000..d0a2cbe --- /dev/null +++ b/extensions/unomi-mailchimp/actions/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> + + <reference id="mailChimpService" + interface="org.apache.unomi.mailchimp.services.MailChimpService"/> + + <service auto-export="interfaces"> + <service-properties> + <entry key="actionExecutorId" value="addVisitorToMailChimpLists"/> + </service-properties> + <bean class="org.apache.unomi.mailchimp.actions.AddVisitorToMailChimpListsAction"> + <property name="mailChimpService" ref="mailChimpService"/> + </bean> + </service> + + <service auto-export="interfaces"> + <service-properties> + <entry key="actionExecutorId" value="removeVisitorFromMailChimpLists"/> + </service-properties> + <bean class="org.apache.unomi.mailchimp.actions.RemoveVisitorFromMailChimpListsAction"> + <property name="mailChimpService" ref="mailChimpService"/> + </bean> + </service> + + <service auto-export="interfaces"> + <service-properties> + <entry key="actionExecutorId" value="unsubscribeVisitorFromMailChimpLists"/> + </service-properties> + <bean class="org.apache.unomi.mailchimp.actions.UnsubscribeVisitorFromMailChimpListsAction"> + <property name="mailChimpService" ref="mailChimpService"/> + </bean> + </service> + +</blueprint> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/core/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/core/pom.xml b/extensions/unomi-mailchimp/core/pom.xml deleted file mode 100644 index 2d7daa0..0000000 --- a/extensions/unomi-mailchimp/core/pom.xml +++ /dev/null @@ -1,50 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ 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 - --> -<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns="http://maven.apache.org/POM/4.0.0" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.unomi</groupId> - <artifactId>unomi-mailchimp-connector</artifactId> - <version>1.3.0-incubating-SNAPSHOT</version> - </parent> - - <artifactId>unomi-mailchimp-connector-core</artifactId> - <name>Apache Unomi :: Extensions :: Mailchimp Beta Connector :: Core</name> - <description>A connector between Unomi and Mailchimp API</description> - <packaging>bundle</packaging> - - <dependencies> - <dependency> - <groupId>org.apache.unomi</groupId> - <artifactId>unomi-api</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.unomi</groupId> - <artifactId>unomi-mailchimp-connector-services</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - - </dependencies> - - -</project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/core/src/main/java/org/apache/unomi/mailchimp/actions/AddToListsMailchimpAction.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/core/src/main/java/org/apache/unomi/mailchimp/actions/AddToListsMailchimpAction.java b/extensions/unomi-mailchimp/core/src/main/java/org/apache/unomi/mailchimp/actions/AddToListsMailchimpAction.java deleted file mode 100644 index 0f60942..0000000 --- a/extensions/unomi-mailchimp/core/src/main/java/org/apache/unomi/mailchimp/actions/AddToListsMailchimpAction.java +++ /dev/null @@ -1,40 +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.MailchimpService; - -public class AddToListsMailchimpAction implements ActionExecutor { - private MailchimpService mailchimpService; - - public void setMailchimpService(MailchimpService mailchimpService) { - this.mailchimpService = mailchimpService; - } - - @Override - public int execute(Action action, Event event) { - if (mailchimpService.executePostAddToMCList(event.getProfile(), action)) - return EventService.PROFILE_UPDATED; - else - return EventService.NO_CHANGE; - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/core/src/main/resources/META-INF/cxs/actions/AddToListsMailchimpAction.json ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/core/src/main/resources/META-INF/cxs/actions/AddToListsMailchimpAction.json b/extensions/unomi-mailchimp/core/src/main/resources/META-INF/cxs/actions/AddToListsMailchimpAction.json deleted file mode 100644 index 3fc3a28..0000000 --- a/extensions/unomi-mailchimp/core/src/main/resources/META-INF/cxs/actions/AddToListsMailchimpAction.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "metadata": { - "id": "addToListsMailchimpAction", - "name": "Add To user to Mailchimp Lists Action", - "description": "Add the user with his name and email to a Mailchimp list", - "systemTags": [ - "event", - "availableToEndUser" - ], - "readOnly": true - }, - "actionExecutor": "addToListsMailchimp", - "parameters": [ - { - "id": "listIdentifier", - "type": "string", - "multivalued": false - } - ] -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/extensions/unomi-mailchimp/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml deleted file mode 100644 index 1020ce1..0000000 --- a/extensions/unomi-mailchimp/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ /dev/null @@ -1,35 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ~ 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. - --> - -<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" - xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> - - <reference id="mailchimpService" - interface="org.apache.unomi.mailchimp.services.MailchimpService"/> - - <service auto-export="interfaces"> - <service-properties> - <entry key="actionExecutorId" value="addToListsMailchimp"/> - </service-properties> - - <bean class="org.apache.unomi.mailchimp.actions.AddToListsMailchimpAction"> - <property name="mailchimpService" ref="mailchimpService"/> - </bean> - </service> -</blueprint> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/karaf-kar/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/karaf-kar/pom.xml b/extensions/unomi-mailchimp/karaf-kar/pom.xml index bc3eafb..8f6be3d 100644 --- a/extensions/unomi-mailchimp/karaf-kar/pom.xml +++ b/extensions/unomi-mailchimp/karaf-kar/pom.xml @@ -18,19 +18,19 @@ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> <groupId>org.apache.unomi</groupId> <artifactId>unomi-mailchimp-connector</artifactId> <version>1.3.0-incubating-SNAPSHOT</version> </parent> - <modelVersion>4.0.0</modelVersion> <artifactId>unomi-mailchimp-connector-karaf-kar</artifactId> - <name>Apache Unomi :: Extensions :: Mailchimp Beta Connector :: Apache Karaf Feature and KAR archive</name> + <name>Apache Unomi :: Extensions :: MailChimp Connector :: Apache Karaf Feature and KAR archive</name> <description>This is a simple Apache Unomi plugin.</description> <packaging>kar</packaging> - <dependencies> <dependency> <groupId>org.apache.unomi</groupId> @@ -50,7 +50,7 @@ </dependency> <dependency> <groupId>org.apache.unomi</groupId> - <artifactId>unomi-mailchimp-connector-core</artifactId> + <artifactId>unomi-mailchimp-connector-actions</artifactId> <version>${project.version}</version> </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/karaf-kar/src/main/feature/feature.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/karaf-kar/src/main/feature/feature.xml b/extensions/unomi-mailchimp/karaf-kar/src/main/feature/feature.xml index aec4106..6f5a512 100644 --- a/extensions/unomi-mailchimp/karaf-kar/src/main/feature/feature.xml +++ b/extensions/unomi-mailchimp/karaf-kar/src/main/feature/feature.xml @@ -17,12 +17,12 @@ --> <features xmlns="http://karaf.apache.org/xmlns/features/v1.2.1" name="unomi-mailchimp-connector-karaf-feature"> <feature name="unomi-mailchimp-connector-karaf-kar" version="${project.version}" - description="Apache Unomi :: Extensions :: Mailchimp Connector :: Apache Karaf Feature"> - <details>Connect external service by Mailchimp</details> + description="Apache Unomi :: Extensions :: MailChimp Connector :: Apache Karaf Feature"> + <details>Connect Apache Unomi to MailChimp API</details> <configfile finalname="/etc/org.apache.unomi.mailchimpconnector.cfg">mvn:org.apache.unomi/unomi-mailchimp-connector-services/1.3.0-incubating-SNAPSHOT/cfg/mailchimpconnectorcfg</configfile> <bundle start-level="90">mvn:org.apache.unomi/unomi-mailchimp-connector-services/${project.version}</bundle> <bundle start-level="90">mvn:org.apache.unomi/unomi-mailchimp-connector-rest/${project.version}</bundle> - <bundle start-level="90">mvn:org.apache.unomi/unomi-mailchimp-connector-core/${project.version}</bundle> + <bundle start-level="90">mvn:org.apache.unomi/unomi-mailchimp-connector-actions/${project.version}</bundle> </feature> </features> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/pom.xml b/extensions/unomi-mailchimp/pom.xml index 0553f35..be2cc2a 100644 --- a/extensions/unomi-mailchimp/pom.xml +++ b/extensions/unomi-mailchimp/pom.xml @@ -26,14 +26,14 @@ </parent> <artifactId>unomi-mailchimp-connector</artifactId> - <name>Apache Unomi :: Extensions :: Mailchimp Beta Connector</name> - <description>Connector with the Mailchimp API service</description> + <name>Apache Unomi :: Extensions :: MailChimp Beta Connector</name> + <description>Connector with the MailChimp API service</description> <packaging>pom</packaging> <modules> <module>services</module> <module>rest</module> - <module>core</module> + <module>actions</module> <module>karaf-kar</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/rest/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/rest/pom.xml b/extensions/unomi-mailchimp/rest/pom.xml index 631ce59..8086434 100644 --- a/extensions/unomi-mailchimp/rest/pom.xml +++ b/extensions/unomi-mailchimp/rest/pom.xml @@ -26,8 +26,8 @@ <modelVersion>4.0.0</modelVersion> <artifactId>unomi-mailchimp-connector-rest</artifactId> - <name>Apache Unomi :: Extensions :: Mailchimp Beta Connector :: REST API</name> - <description>REST API for the Apache Unomi Context Server extension that integrates with Mailchimp</description> + <name>Apache Unomi :: Extensions :: MailChimp Connector :: REST API</name> + <description>REST API for the Apache Unomi Context Server extension that integrates with MailChimp</description> <packaging>bundle</packaging> <dependencies> @@ -42,8 +42,6 @@ <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> - - <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MCEndPoint.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MCEndPoint.java b/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MCEndPoint.java deleted file mode 100644 index 38114e9..0000000 --- a/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MCEndPoint.java +++ /dev/null @@ -1,58 +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.rest; - -import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; -import org.apache.unomi.mailchimp.services.MailchimpService; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import java.util.HashMap; -import java.util.List; - - -@WebService -@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") -@CrossOriginResourceSharing( - allowAllOrigins = true, - allowCredentials = true -) -public class MCEndPoint { - - private MailchimpService mailchimpService; - - public MCEndPoint() { - System.out.println("Initializing MCEndPoint service endpoint..."); - } - - @WebMethod(exclude = true) - public void setMailChimpService(MailchimpService mailchimpService) { - this.mailchimpService = mailchimpService; - } - - @GET - @Path("/getMailchimpLists") - public List<HashMap<String, String>> executeListMailchimpList() { - return mailchimpService.executeGetAllLists(); - } - -} - http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MailChimpEndPoint.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MailChimpEndPoint.java b/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MailChimpEndPoint.java new file mode 100644 index 0000000..4624874 --- /dev/null +++ b/extensions/unomi-mailchimp/rest/src/main/java/org/apache/unomi/mailchimp/rest/MailChimpEndPoint.java @@ -0,0 +1,61 @@ +/* + * 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.rest; + +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; +import org.apache.unomi.mailchimp.services.MailChimpService; + +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.HashMap; +import java.util.List; + + +@WebService +@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") +@CrossOriginResourceSharing( + allowAllOrigins = true, + allowCredentials = true +) +public class MailChimpEndPoint { + private MailChimpService mailChimpService; + + public MailChimpEndPoint() { + System.out.println("Initializing MailChimpEndPoint service endpoint..."); + } + + /** + * This function return the available MailChimp lists ID and Name + * + * @return The Lists of MailChimp List + */ + @GET + @Path("/") + public List<HashMap<String, String>> getAllLists() { + return mailChimpService.getAllLists(); + } + + @WebMethod(exclude = true) + public void setMailChimpService(MailChimpService mailChimpService) { + this.mailChimpService = mailChimpService; + } +} + http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/extensions/unomi-mailchimp/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml index cf5839c..a521770 100644 --- a/extensions/unomi-mailchimp/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/extensions/unomi-mailchimp/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -20,7 +20,7 @@ xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd - http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd"> + http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd"> <bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"/> <bean id="jacksonMapper" class="org.apache.unomi.persistence.spi.CustomObjectMapper"/> <bean id="jaxb-provider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"> @@ -45,7 +45,7 @@ <!--property name="redirectURI" value="/login.jsp"/--> </bean> - <jaxrs:server address="/mc" id="restMCService"> + <jaxrs:server address="/mailchimp" id="restMCService"> <jaxrs:providers> <ref component-id="jaxb-provider"/> <ref component-id="cors-filter"/> @@ -53,13 +53,13 @@ </jaxrs:providers> <jaxrs:serviceBeans> - <ref component-id="mCEndPoint"/> + <ref component-id="mailChimpEndPoint"/> </jaxrs:serviceBeans> </jaxrs:server> - <reference id="mailChimpService" interface="org.apache.unomi.mailchimp.services.MailchimpService"/> + <reference id="mailChimpService" interface="org.apache.unomi.mailchimp.services.MailChimpService"/> - <bean id="mCEndPoint" class="org.apache.unomi.mailchimp.rest.MCEndPoint"> + <bean id="mailChimpEndPoint" class="org.apache.unomi.mailchimp.rest.MailChimpEndPoint"> <property name="mailChimpService" ref="mailChimpService"/> </bean> </blueprint> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/services/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/pom.xml b/extensions/unomi-mailchimp/services/pom.xml index fb125db..2b4604f 100644 --- a/extensions/unomi-mailchimp/services/pom.xml +++ b/extensions/unomi-mailchimp/services/pom.xml @@ -18,18 +18,17 @@ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> <groupId>org.apache.unomi</groupId> <artifactId>unomi-mailchimp-connector</artifactId> <version>1.3.0-incubating-SNAPSHOT</version> </parent> - <modelVersion>4.0.0</modelVersion> - <artifactId>unomi-mailchimp-connector-services</artifactId> - <name>Apache Unomi :: Extensions :: Mailchimp Beta Connector :: Services</name> - <description>Service implementation for the Apache Unomi Context Server extension that integrates with Mailchimp - </description> + <name>Apache Unomi :: Extensions :: MailChimp Connector :: Services</name> + <description>Service implementation for Apache Unomi that integrates with MailChimp</description> <packaging>bundle</packaging> @@ -61,7 +60,6 @@ <artifactId>json</artifactId> <version>20160810</version> </dependency> - <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> @@ -111,8 +109,3 @@ </plugins> </build> </project> - - - - - http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/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 31e0082..9b71dca 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 @@ -19,18 +19,13 @@ package org.apache.unomi.mailchimp.services; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.AbstractHttpEntity; -import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.client.methods.*; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,96 +39,113 @@ import java.util.Map; public class HttpUtils { private static Logger logger = LoggerFactory.getLogger(HttpUtils.class); - public static JsonNode doPostHttp(CloseableHttpClient request, String url, Map<String, String> headers, - JSONObject body) { - AbstractHttpEntity entity; - JsonNode jsonNode = null; + + public static CloseableHttpClient initHttpClient() { + return HttpClients.createDefault(); + } + + public static JsonNode executePatchRequest(CloseableHttpClient httpClient, String url, Map<String, String> headers, String body) { + HttpPatch httpPatch = new HttpPatch(url); + + addHeaders(headers, httpPatch); + + return addBodyAndExecuteRequest(httpClient, body, httpPatch); + } + + public static JsonNode executePostRequest(CloseableHttpClient httpClient, String url, Map<String, String> headers, String body) { HttpPost httpPost = new HttpPost(url); - try { - entity = new ByteArrayEntity(body.toString().getBytes("UTF8")); - CloseableHttpResponse response = null; - entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); + addHeaders(headers, httpPost); - try { - httpPost.setEntity(entity); - try { - for (String key : headers.keySet()) { - httpPost.addHeader(key, headers.get(key)); - } - response = request.execute(httpPost); - HttpEntity entityResponse = response.getEntity(); - String responseString; - try { - responseString = EntityUtils.toString(entityResponse); - ObjectMapper objectMapper = new ObjectMapper(); - try { - jsonNode = objectMapper.readTree(responseString); - } catch (IOException e) { - logger.info(e.getMessage(), e); - } - } catch (IOException e) { - logger.info(e.getMessage(), e); - } - } catch (IOException e) { - logger.info(e.getMessage(), e); - } + return addBodyAndExecuteRequest(httpClient, body, httpPost); + } - } finally { - try { - if (response != null) { - response.close(); - } - } catch (IOException e) { - logger.info(e.getMessage(), e); - } - } + public static JsonNode executeGetRequest(CloseableHttpClient httpClient, String url, Map<String, String> headers) { + HttpGet httpGet = new HttpGet(url); + + addHeaders(headers, httpGet); + + return executeRequest(httpClient, httpGet); + } + + public static JsonNode executeDeleteRequest(CloseableHttpClient httpClient, String url, Map<String, String> headers) { + HttpDelete httpDelete = new HttpDelete(url); + + addHeaders(headers, httpDelete); + return executeRequest(httpClient, httpDelete); + } + + private static JsonNode addBodyAndExecuteRequest(CloseableHttpClient httpClient, String body, HttpEntityEnclosingRequestBase request) { + try { + StringEntity stringEntity = new StringEntity(body); + stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); + request.setEntity(stringEntity); + + return executeRequest(httpClient, request); } catch (UnsupportedEncodingException e) { - logger.info(e.getMessage(), e); + logger.error(e.getMessage(), e); return null; } - return jsonNode; } + private static JsonNode executeRequest(CloseableHttpClient httpClient, HttpRequestBase request) { + try { + CloseableHttpResponse response = httpClient.execute(request); - public static JsonNode doGetHttp(CloseableHttpClient request, String - url, Map<String, String> headers) { - HttpGet httpGet = new HttpGet(url); - - for (String key : headers.keySet()) { - httpGet.addHeader(key, headers.get(key)); + return extractResponse(response); + } catch (IOException e) { + logger.error(e.getMessage(), e); + return null; } - JsonNode jsonNode = null; - CloseableHttpResponse response = null; - try { - response = request.execute(httpGet); - if (response != null) { - HttpEntity entity = response.getEntity(); - String responseString; - if (entity != null) { + } + + private static JsonNode extractResponse(CloseableHttpResponse response) { + if (response != null) { + if (response.getStatusLine().getStatusCode() >= 400) { + logger.error("Error when communicating with MailChimp server, response code was {} and response message was {}", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); + return null; + } + + String responseString; + if (response.getStatusLine().getStatusCode() == 204) { + responseString = ("{ \"response\": \" " + response.getStatusLine().toString() + "\" }"); + } else { + try { + responseString = EntityUtils.toString(response.getEntity()); + } catch (IOException e) { + logger.error("Error when retrieving entity response", e); + return null; + } + } + + ObjectMapper objectMapper = new ObjectMapper(); + try { + JsonNode jsonNode = objectMapper.readTree(responseString); + + if (response.getStatusLine().getStatusCode() != 204) { + EntityUtils.consumeQuietly(response.getEntity()); + } else { try { - responseString = EntityUtils.toString(entity); - ObjectMapper objectMapper = new ObjectMapper(); - jsonNode = objectMapper.readTree(responseString); + response.close(); } catch (IOException e) { - logger.info("Error : With the API json response.", e.getMessage()); + logger.error("Error when trying to close response", e); } } - } - } catch (IOException e) { - logger.info("Error : With the Http Request execution. Wrong parameters given", e - .getMessage()); - } finally { - if (response != null) { - EntityUtils.consumeQuietly(response.getEntity()); + + return jsonNode; + } catch (IOException e) { + logger.error("Error when parsing response with ObjectMapper", e); + return null; } } - return jsonNode; + return null; } - public static CloseableHttpClient initHttpClient() { - return HttpClients.createDefault(); + private static void addHeaders(Map<String, String> headers, HttpRequestBase httpRequestBase) { + for (String key : headers.keySet()) { + httpRequestBase.addHeader(key, headers.get(key)); + } } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpResult.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpResult.java b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpResult.java new file mode 100644 index 0000000..4f100b6 --- /dev/null +++ b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpResult.java @@ -0,0 +1,27 @@ +/* + * 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.services; + +/** + * This enum is use to get the result of the interaction with MailChimp + */ +public enum MailChimpResult { + UPDATED, + REMOVED, + ERROR, + NO_CHANGE +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpService.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpService.java b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpService.java new file mode 100644 index 0000000..4abe3c6 --- /dev/null +++ b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailChimpService.java @@ -0,0 +1,62 @@ +/* + * 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.services; + + +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.actions.Action; + +import java.util.HashMap; +import java.util.List; + +public interface MailChimpService { + + /** + * This function will get all MailChimp lists. + * + * @return a List of MailChimp lists with the name and the id. + */ + List<HashMap<String, String>> getAllLists(); + + /** + * This function will add the current visitor to a MailChimp list. + * + * @param profile @see org.apache.unomi.api.Profile + * @param action @see org.apache.unomi.api.actions.Action + * @return true if the visitor is successfully added to a MailChimp list. + */ + MailChimpResult addToMCList(Profile profile, Action action); + + /** + * This function will remove the current visitor from a MailChimp list. + * + * @param profile @see org.apache.unomi.api.Profile + * @param action @see org.apache.unomi.api.actions.Action + * @return true if the visitor is successfully removed to a MailChimp list. + */ + MailChimpResult removeFromMCList(Profile profile, Action action); + + /** + * This function will unsbscribe the current visitor to a MailChimp list. + * + * @param profile @see org.apache.unomi.api.Profile + * @param action @see org.apache.unomi.api.actions.Action + * @return ue if the visitor is successfully unsbscribed to a MailChimp list. + */ + MailChimpResult unsubscribeFromMCList(Profile profile, Action action); +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailchimpService.java ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailchimpService.java b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailchimpService.java deleted file mode 100644 index bbea3bc..0000000 --- a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/MailchimpService.java +++ /dev/null @@ -1,30 +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.services; - - -import org.apache.unomi.api.Profile; -import org.apache.unomi.api.actions.Action; - -import java.util.HashMap; -import java.util.List; - -public interface MailchimpService { - List<HashMap<String, String>> executeGetAllLists(); - boolean executePostAddToMCList(Profile profile, Action action); -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/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 new file mode 100644 index 0000000..ad2da2a --- /dev/null +++ b/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailChimpServiceImpl.java @@ -0,0 +1,293 @@ +/* + * 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.services.internal; + +import com.fasterxml.jackson.databind.JsonNode; +import org.apache.commons.lang.StringUtils; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.actions.Action; +import org.apache.unomi.mailchimp.services.HttpUtils; +import org.apache.unomi.mailchimp.services.MailChimpResult; +import org.apache.unomi.mailchimp.services.MailChimpService; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class MailChimpServiceImpl implements MailChimpService { + private static Logger logger = LoggerFactory.getLogger(MailChimpServiceImpl.class); + + private String apiKey; + private String urlSubDomain; + private CloseableHttpClient httpClient; + + @Override + public List<HashMap<String, String>> getAllLists() { + List<HashMap<String, String>> mcLists = new ArrayList<>(); + if (isMailChimpConnectorConfigured() && isMailChimpServerOnline()) { + 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")) { + HashMap<String, String> mcListInfo = new HashMap<>(); + 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()); + } + } + } else { + logger.info("No list to return, response was {}", response.asText()); + } + } + } + return mcLists; + } + + @Override + public MailChimpResult addToMCList(Profile profile, Action action) { + if (!isMailChimpConnectorConfigured() || !visitorHasMandatoryProperties(profile)) { + return MailChimpResult.ERROR; + } + + String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + 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; + } + } + JSONObject nameStruct = new JSONObject(); + nameStruct.put("FNAME", profile.getProperty("firstName").toString()); + nameStruct.put("LNAME", profile.getProperty("lastName").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"); + + JSONArray dataMember = new JSONArray(); + dataMember.put(userData); + + JSONObject body = new JSONObject(); + body.put("members", dataMember); + + + // BEFORE DOING THAT CHECK IF THE USER IS NOT ALREADY IN THE LIST + // THEN IF THE USER IS ALREADY IN THE LIST CHECK IF HE IS SUBSCRIBE OR UNSUBSCRIBE + // IF UNSUBSCRIBE SHOULD WE CHANGE IT TO SUBSCRIBE ? + 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; + } + } + return MailChimpResult.ERROR; + + } + + @Override + public MailChimpResult removeFromMCList(Profile profile, Action action) { + if (!isMailChimpConnectorConfigured() || !visitorHasMandatoryProperties(profile)) { + return MailChimpResult.ERROR; + } + + String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + 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) { + 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"); + return MailChimpResult.ERROR; + } + return MailChimpResult.REMOVED; + } + + @Override + public MailChimpResult unsubscribeFromMCList(Profile profile, Action action) { + if (!isMailChimpConnectorConfigured() || !visitorHasMandatoryProperties(profile)) { + return MailChimpResult.ERROR; + } + + String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + 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")) { + return MailChimpResult.NO_CHANGE; + } + + JSONObject body = new JSONObject(); + body.put("status", "unsubscribed"); + + + return updateSubscription(listIdentifier, body.toString(), member, false); + + + } + + private void initHttpClient() { + if (httpClient == null) { + httpClient = HttpUtils.initHttpClient(); + } + } + + private boolean isMailChimpConnectorConfigured() { + if (StringUtils.isNotBlank(apiKey) && StringUtils.isNotBlank(urlSubDomain)) { + initHttpClient(); + return true; + } + logger.error("MailChimp extension isn't correctly configured, please check cfg file."); + return false; + } + + 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) { + logger.warn("Visitor mandatory properties are missing"); + return false; + } + return true; + } + + private JsonNode isMemberOfMailChimpList(Profile profile, Action action) { + String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); + if (StringUtils.isBlank(listIdentifier)) { + logger.warn("MailChimp list identifier not found"); + return null; + } + 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 { + 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); + if (response != null) { + 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"); + return MailChimpResult.ERROR; + } + + private String getBaseUrl() { + return "https://" + urlSubDomain + ".api.mailchimp.com/3.0"; + } + + private HashMap<String, String> getHeaders() { + HashMap<String, String> headers = new HashMap<>(); + headers.put("Accept", "application/json"); + headers.put("Authorization", "apikey " + apiKey); + return headers; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public void setUrlSubDomain(String urlSubDomain) { + this.urlSubDomain = urlSubDomain; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/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 deleted file mode 100644 index 5f41e99..0000000 --- a/extensions/unomi-mailchimp/services/src/main/java/org/apache/unomi/mailchimp/services/internal/MailchimpServiceImpl.java +++ /dev/null @@ -1,230 +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.services.internal; - -import com.fasterxml.jackson.databind.JsonNode; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.unomi.api.Profile; -import org.apache.unomi.api.actions.Action; -import org.apache.unomi.mailchimp.services.HttpUtils; -import org.apache.unomi.mailchimp.services.MailchimpService; -import org.json.JSONArray; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -public class MailchimpServiceImpl implements MailchimpService { - - private static Logger logger = LoggerFactory.getLogger(MailchimpServiceImpl.class); - private String headerAuthorizationPassword; - private String url; - private CloseableHttpClient httpClient; - private HashMap<String, String> headers = new HashMap(); - private JsonNode response; - - public void setHeaderAuthorizationPassword(String headerAuthorizationPassword) { - this.headerAuthorizationPassword = headerAuthorizationPassword; - } - - private boolean isConfigured() { - if (headerAuthorizationPassword != null) { - logger.info("The extension is correctly configured "); - return true; - } else { - logger.info("The extension isn't correctly configured, please check cfg file "); - return false; - } - } - - /*** - *Initialization of the HttpClient - * @return httpClient - */ - private CloseableHttpClient initHttpUtils() { - - if (httpClient == null) { - httpClient = HttpUtils.initHttpClient(); - } - return httpClient; - } - - /*** - * Add the headers request into a HashMap - * @return headers - */ - private HashMap<String, String> addHeaders() { - headers.put("Accept", "application/json"); - headers.put("Authorization", "apikey " + headerAuthorizationPassword); - return headers; - } - - /*** - * Build the request Url - * @return String - */ - private String urlBuilder() { - return "https://us16.api.mailchimp.com/3.0/lists"; - } - - /** - * Build custom Url with parameter - * - * @param otherParameter - * @return String - */ - private String urlBuilder(String otherParameter) { - return urlBuilder() + "/" + otherParameter; - } - - /*** - * Get All lists available on Mailchimp - * @return List of Mailchimp's list - */ - @Override - public List<HashMap<String, String>> executeGetAllLists() { - if (!isConfigured()) { - return null; - } - initHttpUtils(); - response = executeRequestDoGet(); - return extractInfoListsMc(response); - } - - /*** - * Execute Get type requests - * @return JsonNode the response - */ - private JsonNode executeRequestDoGet() { - url = urlBuilder(); - addHeaders(); - return HttpUtils.doGetHttp(httpClient, url, headers); - } - - /** - * @param rawResponse the response from the API - * @return Mailchimp's lists with the id and the name. - */ - private List<HashMap<String, String>> extractInfoListsMc(JsonNode rawResponse) { - List<HashMap<String, String>> mcLists = new ArrayList<>(); - if (rawResponse.has("lists")) { - - - for (JsonNode list : rawResponse.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()); - - mcLists.add(mcListInfo); - } else { - - logger.info("API Response doesn't contains the info" + rawResponse.asText()); - } - } - } - - return mcLists; - } - - /** - * @param action, the selected list from the rule is stocked in the action - * @param profile the current user - */ - @Override - public boolean executePostAddToMCList(Profile profile, Action action) { - initHttpUtils(); - if (!isConfigured()) { - return false; - } - String listIdentifier = (String) action.getParameterValues().get("listIdentifier"); - response = executeRequestDoPost(profile, listIdentifier); - if (response != null) { - if (response.has("errors") && response.get("errors").elements().hasNext() && response.get("errors") - .elements().next().has("error")) { - - logger.info("Info :" + response.get("errors") - .elements().next().get("error")); - return false; - } else { - - logger.info("Success User has been inserted"); - return true; - } - } else { - logger.info("Failed : No Update"); - return false; - } - } - - /*** - * Execute Post type requests - * @param profile profile info, first name, last name and mail - * @param urlList list of Mailchimp - * @return raw response form the API, in JsonNode - */ - - private JsonNode executeRequestDoPost(Profile profile, String urlList) { - url = urlBuilder(urlList); - JSONObject body = CreateBodyPostAddMCList(profile); - - // body is null if the visitor has no email or fist name or last name - if (body != null) - return HttpUtils.doPostHttp(httpClient, url, headers, body); - return null; - } - - /** - * Create the body which will be sent - * - * @param profile the current user - * @return The body to send to the request - */ - private JSONObject CreateBodyPostAddMCList(Profile profile) { - if (profile.getProperty("firstName") == null || profile.getProperty("lastName") == null || profile - .getProperty("email") == null) { - logger.info("Error to get Profile's info"); - return null; - } - - String firstName = profile.getProperty("firstName").toString(); - String lastName = profile.getProperty("lastName").toString(); - String email = profile.getProperty("email").toString(); - addHeaders(); - - JSONObject nameStruct = new JSONObject(); - JSONObject userData = new JSONObject(); - JSONArray dataMember = new JSONArray(); - JSONObject dataMembers = new JSONObject(); - - nameStruct.put("FNAME", firstName); - nameStruct.put("LNAME", lastName); - userData.put("merge_fields", nameStruct); - userData.put("email_type", "html"); - userData.put("email_address", email); - userData.put("status", "subscribed"); - dataMember.put(userData); - dataMembers.put("members", dataMember); - - return dataMembers; - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/extensions/unomi-mailchimp/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 157c12b..180457a 100644 --- a/extensions/unomi-mailchimp/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/extensions/unomi-mailchimp/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -24,16 +24,17 @@ <cm:property-placeholder persistent-id="org.apache.unomi.mailchimpconnector" update-strategy="reload"> <cm:default-properties> - - <cm:property name="mailchimpConnector.headers.authorization.password" value=""/> + <cm:property name="mailChimpConnector.apiKey" value=""/> + <cm:property name="mailChimpConnector.url.subDomain" value=""/> </cm:default-properties> </cm:property-placeholder> - <bean id="mailchimpServiceImpl" class="org.apache.unomi.mailchimp.services.internal.MailchimpServiceImpl"> - <property name="headerAuthorizationPassword" value="${mailchimpConnector.headers.authorization.password}"/> + <bean id="mailChimpServiceImpl" class="org.apache.unomi.mailchimp.services.internal.MailChimpServiceImpl"> + <property name="apiKey" value="${mailChimpConnector.apiKey}"/> + <property name="urlSubDomain" value="${mailChimpConnector.url.subDomain}"/> </bean> - <service id="mailchimpService" ref="mailchimpServiceImpl" auto-export="interfaces"/> + <service id="mailChimpService" ref="mailChimpServiceImpl" auto-export="interfaces"/> </blueprint> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/unomi-mailchimp/services/src/main/resources/org.apache.unomi.mailchimpconnector.cfg ---------------------------------------------------------------------- diff --git a/extensions/unomi-mailchimp/services/src/main/resources/org.apache.unomi.mailchimpconnector.cfg b/extensions/unomi-mailchimp/services/src/main/resources/org.apache.unomi.mailchimpconnector.cfg index 3706120..816f3c5 100644 --- a/extensions/unomi-mailchimp/services/src/main/resources/org.apache.unomi.mailchimpconnector.cfg +++ b/extensions/unomi-mailchimp/services/src/main/resources/org.apache.unomi.mailchimpconnector.cfg @@ -14,4 +14,5 @@ # See the License for the specific language governing permissions and # limitations under the License. # -mailchimpConnector.headers.authorization.password= +mailChimpConnector.apiKey= +mailChimpConnector.url.subDomain= http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b2b1b312/extensions/weather-update/.gitignore ---------------------------------------------------------------------- diff --git a/extensions/weather-update/.gitignore b/extensions/weather-update/.gitignore deleted file mode 100644 index 851a31c..0000000 --- a/extensions/weather-update/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -/.classpath -/.project -/.settings -.idea -*.iml -*.ipr -target -repository.xml.generated
