Files
Athou_commafeed/src/main/java/com/commafeed/backend/pubsubhubbub/SubscriptionHandler.java

97 lines
3.4 KiB
Java
Raw Normal View History

2013-05-20 14:06:09 +02:00
package com.commafeed.backend.pubsubhubbub;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.core.MediaType;
2013-08-11 11:45:32 +02:00
import lombok.extern.slf4j.Slf4j;
2013-05-23 06:55:17 +02:00
import org.apache.commons.lang.StringUtils;
2013-05-20 14:06:09 +02:00
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
2013-05-22 13:27:42 +02:00
import org.apache.http.util.EntityUtils;
2013-05-20 14:06:09 +02:00
import com.commafeed.backend.HttpGetter;
2013-06-07 15:56:24 +02:00
import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
import com.commafeed.backend.feeds.FeedUtils;
2013-05-20 14:06:09 +02:00
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.services.ApplicationSettingsService;
2013-07-26 16:00:02 +02:00
import com.commafeed.frontend.rest.resources.PubSubHubbubCallbackREST;
2013-05-20 14:06:09 +02:00
import com.google.common.collect.Lists;
2013-07-26 16:00:02 +02:00
/**
* Sends push subscription requests. Callback is handled by {@link PubSubHubbubCallbackREST}
*
*/
2013-08-11 11:45:32 +02:00
@Slf4j
2013-05-20 14:06:09 +02:00
public class SubscriptionHandler {
@Inject
ApplicationSettingsService applicationSettingsService;
2013-05-23 06:55:17 +02:00
@Inject
2013-06-07 15:56:24 +02:00
FeedRefreshTaskGiver taskGiver;
2013-05-23 06:55:17 +02:00
2013-05-20 14:06:09 +02:00
public void subscribe(Feed feed) {
2013-06-07 15:54:47 +02:00
try {
// make sure the feed has been updated in the database so that the
// callback works
2013-06-07 15:54:47 +02:00
Thread.sleep(30000);
} catch (InterruptedException e1) {
// do nothing
}
2013-06-05 21:50:26 +02:00
String hub = feed.getPushHub();
String topic = feed.getPushTopic();
2013-07-25 09:17:33 +02:00
String publicUrl = FeedUtils.removeTrailingSlash(applicationSettingsService.get().getPublicUrl());
2013-05-20 14:06:09 +02:00
2013-05-21 14:44:55 +02:00
log.debug("sending new pubsub subscription to {} for {}", hub, topic);
2013-05-20 14:06:09 +02:00
HttpPost post = new HttpPost(hub);
List<NameValuePair> nvp = Lists.newArrayList();
2013-07-25 09:17:33 +02:00
nvp.add(new BasicNameValuePair("hub.callback", publicUrl + "/rest/push/callback"));
2013-05-20 14:06:09 +02:00
nvp.add(new BasicNameValuePair("hub.topic", topic));
nvp.add(new BasicNameValuePair("hub.mode", "subscribe"));
nvp.add(new BasicNameValuePair("hub.verify", "async"));
nvp.add(new BasicNameValuePair("hub.secret", ""));
nvp.add(new BasicNameValuePair("hub.verify_token", ""));
nvp.add(new BasicNameValuePair("hub.lease_seconds", ""));
post.setHeader(HttpHeaders.USER_AGENT, "CommaFeed");
2013-07-25 09:17:33 +02:00
post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
2013-05-20 14:06:09 +02:00
HttpClient client = HttpGetter.newClient(20000);
2013-05-20 14:06:09 +02:00
try {
post.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
2013-05-21 15:03:40 +02:00
if (code != 204 && code != 202 && code != 200) {
2013-05-23 06:55:17 +02:00
String message = EntityUtils.toString(response.getEntity());
String pushpressError = " is value is not allowed. You may only subscribe to";
2013-07-25 09:17:33 +02:00
if (code == 400 && StringUtils.contains(message, pushpressError)) {
2013-05-23 06:55:17 +02:00
String[] tokens = message.split(" ");
2013-06-05 21:50:26 +02:00
feed.setPushTopic(tokens[tokens.length - 1]);
2013-06-07 15:56:24 +02:00
taskGiver.giveBack(feed);
2013-07-25 09:17:33 +02:00
log.debug("handled pushpress subfeed {} : {}", topic, feed.getPushTopic());
2013-05-23 06:55:17 +02:00
} else {
2013-07-25 09:17:33 +02:00
throw new Exception("Unexpected response code: " + code + " " + response.getStatusLine().getReasonPhrase() + " - "
+ message);
2013-05-23 06:55:17 +02:00
}
2013-05-20 14:06:09 +02:00
}
2013-05-21 14:44:55 +02:00
log.debug("subscribed to {} for {}", hub, topic);
2013-05-20 14:06:09 +02:00
} catch (Exception e) {
2013-07-25 09:17:33 +02:00
log.error("Could not subscribe to {} for {} : " + e.getMessage(), hub, topic);
2013-05-20 14:06:09 +02:00
} finally {
client.getConnectionManager().shutdown();
}
}
}