INT-1471 more polishing, introduced Proxy to bridge Twiter4J types with SI-twitter interfaces, added tests

This commit is contained in:
Oleg Zhurakousky
2010-10-26 11:43:03 -04:00
committed by Chris Beams
parent 677fca51a9
commit 9d7be42db5
27 changed files with 570 additions and 472 deletions

View File

@@ -15,8 +15,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.integration</groupId>
@@ -21,9 +22,14 @@
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<!--<version>[2.1,)</version>-->
<!--<version>[2.1,)</version> -->
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>nlog4j</artifactId>

View File

@@ -25,13 +25,8 @@ import java.util.Date;
* @author Josh Long
* @since 2.0
*/
public interface DirectMessage {
int getId();
String getText();
Date getCreatedAt();
public interface DirectMessage extends TwitterMessage{
User getSender() ;
User getRecipient();

View File

@@ -1,37 +1,31 @@
/*
* Copyright 2010 the original author or authors
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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.
* 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.springframework.integration.twitter.core;
import java.util.Date;
/**
* describes a simple status message on twitter. this could be a message
* that updates a timeline on behalf of a user, or it could be a message that contains a reply.
*
*
* describes a simple status message on twitter. this could be a message that
* updates a timeline on behalf of a user, or it could be a message that
* contains a reply.
*
*
* @author Josh Long
* @since 2.0
* @since 2.0
*/
public interface Status {
Date getCreatedAt();
long getId();
String getText();
public interface Status extends TwitterMessage{
String getSource();
@@ -45,11 +39,11 @@ public interface Status {
boolean isFavorited();
User getUser();
User getUser();
boolean isRetweet();
Status getRetweetedStatus();
Status getRetweetedStatus();
String[] getContributors();

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import twitter4j.DirectMessage;
import twitter4j.GeoLocation;
import twitter4j.Status;
import twitter4j.User;
/**
* Sine our Twitter domain model is modeled after Twitter4j API
* this interceptor will delegate method calls that are made on SI Twitter interfaces
* to the corresponding Twitter4j objects.
*
* @author Oleg Zhurakousky
* @since 2.0
*/
class Twitter4jDecorator implements MethodInterceptor {
private final Object twitterObject;
public Twitter4jDecorator(Object twitterObject){
this.validateTwitter4JObject(twitterObject);
this.twitterObject = twitterObject;
}
/* (non-Javadoc)
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<?> twitter4jClass = twitterObject.getClass();
Object[] args = invocation.getArguments();
Class<?>[] argsType = new Class[args.length];
for (int i = 0; i < args.length; i++) {
Object object = args[i];
argsType[i] = object.getClass();
}
String invokedmethodName = invocation.getMethod().getName();
Method targetMethod = ReflectionUtils.findMethod(twitter4jClass, invokedmethodName, argsType);
return targetMethod.invoke(twitterObject, args);
}
private void validateTwitter4JObject(Object twitterObject){
Assert.notNull(twitterObject, "'twitterObject' must not be null");
Assert.isTrue(twitterObject instanceof Status ||
twitterObject instanceof DirectMessage ||
twitterObject instanceof User ||
twitterObject instanceof GeoLocation,
"Only twitter4j.DirectMessage, twitter4j.Status, twitter4j.User, twitter4j.GeoLocation is supported");
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.EmptyTargetSource;
import org.springframework.util.Assert;
import twitter4j.DirectMessage;
import twitter4j.Status;
/**
* Factory class with several static factory methods for constructing
* Twitter objects that are independent from the underlying API (e.g., twitter4j)
*
* @author Oleg Zhurakousky
* @since 2.0
*/
public class TwitterFactory {
private TwitterFactory(){}
/**
* Will construct either {@link org.springframework.integration.twitter.core.Status} or
* {@link org.springframework.integration.twitter.core.DirectMessage} from the instances of
* {@link Status} pr {@link DirectMessage}
*
* @param twitterMessage
* @return
*/
public static TwitterMessage formTwitter4jMessage(Object twitterMessage){
Assert.isTrue(twitterMessage instanceof Status || twitterMessage instanceof DirectMessage,
"'twitterMessage' must be an instance of either twitter4j.DirectMessage or twitter4j.Status");
Class<?> interfaze = twitterMessage instanceof DirectMessage
? org.springframework.integration.twitter.core.DirectMessage.class
: org.springframework.integration.twitter.core.Status.class;
ProxyFactory factory = new ProxyFactory(interfaze, EmptyTargetSource.INSTANCE);
factory.addAdvice(new Twitter4jDecorator(twitterMessage));
return (TwitterMessage) factory.getProxy();
}
/**
* Will constuct {@link GeoLocation} from 'latitude' and 'longitude'
*
* @param latitude
* @param longitude
* @return
*/
public static GeoLocation fromLatitudeLongitude(double latitude, double longitude){
twitter4j.GeoLocation geolocation = new twitter4j.GeoLocation(latitude, longitude);
return fromTwitter4jGeoLocation(geolocation);
}
/**
* Will constuct {@link GeoLocation} from {@link twitter4j.GeoLocation}
* @param geolocation
* @return
*/
public static GeoLocation fromTwitter4jGeoLocation(twitter4j.GeoLocation geolocation){
ProxyFactory factory = new ProxyFactory(GeoLocation.class, EmptyTargetSource.INSTANCE);
factory.addAdvice(new Twitter4jDecorator(geolocation));
return (GeoLocation) factory.getProxy();
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core;
import java.util.Date;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public interface TwitterMessage {
int getId();
String getText();
Date getCreatedAt();
}

View File

@@ -1,79 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core.twitter;
import org.springframework.integration.twitter.core.User;
import org.springframework.util.Assert;
import twitter4j.DirectMessage;
import java.util.Date;
/**
* {@link twitter4j.DirectMessage} wrapper for {@link org.springframework.integration.twitter.core.DirectMessage}.
*
* @author Josh Long
* @author Oleg ZHurakousky
* @since 2.0
*/
public class Twitter4jDirectMessage implements org.springframework.integration.twitter.core.DirectMessage {
private DirectMessage directMessage;
public Twitter4jDirectMessage(DirectMessage directMessage) {
Assert.notNull(directMessage, "'directMessage' must not be null");
this.directMessage = directMessage;
}
public DirectMessage getDirectMessage() {
return this.directMessage;
}
public int getId() {
return this.directMessage.getId();
}
public User getSender() {
return new Twitter4jUser(this.directMessage.getSender());
}
public User getRecipient() {
return new Twitter4jUser(this.directMessage.getRecipient());
}
public String getText() {
return this.directMessage.getText();
}
public int getSenderId() {
return this.directMessage.getSenderId();
}
public int getRecipientId() {
return this.directMessage.getRecipientId();
}
public Date getCreatedAt() {
return this.directMessage.getCreatedAt();
}
public String getSenderScreenName() {
return this.directMessage.getSenderScreenName();
}
public String getRecipientScreenName() {
return this.directMessage.getRecipientScreenName();
}
}

View File

@@ -1,48 +0,0 @@
/*
* Copyright 2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core.twitter;
import org.springframework.integration.twitter.core.GeoLocation;
/**
* Implements a notion of GeoLocation that forwards calls to {@link twitter4j.GeoLocation} instance
*
* @author Josh Long
* @since 2.0
*/
public class Twitter4jGeoLocation implements GeoLocation {
private twitter4j.GeoLocation geoLocation;
public twitter4j.GeoLocation getGeoLocation() {
return this.geoLocation;
}
public Twitter4jGeoLocation(double lat, double lon){
this.geoLocation = new twitter4j.GeoLocation(lat,lon);
}
public Twitter4jGeoLocation(twitter4j.GeoLocation gl) {
this.geoLocation = gl;
}
public double getLongitude() {
return this.geoLocation.getLongitude();
}
public double getLatitude() {
return this.geoLocation.getLatitude();
}
}

View File

@@ -1,89 +0,0 @@
/*
* Copyright 2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core.twitter;
import org.springframework.integration.twitter.core.Status;
import org.springframework.integration.twitter.core.User;
import java.util.Date;
/**
*
* An implemention o the {@link org.springframework.integration.twitter.core.Status} interface that
* forwards requests to a {@link twitter4j.Status} implementation
*
* @author Josh Long
* @since 2.0
*/
public class Twitter4jStatus implements Status {
private twitter4j.Status status;
public Twitter4jStatus(twitter4j.Status s) {
this.status = s;
}
public Date getCreatedAt() {
return this.status.getCreatedAt();
}
public long getId() {
return this.status.getId();
}
public String getText() {
return this.status.getText();
}
public String getSource() {
return this.status.getSource();
}
public boolean isTruncated() {
return this.status.isTruncated();
}
public long getInReplyToStatusId() {
return this.status.getInReplyToStatusId();
}
public int getInReplyToUserId() {
return this.status.getInReplyToUserId();
}
public String getInReplyToScreenName() {
return this.status.getInReplyToScreenName();
}
public boolean isFavorited() {
return this.status.isFavorited();
}
public User getUser() {
return new Twitter4jUser(this.status.getUser());
}
public boolean isRetweet() {
return this.status.isRetweet();
}
public Status getRetweetedStatus() {
return new Twitter4jStatus(this.status.getRetweetedStatus());
}
public String[] getContributors() {
return this.status.getContributors();
}
}

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core.twitter;
import twitter4j.User;
import java.net.URL;
/**
* implementation of the User interfce to represent Users in a Twitter application
*
* @author Josh Long
* @since 2.0
*/
public class Twitter4jUser implements org.springframework.integration.twitter.core.User {
private twitter4j.User user;
public Twitter4jUser(User u) {
this.user = u;
}
public int getId() {
return this.user.getId();
}
public String getName() {
return user.getName();
}
public String getScreenName() {
return this.user.getScreenName();
}
public String getLocation() {
return this.user.getLocation();
}
public String getDescription() {
return user.getDescription();
}
public boolean isContributorsEnabled() {
return user.isContributorsEnabled();
}
public URL getProfileImageURL() {
return user.getProfileImageURL();
}
public URL getURL() {
return this.user.getURL();
}
public boolean isProtected() {
return this.user.isProtected();
}
public int getFollowersCount() {
return this.user.getFollowersCount();
}
}

View File

@@ -14,14 +14,14 @@
* limitations under the License.
*/
package org.springframework.integration.twitter.inbound;
import org.springframework.integration.twitter.core.Status;
import org.springframework.integration.twitter.core.twitter.Twitter4jStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.springframework.integration.twitter.core.Status;
import org.springframework.integration.twitter.core.TwitterFactory;
/**
* Simple base class for the reply and timeline cases (as well as any other {@link twitter4j.Status} implementations of
@@ -40,7 +40,9 @@ abstract public class AbstractInboundTwitterStatusEndpointSupport extends Abstra
protected List<Status> fromTwitter4jStatuses(List<twitter4j.Status> stats) {
List<Status> fwd = new ArrayList<Status>();
for (twitter4j.Status s : stats) {
fwd.add(new Twitter4jStatus(s));
// ProxyFactory factory = new ProxyFactory(Status.class, EmptyTargetSource.INSTANCE);
// factory.addAdvice(new Twitter4jDecorator(s));
fwd.add((Status) TwitterFactory.formTwitter4jMessage(s));
}
return fwd;
}

View File

@@ -22,7 +22,7 @@ import java.util.List;
import org.springframework.integration.MessagingException;
import org.springframework.integration.twitter.core.DirectMessage;
import org.springframework.integration.twitter.core.twitter.Twitter4jDirectMessage;
import org.springframework.integration.twitter.core.TwitterFactory;
import twitter4j.Paging;
@@ -77,7 +77,9 @@ public class InboundDirectMessageEndpoint extends AbstractInboundTwitterEndpoint
List<DirectMessage> dmsToFwd = new ArrayList<DirectMessage>();
for( twitter4j.DirectMessage dm : dms) {
dmsToFwd.add( new Twitter4jDirectMessage( dm));
// ProxyFactory factory = new ProxyFactory(DirectMessage.class, EmptyTargetSource.INSTANCE);
// factory.addAdvice(new Twitter4jDecorator(dm));
dmsToFwd.add((DirectMessage) TwitterFactory.formTwitter4jMessage(dm));
}
forwardAll(dmsToFwd);
} catch (Exception e) {

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.twitter.outbound;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.util.Assert;
@@ -26,35 +27,37 @@ import twitter4j.TwitterException;
* Simple adapter to support sending outbound direct messages ("DM"s) using twitter
*
* @author Josh Long
* @see org.springframework.integration.twitter.core.TwitterHeaders
* @see twitter4j.Twitter
* @author Oleg Zhurakousky
* @since 2.0
*/
public class OutboundDirectMessageMessageHandler extends AbstractOutboundTwitterEndpointSupport {
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
if (this.twitter == null){
this.afterPropertiesSet();
}
try {
String txt = (String) message.getPayload();
Object payload = (String) message.getPayload();
Assert.isInstanceOf(String.class, payload, "Only payload of type String is supported. If your payload " +
"is not of type String you may want to introduce transformer");
Object toUser = message.getHeaders().containsKey(TwitterHeaders.TWITTER_DM_TARGET_USER_ID) ?
message.getHeaders().get(TwitterHeaders.TWITTER_DM_TARGET_USER_ID) :
null;
Assert.notNull(txt, "the message payload must be a String to be used as the direct message body text");
Assert.notNull(toUser, "the header '" + TwitterHeaders.TWITTER_DM_TARGET_USER_ID + "' must be present");
Assert.state(toUser instanceof String || toUser instanceof Integer,
"the header '" + TwitterHeaders.TWITTER_DM_TARGET_USER_ID + "' must be either a String (a screenname) or an int (a user ID)");
if (toUser instanceof Integer) {
this.twitter.sendDirectMessage((Integer) toUser, txt);
} else if (toUser instanceof String) {
this.twitter.sendDirectMessage((String) toUser, txt);
this.twitter.sendDirectMessage((Integer) toUser, (String) payload);
}
else if (toUser instanceof String) {
this.twitter.sendDirectMessage((String) toUser, (String) payload);
}
} catch (TwitterException e) {
logger.debug(e);
throw new RuntimeException(e);
throw new MessageHandlingException(message, e);
}
}
}

View File

@@ -19,8 +19,8 @@ import org.springframework.integration.Message;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.mapping.OutboundMessageMapper;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.integration.twitter.core.twitter.Twitter4jGeoLocation;
import org.springframework.util.StringUtils;
import twitter4j.StatusUpdate;
@@ -33,16 +33,16 @@ import twitter4j.StatusUpdate;
* @since 2.0
*/
public class OutboundStatusUpdateMessageMapper implements OutboundMessageMapper<StatusUpdate> {
/**
* convenient, interface-oriented way of obtaining a reference to a {@link org.springframework.integration.twitter.core.twitter.Twitter4jGeoLocation}
*
* @param lat the latitude
* @param lon the longitude
* @return a {@link org.springframework.integration.twitter.core.GeoLocation} instance
*/
public org.springframework.integration.twitter.core.GeoLocation fromLatitudeLongitudePair(double lat, double lon) {
return new Twitter4jGeoLocation(lat, lon);
}
// /**
// * convenient, interface-oriented way of obtaining a reference to a {@link org.springframework.integration.twitter.core.twitter.Twitter4jGeoLocation}
// *
// * @param lat the latitude
// * @param lon the longitude
// * @return a {@link org.springframework.integration.twitter.core.GeoLocation} instance
// */
// public org.springframework.integration.twitter.core.GeoLocation fromLatitudeLongitudePair(double lat, double lon) {
// return new Twitter4jGeoLocation(lat, lon);
// }
/**
* {@link StatusUpdate} instances are used to drive status updates.
@@ -79,16 +79,16 @@ public class OutboundStatusUpdateMessageMapper implements OutboundMessageMapper<
if (message.getHeaders().containsKey(TwitterHeaders.TWITTER_GEOLOCATION)) {
org.springframework.integration.twitter.core.GeoLocation geoLocation = (org.springframework.integration.twitter.core.GeoLocation) message.getHeaders()
.get(TwitterHeaders.TWITTER_GEOLOCATION);
twitter4j.GeoLocation gl = null;
if (geoLocation instanceof Twitter4jGeoLocation) {
gl = ((Twitter4jGeoLocation) geoLocation).getGeoLocation();
if (null != gl) {
statusUpdate.location(gl);
}
}
// org.springframework.integration.twitter.core.GeoLocation geoLocation = (org.springframework.integration.twitter.core.GeoLocation) message.getHeaders()
// .get(TwitterHeaders.TWITTER_GEOLOCATION);
// twitter4j.GeoLocation gl = null;
//
// if (geoLocation instanceof Twitter4jGeoLocation) {
// gl = ((Twitter4jGeoLocation) geoLocation).getGeoLocation();
// if (null != gl) {
// statusUpdate.location(gl);
// }
// }
}
if (message.getHeaders()

View File

@@ -33,7 +33,6 @@ import org.mockito.Mockito;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.twitter.core.twitter.Twitter4jDirectMessage;
import org.springframework.integration.twitter.inbound.InboundDirectMessageEndpoint;
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -79,10 +78,11 @@ public class InboundDirectMessageStatusEndpointTests {
endpoint.start();
Message message1 = channel.receive(3000);
assertNotNull(message1);
assertEquals(secondMessage, ((Twitter4jDirectMessage)message1.getPayload()).getDirectMessage());
System.out.println();
assertEquals(secondMessage.getId(), ((org.springframework.integration.twitter.core.DirectMessage)message1.getPayload()).getId());
Message message2 = channel.receive(3000);
assertNotNull(message2);
assertEquals(firstMessage, ((Twitter4jDirectMessage)message2.getPayload()).getDirectMessage());
assertEquals(firstMessage.getId(), ((org.springframework.integration.twitter.core.DirectMessage)message2.getPayload()).getId());
}
@Before

View File

@@ -1,60 +0,0 @@
/*
* Copyright 2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.twitter.Twitter4jGeoLocation;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author Josh Long
*/
@ContextConfiguration
public class TestSendingDMsUsingNamespace extends AbstractJUnit4SpringContextTests {
private volatile MessagingTemplate messagingTemplate = new MessagingTemplate();
@Value("#{out}") private MessageChannel channel;
@Test
@Ignore
public void testSendingATweet() throws Throwable {
String dmUsr = System.getProperties().getProperty("twitter.dm.user");
Assert.notNull( dmUsr , "the DM user's null");
MessageBuilder<String> mb = MessageBuilder.withPayload("'Hello world!', from the Spring Integration outbound Twitter adapter")
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new Twitter4jGeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true);
if (StringUtils.hasText(dmUsr)) {
mb.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, dmUsr);
}
this.messagingTemplate.send(this.channel, mb.build());
}
}

View File

@@ -23,7 +23,6 @@ import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.twitter.Twitter4jGeoLocation;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@@ -43,7 +42,7 @@ public class TestSendingUpdatesUsingNamespace extends AbstractJUnit4SpringContex
public void testSendingATweet() throws Throwable {
MessageBuilder<String> mb = MessageBuilder.withPayload("simple test demonstrating the ability to encode location information")
.setHeader(TwitterHeaders.TWITTER_IN_REPLY_TO_STATUS_ID, 21927437001L)
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new Twitter4jGeoLocation(-76.226823, 23.642465)) // antarctica
//.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new Twitter4jGeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true);
Message<String> m = mb.build();
this.messagingTemplate.send(this.channel, m);

View File

@@ -1,21 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2010 the original author or authors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<beans:beans
xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.config;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Oleg Zhurakousky
*
*/
public class TestReceivingUsingNamespace {
@Test
@Ignore
/*
* In order to run this test you need to provide values to the twitter.properties file
*/
public void testUpdatesWithRealTwitter() throws Exception{
CountDownLatch latch = new CountDownLatch(1);
new ClassPathXmlApplicationContext("TestReceivingUsingNamespace-context.xml", this.getClass());
latch.await(10000, TimeUnit.SECONDS);
}
}

View File

@@ -1,21 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2010 the original author or authors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<beans:beans
xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
@@ -33,11 +16,9 @@
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd">
<context:component-scan
base-package="org.springframework.integration.twitter"/>
<context:property-placeholder
location="file://${user.home}/Desktop/twitter.properties"
location="classpath:twitter.properties"
ignore-unresolvable="true"/>
<twitter:twitter-connection
@@ -45,14 +26,12 @@
access-token="${twitter.oauth.accessToken}"
access-token-secret="${twitter.oauth.accessTokenSecret}"
consumer-key="${twitter.oauth.consumerKey}"
consumer-secret="${twitter.oauth.consumerSecret}"
/>
consumer-secret="${twitter.oauth.consumerSecret}"/>
<channel id="out"/>
<twitter:outbound-dm-channel-adapter twitter-connection="tc" channel="out"/>
<channel id="inputChannel"/>
<twitter:outbound-dm-channel-adapter twitter-connection="tc" channel="inputChannel"/>
</beans:beans>

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.config;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.TwitterFactory;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.util.StringUtils;
/**
* @author Josh Long
* @author Oleg Zhurakouksy
*/
@ContextConfiguration
public class TestSendingDMsUsingNamespace extends AbstractJUnit4SpringContextTests {
@Autowired
@Qualifier("inputChannel")
private MessageChannel inputChannel;
@Test
@Ignore
public void testSendigRealDirectMessage() throws Throwable {
String dmUsr = "z_oleg";
MessageBuilder<String> mb = MessageBuilder.withPayload("'Hello world!', from the Spring Integration outbound Twitter adapter " + System.currentTimeMillis())
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, TwitterFactory.fromLatitudeLongitude(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true);
if (StringUtils.hasText(dmUsr)) {
mb.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, dmUsr);
}
inputChannel.send(mb.build());
}
}

View File

@@ -0,0 +1,34 @@
/**
*
*/
package org.springframework.integration.twitter.core;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.EmptyTargetSource;
import org.springframework.integration.twitter.core.Twitter4jDecorator;
import twitter4j.Status;
/**
* @author ozhurakousky
*
*/
public class Twitter4jDecoratorTests {
@Test
public void testTwitter4JDecorator(){
Status status = mock(Status.class);
when(status.getSource()).thenReturn("Hello World");
ProxyFactory factory = new ProxyFactory(org.springframework.integration.twitter.core.Status.class, EmptyTargetSource.INSTANCE);
factory.addAdvice(new Twitter4jDecorator(status));
Object decoratedStatus = factory.getProxy();
assertTrue(decoratedStatus instanceof org.springframework.integration.twitter.core.Status);
String source = ((org.springframework.integration.twitter.core.Status)decoratedStatus).getSource();
System.out.println("source: " + source);
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.inbound;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import twitter4j.DirectMessage;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
*
*/
public class InboundDirectMessageStatusEndpointTests {
private DirectMessage firstMessage;
private DirectMessage secondMessage;
private Twitter twitter;
@Test
public void testTwitterMockedUpdates() throws Exception{
QueueChannel channel = new QueueChannel();
InboundDirectMessageEndpoint endpoint = new InboundDirectMessageEndpoint();
endpoint.setOutputChannel(channel);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
endpoint.setTaskScheduler(scheduler);
endpoint.setConfiguration(this.getTestConfigurationForDirectMessages());
endpoint.afterPropertiesSet();
endpoint.start();
Message message1 = channel.receive(3000);
assertNotNull(message1);
System.out.println();
assertEquals(secondMessage.getId(), ((org.springframework.integration.twitter.core.DirectMessage)message1.getPayload()).getId());
Message message2 = channel.receive(3000);
assertNotNull(message2);
assertEquals(firstMessage.getId(), ((org.springframework.integration.twitter.core.DirectMessage)message2.getPayload()).getId());
}
@Before
public void prepare(){
twitter = mock(Twitter.class);
firstMessage = mock(DirectMessage.class);
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
when(firstMessage.getId()).thenReturn(200);
secondMessage = mock(DirectMessage.class);
when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L));
when(secondMessage.getId()).thenReturn(2000);
}
@SuppressWarnings("unchecked")
private OAuthConfiguration getTestConfigurationForDirectMessages() throws Exception{
OAuthConfiguration configuration = mock(OAuthConfiguration.class);
RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class);
when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatus);
when(configuration.getTwitter()).thenReturn(twitter);
when(rateLimitStatus.getSecondsUntilReset()).thenReturn(2464);
when(rateLimitStatus.getRemainingHits()).thenReturn(250);
ResponseList<DirectMessage> responses = mock(ResponseList.class);
List<DirectMessage> testMessages = new ArrayList<DirectMessage>();
testMessages.add(firstMessage);
testMessages.add(secondMessage);
when(responses.iterator()).thenReturn(testMessages.iterator());
when(twitter.getDirectMessages()).thenReturn(responses);
when(twitter.getDirectMessages(Mockito.any(Paging.class))).thenReturn(responses);
return configuration;
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.outbound;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.TwitterFactory;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
*
*/
public class OutboundDirectMessageMessageHandlerTests {
private Twitter twitter;
@Test
public void validateSendDirectMessage() throws Exception{
MessageBuilder<String> mb = MessageBuilder.withPayload("hello")
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, TwitterFactory.fromLatitudeLongitude(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, "foo");
OutboundDirectMessageMessageHandler handler = new OutboundDirectMessageMessageHandler();
handler.setConfiguration(this.getTestConfiguration());
handler.afterPropertiesSet();
handler.handleMessage(mb.build());
verify(twitter, times(1)).sendDirectMessage("foo", "hello");
mb = MessageBuilder.withPayload("hello")
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, TwitterFactory.fromLatitudeLongitude(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, 123);
handler.handleMessage(mb.build());
verify(twitter, times(1)).sendDirectMessage(123, "hello");
}
private OAuthConfiguration getTestConfiguration() throws Exception{
twitter = mock(Twitter.class);
OAuthConfiguration configuration = mock(OAuthConfiguration.class);
when(configuration.getTwitter()).thenReturn(twitter);
return configuration;
}
}

View File

@@ -1,5 +1,5 @@
twitter.oauth.consumerKey=
twitter.oauth.consumerSecret=
twitter.oauth.pin=
twitter.oauth.accessToken=
twitter.oauth.accessTokenSecret=
twitter.oauth.accessTokenSecret=
twitter.oauth.pin=

View File

@@ -3,10 +3,12 @@ Bundle-Name: Spring Integration Twitter Support
Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Template:
org.aopalliance.*;version="[1.0.0, 2.0.0)",
org.apache.commons.logging;version="[1.1.1, 2.0.0)",
org.apache.commons.lang.*;version="[2.5.0, 3.0.0)",
org.springframework.integration.*;version="[2.0.0, 2.0.1)",
org.springframework.scheduling.*;version="[3.0.5, 4.0.0)",
org.springframework.aop.*;version="[3.0.5, 4.0.0)",
org.springframework.beans.*;version="[3.0.5, 4.0.0)",
org.springframework.context;version="[3.0.5, 4.0.0)",
org.springframework.core.*;version="[3.0.5, 4.0.0)",