Search in sources :

Example 6 with UnexpectedServerException

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;
}
Also used : BatchProcessState(com.pratilipi.common.type.BatchProcessState) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataAccessor(com.pratilipi.data.DataAccessor) BatchProcess(com.pratilipi.data.type.BatchProcess) Date(java.util.Date)

Example 7 with UnexpectedServerException

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();
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson)

Example 8 with UnexpectedServerException

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();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 9 with 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();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) GsonBuilder(com.google.gson.GsonBuilder) BlobEntry(com.pratilipi.data.type.BlobEntry) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) GsonLongDateAdapter(com.pratilipi.common.util.GsonLongDateAdapter)

Example 10 with 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;
}
Also used : HashMap(java.util.HashMap) BlobEntry(com.pratilipi.data.type.BlobEntry) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Aggregations

UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)46 IOException (java.io.IOException)19 JsonObject (com.google.gson.JsonObject)12 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 HashMap (java.util.HashMap)12 Gson (com.google.gson.Gson)10 DataAccessor (com.pratilipi.data.DataAccessor)10 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)6 BlobEntry (com.pratilipi.data.type.BlobEntry)6 Date (java.util.Date)6 File (java.io.File)5 JsonElement (com.google.gson.JsonElement)4 Get (com.pratilipi.api.annotation.Get)4 Post (com.pratilipi.api.annotation.Post)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 OutputStream (java.io.OutputStream)4 URL (java.net.URL)4 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)3 Page (com.pratilipi.data.type.Page)3