/**
  * Retrieve a specific post from a blog by sending a GET request to the posts resource URI
  *
  * @param blogId The blog id
  * @param postId The post id
  * @return the post in the blog
  */
 private Post retrievePostById(String blogId, String postId) {
   Post bloggerPost = null;
   try {
     String json =
         doGetRestBloggerApi(
             "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/" + postId,
             bloggerAccessToken);
     Gson gson = new Gson();
     JsonElement element = gson.fromJson(json, JsonElement.class);
     JsonObject jobj = element.getAsJsonObject();
     String kind = jobj.get("kind").getAsString();
     String id = jobj.get("id").getAsString();
     String published = jobj.get("published").getAsString();
     String updated = jobj.get("updated").getAsString();
     String url = jobj.get("url").getAsString();
     String selfLink = jobj.get("selfLink").getAsString();
     String title = jobj.get("title").getAsString();
     String content = jobj.get("content").getAsString();
     bloggerPost = new Post(id, url, selfLink, title, content);
     bloggerPost.setKind(kind);
     bloggerPost.setPublished(published);
     bloggerPost.setUpdated(updated);
   } catch (Exception e) {
     return null;
   }
   return bloggerPost;
 }
 /**
  * Update a blog post by sending a PUT request to the post collection URI with a post JSON body
  *
  * @param blogId The blog id
  * @param post The post to be updated
  * @param newPostTitle The new title of the post
  * @param newPostContent The new content of the post
  * @return the url of the updated post
  * @throws Exception
  */
 private String updatePost(String blogId, Post post, String newPostTitle, String newPostContent)
     throws Exception {
   String putUrl = null;
   Gson gson = new Gson();
   JsonObject objPost = new JsonObject();
   objPost.addProperty("kind", "blogger#post");
   objPost.addProperty("id", post.getId());
   JsonObject blog = new JsonObject();
   blog.addProperty("id", blogId);
   objPost.add("blog", blog);
   objPost.addProperty("title", newPostTitle);
   objPost.addProperty("content", newPostContent);
   objPost.addProperty("url", post.getUrl());
   objPost.addProperty("selfLink", post.getSelfLink());
   String jsonPost = gson.toJson(objPost);
   try {
     String jsonResult =
         doPutRestBloggerApi(
             "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/" + post.getId(),
             bloggerAccessToken,
             jsonPost);
     JsonElement element = gson.fromJson(jsonResult, JsonElement.class);
     JsonObject jobj = element.getAsJsonObject();
     // putUrl = jobj.get("url").getAsString();
     putUrl = generateEditionUrl(blogId, post.getId());
   } catch (Exception e) {
     e.printStackTrace();
     throw e;
   }
   return putUrl;
 }
 @Override
 public Deploy redeploy(String baseUri, Deploy newDeploy) {
   String bloggerUserid = retrieveBloggerUserId(bloggerAccessToken);
   Blog blog = retrieveUserBlog(bloggerUserid, newDeploy.getCourse().getId());
   // get the url of the blog entry
   String entryUrl = newDeploy.getLiveDeployURL().toString();
   String postId = getPostIdFromUrl(entryUrl);
   Boolean wasPublic = true;
   Post blogPost = retrievePostById(newDeploy.getCourse().getId(), postId);
   if (blogPost == null) {
     // make public the post just to be able to update it by using the blogger API
     publishPost(newDeploy.getCourse().getId(), postId);
     blogPost = retrievePostById(newDeploy.getCourse().getId(), postId);
     wasPublic = false;
   }
   String newPostTitle = newDeploy.getName();
   // generate the html content
   String postContent = generateActivitiesHtml(newDeploy, "bloggerUser");
   // generate the javascript content
   String javascriptContent = generateJavascriptContent();
   try {
     String postUrl;
     // if the post already exists, we update it
     if (blogPost != null) {
       postUrl =
           updatePost(
               blog.getId(),
               blogPost,
               blogPost.getTitle(),
               javascriptContent
                   + "<p>This is the result of the deployment from Glue-!PS</p><p>"
                   + postContent
                   + "</p>");
       if (!wasPublic) {
         // make private it again
         revertPost(newDeploy.getCourse().getId(), postId);
       }
     } else {
       // if not we have to create a new one
       postUrl =
           addPost(
               blog.getId(),
               newPostTitle,
               javascriptContent
                   + "<p>This is the result of the deployment from Glue-!PS</p><p>"
                   + postContent
                   + "</p>");
       // make the new post private
       revertPost(newDeploy.getCourse().getId(), postId);
     }
     newDeploy.setLiveDeployURL(new URL(postUrl));
     return newDeploy;
   } catch (MalformedURLException e) {
     e.printStackTrace();
     // If something went wrong, we set location=null in case it has already a url
     newDeploy.setLiveDeployURL(null);
     return newDeploy;
   } catch (Exception e) {
     // If something went wrong, we set location=null in case it has already a url
     newDeploy.setLiveDeployURL(null);
     return newDeploy;
   }
 }