Search in sources :

Example 1 with ExtensionRepository

use of org.xwiki.extension.repository.ExtensionRepository in project xwiki-platform by xwiki.

the class ExtensionRatingScriptService method getRating.

/**
 * @param extensionId the extension id
 * @param extensionVersion the extension version
 * @return the rating of an extension
 */
public ExtensionRating getRating(String extensionId, String extensionVersion) {
    setError(null);
    Collection<ExtensionRepository> repositories = getRepositories();
    for (ExtensionRepository repository : repositories) {
        if (repository instanceof Ratable) {
            try {
                setError(null);
                return ((Ratable) repository).getRating(extensionId, extensionVersion);
            } catch (ResolveException e) {
                setError(e);
                // Keep looking. Maybe there's another repository with the same extension.
                continue;
            }
        }
    }
    return null;
}
Also used : ResolveException(org.xwiki.extension.ResolveException) ExtensionRepository(org.xwiki.extension.repository.ExtensionRepository) Ratable(org.xwiki.extension.repository.rating.Ratable)

Example 2 with ExtensionRepository

use of org.xwiki.extension.repository.ExtensionRepository in project xwiki-platform by xwiki.

the class RepositoryManager method resolveExtensionVersion.

/**
 * This method factually resolves extension from remote source (when it's possible). Call it only when the data that
 * is going to be obtained cannot be got from extension document xobject
 *
 * @since 9.5RC1
 */
public Extension resolveExtensionVersion(XWikiDocument extensionDocument, String extensionVersion) throws ResolveException {
    BaseObject extensionObject = extensionDocument.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
    if (extensionObject == null) {
        return null;
    }
    String extensionId = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_ID, (String) null);
    BaseObject extensionProxyObject = extensionDocument.getXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE);
    if (extensionProxyObject == null) {
        return null;
    }
    String repositoryId = getValue(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYID, (String) null);
    if (extensionId == null || repositoryId == null) {
        return null;
    }
    ExtensionRepository repository = this.extensionRepositoryManager.getRepository(repositoryId);
    if (isGivenVersionOneOfExtensionVersions(repository, extensionId, extensionVersion)) {
        return repository.resolve(new ExtensionId(extensionId, extensionVersion));
    } else {
        return tryToResolveExtensionFromExtensionFeatures(repository, extensionObject, extensionVersion);
    }
}
Also used : ExtensionRepository(org.xwiki.extension.repository.ExtensionRepository) ExtensionId(org.xwiki.extension.ExtensionId) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with ExtensionRepository

use of org.xwiki.extension.repository.ExtensionRepository 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

ExtensionRepository (org.xwiki.extension.repository.ExtensionRepository)3 ExtensionId (org.xwiki.extension.ExtensionId)2 ResolveException (org.xwiki.extension.ResolveException)2 XWikiException (com.xpn.xwiki.XWikiException)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Disposition (org.restlet.data.Disposition)1 InputRepresentation (org.restlet.representation.InputRepresentation)1 Extension (org.xwiki.extension.Extension)1 ExtensionFile (org.xwiki.extension.ExtensionFile)1 DefaultExtensionRepositoryDescriptor (org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor)1 ExtensionRepositoryDescriptor (org.xwiki.extension.repository.ExtensionRepositoryDescriptor)1 ExtensionRepositoryFactory (org.xwiki.extension.repository.ExtensionRepositoryFactory)1 Ratable (org.xwiki.extension.repository.rating.Ratable)1 QueryException (org.xwiki.query.QueryException)1