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);
}
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);
}
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);
}
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();
}
}
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;
}
}
Aggregations