use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class BatchProcessDataUtil method exec.
public static boolean exec(Long batchProcessId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BatchProcess batchProcess = dataAccessor.getBatchProcess(batchProcessId);
if (batchProcess.getStartAt().after(new Date()))
return false;
if (batchProcess.getStateInProgress() != null && !batchProcess.getStateInProgress().isTimedOut(batchProcess.getLastUpdated()))
return false;
BatchProcessState currState = batchProcess.getType().getNextState(batchProcess.getStateCompleted());
if (currState == null)
throw new UnexpectedServerException();
batchProcess.setStateInProgress(currState);
batchProcess.setLastUpdated(new Date());
batchProcess = dataAccessor.createOrUpdateBatchProcess(batchProcess);
System.out.println("Process Id: " + batchProcess.getId());
System.out.println("Process Type: " + batchProcess.getType());
System.out.println("Process State: " + batchProcess.getStateInProgress());
if (currState == BatchProcessState.INIT)
_execStateInit(batchProcess);
else if (currState == BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER)
_execStateGetUserIdsByAuthorFilter(batchProcess);
else if (currState == BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS)
_execStateCreateNotificationsForUserIds(batchProcess);
else if (currState == BatchProcessState.VALIDATE_NOTIFICATION_COUNT)
_execStateValidateNotificationCount(batchProcess);
else if (currState == BatchProcessState.COMPLETED)
batchProcess.setStateCompleted(currState);
batchProcess.setStateInProgress(null);
batchProcess.setLastUpdated(new Date());
// Saving Entity
batchProcess = dataAccessor.createOrUpdateBatchProcess(batchProcess);
return true;
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class FacebookApi method getUrlShareCount.
public static long getUrlShareCount(String shareUrl) throws UnexpectedServerException {
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("id", shareUrl);
paramsMap.put("access_token", getAccessToken());
String responsePayload = HttpUtil.doGet(GRAPH_API_2p6_URL, paramsMap);
JsonElement responseJson = new Gson().fromJson(responsePayload, JsonElement.class);
// Facebook might return an error response.
if (responseJson.getAsJsonObject().get("error") != null) {
logger.log(Level.SEVERE, "Facebook responded with an error message: " + responseJson.getAsJsonObject().get("error").getAsJsonObject().get("message"));
throw new UnexpectedServerException();
}
JsonElement shareJson = responseJson.getAsJsonObject().get("share");
if (shareJson == null)
return 0L;
JsonElement shareCountJson = shareJson.getAsJsonObject().get("share_count");
if (shareCountJson == null)
return 0L;
return shareCountJson.getAsLong();
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class FacebookApi method postScrapeRequest.
public static void postScrapeRequest(String url) throws UnexpectedServerException {
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("id", url);
paramsMap.put("scrape", "true");
paramsMap.put("access_token", getAccessToken());
String responsePayload = HttpUtil.doPost(GRAPH_API_2p4_URL, paramsMap);
JsonObject responseJson = new Gson().fromJson(responsePayload, JsonObject.class);
if (responseJson.get("error") != null) {
logger.log(Level.SEVERE, "Facebook scrapping for url: " + url + " failed with error \"" + responseJson.get("error") + "\"");
throw new UnexpectedServerException();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class DocAccessorImpl method _save.
private <T> void _save(String docPath, T doc) throws UnexpectedServerException {
try {
byte[] blobData = new GsonBuilder().registerTypeAdapter(Date.class, new GsonLongDateAdapter()).create().toJson(doc).getBytes("UTF-8");
BlobEntry blobEntry = blobAccessor.newBlob(docPath, blobData, "application/json");
blobAccessor.createOrUpdateBlob(blobEntry);
} catch (UnsupportedEncodingException e) {
logger.log(Level.SEVERE, e.getMessage());
throw new UnexpectedServerException();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class RtdbAccessorFirebaseImpl method getUserPreferences.
@Override
public Map<Long, UserPreferenceRtdb> getUserPreferences(Collection<Long> userIds) throws UnexpectedServerException {
if (userIds == null || userIds.isEmpty())
return new HashMap<>();
userIds = new HashSet<>(userIds);
List<String> targetUrlList = new ArrayList<>(userIds.size());
for (Long userId : userIds) targetUrlList.add(_getUserPreferenceDbUrl(userId));
Map<String, BlobEntry> blobEntries = HttpUtil.doGet(targetUrlList, headersMap);
Map<Long, UserPreferenceRtdb> userPreferences = new HashMap<>(userIds.size());
try {
for (Long userId : userIds) {
BlobEntry blobEntry = blobEntries.get(_getUserPreferenceDbUrl(userId));
String jsonStr = new String(blobEntry.getData(), "UTF-8");
userPreferences.put(userId, _getUserPreferenceRtdb(jsonStr));
}
} catch (UnsupportedEncodingException e) {
logger.log(Level.SEVERE, "Failed to parse response from Firebase.", e);
throw new UnexpectedServerException();
}
return userPreferences;
}
Aggregations