@Override
  protected Map<String, Object> executeImpl(
      SiteInfo site,
      String pageTitle,
      WebScriptRequest req,
      JSONObject json,
      Status status,
      Cache cache) {
    Map<String, Object> model = new HashMap<>();

    // Grab the version string
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String versionId = templateVars.get("versionId");
    if (versionId == null) {
      String error = "No versionId supplied";
      throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }

    // Try to find the page
    WikiPageInfo page = wikiService.getWikiPage(site.getShortName(), pageTitle);
    if (page == null) {
      String message = "The Wiki Page could not be found";
      status.setCode(Status.STATUS_NOT_FOUND);
      status.setMessage(message);

      // Return an empty string though
      model.put(PARAM_CONTENT, "");
      return model;
    }

    // Fetch the version history for the node
    VersionHistory versionHistory = null;
    Version version = null;
    try {
      versionHistory = versionService.getVersionHistory(page.getNodeRef());
    } catch (AspectMissingException e) {
    }

    if (versionHistory == null) {
      // Not been versioned, return an empty string
      model.put(PARAM_CONTENT, "");
      return model;
    }

    // Fetch the version by either ID or Label
    Matcher m = LABEL_PATTERN.matcher(versionId);
    if (m.matches()) {
      // It's a version label like 2.3
      try {
        version = versionHistory.getVersion(versionId);
      } catch (VersionDoesNotExistException e) {
      }
    } else {
      // It's a version ID like ed00bac1-f0da-4042-8598-45a0d39cb74d
      // (The ID is usually part of the NodeRef of the frozen node, but we
      //  don't assume to be able to just generate the full NodeRef)
      for (Version v : versionHistory.getAllVersions()) {
        if (v.getFrozenStateNodeRef().getId().equals(versionId)) {
          version = v;
        }
      }
    }

    // Did we find the right version in the end?
    String contents;
    if (version != null) {
      ContentReader reader =
          contentService.getReader(version.getFrozenStateNodeRef(), ContentModel.PROP_CONTENT);
      if (reader != null) {
        contents = reader.getContentString();
      } else {
        // No content was stored in the version history
        contents = "";
      }
    } else {
      // No warning of the missing version, just return an empty string
      contents = "";
    }

    // All done
    model.put(PARAM_CONTENT, contents);
    model.put("page", page);
    model.put("site", site);
    model.put("siteId", site.getShortName());
    return model;
  }
  /** Test checkIn */
  public void testCheckIn() {
    NodeRef workingCopy = checkout();

    // Test standard check-in
    Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
    versionProperties.put(Version.PROP_DESCRIPTION, "This is a test version");
    cociService.checkin(workingCopy, versionProperties);

    // Test check-in with content
    NodeRef workingCopy3 = checkout();

    nodeService.setProperty(workingCopy3, PROP_NAME_QNAME, TEST_VALUE_2);
    nodeService.setProperty(workingCopy3, PROP2_QNAME, TEST_VALUE_3);
    ContentWriter tempWriter =
        this.contentService.getWriter(workingCopy3, ContentModel.PROP_CONTENT, false);
    assertNotNull(tempWriter);
    tempWriter.putContent(CONTENT_2);
    String contentUrl = tempWriter.getContentUrl();
    Map<String, Serializable> versionProperties3 = new HashMap<String, Serializable>();
    versionProperties3.put(Version.PROP_DESCRIPTION, "description");
    versionProperties3.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR);
    NodeRef origNodeRef = cociService.checkin(workingCopy3, versionProperties3, contentUrl, true);
    assertNotNull(origNodeRef);

    // Check the checked in content
    ContentReader contentReader =
        this.contentService.getReader(origNodeRef, ContentModel.PROP_CONTENT);
    assertNotNull(contentReader);
    assertEquals(CONTENT_2, contentReader.getContentString());

    // Check that the version history is correct
    Version version = this.versionService.getCurrentVersion(origNodeRef);
    assertNotNull(version);
    assertEquals("description", version.getDescription());
    assertEquals(VersionType.MAJOR, version.getVersionType());
    NodeRef versionNodeRef = version.getFrozenStateNodeRef();
    assertNotNull(versionNodeRef);

    // Check the verioned content
    ContentReader versionContentReader =
        this.contentService.getReader(versionNodeRef, ContentModel.PROP_CONTENT);
    assertNotNull(versionContentReader);
    assertEquals(CONTENT_2, versionContentReader.getContentString());

    // Check that the name is not updated during the check-in
    assertEquals(TEST_VALUE_2, nodeService.getProperty(versionNodeRef, PROP_NAME_QNAME));
    assertEquals(TEST_VALUE_2, nodeService.getProperty(origNodeRef, PROP_NAME_QNAME));

    // Check that the other properties are updated during the check-in
    assertEquals(TEST_VALUE_3, nodeService.getProperty(versionNodeRef, PROP2_QNAME));
    assertEquals(TEST_VALUE_3, nodeService.getProperty(origNodeRef, PROP2_QNAME));

    // Cancel the check out after is has been left checked out
    cociService.cancelCheckout(workingCopy3);

    // Test keep checked out flag
    NodeRef workingCopy2 = checkout();
    Map<String, Serializable> versionProperties2 = new HashMap<String, Serializable>();
    versionProperties2.put(Version.PROP_DESCRIPTION, "Another version test");
    this.cociService.checkin(workingCopy2, versionProperties2, null, true);
    this.cociService.checkin(workingCopy2, new HashMap<String, Serializable>(), null, true);
  }