Search in sources :

Example 6 with ResolveException

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

the class ExtensionInstanceOutputFilterStream method endExtension.

@Override
public void endExtension(String id, String version, FilterEventParameters parameters) throws FilterException {
    // TODO: add support for complete extension
    ExtensionId extensionId = new ExtensionId(id, factory.getVersion(version));
    try {
        LocalExtension localExtension = this.localRepository.getLocalExtension(extensionId);
        if (localExtension == null) {
            Extension extension;
            try {
                // Try to find and download the extension from a repository
                extension = this.extensionRepository.resolve(extensionId);
            } catch (ResolveException e) {
                this.logger.debug("Can't find extension [{}]", extensionId, e);
                // FIXME: Create a dummy extension. Need support for partial/lazy extension.
                return;
            }
            localExtension = this.localRepository.storeExtension(extension);
        }
        String namespace = getCurrentNamespace();
        // TODO: should probably make it configurable
        if (installedRepository.getInstalledExtension(localExtension.getId().getId(), namespace) == null) {
            for (ExtensionId feature : localExtension.getExtensionFeatures()) {
                if (installedRepository.getInstalledExtension(feature.getId(), namespace) != null) {
                    // Already exist so don't register it or it could create a mess
                    return;
                }
            }
        } else {
            return;
        }
        // Register the extension as installed
        installedRepository.installExtension(localExtension, namespace, false);
    } catch (Exception e) {
        this.logger.error("Failed to register extenion [{}] from the XAR", extensionId, e);
    }
}
Also used : Extension(org.xwiki.extension.Extension) LocalExtension(org.xwiki.extension.LocalExtension) ResolveException(org.xwiki.extension.ResolveException) LocalExtension(org.xwiki.extension.LocalExtension) ExtensionId(org.xwiki.extension.ExtensionId) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) FilterException(org.xwiki.filter.FilterException)

Example 7 with ResolveException

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

the class FlavorSearchJob method findValidVersion.

private Extension findValidVersion(String flavorId, String namespace) {
    IterableResult<Version> versions;
    try {
        versions = this.repositoryManager.resolveVersions(flavorId, 0, -1);
        if (versions.getSize() == 0) {
            this.logger.debug("Could not find any version for the flavor extension [{}]", flavorId);
            return null;
        }
        List<Version> versionList = new ArrayList<Version>(versions.getSize());
        for (Version version : versions) {
            versionList.add(version);
        }
        return findValidVersion(flavorId, namespace, versionList);
    } catch (ResolveException e) {
        this.logger.debug("Failed to resolve versions for extension id [{}]", flavorId, e);
    }
    return null;
}
Also used : ResolveException(org.xwiki.extension.ResolveException) Version(org.xwiki.extension.version.Version) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 8 with ResolveException

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

the class Package method registerExtension.

private void registerExtension(XWikiContext context) {
    // Register the package as extension if it's one
    if (isInstallExtension() && StringUtils.isNotEmpty(getExtensionId()) && StringUtils.isNotEmpty(getVersion())) {
        ExtensionId extensionId = new ExtensionId(getExtensionId(), getVersion());
        try {
            LocalExtensionRepository localRepository = Utils.getComponent(LocalExtensionRepository.class);
            LocalExtension localExtension = localRepository.getLocalExtension(extensionId);
            if (localExtension == null) {
                Extension extension;
                try {
                    // Try to find and download the extension from a repository
                    extension = Utils.getComponent(ExtensionRepositoryManager.class).resolve(extensionId);
                } catch (ResolveException e) {
                    LOGGER.debug("Can't find extension [{}]", extensionId, e);
                    // FIXME: Create a dummy extension. Need support for partial/lazy extension.
                    return;
                }
                localExtension = localRepository.storeExtension(extension);
            }
            InstalledExtensionRepository installedRepository = Utils.getComponent(InstalledExtensionRepository.class);
            String namespace = "wiki:" + context.getWikiId();
            // Make sure it's not already there
            if (installedRepository.getInstalledExtension(localExtension.getId().getId(), namespace) == null) {
                for (ExtensionId feature : localExtension.getExtensionFeatures()) {
                    if (installedRepository.getInstalledExtension(feature.getId(), namespace) != null) {
                        // Already exist so don't register it or it could create a mess
                        return;
                    }
                }
            } else {
                return;
            }
            // Register the extension as installed
            InstalledExtension installedExtension = installedRepository.installExtension(localExtension, namespace, false);
            // Tell the world about it
            Utils.getComponent(ObservationManager.class).notify(new ExtensionInstalledEvent(installedExtension.getId(), namespace), installedExtension);
        } catch (Exception e) {
            LOGGER.error("Failed to register extenion [{}] from the XAR", extensionId, e);
        }
    }
}
Also used : InstalledExtension(org.xwiki.extension.InstalledExtension) Extension(org.xwiki.extension.Extension) LocalExtension(org.xwiki.extension.LocalExtension) ResolveException(org.xwiki.extension.ResolveException) InstalledExtension(org.xwiki.extension.InstalledExtension) LocalExtensionRepository(org.xwiki.extension.repository.LocalExtensionRepository) LocalExtension(org.xwiki.extension.LocalExtension) ExtensionId(org.xwiki.extension.ExtensionId) ObservationManager(org.xwiki.observation.ObservationManager) InstalledExtensionRepository(org.xwiki.extension.repository.InstalledExtensionRepository) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) ExtensionInstalledEvent(org.xwiki.extension.event.ExtensionInstalledEvent)

Example 9 with ResolveException

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

the class RepositoryManager method isVersionValid.

private boolean isVersionValid(XWikiDocument document, BaseObject extensionVersionObject, XWikiContext context) {
    // Has a version
    String extensionVersion = getValue(extensionVersionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);
    if (StringUtils.isBlank(extensionVersion)) {
        this.logger.debug("No actual version provided for object [{}({})]", XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE, extensionVersionObject.getNumber());
        return false;
    }
    boolean valid;
    ResourceReference resourceReference = null;
    try {
        resourceReference = getDownloadReference(document, extensionVersion);
    } catch (ResolveException e) {
        logger.debug("Cannot obtain download source reference for object [{}({})]", XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE, extensionVersionObject.getNumber());
        return false;
    }
    if (resourceReference != null) {
        if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
            AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), document.getDocumentReference());
            XWikiDocument attachmentDocument;
            try {
                attachmentDocument = context.getWiki().getDocument(attachmentReference.getDocumentReference(), context);
                valid = attachmentDocument.getAttachment(attachmentReference.getName()) != null;
            } catch (XWikiException e) {
                this.logger.error("Failed to get document [{}]", attachmentReference.getDocumentReference(), e);
                valid = false;
            }
            if (!valid) {
                this.logger.debug("Attachment [{}] does not exists", attachmentReference);
            }
        } else if (ResourceType.URL.equals(resourceReference.getType()) || ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
            valid = true;
        } else {
            valid = false;
            this.logger.debug("Unknown resource type [{}]", resourceReference.getType());
        }
    } else {
        valid = false;
        this.logger.debug("No actual download provided for object [{}({})]", XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE, extensionVersionObject.getNumber());
    }
    return valid;
}
Also used : ResolveException(org.xwiki.extension.ResolveException) AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 10 with ResolveException

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

the class ExtensionVersionFileRESTResource method downloadRemoteExtension.

private ResponseBuilder downloadRemoteExtension(ExtensionResourceReference extensionResource) throws ResolveException, IOException {
    ExtensionRepository repository = null;
    if (extensionResource.getRepositoryId() != null) {
        repository = this.extensionRepositoryManager.getRepository(extensionResource.getRepositoryId());
    }
    if (repository == null && extensionResource.getRepositoryType() != null && extensionResource.getRepositoryURI() != null) {
        ExtensionRepositoryDescriptor repositoryDescriptor = new DefaultExtensionRepositoryDescriptor("tmp", extensionResource.getRepositoryType(), extensionResource.getRepositoryURI());
        try {
            ExtensionRepositoryFactory repositoryFactory = this.componentManager.getInstance(ExtensionRepositoryFactory.class, repositoryDescriptor.getType());
            repository = repositoryFactory.createRepository(repositoryDescriptor);
        } catch (Exception e) {
            // Ignore invalid repository
            getLogger().warn("Invalid repository in download link [{}]", extensionResource);
        }
    }
    // Resolve extension
    Extension downloadExtension;
    if (repository == null) {
        downloadExtension = this.extensionRepositoryManager.resolve(new ExtensionId(extensionResource.getExtensionId(), extensionResource.getExtensionVersion()));
    } else {
        downloadExtension = repository.resolve(new ExtensionId(extensionResource.getExtensionId(), extensionResource.getExtensionVersion()));
    }
    // Get file
    // TODO: find media type
    ExtensionFile extensionFile = downloadExtension.getFile();
    long length = extensionFile.getLength();
    // TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
    // Should probably use javax.ws.rs.ext.MessageBodyWriter
    InputRepresentation content = new InputRepresentation(extensionFile.openStream(), MediaType.ALL, length);
    Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
    disposition.setFilename(downloadExtension.getId().toString() + '.' + downloadExtension.getType());
    content.setDisposition(disposition);
    ResponseBuilder response = Response.ok();
    response.entity(content);
    return response;
}
Also used : Extension(org.xwiki.extension.Extension) InputRepresentation(org.restlet.representation.InputRepresentation) ExtensionRepository(org.xwiki.extension.repository.ExtensionRepository) ExtensionRepositoryFactory(org.xwiki.extension.repository.ExtensionRepositoryFactory) Disposition(org.restlet.data.Disposition) ExtensionId(org.xwiki.extension.ExtensionId) DefaultExtensionRepositoryDescriptor(org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor) ExtensionFile(org.xwiki.extension.ExtensionFile) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) XWikiException(com.xpn.xwiki.XWikiException) URISyntaxException(java.net.URISyntaxException) WebApplicationException(javax.ws.rs.WebApplicationException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) ExtensionRepositoryDescriptor(org.xwiki.extension.repository.ExtensionRepositoryDescriptor) DefaultExtensionRepositoryDescriptor(org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor)

Aggregations

ResolveException (org.xwiki.extension.ResolveException)10 XWikiException (com.xpn.xwiki.XWikiException)6 Extension (org.xwiki.extension.Extension)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 IOException (java.io.IOException)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 ExtensionId (org.xwiki.extension.ExtensionId)4 QueryException (org.xwiki.query.QueryException)4 BaseObject (com.xpn.xwiki.objects.BaseObject)3 LocalExtension (org.xwiki.extension.LocalExtension)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 URISyntaxException (java.net.URISyntaxException)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 Disposition (org.restlet.data.Disposition)2 InputRepresentation (org.restlet.representation.InputRepresentation)2 InstalledExtension (org.xwiki.extension.InstalledExtension)2 ExtensionRepository (org.xwiki.extension.repository.ExtensionRepository)2 Version (org.xwiki.extension.version.Version)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1