Search in sources :

Example 6 with ExtensionId

use of org.xwiki.extension.ExtensionId in project xwiki-platform by xwiki.

the class RepositoryManager method importExtension.

public DocumentReference importExtension(String extensionId, ExtensionRepository repository, Type type) throws QueryException, XWikiException, ResolveException {
    TreeMap<Version, String> versions = new TreeMap<Version, String>();
    Version lastVersion = getVersions(extensionId, repository, type, versions);
    if (lastVersion == null) {
        throw new ExtensionNotFoundException("Can't find any version for the extension [" + extensionId + "] on repository [" + repository + "]");
    } else if (versions.isEmpty()) {
        // If no valid version import the last version
        versions.put(lastVersion, extensionId);
    } else {
        // Select the last valid version
        lastVersion = versions.lastKey();
    }
    Extension extension = repository.resolve(new ExtensionId(extensionId, lastVersion));
    // Get former ids versions
    Collection<ExtensionId> features = extension.getExtensionFeatures();
    for (ExtensionId feature : features) {
        try {
            getVersions(feature.getId(), repository, type, versions);
        } catch (ResolveException e) {
        // Ignore
        }
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    boolean needSave = false;
    XWikiDocument document = getExistingExtensionDocumentById(extensionId);
    if (document == null) {
        // Create document
        document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(), Arrays.asList("Extension", extension.getName()), "WebHome"), xcontext);
        for (int i = 1; !document.isNew(); ++i) {
            document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(), Arrays.asList("Extension", extension.getName() + ' ' + i), "WebHome"), xcontext);
        }
        document.readFromTemplate(this.currentResolver.resolve(XWikiRepositoryModel.EXTENSION_TEMPLATEREFERENCE), xcontext);
        needSave = true;
    }
    // Update document
    BaseObject extensionObject = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
    if (extensionObject == null) {
        extensionObject = document.newXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE, xcontext);
        needSave = true;
    }
    if (!StringUtils.equals(extensionId, getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_ID, (String) null))) {
        extensionObject.set(XWikiRepositoryModel.PROP_EXTENSION_ID, extensionId, xcontext);
        needSave = true;
    }
    // Update extension informations
    needSave |= updateExtension(extension, extensionObject, xcontext);
    // Proxy marker
    BaseObject extensionProxyObject = document.getXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE);
    if (extensionProxyObject == null) {
        extensionProxyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE, xcontext);
        extensionProxyObject.setIntValue(XWikiRepositoryModel.PROP_PROXY_AUTOUPDATE, 1);
        needSave = true;
    }
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYID, repository.getDescriptor().getId());
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYTYPE, repository.getDescriptor().getType());
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYURI, repository.getDescriptor().getURI().toString());
    // Remove unexisting versions
    Set<String> validVersions = new HashSet<String>();
    List<BaseObject> versionObjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
    if (versionObjects != null) {
        for (BaseObject versionObject : versionObjects) {
            if (versionObject != null) {
                String version = getValue(versionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);
                if (StringUtils.isBlank(version) || (isVersionProxyingEnabled(document) && !new DefaultVersion(version).equals(extension.getId().getVersion()))) {
                    // Empty version OR old versions should be proxied
                    document.removeXObject(versionObject);
                    needSave = true;
                } else {
                    if (!versions.containsKey(new DefaultVersion(version))) {
                        // The version does not exist on remote repository
                        if (!isVersionValid(document, versionObject, xcontext)) {
                            // The version is invalid, removing it to not make the whole extension invalid
                            document.removeXObject(versionObject);
                            needSave = true;
                        } else {
                            // The version is valid, lets keep it
                            validVersions.add(version);
                        }
                    } else {
                        // This version exist on remote repository
                        validVersions.add(version);
                    }
                }
            }
        }
    }
    List<BaseObject> dependencyObjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
    if (dependencyObjects != null) {
        for (BaseObject dependencyObject : dependencyObjects) {
            if (dependencyObject != null) {
                String version = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION);
                if (!validVersions.contains(version)) {
                    // The version is invalid, removing it to not make the whole extension invalid
                    document.removeXObject(dependencyObject);
                    needSave = true;
                }
            }
        }
    }
    for (Map.Entry<Version, String> entry : versions.entrySet()) {
        Version version = entry.getKey();
        String id = entry.getValue();
        try {
            Extension versionExtension;
            if (version.equals(extension.getId().getVersion())) {
                versionExtension = extension;
            } else if (isVersionProxyingEnabled(document)) {
                continue;
            } else {
                versionExtension = repository.resolve(new ExtensionId(id, version));
            }
            // Update version related informations
            needSave |= updateExtensionVersion(document, versionExtension);
        } catch (Exception e) {
            this.logger.error("Failed to resolve extension with id [" + id + "] and version [" + version + "] on repository [" + repository + "]", e);
        }
    }
    if (needSave) {
        document.setAuthorReference(xcontext.getUserReference());
        if (document.isNew()) {
            document.setContentAuthorReference(xcontext.getUserReference());
            document.setCreatorReference(xcontext.getUserReference());
        }
        xcontext.getWiki().saveDocument(document, "Imported extension [" + extensionId + "] from repository [" + repository.getDescriptor() + "]", true, xcontext);
    }
    return document.getDocumentReference();
}
Also used : ExtensionNotFoundException(org.xwiki.extension.ExtensionNotFoundException) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) XWikiContext(com.xpn.xwiki.XWikiContext) ExtensionId(org.xwiki.extension.ExtensionId) TreeMap(java.util.TreeMap) DefaultVersionConstraint(org.xwiki.extension.version.internal.DefaultVersionConstraint) XWikiException(com.xpn.xwiki.XWikiException) ComponentLifecycleException(org.xwiki.component.manager.ComponentLifecycleException) InitializationException(org.xwiki.component.phase.InitializationException) ExtensionNotFoundException(org.xwiki.extension.ExtensionNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) QueryException(org.xwiki.query.QueryException) ResolveException(org.xwiki.extension.ResolveException) CacheException(org.xwiki.cache.CacheException) BaseObject(com.xpn.xwiki.objects.BaseObject) Extension(org.xwiki.extension.Extension) ResolveException(org.xwiki.extension.ResolveException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Version(org.xwiki.extension.version.Version) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) Map(java.util.Map) TreeMap(java.util.TreeMap) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 7 with ExtensionId

use of org.xwiki.extension.ExtensionId in project xwiki-platform by xwiki.

the class AllTests method initExtensions.

public static void initExtensions(PersistentTestContext context) throws Exception {
    // Initialize extensions and repositories
    RepositoryTestUtils repositoryTestUtil = new RepositoryTestUtils(context.getUtil(), repositoryUtil, new SolrTestUtils(context.getUtil()));
    repositoryTestUtil.init();
    // Set integration repository and extension utils.
    context.getProperties().put(RepositoryTestUtils.PROPERTY_KEY, repositoryTestUtil);
    // Populate maven repository
    File extensionFile = repositoryTestUtil.getTestExtension(new ExtensionId("emptyjar", "1.0"), "jar").getFile().getFile();
    FileUtils.copyFile(extensionFile, new File(repositoryTestUtil.getRepositoryUtil().getMavenRepository(), "maven/extension/1.0/extension-1.0.jar"));
    FileUtils.copyFile(extensionFile, new File(repositoryTestUtil.getRepositoryUtil().getMavenRepository(), "maven/extension/2.0/extension-2.0.jar"));
    FileUtils.copyFile(extensionFile, new File(repositoryTestUtil.getRepositoryUtil().getMavenRepository(), "maven/oldextension/0.9/oldextension-0.9.jar"));
    FileUtils.copyFile(extensionFile, new File(repositoryTestUtil.getRepositoryUtil().getMavenRepository(), "maven/dependency/version/dependency-version.jar"));
}
Also used : SolrTestUtils(org.xwiki.repository.test.SolrTestUtils) RepositoryTestUtils(org.xwiki.repository.test.RepositoryTestUtils) ExtensionId(org.xwiki.extension.ExtensionId) File(java.io.File)

Example 8 with ExtensionId

use of org.xwiki.extension.ExtensionId in project xwiki-platform by xwiki.

the class RepositoryTest method testRestAccessToImportedExtension.

private void testRestAccessToImportedExtension() throws Exception {
    // 2.0
    TestExtension emptyExtension = getRepositoryTestUtils().getTestExtension(new ExtensionId("emptyjar", "1.0"), "jar");
    long fileSize = FileUtils.sizeOf(emptyExtension.getFile().getFile());
    ExtensionVersion extension = getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, "maven:extension", "2.0");
    Assert.assertEquals("maven:extension", extension.getId());
    Assert.assertEquals("jar", extension.getType());
    Assert.assertEquals("2.0", extension.getVersion());
    Assert.assertEquals("name", extension.getName());
    Assert.assertEquals("summary2", extension.getSummary());
    Assert.assertEquals("summary2\n      some more details", extension.getDescription());
    Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
    Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
    Assert.assertEquals(Arrays.asList("maven:oldextension", "maven:oldversionnedextension"), extension.getFeatures());
    Assert.assertEquals("maven:oldextension", extension.getExtensionFeatures().get(0).getId());
    Assert.assertEquals("2.0", extension.getExtensionFeatures().get(0).getVersion());
    Assert.assertEquals("maven:oldversionnedextension", extension.getExtensionFeatures().get(1).getId());
    Assert.assertEquals("10.0", extension.getExtensionFeatures().get(1).getVersion());
    Assert.assertEquals("GNU Lesser General Public License 2.1", extension.getLicenses().get(0).getName());
    Assert.assertEquals(fileSize, getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, "maven:extension", "2.0").length);
    // 1.0
    extension = getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, "maven:extension", "1.0");
    Assert.assertEquals("maven:extension", extension.getId());
    Assert.assertEquals("jar", extension.getType());
    Assert.assertEquals("1.0", extension.getVersion());
    Assert.assertEquals("name", extension.getName());
    Assert.assertEquals("summary2", extension.getSummary());
    Assert.assertEquals("summary2\n      some more details", extension.getDescription());
    Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
    Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
    Assert.assertEquals(Arrays.asList("maven:oldextension", "maven:oldversionnedextension"), extension.getFeatures());
    Assert.assertEquals("maven:oldextension", extension.getExtensionFeatures().get(0).getId());
    Assert.assertEquals("1.0", extension.getExtensionFeatures().get(0).getVersion());
    Assert.assertEquals("maven:oldversionnedextension", extension.getExtensionFeatures().get(1).getId());
    Assert.assertEquals("10.0", extension.getExtensionFeatures().get(1).getVersion());
    Assert.assertEquals("GNU Lesser General Public License 2.1", extension.getLicenses().get(0).getName());
    Assert.assertEquals(FileUtils.sizeOf(emptyExtension.getFile().getFile()), getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, "maven:extension", "1.0").length);
    // 0.9
    extension = getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, "maven:extension", "0.9");
    Assert.assertEquals("maven:extension", extension.getId());
    Assert.assertEquals("jar", extension.getType());
    Assert.assertEquals("0.9", extension.getVersion());
    Assert.assertEquals("name", extension.getName());
    Assert.assertEquals("summary2", extension.getSummary());
    Assert.assertEquals("summary2\n      some more details", extension.getDescription());
    Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
    Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
    Assert.assertEquals(Arrays.asList(), extension.getFeatures());
    Assert.assertEquals(Arrays.asList(), extension.getExtensionFeatures());
    Assert.assertEquals("GNU Lesser General Public License 2.1", extension.getLicenses().get(0).getName());
    Assert.assertEquals(fileSize, getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, "maven:extension", "0.9").length);
}
Also used : TestExtension(org.xwiki.repository.test.TestExtension) ExtensionId(org.xwiki.extension.ExtensionId) ExtensionVersion(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersion)

Example 9 with ExtensionId

use of org.xwiki.extension.ExtensionId in project xwiki-platform by xwiki.

the class WikiCopiedEventListenerTest method setUp.

@Before
public void setUp() throws Exception {
    this.localExtensionRepository = this.repositoryUtil.getComponentManager().getInstance(LocalExtensionRepository.class);
    this.installedExtensionRepository = this.repositoryUtil.getComponentManager().getInstance(InstalledExtensionRepository.class);
    this.observation = this.repositoryUtil.getComponentManager().getInstance(ObservationManager.class);
    // Extensions
    EmptyExtension extension = new EmptyExtension(new ExtensionId("id", "version"), "xar");
    EmptyExtension extensionDependency = new EmptyExtension(new ExtensionId("dependency", "version"), "xar");
    extension.addDependency(new DefaultExtensionDependency(extensionDependency.getId().getId(), new DefaultVersionConstraint(null, extensionDependency.getId().getVersion())));
    this.localExtensionDependency1 = this.localExtensionRepository.storeExtension(extensionDependency);
    this.localExtension1 = this.localExtensionRepository.storeExtension(extension);
}
Also used : EmptyExtension(org.xwiki.extension.test.EmptyExtension) DefaultVersionConstraint(org.xwiki.extension.version.internal.DefaultVersionConstraint) DefaultLocalExtensionRepository(org.xwiki.extension.repository.internal.local.DefaultLocalExtensionRepository) LocalExtensionRepository(org.xwiki.extension.repository.LocalExtensionRepository) ObservationManager(org.xwiki.observation.ObservationManager) ExtensionId(org.xwiki.extension.ExtensionId) DefaultExtensionDependency(org.xwiki.extension.DefaultExtensionDependency) InstalledExtensionRepository(org.xwiki.extension.repository.InstalledExtensionRepository) Before(org.junit.Before)

Example 10 with ExtensionId

use of org.xwiki.extension.ExtensionId in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    // mock
    this.contextUser = new DocumentReference(getXWikiContext().getWikiId(), "XWiki", "ExtensionUser");
    this.localXarExtensiontId1 = new ExtensionId("test", "1.0");
    this.localXarExtensiontId2 = new ExtensionId("test", "2.0");
    this.collisionextension1 = new ExtensionId("collisionextension1", "version");
    this.collisionextension2 = new ExtensionId("collisionextension2", "version");
    // classes
    BaseClass styleSheetClass = new BaseClass();
    this.classes.put("StyleSheetExtension", styleSheetClass);
    // checking
    doReturn(true).when(this.oldcore.getSpyXWiki()).hasAttachmentRecycleBin(any(XWikiContext.class));
    getXWikiContext().setUserReference(this.contextUser);
    ((XWikiStubContextProvider) this.componentManager.getInstance(XWikiStubContextProvider.class)).initialize(getXWikiContext());
    CoreConfiguration coreConfiguration = this.componentManager.getInstance(CoreConfiguration.class);
    doReturn(Syntax.PLAIN_1_0).when(coreConfiguration).getDefaultDocumentSyntax();
    // lookup
    this.jobExecutor = this.componentManager.getInstance(JobExecutor.class);
    this.xarExtensionRepository = this.componentManager.getInstance(InstalledExtensionRepository.class, XarExtensionHandler.TYPE);
    this.observation = this.repositoryUtil.getComponentManager().getInstance(ObservationManager.class);
    // Get rid of wiki macro listener
    this.componentManager.<ObservationManager>getInstance(ObservationManager.class).removeListener("RegisterMacrosOnImportListener");
    this.installedExtensionRepository = this.componentManager.getInstance(InstalledExtensionRepository.class, "xar");
    // Programming right is not required for XAR extensions
    doThrow(AccessDeniedException.class).when(this.oldcore.getMockAuthorizationManager()).checkAccess(eq(Right.PROGRAM), any(), any());
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) JobExecutor(org.xwiki.job.JobExecutor) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) ExtensionId(org.xwiki.extension.ExtensionId) ObservationManager(org.xwiki.observation.ObservationManager) CoreConfiguration(com.xpn.xwiki.CoreConfiguration) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XarInstalledExtensionRepository(org.xwiki.extension.xar.internal.repository.XarInstalledExtensionRepository) InstalledExtensionRepository(org.xwiki.extension.repository.InstalledExtensionRepository) Before(org.junit.Before)

Aggregations

ExtensionId (org.xwiki.extension.ExtensionId)67 Test (org.junit.Test)37 InstalledExtension (org.xwiki.extension.InstalledExtension)13 ExtensionAdministrationPage (org.xwiki.extension.test.po.ExtensionAdministrationPage)11 TestExtension (org.xwiki.repository.test.TestExtension)11 ExtensionPane (org.xwiki.extension.test.po.ExtensionPane)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 InstalledExtensionRepository (org.xwiki.extension.repository.InstalledExtensionRepository)8 Extension (org.xwiki.extension.Extension)7 InstallRequest (org.xwiki.extension.job.InstallRequest)6 DefaultVersionConstraint (org.xwiki.extension.version.internal.DefaultVersionConstraint)6 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)6 IOException (java.io.IOException)5 DefaultExtensionDependency (org.xwiki.extension.DefaultExtensionDependency)5 SearchResultsPane (org.xwiki.extension.test.po.SearchResultsPane)5 ViewPage (org.xwiki.test.ui.po.ViewPage)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 ResolveException (org.xwiki.extension.ResolveException)4 DependencyPane (org.xwiki.extension.test.po.DependencyPane)4 LogItemPane (org.xwiki.extension.test.po.LogItemPane)4