use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class BatchProcessDocImpl method setData.
@Override
public void setData(String name, Object data) {
if (this.data == null)
this.data = new JsonObject();
this.data.add(name, gson.toJsonTree(data));
this.lastUpdated = new Date();
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class GenericApi method executeApi.
final Object executeApi(GenericApi api, Method apiMethod, JsonObject requestPayloadJson, Class<? extends GenericRequest> apiMethodParameterType, HttpServletRequest request) {
try {
GenericRequest apiRequest = new Gson().fromJson(requestPayloadJson, apiMethodParameterType);
if (apiRequest instanceof GenericFileUploadRequest) {
GenericFileUploadRequest gfuRequest = (GenericFileUploadRequest) apiRequest;
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream fileItemStream = iterator.next();
if (!fileItemStream.isFormField()) {
gfuRequest.setName(fileItemStream.getName());
gfuRequest.setData(IOUtils.toByteArray(fileItemStream.openStream()));
gfuRequest.setMimeType(fileItemStream.getContentType());
break;
}
}
} catch (IOException | FileUploadException e) {
throw new UnexpectedServerException();
}
}
JsonObject errorMessages = apiRequest.validate();
if (errorMessages.entrySet().size() > 0)
return new InvalidArgumentException(errorMessages);
else
return apiMethod.invoke(api, apiRequest);
} catch (JsonSyntaxException e) {
logger.log(Level.SEVERE, "Invalid JSON in request body.", e);
return new InvalidArgumentException("Invalid JSON in request body.");
} catch (UnexpectedServerException e) {
return e;
} catch (InvocationTargetException e) {
Throwable te = e.getTargetException();
if (te instanceof InvalidArgumentException || te instanceof InsufficientAccessException || te instanceof UnexpectedServerException) {
return te;
} else {
logger.log(Level.SEVERE, "Failed to execute API.", te);
return new UnexpectedServerException();
}
} catch (IllegalAccessException | IllegalArgumentException e) {
logger.log(Level.SEVERE, "Failed to execute API.", e);
return new UnexpectedServerException();
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class GenericBatchApi method createRequestPayloadJson.
final JsonObject createRequestPayloadJson(String queryStr, HttpServletRequest request) {
JsonObject requestPayloadJson = new JsonObject();
for (String paramValue : queryStr.split("&")) {
int index = paramValue.indexOf('=');
String param = index == -1 ? paramValue : paramValue.substring(0, index);
String value = index == -1 ? null : paramValue.substring(index + 1);
requestPayloadJson.addProperty(param, value);
requestPayloadJson.addProperty("has" + Character.toUpperCase(param.charAt(0)) + param.substring(1), true);
}
return requestPayloadJson;
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updatePratilipiContent.
public static void updatePratilipiContent(Long pratilipiId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = DataAccessorFactory.getDocAccessor().newPratilipiContentDoc();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!pratilipi.isOldContent()) {
return;
} else if (pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
BlobEntry blobEntry = blobAccessor.getBlob("pratilipi-content/pratilipi/" + pratilipiId);
if (blobEntry == null)
return;
String contentHtml = new String(blobEntry.getData(), Charset.forName("UTF-8"));
List<Object[]> pageletList = _createPageletList(pratilipi, Jsoup.parse(contentHtml).body());
if (pageletList.size() > 0) {
PratilipiContentDoc.Chapter chapter = null;
if (pageletList.get(0)[0] != PratilipiContentDoc.PageletType.HEAD)
chapter = pcDoc.addChapter(pratilipi.getTitle() == null ? pratilipi.getTitleEn() : pratilipi.getTitle());
for (Object[] pagelet : pageletList) {
if (pagelet[0] == PratilipiContentDoc.PageletType.HEAD) {
chapter = pcDoc.addChapter((String) pagelet[1]);
} else {
PratilipiContentDoc.Page page = chapter.getPage(1);
if (page == null)
page = chapter.addPage();
page.addPagelet((PratilipiContentDoc.PageletType) pagelet[0], pagelet[1], (PratilipiContentDoc.AlignmentType) pagelet[2]);
}
}
}
} else if (pratilipi.getContentType() == PratilipiContentType.IMAGE) {
for (int i = 1; i <= pratilipi.getPageCount(); i++) {
BlobEntry blobEntry = blobAccessor.getBlob("pratilipi/" + pratilipiId + "/images/" + i);
if (pratilipi.getId() == 5639838220943360L && i <= 5)
// Skipping first 5 pages as per Shally's request
continue;
else if (pratilipi.getId() == 5749258686824448L && i <= 4)
// Skipping first 4 pages as per Shally's request
continue;
else if (pratilipi.getId() == 5486454792781824L && i <= 1)
// Skipping first page as per Shally's request
continue;
else if (blobEntry == null && pratilipi.getId() == 5768181499035648L)
// Skipping missing pages as per Dileepan's request
continue;
JsonObject imgData = new JsonObject();
imgData.addProperty("name", i + "");
imgData.addProperty("height", ImageUtil.getHeight(blobEntry));
imgData.addProperty("width", ImageUtil.getWidth(blobEntry));
pcDoc.addChapter(null).addPage().addPagelet(PratilipiContentDoc.PageletType.IMAGE, imgData);
}
} else {
throw new UnexpectedServerException("ContentType " + pratilipi.getContentType() + " is not supported !");
}
docAccessor.save(pratilipiId, pcDoc);
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class UserDataUtil method _validateUserDataForSave.
private static void _validateUserDataForSave(UserData userData) throws InvalidArgumentException {
boolean isNew = userData.getId() == null;
JsonObject errorMessages = new JsonObject();
// New user profile must have name.
if (isNew && userData.getFirstName() == null)
errorMessages.addProperty("name", GenericRequest.ERR_NAME_REQUIRED);
// New user profile must have email.
if (isNew && (!userData.hasEmail() || userData.getEmail() == null))
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
else // Email can not be un-set or set to null.
if (!isNew && userData.hasEmail() && userData.getEmail() == null)
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
// For new user, user email should be not registered already.
if (isNew && DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail()) != null)
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
else // Email, if provided, must not be registered with some other user.
if (!isNew && userData.hasEmail() && userData.getEmail() != null) {
User user = DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail());
if (user != null && !user.getId().equals(userData.getId()))
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
}
if (errorMessages.entrySet().size() > 0)
throw new InvalidArgumentException(errorMessages);
}
Aggregations