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

    // Try to find the link
    LinkInfo link = linksService.getLink(site.getShortName(), linkName);
    if (link == null) {
      String message = "No link found with that name";

      status.setCode(Status.STATUS_NOT_FOUND);
      status.setMessage(message);
      model.put(PARAM_MESSAGE, message);
      return model;
    }

    // Get the new link details from the JSON
    // Update the main properties
    link.setTitle(getOrNull(json, "title"));
    link.setDescription(getOrNull(json, "description"));
    link.setURL(getOrNull(json, "url"));

    // Handle internal / not internal
    if (json.containsKey("internal")) {
      link.setInternal(true);
    } else {
      link.setInternal(false);
    }

    // Do the tags
    link.getTags().clear();
    List<String> tags = getTags(json);
    if (tags != null && tags.size() > 0) {
      link.getTags().addAll(tags);
    }

    // Update the link
    try {
      link = linksService.updateLink(link);
    } catch (AccessDeniedException e) {
      String message = "You don't have permission to update that link";

      status.setCode(Status.STATUS_FORBIDDEN);
      status.setMessage(message);
      model.put(PARAM_MESSAGE, message);
      return model;
    }

    // Generate an activity for the change
    addActivityEntry("updated", link, site, req, json);

    // Build the model
    model.put(PARAM_MESSAGE, "Node " + link.getNodeRef() + " updated");
    model.put("link", link);
    model.put("site", site);
    model.put("siteId", site.getShortName());

    // All done
    return model;
  }
  @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;
  }