Unverified Commit 53ad01d0 authored by Adrien's avatar Adrien Committed by GitHub
Browse files

Merge pull request #3 from bonitasoft/fix_twitter_connector

Update twitter4j version to the last version and perform some refactoring
parents 133160db be8649d4
......@@ -29,7 +29,7 @@
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.2</version>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
......
......@@ -18,76 +18,55 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.bonitasoft.engine.connector.Connector;
import org.bonitasoft.engine.connector.ConnectorException;
import org.bonitasoft.engine.connector.ConnectorValidationException;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
/**
* This connector provides a twitter sending service.
*
* @author Matthieu Chaffotte
* @author Yanyan Liu
* @author Baptiste Mesta
* @author Haris Subašić
* @author Maxence Raoux
*/
public abstract class TwitterConnector implements Connector {
private String proxyHost;
private Optional<String> proxyHost;
private Integer proxyPort;
private Optional<Integer> proxyPort;
private String proxyUser;
private Optional<String> proxyUser;
private String proxyPass;
private Optional<String> proxyPass;
private String consumerKey;
private Optional<String> consumerKey;
private String consumerSecret;
private Optional<String> consumerSecret;
private String accessToken;
private Optional<String> accessToken;
private String accessTokenSecret;
private Optional<String> accessTokenSecret;
@Override
public void setInputParameters(final Map<String, Object> parameters) {
final Object proxyHostObject = parameters.get("proxyHost");
proxyHost = proxyHostObject != null ? (String) proxyHostObject : "";
final Object proxyPortObject = parameters.get("proxyPort");
proxyPort = proxyPortObject != null ? (Integer) proxyPortObject : 0;
final Object proxyUserObject = parameters.get("proxyUser");
proxyUser = proxyUserObject != null ? (String) proxyUserObject : "";
final Object proxyPassObject = parameters.get("proxyPass");
proxyPass = proxyPassObject != null ? (String) proxyPassObject : "";
final Object consumerKeyObject = parameters.get("consumerKey");
consumerKey = consumerKeyObject != null ? (String) consumerKeyObject
: "";
final Object consumerSecretObject = parameters.get("consumerSecret");
consumerSecret = consumerSecretObject != null ? (String) consumerSecretObject
: "";
final Object accessTokenObject = parameters.get("accessToken");
accessToken = accessTokenObject != null ? (String) accessTokenObject
: "";
final Object accessTokenSecretObject = parameters
.get("accessTokenSecret");
accessTokenSecret = accessTokenSecretObject != null ? (String) accessTokenSecretObject
: "";
public void setInputParameters(Map<String, Object> parameters) {
proxyHost = getStringParameter(parameters, "proxyHost");
proxyPort = getIntegerParameter(parameters, "proxyPort");
proxyUser = getStringParameter(parameters, "proxyUser");
proxyPass = getStringParameter(parameters, "proxyPass");
consumerKey = getStringParameter(parameters, "consumerKey");
consumerSecret = getStringParameter(parameters, "consumerSecret");
accessToken = getStringParameter(parameters, "accessToken");
accessTokenSecret = getStringParameter(parameters, "accessTokenSecret");
}
@Override
public void validateInputParameters() throws ConnectorValidationException {
final List<String> errors = new ArrayList<String>(1);
List<String> errors = new ArrayList<>();
if (proxyPort < 0) {
if (proxyPort.orElse(0) < 0) {
errors.add("proxyPort cannot be less than 0!");
} else if (proxyPort > 65535) {
} else if (proxyPort.orElse(0) > 65535) {
errors.add("proxyPort cannot be greater than 65535!");
}
......@@ -98,33 +77,38 @@ public abstract class TwitterConnector implements Connector {
@Override
public Map<String, Object> execute() throws ConnectorException {
final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
if (proxyHost != null && proxyPort != null) {
configurationBuilder.setHttpProxyHost(proxyHost);
configurationBuilder.setHttpProxyPort(proxyPort);
if (proxyUser != null && proxyPass != null) {
configurationBuilder.setHttpProxyUser(proxyUser);
configurationBuilder.setHttpProxyPassword(proxyPass);
}
}
configurationBuilder.setOAuthConsumerKey(consumerKey);
configurationBuilder.setOAuthConsumerSecret(consumerSecret);
configurationBuilder.setOAuthAccessToken(accessToken);
configurationBuilder.setOAuthAccessTokenSecret(accessTokenSecret);
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
setProxyConfiguration(configurationBuilder);
setOAuthCOnfiguration(configurationBuilder);
try {
final TwitterFactory tf = new TwitterFactory(
configurationBuilder.build());
final Twitter twitter = tf.getInstance();
TwitterFactory tf = new TwitterFactory(configurationBuilder.build());
Twitter twitter = tf.getInstance();
executeTask(twitter);
} catch (final Exception e) {
} catch (TwitterException e) {
throw new ConnectorException(e);
}
return Collections.emptyMap();
}
protected abstract void executeTask(Twitter twitter) throws Exception;
private void setOAuthCOnfiguration(ConfigurationBuilder configurationBuilder) {
configurationBuilder.setOAuthConsumerKey(consumerKey.orElse(""));
configurationBuilder.setOAuthConsumerSecret(consumerSecret.orElse(""));
configurationBuilder.setOAuthAccessToken(accessToken.orElse(""));
configurationBuilder.setOAuthAccessTokenSecret(accessTokenSecret.orElse(""));
}
private void setProxyConfiguration(ConfigurationBuilder configurationBuilder) {
if (proxyHost.isPresent() && proxyPort.isPresent()) {
configurationBuilder.setHttpProxyHost(proxyHost.get());
configurationBuilder.setHttpProxyPort(proxyPort.get());
if (proxyUser.isPresent() && proxyPass.isPresent()) {
configurationBuilder.setHttpProxyUser(proxyUser.get());
configurationBuilder.setHttpProxyPassword(proxyPass.get());
}
}
}
protected abstract void executeTask(Twitter twitter) throws TwitterException;
@Override
public void connect() throws ConnectorException {
......@@ -133,4 +117,16 @@ public abstract class TwitterConnector implements Connector {
@Override
public void disconnect() throws ConnectorException {
}
protected Optional<String> getStringParameter(Map<String, Object> parameters, String key) {
return getParameter(parameters, key).filter(String.class::isInstance).map(String.class::cast);
}
protected Optional<Integer> getIntegerParameter(Map<String, Object> parameters, String key) {
return getParameter(parameters, key).filter(Integer.class::isInstance).map(Integer.class::cast);
}
private Optional<Object> getParameter(Map<String, Object> parameters, String key) {
return Optional.ofNullable(parameters.get(key));
}
}
......@@ -15,32 +15,26 @@
package org.bonitasoft.connectors.twitter;
import java.util.Map;
import java.util.Optional;
import twitter4j.Twitter;
import twitter4j.TwitterException;
/**
* @author Matthieu Chaffotte
* @author Haris Subašić
*/
public class TwitterDirectMessageConnector extends TwitterConnector {
private String message;
private String recipientId;
private Optional<String> message;
private Optional<String> recipientId;
@Override
public void setInputParameters(final Map<String, Object> parameters) {
public void setInputParameters(Map<String, Object> parameters) {
super.setInputParameters(parameters);
final Object messageObject = parameters.get("message");
message = messageObject != null ? (String) messageObject : "";
final Object recipientIdObject = parameters.get("recipientId");
recipientId = recipientIdObject != null ? (String) recipientIdObject
: "";
message = getStringParameter(parameters, "message");
recipientId = getStringParameter(parameters, "recipientId");
}
@Override
protected void executeTask(final Twitter twitter) throws Exception {
twitter.sendDirectMessage(recipientId, message);
protected void executeTask(Twitter twitter) throws TwitterException {
twitter.sendDirectMessage(recipientId.orElse(""), message.orElse(""));
}
}
......@@ -15,26 +15,24 @@
package org.bonitasoft.connectors.twitter;
import java.util.Map;
import java.util.Optional;
import twitter4j.Twitter;
import twitter4j.TwitterException;
/**
* @author Matthieu Chaffotte
* @author Haris Subasic
*/
public class TwitterUpdateStatusConnector extends TwitterConnector {
private String status;
private Optional<String> status;
@Override
public void setInputParameters(final Map<String, Object> parameters) {
public void setInputParameters(Map<String, Object> parameters) {
super.setInputParameters(parameters);
final Object statusObject = parameters.get("status");
status = statusObject != null ? (String) statusObject : "";
status = getStringParameter(parameters, "status");
}
@Override
protected void executeTask(final Twitter twitter) throws Exception {
twitter.updateStatus(status);
protected void executeTask(Twitter twitter) throws TwitterException {
twitter.updateStatus(status.orElse(""));
}
}
......@@ -2,8 +2,8 @@
<implementation:connectorImplementation xmlns:implementation="http://www.bonitasoft.org/ns/connector/implementation/6.0">
<definitionId>twitter-direct</definitionId>
<definitionVersion>$definition.version$</definitionVersion>
<description>Implementation based on twitter4j-core 3.0.3</description>
<definitionVersion>1.0.0</definitionVersion>
<description>Implementation based on twitter4j-core 4.0.7</description>
<implementationClassname>org.bonitasoft.connectors.twitter.TwitterDirectMessageConnector</implementationClassname>
<implementationId>twitter-direct</implementationId>
<implementationVersion>$implementation.version$</implementationVersion>
......
......@@ -2,8 +2,8 @@
<implementation:connectorImplementation xmlns:implementation="http://www.bonitasoft.org/ns/connector/implementation/6.0">
<definitionId>twitter-update-status</definitionId>
<definitionVersion>$definition.version$</definitionVersion>
<description>Implementation based on twitter4j-core 3.0.3</description>
<definitionVersion>1.0.0</definitionVersion>
<description>Implementation based on twitter4j-core 4.0.7</description>
<implementationClassname>org.bonitasoft.connectors.twitter.TwitterUpdateStatusConnector</implementationClassname>
<implementationId>twitter-update-status</implementationId>
<implementationVersion>$implementation.version$</implementationVersion>
......
......@@ -33,100 +33,89 @@ import org.junit.Test;
public abstract class TwitterConnectorTest {
protected static final Logger LOG = Logger
.getLogger(TwitterConnectorTest.class.getName());
@Before
public void initialize() throws Exception {
if (TwitterConnectorTest.LOG.isLoggable(Level.WARNING)) {
TwitterConnectorTest.LOG.warning("======== Starting test: "
+ this.getClass().getName() + "() ==========");
}
}
@After
public void addLogEnding() throws Exception {
if (TwitterConnectorTest.LOG.isLoggable(Level.WARNING)) {
TwitterConnectorTest.LOG.warning("======== Ending test: "
+ this.getClass().getName() + " ==========");
}
}
protected abstract Class<? extends Connector> getConnectorClass();
//
// @Test(expected = ConnectorValidationException.class)
// public void validateConnector() throws BonitaException {
// Connector.validate();
// Assert.assertTrue(Connector.validateConnector(getConnectorClass())
// .isEmpty())
// assertTrue();
// }
@Test(expected = ConnectorValidationException.class)
public void setWrappedSmtpPortWithLessThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("proxyHost", "");
parameters.put("proxyPort", -1);
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Test(expected = ConnectorValidationException.class)
public void setWrappedSmtpPortWithGreaterThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("proxyHost", "");
parameters.put("proxyPort", 65536);
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Test(expected = ConnectorValidationException.class)
public void setSmtpPortWithLessThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("proxyPort", -1);
parameters.put("proxyHost", "");
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Test(expected = ConnectorValidationException.class)
public void setSmtpPortWithGreaterThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("proxyPort", 65536);
parameters.put("proxyHost", "");
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Ignore
@Test
public void execute() {
final Map<String, Object> emptyMap = Collections.emptyMap();
final TwitterConnector connector = getTwitterConnector(emptyMap);
try {
connector.execute();
} catch (final Exception e) {
e.printStackTrace();
fail("Impossible! A Tweet must be sent");
}
}
public Map<String, Object> getTwitterConnectorParameters() {
final Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("consumerKey", "zerzer");
parameters.put("consumerSecret", "mljpoi");
parameters.put("accesToken",
"7MyWkyU2RuQxeNGTqn8AItTWuAIb9juhN3mhgBmUKn8");
parameters.put("accessTokenSecret",
"zTf1uz5RZR9XmuAdPnMwk4PfnLRoI1o4gyNU7wSlQ");
parameters.put("status", "poafjaofj");
parameters.put("proxyHost", "myproxy.bonitasoft.com");
parameters.put("proxyPort", 8080);
return parameters;
}
public abstract TwitterConnector getTwitterConnector(
Map<String, Object> parameters);
protected static final Logger LOG = Logger
.getLogger(TwitterConnectorTest.class.getName());
@Before
public void initialize() throws Exception {
if (TwitterConnectorTest.LOG.isLoggable(Level.WARNING)) {
TwitterConnectorTest.LOG.warning("======== Starting test: " + this.getClass().getName() + "() ==========");
}
}
@After
public void addLogEnding() throws Exception {
if (TwitterConnectorTest.LOG.isLoggable(Level.WARNING)) {
TwitterConnectorTest.LOG.warning("======== Ending test: " + this.getClass().getName() + " ==========");
}
}
protected abstract Class<? extends Connector> getConnectorClass();
@Test(expected = ConnectorValidationException.class)
public void setWrappedSmtpPortWithLessThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<>();
parameters.put("proxyHost", "");
parameters.put("proxyPort", -1);
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Test(expected = ConnectorValidationException.class)
public void setWrappedSmtpPortWithGreaterThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<>();
parameters.put("proxyHost", "");
parameters.put("proxyPort", 65536);
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Test(expected = ConnectorValidationException.class)
public void setSmtpPortWithLessThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<>();
parameters.put("proxyPort", -1);
parameters.put("proxyHost", "");
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Test(expected = ConnectorValidationException.class)
public void setSmtpPortWithGreaterThanRange() throws BonitaException {
final Map<String, Object> parameters = new HashMap<>();
parameters.put("proxyPort", 65536);
parameters.put("proxyHost", "");
final TwitterConnector connector = getTwitterConnector(parameters);
connector.validateInputParameters();
}
@Ignore
@Test
public void execute() {
final Map<String, Object> emptyMap = Collections.emptyMap();
final TwitterConnector connector = getTwitterConnector(emptyMap);
try {
connector.execute();
} catch (final Exception e) {
e.printStackTrace();
fail("Impossible! A Tweet must be sent");
}
}
public Map<String, Object> getTwitterConnectorParameters() {
final Map<String, Object> parameters = new HashMap<>();
parameters.put("consumerKey", "zerzer");
parameters.put("consumerSecret", "mljpoi");
parameters.put("accesToken",
"7MyWkyU2RuQxeNGTqn8AItTWuAIb9juhN3mhgBmUKn8");
parameters.put("accessTokenSecret",
"zTf1uz5RZR9XmuAdPnMwk4PfnLRoI1o4gyNU7wSlQ");
parameters.put("status", "poafjaofj");
parameters.put("proxyHost", "myproxy.bonitasoft.com");
parameters.put("proxyPort", 8080);
return parameters;
}
public abstract TwitterConnector getTwitterConnector(
Map<String, Object> parameters);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment