use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.
the class NotificationDataUtil method saveNotificationState.
public static void saveNotificationState(Long notificationId, NotificationState state) throws InsufficientAccessException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Notification notification = dataAccessor.getNotification(notificationId);
if (notification.getState() == state)
return;
if (!hasAccessToUpdateData(notification))
throw new InsufficientAccessException();
notification.setState(state);
notification = dataAccessor.createOrUpdateNotification(notification);
}
use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.
the class AuthorDataUtil method saveAuthorCoverImage.
public static String saveAuthorCoverImage(Long authorId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Author author = dataAccessor.getAuthor(authorId);
if (!hasAccessToUpdateAuthorData(author, null))
throw new InsufficientAccessException();
String coverImageName = new Date().getTime() + "";
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
blobEntry.setName("author/" + authorId + "/images/cover/" + coverImageName);
blobAccessor.createOrUpdateBlob(blobEntry);
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.AUTHOR_UPDATE, author);
author.setCoverImage(coverImageName);
author.setLastUpdated(new Date());
author = dataAccessor.createOrUpdateAuthor(author, auditLog);
return createAuthorCoverImageUrl(author);
}
use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.
the class GenericApi method dispatchApiResponse.
final void dispatchApiResponse(Object apiResponse, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (apiResponse instanceof GenericFileDownloadResponse) {
GenericFileDownloadResponse gfdResponse = (GenericFileDownloadResponse) apiResponse;
String eTag = request.getHeader("If-None-Match");
if (eTag == null)
logger.log(Level.INFO, "No eTag found !");
if (eTag != null && eTag.equals(gfdResponse.getETag())) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} else {
response.setContentType(gfdResponse.getMimeType());
response.setHeader("Cache-Control", "max-age=315360000");
response.setHeader("ETag", gfdResponse.getETag());
OutputStream out = response.getOutputStream();
out.write(gfdResponse.getData());
out.close();
}
} else if (apiResponse instanceof GenericResponse) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", getAccessControlAllowOrigin());
}
PrintWriter writer = response.getWriter();
writer.println(new Gson().toJson(apiResponse));
writer.close();
} else if (apiResponse instanceof Throwable) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", getAccessControlAllowOrigin());
}
PrintWriter writer = response.getWriter();
if (apiResponse instanceof InvalidArgumentException) {
logger.log(Level.INFO, ((Throwable) apiResponse).getMessage());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else if (apiResponse instanceof InsufficientAccessException) {
logger.log(Level.INFO, ((Throwable) apiResponse).getMessage());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
} else if (apiResponse instanceof UnexpectedServerException)
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
else
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
writer.println(((Throwable) apiResponse).getMessage());
writer.close();
}
}
use of com.pratilipi.common.exception.InsufficientAccessException 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 com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.
the class GenericBatchApi method dispatchApiResponse.
final void dispatchApiResponse(Map<String, Object> apiResps, HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", super.getAccessControlAllowOrigin());
}
boolean bool = true;
PrintWriter writer = response.getWriter();
writer.print("{");
for (Entry<String, Object> apiResp : apiResps.entrySet()) {
if (bool)
bool = false;
else
writer.print(",");
writer.print("\"" + apiResp.getKey() + "\":{");
if (apiResp.getValue() instanceof JsonElement) {
writer.print("\"status\":" + HttpServletResponse.SC_OK + ",");
writer.print("\"response\":" + apiResp.getValue());
} else if (apiResp.getValue() instanceof InvalidArgumentException) {
logger.log(Level.INFO, ((Throwable) apiResp.getValue()).getMessage());
writer.print("\"status\":" + HttpServletResponse.SC_BAD_REQUEST + ",");
writer.print("\"response\":" + ((Throwable) apiResp.getValue()).getMessage());
} else if (apiResp.getValue() instanceof InsufficientAccessException) {
logger.log(Level.INFO, ((Throwable) apiResp.getValue()).getMessage());
writer.print("\"status\":" + HttpServletResponse.SC_UNAUTHORIZED + ",");
writer.print("\"response\":" + ((Throwable) apiResp.getValue()).getMessage());
} else if (apiResp.getValue() instanceof UnexpectedServerException) {
writer.print("\"status\":" + HttpServletResponse.SC_INTERNAL_SERVER_ERROR + ",");
writer.print("\"response\":" + ((Throwable) apiResp.getValue()).getMessage());
} else {
writer.print("\"status\":" + HttpServletResponse.SC_INTERNAL_SERVER_ERROR + ",");
writer.print("\"response\":" + ((Throwable) apiResp.getValue()).getMessage());
}
writer.print("}");
}
writer.print("}");
writer.close();
}
Aggregations