1 | |
package fr.paris.lutece.plugins.releaser.service; |
2 | |
|
3 | |
import java.util.Properties; |
4 | |
|
5 | |
import org.apache.commons.lang3.StringUtils; |
6 | |
|
7 | |
import twitter4j.Status; |
8 | |
import twitter4j.Twitter; |
9 | |
import twitter4j.TwitterException; |
10 | |
import twitter4j.TwitterFactory; |
11 | |
import twitter4j.auth.Authorization; |
12 | |
import twitter4j.auth.AuthorizationFactory; |
13 | |
import twitter4j.conf.Configuration; |
14 | |
import twitter4j.conf.PropertyConfiguration; |
15 | |
import fr.paris.lutece.plugins.releaser.util.CommandResult; |
16 | |
import fr.paris.lutece.plugins.releaser.util.ConstanteUtils; |
17 | |
import fr.paris.lutece.plugins.releaser.util.ReleaserUtils; |
18 | |
import fr.paris.lutece.portal.service.spring.SpringContextService; |
19 | |
import fr.paris.lutece.portal.service.util.AppPropertiesService; |
20 | |
|
21 | |
public class TwitterService implements ITwitterService |
22 | |
{ |
23 | |
|
24 | |
|
25 | |
|
26 | |
private static final String TWITTER_KEY_OAUTH_CONSUMER_KEY="oauth.consumerKey"; |
27 | |
private static final String TWITTER_KEY_OAUTH_CONSUMER_SECRET="oauth.consumerSecret"; |
28 | |
private static final String TWITTER_KEY_OAUTH_ACCESS_TOKEN="oauth.accessToken"; |
29 | |
private static final String TWITTER_KEY_OAUTH_ACCESS_TOKEN_SECRET="oauth.accessTokenSecret"; |
30 | |
private static final String TWITTER_KEY_OAUTH_ACCESS_TOKEN_URL="oauth.accessTokenURL"; |
31 | |
private static final String TWITTER_KEY_OAUTH_REQUEST_TOKEN_URL="oauth.requestTokenURL"; |
32 | |
private static final String TWITTER_KEY_OAUTH_AUTHORIZATION_URL="oauth.authorizationURL"; |
33 | |
|
34 | |
private static final String TWITTER_KEY_HTTP_PROXY_HOST=" http.proxyHost"; |
35 | |
private static final String TWITTER_KEY_HTTP_PROXY_PORT=" http.proxyPort"; |
36 | |
|
37 | |
|
38 | 0 | private static Twitter _twitter = null; |
39 | 0 | private static ITwitterService _instance = null; |
40 | |
|
41 | |
public static ITwitterService getService() |
42 | |
{ |
43 | 0 | if(_instance==null) |
44 | |
{ |
45 | 0 | _instance=SpringContextService.getBean(ConstanteUtils.BEAN_MAVEN_SERVICE ); |
46 | 0 | _instance.init( ); |
47 | |
} |
48 | |
|
49 | 0 | return _instance; |
50 | |
} |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public TwitterService( ) |
57 | 0 | { |
58 | |
|
59 | 0 | } |
60 | |
|
61 | |
|
62 | |
public void sendTweet(String strTweet, CommandResult commandResult ) |
63 | |
{ |
64 | |
|
65 | |
try |
66 | |
{ |
67 | 0 | if(!StringUtils.isEmpty( strTweet )) |
68 | |
{ |
69 | 0 | Status status = _twitter.updateStatus( strTweet ); |
70 | 0 | commandResult.getLog( ).append("Le Tweet " + status.getText( ) + " a été envoyé depuis " + status.getUser( ).getScreenName( ) + "\n"); |
71 | 0 | } |
72 | |
else |
73 | |
{ |
74 | 0 | ReleaserUtils.addTechnicalError( commandResult, "Le tweet n'a pas été envoyé car il est vide "); |
75 | |
} |
76 | |
|
77 | |
} |
78 | 0 | catch ( TwitterException e ) |
79 | |
{ |
80 | |
|
81 | 0 | ReleaserUtils.addTechnicalError( commandResult, "Une erreur est surnvenue lors de l'envoi du tweet:"+strTweet, e ); |
82 | 0 | } |
83 | |
|
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void init( ) |
92 | |
{ |
93 | 0 | Properties props = new Properties( ); |
94 | 0 | props.put( TWITTER_KEY_OAUTH_CONSUMER_KEY, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_CONSUMER_KEY )); |
95 | 0 | props.put( TWITTER_KEY_OAUTH_CONSUMER_SECRET, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_CONSUMER_SECRET ) ); |
96 | 0 | props.put( TWITTER_KEY_OAUTH_ACCESS_TOKEN, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_ACCESS_TOKEN ) ); |
97 | 0 | props.put( TWITTER_KEY_OAUTH_ACCESS_TOKEN_SECRET, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_ACCESS_TOKEN_SECRET ) ); |
98 | 0 | props.put( TWITTER_KEY_OAUTH_REQUEST_TOKEN_URL, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_REQUEST_TOKEN_URL ) ); |
99 | 0 | props.put( TWITTER_KEY_OAUTH_AUTHORIZATION_URL, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_AUTHORIZATION_URL ) ); |
100 | 0 | props.put( TWITTER_KEY_OAUTH_ACCESS_TOKEN_URL, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_ACCESSTOKEN_URL ) ); |
101 | 0 | props.put( TWITTER_KEY_HTTP_PROXY_HOST, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_PROXY_HOST ) ); |
102 | 0 | props.put( TWITTER_KEY_HTTP_PROXY_PORT, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_PROXY_PORT ) ); |
103 | |
|
104 | 0 | Configuration conf = new PropertyConfiguration( props ); |
105 | 0 | Authorization auth = AuthorizationFactory.getInstance( conf ); |
106 | |
|
107 | 0 | _twitter = new TwitterFactory( conf ).getInstance( auth ); |
108 | |
|
109 | 0 | } |
110 | |
|
111 | |
} |