Search in sources :

Example 21 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class PratilipiDataUtil method _validatePratilipiDataForSave.

private static void _validatePratilipiDataForSave(PratilipiData pratilipiData) throws InvalidArgumentException {
    boolean isNew = pratilipiData.getId() == null;
    JsonObject errorMessages = new JsonObject();
    // New content piece must have language.
    if (isNew && (!pratilipiData.hasLanguage() || pratilipiData.getLanguage() == null))
        errorMessages.addProperty("language", GenericRequest.ERR_LANGUAGE_REQUIRED);
    else // Language can not be un-set or set to null.
    if (!isNew && pratilipiData.hasLanguage() && pratilipiData.getLanguage() == null)
        errorMessages.addProperty("language", GenericRequest.ERR_LANGUAGE_REQUIRED);
    // New content piece must have type.
    if (isNew && (!pratilipiData.hasType() || pratilipiData.getType() == null))
        errorMessages.addProperty("type", GenericRequest.ERR_PRATILIPI_TYPE_REQUIRED);
    else // Type can not be un-set or set to null.
    if (!isNew && pratilipiData.hasType() && pratilipiData.getType() == null)
        errorMessages.addProperty("type", GenericRequest.ERR_PRATILIPI_TYPE_REQUIRED);
    // Content type can not be un-set or set to null.
    if (!isNew && pratilipiData.hasContentType() && pratilipiData.getContentType() == null)
        errorMessages.addProperty("type", GenericRequest.ERR_PRATILIPI_CONTENT_TYPE_REQUIRED);
    // State can not be un-set or set to null.
    if (// isNew || ! isNew
    pratilipiData.hasState() && pratilipiData.getState() == null)
        errorMessages.addProperty("state", GenericRequest.ERR_PRATILIPI_STATE_REQUIRED);
    else // isNew || !isNew
    if (pratilipiData.hasState() && pratilipiData.getState() != PratilipiState.DRAFTED && pratilipiData.getState() != PratilipiState.SUBMITTED && pratilipiData.getState() != PratilipiState.PUBLISHED && pratilipiData.getState() != PratilipiState.DELETED)
        errorMessages.addProperty("state", GenericRequest.ERR_PRATILIPI_STATE_INVALID);
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Example 22 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class PratilipiDocUtil method saveContentPageBatch.

public static void saveContentPageBatch(Long pratilipiId, JsonObject chapters) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    // Doc
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    for (Map.Entry<String, JsonElement> chapterEntry : chapters.entrySet()) {
        Integer chapterNo = Integer.parseInt(chapterEntry.getKey());
        // chapterTitle
        String chapterTitle = null;
        if (chapterEntry.getValue().getAsJsonObject().has("chapterTitle"))
            chapterTitle = chapterEntry.getValue().getAsJsonObject().get("chapterTitle").getAsString();
        // content
        String content = null;
        if (chapterEntry.getValue().getAsJsonObject().has("content"))
            content = chapterEntry.getValue().getAsJsonObject().get("content").getAsString();
        // pageNo
        if (!chapterEntry.getValue().getAsJsonObject().has("pageNo"))
            throw new InvalidArgumentException(GenericRequest.ERR_PRATILIPI_PAGE_NO_REQUIRED);
        Integer pageNo = chapterEntry.getValue().getAsJsonObject().get("pageNo").getAsInt();
        // Chapter
        Chapter chapter = pcDoc.getChapter(chapterNo);
        if (chapter == null)
            chapter = pcDoc.addChapter(chapterNo, chapterTitle);
        else if (chapterTitle != null)
            chapter.setTitle(chapterTitle);
        // Page
        PratilipiContentDoc.Page page = chapter.getPage(pageNo);
        if (page == null)
            page = chapter.addPage(pageNo);
        _setPage(pratilipiId, page, content);
    }
    // Save
    docAccessor.save(pratilipiId, pcDoc);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonElement(com.google.gson.JsonElement) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Map(java.util.Map) HashMap(java.util.HashMap)

Example 23 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiSearchIndex.

public static void updatePratilipiSearchIndex(Long pratilipiId, Long authorId) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Pratilipi> pratilipiList = null;
    if (authorId != null) {
        PratilipiFilter pratilipiFilter = new PratilipiFilter();
        pratilipiFilter.setAuthorId(authorId);
        pratilipiList = dataAccessor.getPratilipiList(pratilipiFilter, null, null).getDataList();
    } else if (pratilipiId != null) {
        pratilipiList = new ArrayList<>(1);
        pratilipiList.add(dataAccessor.getPratilipi(pratilipiId));
    } else {
        logger.log(Level.SEVERE, "Neither AuthorId, nor PratilipiId is provided !");
        throw new InvalidArgumentException("Neither AuthorId, nor PratilipiId is provided !");
    }
    _updatePratilipiSearchIndex(pratilipiList);
}
Also used : PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 24 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class GoogleApi method getUserData.

public static UserData getUserData(String googleIdToken) throws InvalidArgumentException, UnexpectedServerException {
    try {
        GoogleIdToken idToken = UxModeFilter.isAndroidApp() ? getAndroidIdTokenVerifier().verify(googleIdToken) : getWebIdTokenVerifier().verify(googleIdToken);
        String authorisedParty = UxModeFilter.isAndroidApp() ? getAppClientId() : getWebClientId();
        if (idToken == null || idToken.getPayload() == null || !idToken.getPayload().getAuthorizedParty().equals(authorisedParty)) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("googleIdToken", "Invalid GoogleIdToken !");
            throw new InvalidArgumentException(jsonObject);
        }
        Payload payload = idToken.getPayload();
        logger.log(Level.INFO, "GoogleApi Payload : " + new Gson().toJson(payload));
        if (payload.get("given_name") == null || ((String) payload.get("given_name")).isEmpty()) {
            logger.log(Level.SEVERE, "Google given_name is missing for GoogleUser: " + payload.getSubject());
            throw new UnexpectedServerException();
        }
        UserData userData = new UserData();
        userData.setGoogleId(payload.getSubject());
        userData.setFirstName((String) payload.get("given_name"));
        userData.setLastName((String) payload.get("family_name"));
        userData.setEmail(payload.getEmail());
        return userData;
    } catch (GeneralSecurityException | IOException e) {
        logger.log(Level.SEVERE, "Google id token verification failed: " + e);
        throw new UnexpectedServerException();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserData(com.pratilipi.data.client.UserData) GeneralSecurityException(java.security.GeneralSecurityException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) IOException(java.io.IOException) GoogleIdToken(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)

Example 25 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class FacebookApi method getUserData.

public static UserData getUserData(String fbUserAccessToken) throws InvalidArgumentException, UnexpectedServerException {
    Map<String, String> paramsMap = new HashMap<String, String>();
    paramsMap.put("access_token", fbUserAccessToken);
    paramsMap.put("fields", "id,first_name,last_name,gender,birthday,email");
    String responsePayload = HttpUtil.doGet(GRAPH_API_2p6_URL + "/me", paramsMap);
    JsonObject responseJson = new Gson().fromJson(responsePayload, JsonElement.class).getAsJsonObject();
    if (responseJson.get("error") != null) {
        logger.log(Level.SEVERE, "Error response from Graph Api.");
        // Facebook returns code 190 if access-token is expired
        if (responseJson.get("error").getAsJsonObject().get("code").getAsInt() == 190)
            throw new InvalidArgumentException("Facebook AccessToken is expired.");
        else
            throw new UnexpectedServerException();
    } else {
        if (responseJson.get("first_name") == null || responseJson.get("first_name").getAsString().isEmpty()) {
            logger.log(Level.INFO, "HTTP Response : " + responseJson);
            logger.log(Level.SEVERE, "Facebook first_name is missing for FacebookUser: " + responseJson.get("id").getAsString());
            throw new UnexpectedServerException();
        }
        UserData userData = new UserData();
        userData.setFacebookId(responseJson.get("id").getAsString());
        userData.setFirstName(responseJson.get("first_name").getAsString());
        userData.setLastName(responseJson.get("last_name").getAsString());
        if (responseJson.get("gender") != null)
            userData.setGender(Gender.valueOf(responseJson.get("gender").getAsString().toUpperCase()));
        if (responseJson.get("birthday") != null) {
            String dob = responseJson.get("birthday").getAsString();
            String year = dob.substring(6);
            String month = dob.substring(0, 2);
            String date = dob.substring(3, 5);
            userData.setDateOfBirth(year + "-" + month + "-" + date);
        }
        if (responseJson.get("email") != null)
            userData.setEmail(responseJson.get("email").getAsString());
        return userData;
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) UserData(com.pratilipi.data.client.UserData) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)37 JsonObject (com.google.gson.JsonObject)21 DataAccessor (com.pratilipi.data.DataAccessor)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)11 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)9 User (com.pratilipi.data.type.User)9 Gson (com.google.gson.Gson)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)7 AuditLog (com.pratilipi.data.type.AuditLog)7 Author (com.pratilipi.data.type.Author)6 JsonElement (com.google.gson.JsonElement)5 Post (com.pratilipi.api.annotation.Post)5 UserData (com.pratilipi.data.client.UserData)5 Page (com.pratilipi.data.type.Page)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 Date (java.util.Date)5 HashMap (java.util.HashMap)5 Language (com.pratilipi.common.type.Language)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 PrintWriter (java.io.PrintWriter)3