use of org.b3log.latke.urlfetch.HTTPRequest in project solo by b3log.
the class BlogProcessor method syncUser.
/**
* Sync user to https://hacpai.com.
*
* @param context the specified context
* @throws Exception exception
*/
@RequestProcessing(value = "/blog/symphony/user", method = HTTPRequestMethod.GET)
public void syncUser(final HTTPRequestContext context) throws Exception {
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject jsonObject = new JSONObject();
renderer.setJSONObject(jsonObject);
if (Latkes.getServePath().contains("localhost")) {
return;
}
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
// not init yet
return;
}
final HTTPRequest httpRequest = new HTTPRequest();
httpRequest.setURL(new URL(SoloServletListener.B3LOG_SYMPHONY_SERVE_PATH + "/apis/user"));
httpRequest.setRequestMethod(HTTPRequestMethod.POST);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject admin = userQueryService.getAdmin();
requestJSONObject.put(User.USER_NAME, admin.getString(User.USER_NAME));
requestJSONObject.put(User.USER_EMAIL, admin.getString(User.USER_EMAIL));
requestJSONObject.put(User.USER_PASSWORD, admin.getString(User.USER_PASSWORD));
requestJSONObject.put("userB3Key", preference.optString(Option.ID_C_KEY_OF_SOLO));
requestJSONObject.put("clientHost", Latkes.getServePath());
httpRequest.setPayload(requestJSONObject.toString().getBytes("UTF-8"));
urlFetchService.fetchAsync(httpRequest);
}
use of org.b3log.latke.urlfetch.HTTPRequest in project solo by b3log.
the class AddArticleGoogleBlogSearchPinger method action.
@Override
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject eventData = event.getData();
String articleTitle = null;
final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
try {
final JSONObject article = eventData.getJSONObject(Article.ARTICLE);
articleTitle = article.getString(Article.ARTICLE_TITLE);
final JSONObject preference = preferenceQueryService.getPreference();
final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
if (Latkes.getServePath().contains("localhost")) {
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not ping " + "Google Blog Search Service for the article[title={0}]", new Object[] { article.getString(Article.ARTICLE_TITLE) });
return;
}
final String articlePermalink = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
final String spec = "http://blogsearch.google.com/ping?name=" + URLEncoder.encode(blogTitle, "UTF-8") + "&url=" + URLEncoder.encode(Latkes.getServePath(), "UTF-8") + "&changesURL=" + URLEncoder.encode(articlePermalink, "UTF-8");
LOGGER.log(Level.DEBUG, "Request Google Blog Search Service API[{0}] while adding an " + "article[title=" + articleTitle + "]", spec);
final HTTPRequest request = new HTTPRequest();
request.setURL(new URL(spec));
URL_FETCH_SERVICE.fetchAsync(request);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Ping Google Blog Search Service fail while adding an article[title=" + articleTitle + "]", e);
}
}
use of org.b3log.latke.urlfetch.HTTPRequest in project solo by b3log.
the class ArticleSender method action.
@Override
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject data = event.getData();
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", new Object[] { event.getType(), data, ArticleSender.class.getName() });
try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
throw new EventException("Not found preference");
}
if (!Strings.isEmptyOrNull(originalArticle.optString(Article.ARTICLE_VIEW_PWD))) {
return;
}
if (Latkes.getServePath().contains("localhost")) {
LOGGER.log(Level.INFO, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm", new Object[] { originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE) });
return;
}
final HTTPRequest httpRequest = new HTTPRequest();
httpRequest.setURL(ADD_ARTICLE_URL);
httpRequest.setRequestMethod(HTTPRequestMethod.POST);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject article = new JSONObject();
article.put(Keys.OBJECT_ID, originalArticle.getString(Keys.OBJECT_ID));
article.put(Article.ARTICLE_TITLE, originalArticle.getString(Article.ARTICLE_TITLE));
article.put(Article.ARTICLE_PERMALINK, originalArticle.getString(Article.ARTICLE_PERMALINK));
article.put(Article.ARTICLE_TAGS_REF, originalArticle.getString(Article.ARTICLE_TAGS_REF));
article.put(Article.ARTICLE_AUTHOR_EMAIL, originalArticle.getString(Article.ARTICLE_AUTHOR_EMAIL));
article.put(Article.ARTICLE_CONTENT, originalArticle.getString(Article.ARTICLE_CONTENT));
article.put(Article.ARTICLE_CREATE_DATE, ((Date) originalArticle.get(Article.ARTICLE_CREATE_DATE)).getTime());
article.put(Common.POST_TO_COMMUNITY, originalArticle.getBoolean(Common.POST_TO_COMMUNITY));
// Removes this property avoid to persist
originalArticle.remove(Common.POST_TO_COMMUNITY);
requestJSONObject.put(Article.ARTICLE, article);
requestJSONObject.put(Common.BLOG_VERSION, SoloServletListener.VERSION);
requestJSONObject.put(Common.BLOG, "B3log Solo");
requestJSONObject.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
requestJSONObject.put("blogHost", Latkes.getServePath());
requestJSONObject.put("userB3Key", preference.optString(Option.ID_C_KEY_OF_SOLO));
requestJSONObject.put("clientAdminEmail", preference.optString(Option.ID_C_ADMIN_EMAIL));
requestJSONObject.put("clientRuntimeEnv", Latkes.getRuntimeEnv().name());
httpRequest.setPayload(requestJSONObject.toString().getBytes("UTF-8"));
urlFetchService.fetchAsync(httpRequest);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Sends an article to Rhythm error: {0}", e.getMessage());
}
LOGGER.log(Level.DEBUG, "Sent an article to Rhythm");
}
use of org.b3log.latke.urlfetch.HTTPRequest in project latke by b3log.
the class Cron method run.
@Override
public void run() {
LOGGER.debug("Executing scheduled task....");
final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
final HTTPRequest request = new HTTPRequest();
try {
request.setURL(new URL(url));
request.setRequestMethod(HTTPRequestMethod.GET);
urlFetchService.fetchAsync(request);
LOGGER.log(Level.DEBUG, "Executed scheduled task[url={0}]", url);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Scheduled task execute failed", e);
}
}
use of org.b3log.latke.urlfetch.HTTPRequest in project latke by b3log.
the class LocalTaskRunner method run.
@Override
public void run() {
urlFetchService = URLFetchServiceFactory.getURLFetchService();
final HTTPRequest httpRequest = new HTTPRequest();
try {
httpRequest.setURL(new URL(Latkes.getServer() + Latkes.getContextPath() + task.getURL()));
httpRequest.setRequestMethod(task.getRequestMethod());
httpRequest.setPayload(task.getPayload());
} catch (final MalformedURLException e) {
e.printStackTrace();
}
Integer retry = 0;
while (retry < retryLimit) {
if (!doUrlFetch(httpRequest)) {
retry++;
try {
Thread.sleep(new Integer("1000"));
} catch (final InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
}
Aggregations