Search in sources :

Example 6 with InsufficientAccessException

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);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Notification(com.pratilipi.data.type.Notification)

Example 7 with InsufficientAccessException

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);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 8 with InsufficientAccessException

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();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) GenericResponse(com.pratilipi.api.shared.GenericResponse) OutputStream(java.io.OutputStream) GenericFileDownloadResponse(com.pratilipi.api.shared.GenericFileDownloadResponse) Gson(com.google.gson.Gson) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PrintWriter(java.io.PrintWriter)

Example 9 with InsufficientAccessException

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();
    }
}
Also used : GenericFileUploadRequest(com.pratilipi.api.shared.GenericFileUploadRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItemStream(org.apache.commons.fileupload.FileItemStream) GenericRequest(com.pratilipi.api.shared.GenericRequest) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 10 with InsufficientAccessException

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();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PrintWriter(java.io.PrintWriter)

Aggregations

InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)48 DataAccessor (com.pratilipi.data.DataAccessor)37 Date (java.util.Date)21 AuditLog (com.pratilipi.data.type.AuditLog)19 UserPratilipi (com.pratilipi.data.type.UserPratilipi)18 Pratilipi (com.pratilipi.data.type.Pratilipi)14 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)11 BlobAccessor (com.pratilipi.data.BlobAccessor)7 DocAccessor (com.pratilipi.data.DocAccessor)7 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)7 JsonObject (com.google.gson.JsonObject)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)5 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)5 AccessToken (com.pratilipi.data.type.AccessToken)5 Author (com.pratilipi.data.type.Author)5 Gson (com.google.gson.Gson)4 UserData (com.pratilipi.data.client.UserData)4 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)4 User (com.pratilipi.data.type.User)4 HashMap (java.util.HashMap)4