View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.releaser.service;
35  
36  import java.util.Properties;
37  
38  import org.apache.commons.lang3.StringUtils;
39  
40  import twitter4j.Status;
41  import twitter4j.Twitter;
42  import twitter4j.TwitterException;
43  import twitter4j.TwitterFactory;
44  import twitter4j.auth.Authorization;
45  import twitter4j.auth.AuthorizationFactory;
46  import twitter4j.conf.Configuration;
47  import twitter4j.conf.PropertyConfiguration;
48  import fr.paris.lutece.plugins.releaser.util.CommandResult;
49  import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
50  import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
51  import fr.paris.lutece.portal.service.spring.SpringContextService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  
54  // TODO: Auto-generated Javadoc
55  /**
56   * The Class TwitterService.
57   */
58  public class TwitterService implements ITwitterService
59  {
60  
61      /** The Constant TWITTER_KEY_OAUTH_CONSUMER_KEY. */
62      private static final String TWITTER_KEY_OAUTH_CONSUMER_KEY = "oauth.consumerKey";
63  
64      /** The Constant TWITTER_KEY_OAUTH_CONSUMER_SECRET. */
65      private static final String TWITTER_KEY_OAUTH_CONSUMER_SECRET = "oauth.consumerSecret";
66  
67      /** The Constant TWITTER_KEY_OAUTH_ACCESS_TOKEN. */
68      private static final String TWITTER_KEY_OAUTH_ACCESS_TOKEN = "oauth.accessToken";
69  
70      /** The Constant TWITTER_KEY_OAUTH_ACCESS_TOKEN_SECRET. */
71      private static final String TWITTER_KEY_OAUTH_ACCESS_TOKEN_SECRET = "oauth.accessTokenSecret";
72  
73      /** The Constant TWITTER_KEY_OAUTH_ACCESS_TOKEN_URL. */
74      private static final String TWITTER_KEY_OAUTH_ACCESS_TOKEN_URL = "oauth.accessTokenURL";
75  
76      /** The Constant TWITTER_KEY_OAUTH_REQUEST_TOKEN_URL. */
77      private static final String TWITTER_KEY_OAUTH_REQUEST_TOKEN_URL = "oauth.requestTokenURL";
78  
79      /** The Constant TWITTER_KEY_OAUTH_AUTHORIZATION_URL. */
80      private static final String TWITTER_KEY_OAUTH_AUTHORIZATION_URL = "oauth.authorizationURL";
81  
82      /** The Constant TWITTER_KEY_HTTP_PROXY_HOST. */
83      private static final String TWITTER_KEY_HTTP_PROXY_HOST = " http.proxyHost";
84  
85      /** The Constant TWITTER_KEY_HTTP_PROXY_PORT. */
86      private static final String TWITTER_KEY_HTTP_PROXY_PORT = " http.proxyPort";
87  
88      /** The twitter. */
89      private static Twitter _twitter = null;
90  
91      /** The instance. */
92      private static ITwitterService _instance = null;
93  
94      /**
95       * Gets the service.
96       *
97       * @return the service
98       */
99      public static ITwitterService getService( )
100     {
101         if ( _instance == null )
102         {
103             _instance = SpringContextService.getBean( ConstanteUtils.BEAN_TWITTER_SERVICE );
104             _instance.init( );
105         }
106 
107         return _instance;
108     }
109 
110     /**
111      * Instantiates a new twitter service.
112      */
113     public TwitterService( )
114     {
115         // TODO Auto-generated constructor stub
116     }
117 
118     /**
119      * Send tweet.
120      *
121      * @param strTweet
122      *            the str tweet
123      * @param commandResult
124      *            the command result
125      */
126     public void sendTweet( String strTweet, CommandResult commandResult )
127     {
128 
129         try
130         {
131             if ( !StringUtils.isEmpty( strTweet ) )
132             {
133                 Status status = _twitter.updateStatus( strTweet );
134                 commandResult.getLog( ).append( "Le Tweet " + status.getText( ) + " a été envoyé depuis " + status.getUser( ).getScreenName( ) + "\n" );
135             }
136 
137         }
138         catch( TwitterException e )
139         {
140 
141             ReleaserUtils.addInfoError( commandResult, "Une erreur est surnvenue lors de l'envoi du tweet:" + strTweet, e );
142         }
143 
144     }
145 
146     /**
147      * Configure la lib tweeter.
148      */
149     public void init( )
150     {
151         Properties props = new Properties( );
152         props.put( TWITTER_KEY_OAUTH_CONSUMER_KEY, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_CONSUMER_KEY ) );
153         props.put( TWITTER_KEY_OAUTH_CONSUMER_SECRET, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_CONSUMER_SECRET ) );
154         props.put( TWITTER_KEY_OAUTH_ACCESS_TOKEN, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_ACCESS_TOKEN ) );
155         props.put( TWITTER_KEY_OAUTH_ACCESS_TOKEN_SECRET, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_ACCESS_TOKEN_SECRET ) );
156         props.put( TWITTER_KEY_OAUTH_REQUEST_TOKEN_URL, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_REQUEST_TOKEN_URL ) );
157         props.put( TWITTER_KEY_OAUTH_AUTHORIZATION_URL, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_AUTHORIZATION_URL ) );
158         props.put( TWITTER_KEY_OAUTH_ACCESS_TOKEN_URL, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_TWITTER_OAUTH_ACCESSTOKEN_URL ) );
159         props.put( TWITTER_KEY_HTTP_PROXY_HOST, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_PROXY_HOST ) );
160         props.put( TWITTER_KEY_HTTP_PROXY_PORT, AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_PROXY_PORT ) );
161 
162         Configuration conf = new PropertyConfiguration( props );
163         Authorization auth = AuthorizationFactory.getInstance( conf );
164 
165         _twitter = new TwitterFactory( conf ).getInstance( auth );
166 
167     }
168 
169 }