Search in sources :

Example 1 with StoreURIMatcher

use of org.commonjava.indy.dotmaven.util.StoreURIMatcher in project indy by Commonjava.

the class ArtifactStoreSubStore method createFolder.

@Override
public void createFolder(final ITransaction transaction, final String folderUri) throws WebdavException {
    final StoreURIMatcher matcher = new StoreURIMatcher(folderUri);
    if (!matcher.hasStorePath()) {
        throw new WebdavException("No store-level path specified: '" + folderUri + "'. This URI references either a list of stores, a root store directory, or something else equally read-only.");
    }
    final StorageAdvice advice = getStorageAdviceFor(matcher);
    final String path = matcher.getStorePath();
    final Transfer item = fileManager.getStorageReference(advice.getHostedStore(), path);
    try {
        item.mkdirs();
    } catch (final IOException e) {
        logger.error("Failed to create folder: {} in store: {}. Reason: {}", e, path, advice.getStore().getKey(), e.getMessage());
        throw new WebdavException("Failed to create folder: " + folderUri);
    }
}
Also used : StoreURIMatcher(org.commonjava.indy.dotmaven.util.StoreURIMatcher) StorageAdvice(org.commonjava.indy.dotmaven.data.StorageAdvice) WebdavException(net.sf.webdav.exceptions.WebdavException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException)

Example 2 with StoreURIMatcher

use of org.commonjava.indy.dotmaven.util.StoreURIMatcher in project indy by Commonjava.

the class ArtifactStoreSubStore method getStoredObject.

@Override
public StoredObject getStoredObject(final ITransaction transaction, final String uri) throws WebdavException {
    final StoredObject so = new StoredObject();
    final StoreURIMatcher matcher = new StoreURIMatcher(uri);
    if (matcher.hasStorePath()) {
        final Transfer item = getTransfer(matcher);
        if (item == null) {
            return null;
        }
        so.setCreationDate(new Date(item.lastModified()));
        so.setLastModified(new Date(item.lastModified()));
        so.setFolder(item.isDirectory());
        so.setResourceLength(item.length());
    } else {
        final Date d = new Date();
        so.setCreationDate(d);
        so.setLastModified(d);
        so.setFolder(true);
    }
    return so;
}
Also used : StoredObject(net.sf.webdav.StoredObject) StoreURIMatcher(org.commonjava.indy.dotmaven.util.StoreURIMatcher) Transfer(org.commonjava.maven.galley.model.Transfer) Date(java.util.Date)

Example 3 with StoreURIMatcher

use of org.commonjava.indy.dotmaven.util.StoreURIMatcher in project indy by Commonjava.

the class ArtifactStoreSubStore method setResourceContent.

@Override
public long setResourceContent(final ITransaction transaction, final String resourceUri, final InputStream content, final String contentType, final String characterEncoding) throws WebdavException {
    final StoreURIMatcher matcher = new StoreURIMatcher(resourceUri);
    if (!matcher.hasStorePath()) {
        throw new WebdavException("No store-level path specified: '" + resourceUri + "'. This URI references either a list of stores, a root store directory, or something else equally read-only.");
    }
    final StorageAdvice advice = getStorageAdviceFor(matcher);
    final String path = matcher.getStorePath();
    final Transfer item = fileManager.getStorageReference(advice.getHostedStore(), path);
    Writer writer = null;
    try {
        if (characterEncoding != null) {
            writer = new OutputStreamWriter(item.openOutputStream(TransferOperation.UPLOAD), characterEncoding);
        } else {
            writer = new OutputStreamWriter(item.openOutputStream(TransferOperation.UPLOAD));
        }
        copy(content, writer);
        return item.length();
    } catch (final IOException e) {
        logger.error("Failed to write file: {} in store: {}. Reason: {}", e, path, advice.getStore().getKey(), e.getMessage());
        throw new WebdavException("Failed to write file: " + resourceUri);
    } finally {
        closeQuietly(writer);
    }
}
Also used : StoreURIMatcher(org.commonjava.indy.dotmaven.util.StoreURIMatcher) StorageAdvice(org.commonjava.indy.dotmaven.data.StorageAdvice) WebdavException(net.sf.webdav.exceptions.WebdavException) Transfer(org.commonjava.maven.galley.model.Transfer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 4 with StoreURIMatcher

use of org.commonjava.indy.dotmaven.util.StoreURIMatcher in project indy by Commonjava.

the class ArtifactStoreSubStore method removeObject.

@Override
public void removeObject(final ITransaction transaction, final String uri) throws WebdavException {
    final StoreURIMatcher matcher = new StoreURIMatcher(uri);
    if (!matcher.hasStorePath()) {
        throw new WebdavException("No store-level path specified: '" + uri + "'. This URI references either a list of stores, a root store directory, or something else equally read-only.");
    }
    final StorageAdvice advice = getStorageAdviceFor(matcher);
    final String path = matcher.getStorePath();
    final Transfer item = fileManager.getStorageReference(advice.getHostedStore(), path);
    try {
        if (item.exists()) {
            item.delete();
        }
    } catch (final IOException e) {
        logger.error("Failed to delete file: {} in store: {}. Reason: {}", e, path, advice.getStore().getKey(), e.getMessage());
        throw new WebdavException("Failed to delete file: " + uri);
    }
}
Also used : StoreURIMatcher(org.commonjava.indy.dotmaven.util.StoreURIMatcher) StorageAdvice(org.commonjava.indy.dotmaven.data.StorageAdvice) WebdavException(net.sf.webdav.exceptions.WebdavException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException)

Example 5 with StoreURIMatcher

use of org.commonjava.indy.dotmaven.util.StoreURIMatcher in project indy by Commonjava.

the class ArtifactStoreSubStore method getChildrenNames.

@Override
public String[] getChildrenNames(final ITransaction transaction, final String folderUri) throws WebdavException {
    String[] names;
    final StoreURIMatcher matcher = new StoreURIMatcher(folderUri);
    if (matcher.hasStorePath() || matcher.hasStoreName()) {
        String path = matcher.getStorePath();
        if (isEmpty(path)) {
            path = PathUtils.ROOT;
        }
        final StoreKey key = matcher.getStoreKey();
        try {
            if (key != null && StoreType.group == key.getType()) {
                final List<ArtifactStore> stores = indy.query().packageType(key.getPackageType()).getOrderedStoresInGroup(key.getName());
                final Set<String> noms = new TreeSet<>();
                for (final ArtifactStore store : stores) {
                    final Transfer item = fileManager.getStorageReference(store, path);
                    if (!item.exists()) {
                        continue;
                    }
                    if (!item.isDirectory()) {
                        logger.error("Transfer: {} in {} is not a directory.", path, store.getKey());
                        continue;
                    }
                    noms.addAll(Arrays.asList(item.list()));
                }
                names = noms.toArray(new String[noms.size()]);
            } else {
                final ArtifactStore store = indy.getArtifactStore(key);
                if (store == null) {
                    logger.error("Cannot find ArtifactStore to match key: {}.", key);
                    names = new String[] {};
                } else {
                    final Transfer item = fileManager.getStorageReference(store, path);
                    if (!item.exists() || !item.isDirectory()) {
                        logger.error("Transfer: {} in {} is not a directory.", path, store.getKey());
                        names = new String[] {};
                    } else {
                        names = item.list();
                    }
                }
            }
        } catch (final IndyDataException e) {
            logger.error(String.format("Failed to lookup ArtifactStore(s) for key: %s. Reason: %s", key, e.getMessage()), e);
            throw new WebdavException("Failed to get listing for: " + folderUri);
        } catch (final IOException e) {
            logger.error(String.format("Failed to list %s in %s. Reason: %s", path, key, e.getMessage()), e);
            throw new WebdavException("Failed to get listing for: " + folderUri);
        }
    } else if (matcher.hasStoreType()) {
        String packageType = matcher.getPackageType();
        final StoreType type = matcher.getStoreType();
        try {
            List<String> noms = indy.query().packageType(packageType).storeTypes(type).stream().map(ArtifactStore::getName).collect(Collectors.toList());
            names = noms.toArray(new String[noms.size()]);
        } catch (final IndyDataException e) {
            logger.error(String.format("Failed to lookup ArtifactStores of type: %s. Reason: %s", type, e.getMessage()), e);
            throw new WebdavException("Failed to get listing for: " + folderUri);
        }
    } else {
        names = new String[] { StoreType.hosted.singularEndpointName(), StoreType.group.singularEndpointName(), StoreType.remote.singularEndpointName() };
    }
    return names;
}
Also used : StoreURIMatcher(org.commonjava.indy.dotmaven.util.StoreURIMatcher) WebdavException(net.sf.webdav.exceptions.WebdavException) IOException(java.io.IOException) StoreKey(org.commonjava.indy.model.core.StoreKey) IndyDataException(org.commonjava.indy.data.IndyDataException) StoreType(org.commonjava.indy.model.core.StoreType) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) TreeSet(java.util.TreeSet) Transfer(org.commonjava.maven.galley.model.Transfer) List(java.util.List)

Aggregations

StoreURIMatcher (org.commonjava.indy.dotmaven.util.StoreURIMatcher)7 Transfer (org.commonjava.maven.galley.model.Transfer)7 IOException (java.io.IOException)6 WebdavException (net.sf.webdav.exceptions.WebdavException)6 StorageAdvice (org.commonjava.indy.dotmaven.data.StorageAdvice)4 StoreKey (org.commonjava.indy.model.core.StoreKey)2 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Date (java.util.Date)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 StoredObject (net.sf.webdav.StoredObject)1 IndyDataException (org.commonjava.indy.data.IndyDataException)1 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)1 StoreType (org.commonjava.indy.model.core.StoreType)1