use of org.xwiki.extension.repository.ExtensionRepositoryFactory 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;
}
Aggregations