Java Code Examples for com.twitter.hbc.httpclient.auth.Authentication

The following examples show how to use com.twitter.hbc.httpclient.auth.Authentication. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: hazelcast-jet-contrib   Author: hazelcast   File: TwitterSources.java    License: Apache License 2.0 6 votes vote down vote up
private TwitterStreamSourceContext(
        @Nonnull ILogger logger,
        @Nonnull Properties credentials,
        @Nonnull String host,
        @Nonnull SupplierEx<? extends StreamingEndpoint> endpointSupplier
) {
    this.logger = logger;
    checkTwitterCredentials(credentials);
    String consumerKey = credentials.getProperty("consumerKey");
    String consumerSecret = credentials.getProperty("consumerSecret");
    String token = credentials.getProperty("token");
    String tokenSecret = credentials.getProperty("tokenSecret");

    Authentication auth = new OAuth1(consumerKey, consumerSecret, token, tokenSecret);
    client = new ClientBuilder()
            .hosts(host)
            .endpoint(endpointSupplier.get())
            .authentication(auth)
            .processor(new StringDelimitedProcessor(queue))
            .build();
    client.connect();
}
 
Example #2
Source Project: kafka-streams   Author: bbejeck   File: TwitterDataSource.java    License: Apache License 2.0 6 votes vote down vote up
private  Client getTwitterClient(Properties props, BlockingQueue<String> messageQueue) {

        String clientName = props.getProperty("clientName");
        String consumerKey = props.getProperty("consumerKey");
        String consumerSecret = props.getProperty("consumerSecret");
        String token = props.getProperty("token");
        String tokenSecret = props.getProperty("tokenSecret");
        List<String> searchTerms = Arrays.asList(props.getProperty("searchTerms").split(","));

        Authentication authentication = new OAuth1(consumerKey,consumerSecret,token,tokenSecret);
        Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
        StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();

        hosebirdEndpoint.trackTerms(searchTerms);

        ClientBuilder clientBuilder = new ClientBuilder();
        clientBuilder.name(clientName)
                .hosts(hosebirdHosts)
                .authentication(authentication)
                .endpoint(hosebirdEndpoint)
                .processor(new StringDelimitedProcessor(messageQueue));

          return clientBuilder.build();

    }
 
Example #3
Source Project: hbc   Author: twitter   File: BasicClient.java    License: Apache License 2.0 6 votes vote down vote up
public BasicClient(String name, Hosts hosts, StreamingEndpoint endpoint, Authentication auth, boolean enableGZip, HosebirdMessageProcessor processor,
                   ReconnectionManager reconnectionManager, RateTracker rateTracker, ExecutorService executorService,
                   @Nullable BlockingQueue<Event> eventsQueue, HttpParams params, SchemeRegistry schemeRegistry) {
  Preconditions.checkNotNull(auth);
  HttpClient client;
  if (enableGZip) {
    client = new RestartableHttpClient(auth, enableGZip, params, schemeRegistry);
  } else {
    DefaultHttpClient defaultClient = new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry), params);

    /** Set auth **/
    auth.setupConnection(defaultClient);
    client = defaultClient;
  }

  this.canRun = new AtomicBoolean(true);
  this.executorService = executorService;
  this.clientBase = new ClientBase(name, client, hosts, endpoint, auth, processor, reconnectionManager, rateTracker, eventsQueue);
}
 
Example #4
Source Project: hbc   Author: twitter   File: ClientBase.java    License: Apache License 2.0 6 votes vote down vote up
ClientBase(String name, HttpClient client, Hosts hosts, StreamingEndpoint endpoint, Authentication auth,
           HosebirdMessageProcessor processor, ReconnectionManager manager, RateTracker rateTracker,
           @Nullable BlockingQueue<Event> eventsQueue) {
  this.client = Preconditions.checkNotNull(client);
  this.name = Preconditions.checkNotNull(name);

  this.endpoint = Preconditions.checkNotNull(endpoint);
  this.hosts = Preconditions.checkNotNull(hosts);
  this.auth = Preconditions.checkNotNull(auth);

  this.processor = Preconditions.checkNotNull(processor);
  this.reconnectionManager = Preconditions.checkNotNull(manager);
  this.rateTracker = Preconditions.checkNotNull(rateTracker);

  this.eventsQueue = eventsQueue;

  this.exitEvent = new AtomicReference<Event>();

  this.isRunning = new CountDownLatch(1);
  this.statsReporter = new StatsReporter();

  this.connectionEstablished = new AtomicBoolean(false);
  this.reconnect = new AtomicBoolean(false);
}
 
Example #5
Source Project: hbc   Author: twitter   File: HttpConstants.java    License: Apache License 2.0 6 votes vote down vote up
public static HttpUriRequest constructRequest(String host, Endpoint endpoint, Authentication auth) {
  String url = host + endpoint.getURI();
  if (endpoint.getHttpMethod().equalsIgnoreCase(HttpGet.METHOD_NAME)) {
    HttpGet get = new HttpGet(url);
    if (auth != null)
      auth.signRequest(get, null);
    return get;
  } else if (endpoint.getHttpMethod().equalsIgnoreCase(HttpPost.METHOD_NAME) ) {
    HttpPost post = new HttpPost(url);

    post.setEntity(new StringEntity(endpoint.getPostParamString(), Constants.DEFAULT_CHARSET));
    post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    if (auth != null)
      auth.signRequest(post, endpoint.getPostParamString());

    return post;
  } else {
    throw new IllegalArgumentException("Bad http method: " + endpoint.getHttpMethod());
  }
}
 
Example #6
Source Project: AIBlueprints   Author: PacktPublishing   File: TwitterStream.java    License: MIT License 5 votes vote down vote up
public TwitterStream(SentimentDetector sentimentDetector,
                     Gson gson,
                     Properties props) {
    this.sentimentDetector = sentimentDetector;
    this.gson = gson;

    msgQueue = new LinkedBlockingQueue<String>(100000);

    Hosts hosts = new HttpHosts(Constants.STREAM_HOST);
    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

    List<String> terms = Lists.newArrayList(
            props.getProperty("twitter_terms")
                    .split("\\s*,\\s*"));
    endpoint.trackTerms(terms);

    Authentication auth = new OAuth1(
            props.getProperty("twitter_consumer_key"),
            props.getProperty("twitter_consumer_secret"),
            props.getProperty("twitter_token"),
            props.getProperty("twitter_token_secret"));
    ClientBuilder builder = new ClientBuilder()
            .name("SmartCode-Client-01")
            .hosts(hosts)
            .authentication(auth)
            .endpoint(endpoint)
            .processor(new StringDelimitedProcessor(msgQueue));
    client = builder.build();
    client.connect();
}
 
Example #7
Source Project: AIBlueprints   Author: PacktPublishing   File: TwitterStream.java    License: MIT License 5 votes vote down vote up
public TwitterStream(Gson gson, Properties props, BlockingQueue<Map<String,Object>> imageQueue) {
    this.gson = gson;
    this.imageQueue = imageQueue;

    msgQueue = new LinkedBlockingQueue<String>(100000);

    Hosts hosts = new HttpHosts(Constants.STREAM_HOST);
    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

    List<String> terms = Lists.newArrayList(
            props.getProperty("twitter_terms")
                    .split("\\s*,\\s*"));
    endpoint.trackTerms(terms);

    Authentication auth = new OAuth1(
            props.getProperty("twitter_consumer_key"),
            props.getProperty("twitter_consumer_secret"),
            props.getProperty("twitter_token"),
            props.getProperty("twitter_token_secret"));
    ClientBuilder builder = new ClientBuilder()
            .name("SmartCode-Client-01")
            .hosts(hosts)
            .authentication(auth)
            .endpoint(endpoint)
            .processor(new StringDelimitedProcessor(msgQueue));
    client = builder.build();
    client.connect();
}
 
Example #8
Source Project: Java-for-Data-Science   Author: PacktPublishing   File: SampleStreamExample.java    License: MIT License 5 votes vote down vote up
public static void streamTwitter(String consumerKey, String consumerSecret, String accessToken, String accessSecret) throws InterruptedException {

		BlockingQueue<String> statusQueue = new LinkedBlockingQueue<String>(10000);

		StatusesSampleEndpoint ending = new StatusesSampleEndpoint();
		ending.stallWarnings(false);

		Authentication twitterAuth = new OAuth1(consumerKey, consumerSecret, accessToken, accessSecret);

		BasicClient twitterClient = new ClientBuilder()
				.name("Twitter client")
				.hosts(Constants.STREAM_HOST)
				.endpoint(ending)
				.authentication(twitterAuth)
				.processor(new StringDelimitedProcessor(statusQueue))
				.build();


		twitterClient.connect();


		for (int msgRead = 0; msgRead < 1000; msgRead++) {
			if (twitterClient.isDone()) {
				System.out.println(twitterClient.getExitEvent().getMessage());
				break;
			}

			String msg = statusQueue.poll(10, TimeUnit.SECONDS);
			if (msg == null) {
				System.out.println("Waited 10 seconds - no message received");
			} else {
				System.out.println(msg);
			}
		}

		twitterClient.stop();

		System.out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());
	}
 
Example #9
Source Project: hbc   Author: twitter   File: RestartableHttpClient.java    License: Apache License 2.0 5 votes vote down vote up
public RestartableHttpClient(Authentication auth, boolean enableGZip, HttpParams params, SchemeRegistry schemeRegistry) {
  this.auth = Preconditions.checkNotNull(auth);
  this.enableGZip = enableGZip;
  this.params = Preconditions.checkNotNull(params);
  this.schemeRegistry = Preconditions.checkNotNull(schemeRegistry);

  this.underlying = new AtomicReference<HttpClient>();
}
 
Example #10
Source Project: hbc   Author: twitter   File: ClientBaseTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  mock = mock(HttpClient.class);
  mockResponse = mock(HttpResponse.class);
  mockStatusLine = mock(StatusLine.class);

  mockConnection = mock(Connection.class);
  mockReconnectionManager = mock(BasicReconnectionManager.class);
  mockRateTracker = mock(RateTracker.class);

  mockInputStream = mock(InputStream.class);
  mockAuth = mock(Authentication.class);

  mockProcessor = mock(HosebirdMessageProcessor.class);

  HttpEntity mockHttpEntity = mock(HttpEntity.class);

  // set up required mocks to mock out all of the clientbase stuff
  when(mock.execute(any(HttpUriRequest.class)))
          .thenReturn(mockResponse);
  when(mockResponse.getStatusLine())
          .thenReturn(mockStatusLine);
  when(mockResponse.getEntity())
          .thenReturn(mockHttpEntity);
  when(mockHttpEntity.getContent())
          .thenReturn(mockInputStream);
}
 
Example #11
Source Project: hbc   Author: twitter   File: BasicClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  mockClient = mock(HttpClient.class);
  mockResponse = mock(HttpResponse.class);
  mockStatusLine = mock(StatusLine.class);

  mockReconnectionManager = mock(BasicReconnectionManager.class);
  mockConnectionManager = mock(ClientConnectionManager.class);
  mockRateTracker = mock(RateTracker.class);

  mockInputStream = mock(InputStream.class);
  mockAuth = mock(Authentication.class);

  mockProcessor = mock(HosebirdMessageProcessor.class);

  mockHttpEntity = mock(HttpEntity.class);

  // set up required mocks to mock out all of the clientbase stuff
  when(mockClient.execute(any(HttpUriRequest.class)))
          .thenReturn(mockResponse);
  when(mockClient.getConnectionManager())
          .thenReturn(mockConnectionManager);

  when(mockResponse.getStatusLine())
          .thenReturn(mockStatusLine);
  when(mockResponse.getEntity())
          .thenReturn(mockHttpEntity);
  when(mockHttpEntity.getContent())
          .thenReturn(mockInputStream);
  when(mockStatusLine.getReasonPhrase())
          .thenReturn("reason");
}
 
Example #12
Source Project: hbc   Author: twitter   File: RestartableHttpClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  mockAuth = mock(Authentication.class);
  mockParams = mock(HttpParams.class);
  defaultSchemeRegistry = SchemeRegistryFactory.createDefault();
  request = new HttpGet("http://hi");
}
 
Example #13
Source Project: hbc   Author: twitter   File: SitestreamControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  client = mock(HttpClient.class);
  hosts = new HttpHosts("https://host.com");
  auth = mock(Authentication.class);
  request = mock(HttpUriRequest.class);
  mockResponse = mock(HttpResponse.class);
  mockStatusLine = mock(StatusLine.class);
  mockEntity = mock(HttpEntity.class);
}
 
Example #14
Source Project: hbc   Author: twitter   File: Twitter4jSampleStreamExample.java    License: Apache License 2.0 5 votes vote down vote up
public void oauth(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
  // Create an appropriately sized blocking queue
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  // Define our endpoint: By default, delimited=length is set (we need this for our processor)
  // and stall warnings are on.
  StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  // Authentication auth = new BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
    .hosts(Constants.STREAM_HOST)
    .endpoint(endpoint)
    .authentication(auth)
    .processor(new StringDelimitedProcessor(queue))
    .build();

  // Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
  // calling the listeners on each message
  int numProcessingThreads = 4;
  ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);

  // Wrap our BasicClient with the twitter4j client
  Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(
    client, queue, Lists.newArrayList(listener1, listener2), service);

  // Establish a connection
  t4jClient.connect();
  for (int threads = 0; threads < numProcessingThreads; threads++) {
    // This must be called once per processing thread
    t4jClient.process();
  }

  Thread.sleep(5000);

  client.stop();
}
 
Example #15
Source Project: hbc   Author: twitter   File: FilterStreamExample.java    License: Apache License 2.0 5 votes vote down vote up
public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
  StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
  // add some track terms
  endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  // Authentication auth = new BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  Client client = new ClientBuilder()
          .hosts(Constants.STREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Establish a connection
  client.connect();

  // Do whatever needs to be done with messages
  for (int msgRead = 0; msgRead < 1000; msgRead++) {
    String msg = queue.take();
    System.out.println(msg);
  }

  client.stop();

}
 
Example #16
Source Project: spring4ws-demos   Author: ralscha   File: TwitterHandler.java    License: Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
	BlockingQueue<String> queue = new LinkedBlockingQueue<>(100);
	StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

	endpoint.trackTerms(ImmutableList.of("ExtJS", "#extjs", "Sencha", "#java",
			"java8", "java9", "#websocket", "#SpringFramework", "html5", "javascript",
			"#kotlin", "kotlin"));
	endpoint.languages(ImmutableList.of("en", "de"));

	String consumerKey = this.environment.getProperty("twitter4j.oauth.consumerKey");
	String consumerSecret = this.environment
			.getProperty("twitter4j.oauth.consumerSecret");
	String accessToken = this.environment.getProperty("twitter4j.oauth.accessToken");
	String accessTokenSecret = this.environment
			.getProperty("twitter4j.oauth.accessTokenSecret");

	Authentication auth = new OAuth1(consumerKey, consumerSecret, accessToken,
			accessTokenSecret);

	this.client = new ClientBuilder().hosts(Constants.STREAM_HOST).endpoint(endpoint)
			.authentication(auth).processor(new StringDelimitedProcessor(queue))
			.build();

	this.executorService = Executors.newSingleThreadExecutor();

	TwitterStatusListener statusListener = new TwitterStatusListener(
			this.messagingTemplate, this.lastTweets);
	this.t4jClient = new Twitter4jStatusClient(this.client, queue,
			ImmutableList.of(statusListener), this.executorService);

	this.t4jClient.connect();
	this.t4jClient.process();
}
 
Example #17
Source Project: Java-for-Data-Science   Author: PacktPublishing   File: TwitterStream.java    License: MIT License 4 votes vote down vote up
public Stream<TweetHandler> stream() {
        String myKey = "sl2WbCf4UnIr08xvHVitHJ99r";
        String mySecret = "PE6yauvXjKLuvoQNXZAJo5C8N5U5piSFb3udwkoI76paK6KyqI";
        String myToken = "1098376471-p6iWfxCLtyMvMutTb010w1D1xZ3UyJhcC2kkBjN";
        String myAccess = "2o1uGcp4b2bFynOfu2cA1uz63n5aruV0RwNsUjRpjDBZS";

        out.println("Creating Twitter Stream");
        BlockingQueue<String> statusQueue = new LinkedBlockingQueue<>(1000);
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
        endpoint.trackTerms(Lists.newArrayList("twitterapi", this.topic));
        endpoint.stallWarnings(false);
        Authentication twitterAuth = new OAuth1(myKey, mySecret, myToken, myAccess);

        BasicClient twitterClient = new ClientBuilder()
                .name("Twitter client")
                .hosts(Constants.STREAM_HOST)
                .endpoint(endpoint)
                .authentication(twitterAuth)
                .processor(new StringDelimitedProcessor(statusQueue))
                .build();

        twitterClient.connect();

        List<TweetHandler> list = new ArrayList();
        List<String> twitterList = new ArrayList();

        statusQueue.drainTo(twitterList);
        for(int i=0; i<numberOfTweets; i++) {
            String message;
            try {
                message = statusQueue.take();
                list.add(new TweetHandler(message));
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

//        for (int msgRead = 0; msgRead < this.numberOfTweets; msgRead++) {
//            try {
//                if (twitterClient.isDone()) {
//                  //  out.println(twitterClient.getExitEvent().getMessage());
//                    break;
//                }
//
//                String msg = statusQueue.poll(10, TimeUnit.SECONDS);
//                if (msg == null) {
//                    out.println("Waited 10 seconds - no message received");
//                } else {
//                    list.add(new TweetHandler(msg));
//                    out.println("Added message: " + msg.length());
//                }
//            } catch (InterruptedException ex) {
//                ex.printStackTrace();
//            }
//        }
        twitterClient.stop();
        out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());

        return list.stream();
    }
 
Example #18
Source Project: pulsar   Author: apache   File: TwitterFireHose.java    License: Apache License 2.0 4 votes vote down vote up
private Authentication getAuthentication(TwitterFireHoseConfig config) {
    return new OAuth1(config.getConsumerKey(),
            config.getConsumerSecret(),
            config.getToken(),
            config.getTokenSecret());
}
 
Example #19
Source Project: aws-big-data-blog   Author: amazon-archives   File: TwitterProducer.java    License: Apache License 2.0 4 votes vote down vote up
public void run(String propFilePath) {
	loadFileProperties(propFilePath, DEFAULT_PROP_FILE_NAME);

	String consumerKey = System.getProperty(TWIT_CONSUMER_KEY);
	String consumerSecret = System.getProperty(TWIT_CONSUMER_SECRET);
	String token = System.getProperty(TWIT_TOKEN);
	String secret = System.getProperty(TWIT_SECRET);
	String streamName = System.getProperty(STREAM_NAME);
	String regionName = System.getProperty(REGION_NAME);
	
	while (true) {
		/**
		 * Set up your blocking queues: Be sure to size these properly based
		 * on expected TPS of your stream
		 */
		BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(
				10000);

		/**
		 * Declare the host you want to connect to, the endpoint, and
		 * authentication (basic auth or oauth)
		 */
		StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
		
		// Track  anything that is geo-tagged
		endpoint.addQueryParameter("locations", "-180,-90,180,90");

		// These secrets should be read from a config file
		Authentication hosebirdAuth = new OAuth1(consumerKey,
				consumerSecret, token, secret);

		// create a new basic client - by default gzip is enabled
		Client client = new ClientBuilder().hosts(Constants.STREAM_HOST)
				.endpoint(endpoint).authentication(hosebirdAuth)
				.processor(new StringDelimitedProcessor(msgQueue)).build();

		client.connect();
		

		LOG.info("Got connection to Twitter");
		
		// create producer
		ProducerClient producer = new ProducerBuilder()
				.withName("Twitter")
				.withStreamName(streamName)
				.withRegion(regionName)
				.withThreads(10)
				.build();
		
		producer.connect();
		
		LOG.info("Got connection to Kinesis");

		try {
			if (process(msgQueue, producer)) {
				break;
			}

		} catch (Exception e) {
			// if we get here, our client has broken, throw away and
			// recreate
			e.printStackTrace();
		}
		// also, if we make it here, we have had a problem, so start again
		client.stop();
	}
}
 
Example #20
Source Project: hbc   Author: twitter   File: ClientBase.java    License: Apache License 2.0 4 votes vote down vote up
ClientBase(String name, HttpClient client, Hosts hosts, StreamingEndpoint endpoint, Authentication auth,
           HosebirdMessageProcessor processor, ReconnectionManager manager, RateTracker rateTracker) {
  this(name, client, hosts, endpoint, auth, processor, manager, rateTracker, null);
}
 
Example #21
Source Project: hbc   Author: twitter   File: SitestreamController.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Construct a sitestream controller.
 */
public SitestreamController(HttpClient client, Hosts hosts, Authentication auth) {
  this.client = Preconditions.checkNotNull(client);
  this.hosts = Preconditions.checkNotNull(hosts);
  this.auth = Preconditions.checkNotNull(auth);
}
 
Example #22
Source Project: hbc   Author: twitter   File: SitestreamController.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Construct a sitestream controller using the default host (sitestream.twitter.com)
 */
public SitestreamController(HttpClient client, Authentication auth) {
  this.client = Preconditions.checkNotNull(client);
  this.hosts = new HttpHosts(Constants.SITESTREAM_HOST);
  this.auth = Preconditions.checkNotNull(auth);
}
 
Example #23
Source Project: hbc   Author: twitter   File: SitestreamController.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Construct a sitestream controller using a DefaultHttpClient
 */
public SitestreamController(Hosts hosts, Authentication auth) {
  this.client = new DefaultHttpClient(new PoolingClientConnectionManager());
  this.hosts = Preconditions.checkNotNull(hosts);
  this.auth = Preconditions.checkNotNull(auth);
}
 
Example #24
Source Project: hbc   Author: twitter   File: SitestreamController.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Construct a sitestream controller using the default host (sitestream.twitter.com) and a
 * DefaultHttpClient.
 */
public SitestreamController(Authentication auth) {
  this.client = new DefaultHttpClient(new PoolingClientConnectionManager());
  this.hosts = new HttpHosts(Constants.SITESTREAM_HOST);
  this.auth = Preconditions.checkNotNull(auth);
}
 
Example #25
Source Project: hbc   Author: twitter   File: ClientBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public ClientBuilder authentication(Authentication auth) {
  this.auth = auth;
  return this;
}
 
Example #26
Source Project: hbc   Author: twitter   File: SampleStreamExample.java    License: Apache License 2.0 4 votes vote down vote up
public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
  // Create an appropriately sized blocking queue
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  // Define our endpoint: By default, delimited=length is set (we need this for our processor)
  // and stall warnings are on.
  StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
  endpoint.stallWarnings(false);

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  //Authentication auth = new com.twitter.hbc.httpclient.auth.BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
          .name("sampleExampleClient")
          .hosts(Constants.STREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Establish a connection
  client.connect();

  // Do whatever needs to be done with messages
  for (int msgRead = 0; msgRead < 1000; msgRead++) {
    if (client.isDone()) {
      System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
      break;
    }

    String msg = queue.poll(5, TimeUnit.SECONDS);
    if (msg == null) {
      System.out.println("Did not receive a message in 5 seconds");
    } else {
      System.out.println(msg);
    }
  }

  client.stop();

  // Print some stats
  System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
}
 
Example #27
Source Project: hbc   Author: twitter   File: SitestreamExample.java    License: Apache License 2.0 4 votes vote down vote up
public void run(String consumerKey, String consumerSecret, String token, String tokenSecret)
        throws InterruptedException, ControlStreamException, IOException {
  // Create an appropriately sized blocking queue
  BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);

  // Define our endpoint: By default, delimited=length is set (we need this for our processor)
  // and stall warnings are on.
  List<Long> followings = new ArrayList<Long>();
  followings.add(111111111L);
  followings.add(222222222L);

  SitestreamEndpoint endpoint = new SitestreamEndpoint(followings);
  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, tokenSecret);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
          .hosts(Constants.SITESTREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
  // calling the listeners on each message
  int numProcessingThreads = 4;
  ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);

  // Wrap our BasicClient with the twitter4j client
  Twitter4jSitestreamClient t4jClient = new Twitter4jSitestreamClient(
          client, queue, Lists.newArrayList(listener), service);

  // Establish a connection
  t4jClient.connect();
  for (int threads = 0; threads < numProcessingThreads; threads++) {
    // This must be called once per processing thread
    t4jClient.process();
  }

  Thread.sleep(5000);

  // Create a sitestream controller to issue controlstream requests
  SitestreamController controller = new SitestreamController(auth);

  controller.getFriends(t4jClient.getStreamId(), 12345L);
  controller.addUser(t4jClient.getStreamId(), 987765L);

  client.stop();
}
 
Example #28
Source Project: kafka-streams   Author: bbejeck   File: HoseBirdTester.java    License: Apache License 2.0 3 votes vote down vote up
public static void main(String[] args)  throws InterruptedException {

        BlockingQueue<String> msgQueue = new LinkedBlockingDeque<>();



        Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
        StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
        List<String> terms = Lists.newArrayList("superman vs batman","#supermanvsbatman");
        hosebirdEndpoint.trackTerms(terms);




        Authentication hosebirdAuth  = new OAuth1("18qydWMuiUohwCtQpp1MOFCFr",
                                                  "YrYhYd09LKZLbhsKT1o4XcEPl6HiAoNykiOxYBq0dAB8t0vRCo",
                                                  "16972669-KSvyDEMc7dussPfW6a9Ru65L4eWGj637ciHLHZLyn",
                                                  "ky53NE6cbBvtNLopto7o9gVyHDejSB2kPsRhHGKEd1MrS");


        ClientBuilder clientBuilder = new ClientBuilder();
        clientBuilder.name("bbejeck-hosebird")
                     .hosts(hosebirdHosts)
                     .authentication(hosebirdAuth)
                     .endpoint(hosebirdEndpoint)
                     .processor(new StringDelimitedProcessor(msgQueue));

        Client hosebirdClient = clientBuilder.build();
        hosebirdClient.connect();

        for (int msgRead = 0; msgRead < 100; msgRead++) {
            String msg = msgQueue.take();
            System.out.println(msg);
        }

        hosebirdClient.stop();

    }