-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
TwitterFeed.java
166 lines (140 loc) · 6.57 KB
/
TwitterFeed.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright 2008-2022 Async-IO.org
*
* 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.atmosphere.samples.twitter;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import com.ning.http.util.Base64;
import com.sun.jersey.spi.resource.Singleton;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.jersey.SuspendResponse;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletConfig;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@Path("/search/{tagid}")
@Produces("text/html;charset=ISO-8859-1")
@Singleton
public class TwitterFeed {
private static final Logger logger = LoggerFactory.getLogger(TwitterFeed.class);
private final AsyncHttpClient asyncClient = new AsyncHttpClient();
private final ConcurrentHashMap<String, Future<?>> futures = new ConcurrentHashMap<String, Future<?>>();
private final CountDownLatch suspendLatch = new CountDownLatch(1);
@Context
private ServletConfig sc;
private boolean initialized;
private String authorizationValue;
private Exception authorizationException;
@GET
public SuspendResponse<String> search(final @PathParam("tagid") Broadcaster feed,
final @PathParam("tagid") String tagid) {
if (tagid.isEmpty()) {
throw new WebApplicationException();
}
if (!initialized) {
initialized = true;
String key = sc.getInitParameter("com.twitter.consumer.key");
String secret = sc.getInitParameter("com.twitter.consumer.secret");
try {
Future<Response> f = asyncClient.preparePost("https://api.twitter.com/oauth2/token")
.setHeader("Authorization", "Basic " + Base64.encode((key + ":" + secret).getBytes()))
.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8").setBody("grant_type=client_credentials").execute();
JSONObject jtoken = new JSONObject(f.get().getResponseBody());
authorizationValue = "Bearer " + jtoken.getString("access_token");
} catch (Exception e) {
logger.error("Unable to obtain a valid bearer token", e);
authorizationException = e;
}
}
// we avoid repeatedly invoking the above authorization tkoen retrieval call
if (authorizationValue == null) {
throw new WebApplicationException(authorizationException);
}
if (feed.getAtmosphereResources().size() == 0) {
final Future<?> future = feed.scheduleFixedBroadcast(new Callable<String>() {
private final AtomicReference<String> refreshUrl = new AtomicReference<String>("");
public String call() throws Exception {
String query = null;
if (!refreshUrl.get().isEmpty()) {
query = refreshUrl.get();
} else {
query = "?q=" + tagid;
}
// Wait for the connection to be suspended.
suspendLatch.await();
asyncClient.prepareGet("https://api.twitter.com/1.1/search/tweets.json" + query)
.setHeader("Authorization", authorizationValue)
.execute(
new AsyncCompletionHandler<Object>() {
@Override
public Object onCompleted(Response response) throws Exception {
String s = response.getResponseBody();
if (response.getStatusCode() != 200) {
feed.resumeAll();
feed.destroy();
logger.info("Twitter Search API unavaileble\n{}", s);
return null;
}
JSONObject json = new JSONObject(s);
JSONObject searchMetadata = json.getJSONObject("search_metadata");
refreshUrl.set(searchMetadata.getString("refresh_url"));
if (json.getJSONArray("statuses").length() > 0) {
feed.broadcast(s).get();
}
return null;
}
});
return null;
}
}, 5, TimeUnit.SECONDS);
futures.put(tagid, future);
}
return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(feed).outputComments(true)
.addListener(new EventsLogger() {
@Override
public void onSuspend(
final AtmosphereResourceEvent event) {
super.onSuspend(event);
// OK, we can start polling Twitter!
suspendLatch.countDown();
}
}).build();
}
@GET
@Path("/stop")
public String stopSearch(final @PathParam("tagid") Broadcaster feed,
final @PathParam("tagid") String tagid) {
feed.resumeAll();
if (futures.get(tagid) != null) {
futures.get(tagid).cancel(true);
}
logger.info("Stopping real time update for {}", tagid);
return "DONE";
}
}