From cdf61e73c4de83509d4a93964cfe888fbd1f33b6 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 2 Feb 2023 19:17:37 +0100 Subject: [PATCH] added get-post by id --- .../networking/ws/methods/v2/Posts.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java index f679897..424ce18 100644 --- a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java +++ b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Posts.java @@ -28,6 +28,7 @@ import org.gcube.portal.databook.shared.Feed; import org.gcube.portal.databook.shared.Post; import org.gcube.portal.databook.shared.RangePosts; import org.gcube.portal.databook.shared.ex.ColumnNameNotFoundException; +import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException; import org.gcube.portal.databook.shared.ex.FeedTypeNotFoundException; import org.gcube.portal.databook.shared.ex.PrivacyLevelTypeNotFoundException; import org.gcube.portal.social.networking.ws.inputs.PostInputBean; @@ -138,6 +139,53 @@ public class Posts { return Response.status(status).entity(responseBean).build(); } + /** + * Retrieve a post by id + * @return the post if the post id belongs to a post in the context identified by the token + */ + @GET + @Path("get-post/") + @Produces(MediaType.APPLICATION_JSON) + @StatusCodes ({ + @ResponseCode ( code = 200, condition = "Successful retrieval of posts, reported in the 'result' field of the returned object"), + @ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT) + }) + public Response getPost(@QueryParam("id") String id) { + Caller caller = AuthorizationProvider.instance.get(); + String context = ScopeProvider.instance.get(); + ResponseBean responseBean = new ResponseBean(); + Status status = Status.OK; + List posts = new ArrayList<>(); + try{ + logger.debug("Retrieving post with id " + id); + try { + posts.add(CassandraConnection.getInstance().getDatabookStore().readPost(id)); + } catch(FeedIDNotFoundException e){ + responseBean.setMessage("The post with id " + id + " does not exist in context " + context); + responseBean.setSuccess(false); + return Response.status(status).entity(responseBean).build(); + } + Filters.filterPostsPerContext(posts, context); + Filters.hideSensitiveInformation(posts, caller.getClient().getId()); + if (posts.isEmpty()) { + responseBean.setMessage("The post with id " + id + " does not belong to this context " + context); + responseBean.setSuccess(false); + status = Status.FORBIDDEN; + return Response.status(status).entity(responseBean).build(); + } + responseBean.setResult(posts.get(0)); + responseBean.setMessage(""); + responseBean.setSuccess(true); + }catch(Exception e){ + logger.error("Unable to retrieve such post.", e); + responseBean.setMessage(e.getMessage()); + responseBean.setSuccess(false); + status = Status.INTERNAL_SERVER_ERROR; + } + + return Response.status(status).entity(responseBean).build(); + } + /** * Retrieve a given quantity of latest user's posts * @param quantity the number of latest post to get