@Test
  public void testIncludeWiki() {
    EntityReferenceSet set = new EntityReferenceSet();

    set.includes(new EntityReference("wiki", EntityType.WIKI));

    Assert.assertTrue(set.matches(new EntityReference("wiki", EntityType.WIKI)));
    Assert.assertFalse(set.matches(new EntityReference("notwiki", EntityType.WIKI)));

    Assert.assertTrue(
        set.matches(
            new EntityReference(
                "space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))));
    Assert.assertFalse(
        set.matches(
            new EntityReference(
                "space", EntityType.SPACE, new EntityReference("notwiki", EntityType.WIKI))));

    set.includes(new EntityReference("otherwiki", EntityType.WIKI));

    Assert.assertTrue(set.matches(new EntityReference("wiki", EntityType.WIKI)));

    Assert.assertTrue(set.matches(new EntityReference("otherwiki", EntityType.WIKI)));

    Assert.assertFalse(set.matches(new EntityReference("notwiki", EntityType.WIKI)));
  }
  @Test
  public void testIncludeDocument() {
    EntityReferenceSet set = new EntityReferenceSet();

    set.includes(
        new EntityReference(
            "document",
            EntityType.DOCUMENT,
            new EntityReference(
                "space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))));

    Assert.assertTrue(
        set.matches(
            new EntityReference(
                "document",
                EntityType.DOCUMENT,
                new EntityReference(
                    "space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));

    Assert.assertFalse(
        set.matches(
            new EntityReference(
                "document",
                EntityType.DOCUMENT,
                new EntityReference(
                    "space", EntityType.SPACE, new EntityReference("notwiki", EntityType.WIKI)))));
    Assert.assertFalse(
        set.matches(
            new EntityReference(
                "document",
                EntityType.DOCUMENT,
                new EntityReference(
                    "notspace", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));
    Assert.assertFalse(
        set.matches(
            new EntityReference(
                "notdocument",
                EntityType.DOCUMENT,
                new EntityReference(
                    "space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));
  }
Exemplo n.º 3
0
  private String exportXAR(XWikiContext context)
      throws XWikiException, IOException, FilterException {
    XWikiRequest request = context.getRequest();

    boolean history = Boolean.valueOf(request.get("history"));
    boolean backup = Boolean.valueOf(request.get("backup"));
    String author = request.get("author");
    String description = request.get("description");
    String licence = request.get("licence");
    String version = request.get("version");
    String name = request.get("name");
    String[] pages = request.getParameterValues("pages");
    boolean all = ArrayUtils.isEmpty(pages);

    if (!context.getWiki().getRightService().hasWikiAdminRights(context)) {
      context.put("message", "needadminrights");
      return "exception";
    }

    if (name == null) {
      return "export";
    }

    if (StringUtils.isBlank(name)) {
      if (all) {
        name = "backup";
      } else {
        name = "export";
      }
    }

    if (context.getWiki().ParamAsLong("xwiki.action.export.xar.usefilter", 1) == 1) {
      // Create input wiki stream
      DocumentInstanceInputProperties inputProperties = new DocumentInstanceInputProperties();

      // We don't want to log the details
      inputProperties.setVerbose(false);

      inputProperties.setWithJRCSRevisions(history);
      inputProperties.setWithRevisions(false);

      EntityReferenceSet entities = new EntityReferenceSet();

      if (all) {
        entities.includes(new WikiReference(context.getWikiId()));
      } else {
        // Find all page references and add them for processing
        Collection<String> pageList = getPagesToExport(pages, context);
        DocumentReferenceResolver<String> resolver =
            Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "current");
        for (String pageName : pageList) {
          entities.includes(resolver.resolve(pageName));
        }
      }

      inputProperties.setEntities(entities);

      InputFilterStreamFactory inputFilterStreamFactory =
          Utils.getComponent(
              InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());

      InputFilterStream inputFilterStream =
          inputFilterStreamFactory.createInputFilterStream(inputProperties);

      // Create output wiki stream
      XAROutputProperties xarProperties = new XAROutputProperties();

      // We don't want to log the details
      xarProperties.setVerbose(false);

      XWikiResponse response = context.getResponse();

      xarProperties.setTarget(new DefaultOutputStreamOutputTarget(response.getOutputStream()));
      xarProperties.setPackageName(name);
      if (description != null) {
        xarProperties.setPackageDescription(description);
      }
      if (licence != null) {
        xarProperties.setPackageLicense(licence);
      }
      if (author != null) {
        xarProperties.setPackageAuthor(author);
      }
      if (version != null) {
        xarProperties.setPackageVersion(version);
      }
      xarProperties.setPackageBackupPack(backup);
      xarProperties.setPreserveVersion(backup || history);

      BeanOutputFilterStreamFactory<XAROutputProperties> xarFilterStreamFactory =
          Utils.getComponent(
              (Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_11.serialize());

      OutputFilterStream outputFilterStream =
          xarFilterStreamFactory.createOutputFilterStream(xarProperties);

      // Export
      response.setContentType("application/zip");
      response.addHeader(
          "Content-disposition", "attachment; filename=" + Util.encodeURI(name, context) + ".xar");

      inputFilterStream.read(outputFilterStream.getFilter());

      inputFilterStream.close();
      outputFilterStream.close();

      // Flush
      response.getOutputStream().flush();

      // Indicate that we are done with the response so no need to add anything
      context.setFinished(true);
    } else {
      PackageAPI export = ((PackageAPI) context.getWiki().getPluginApi("package", context));
      if (export == null) {
        // No Packaging plugin configured
        return "exception";
      }

      export.setWithVersions(history);

      if (author != null) {
        export.setAuthorName(author);
      }

      if (description != null) {
        export.setDescription(description);
      }

      if (licence != null) {
        export.setLicence(licence);
      }

      if (version != null) {
        export.setVersion(version);
      }

      export.setBackupPack(backup);

      export.setName(name);

      if (all) {
        export.backupWiki();
      } else {
        if (pages != null) {
          for (int i = 0; i < pages.length; i++) {
            String pageName = pages[i];
            String defaultAction = request.get("action_" + pageName);
            int iAction;
            try {
              iAction = Integer.parseInt(defaultAction);
            } catch (Exception e) {
              iAction = 0;
            }
            export.add(pageName, iAction);
          }
        }
        export.export();
      }
    }

    return null;
  }