Search in sources :

Example 6 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class AdminController method store.

public boolean store(final ArtifactStore store, final String user, final boolean skipExisting) throws IndyWorkflowException {
    try {
        String changelog = store.getMetadata(ArtifactStore.METADATA_CHANGELOG);
        if (changelog == null) {
            changelog = "Changelog not provided";
        }
        final ChangeSummary summary = new ChangeSummary(user, changelog);
        logger.info("Persisting artifact store: {} using: {}", store, storeManager);
        return storeManager.storeArtifactStore(store, summary, skipExisting, true, new EventMetadata());
    } catch (final IndyDataException e) {
        throw new IndyWorkflowException(ApplicationStatus.SERVER_ERROR.code(), "Failed to store: {}. Reason: {}", e, store.getKey(), e.getMessage());
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 7 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class ContentController method deleteAll.

public void deleteAll(final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    try {
        final List<ArtifactStore> stores = storeManager.query().concreteStores().getAll();
        contentManager.deleteAll(stores, path, eventMetadata);
    } catch (final IndyDataException e) {
        throw new IndyWorkflowException(ApplicationStatus.SERVER_ERROR.code(), "Failed to retrieve list of concrete stores. Reason: {}", e, e.getMessage());
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException)

Example 8 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class ContentController method renderListing.

public String renderListing(final String acceptHeader, final StoreKey key, final String requestPath, final String serviceUrl, final UriFormatter uriFormatter) throws IndyWorkflowException {
    String path = requestPath;
    if (path.endsWith(LISTING_HTML_FILE)) {
        path = normalize(parentPath(path));
    }
    final List<StoreResource> listed = getListing(key, path);
    if (ApplicationContent.application_json.equals(acceptHeader)) {
        final DirectoryListingDTO dto = new DirectoryListingDTO(StoreResource.convertToEntries(listed));
        try {
            return mapper.writeValueAsString(dto);
        } catch (final JsonProcessingException e) {
            throw new IndyWorkflowException("Failed to render listing to JSON: %s. Reason: %s", e, dto, e.getMessage());
        }
    }
    final Map<String, Set<String>> listingUrls = new TreeMap<>();
    final String storeUrl = uriFormatter.formatAbsolutePathTo(serviceUrl, key.getType().singularEndpointName(), key.getName());
    if (listed != null) {
        // second pass, process the remainder.
        for (int pass = 0; pass < 2; pass++) {
            for (final ConcreteResource res : listed) {
                String p = res.getPath();
                if (pass == 0 && !p.endsWith("/")) {
                    continue;
                } else if (pass == 1) {
                    if (!p.endsWith("/")) {
                        final String dirpath = p + "/";
                        if (listingUrls.containsKey(normalize(storeUrl, dirpath))) {
                            p = dirpath;
                        }
                    } else {
                        continue;
                    }
                }
                final String localUrl = normalize(storeUrl, p);
                Set<String> sources = listingUrls.get(localUrl);
                if (sources == null) {
                    sources = new HashSet<>();
                    listingUrls.put(localUrl, sources);
                }
                sources.add(normalize(res.getLocationUri(), res.getPath()));
            }
        }
    }
    final List<String> sources = new ArrayList<>();
    if (listed != null) {
        for (final ConcreteResource res : listed) {
            // KeyedLocation is all we use in Indy.
            logger.debug("Formatting sources URL for: {}", res);
            final KeyedLocation kl = (KeyedLocation) res.getLocation();
            final String uri = uriFormatter.formatAbsolutePathTo(serviceUrl, kl.getKey().getType().singularEndpointName(), kl.getKey().getName());
            if (!sources.contains(uri)) {
                logger.debug("adding source URI: '{}'", uri);
                sources.add(uri);
            }
        }
    }
    Collections.sort(sources);
    String parentPath = normalize(parentPath(path));
    if (!parentPath.endsWith("/")) {
        parentPath += "/";
    }
    final String parentUrl;
    if (parentPath.equals(path)) {
        parentPath = null;
        parentUrl = null;
    } else {
        parentUrl = uriFormatter.formatAbsolutePathTo(serviceUrl, key.getType().singularEndpointName(), key.getName(), parentPath);
    }
    final Map<String, Object> params = new HashMap<>();
    params.put("items", listingUrls);
    params.put("parentUrl", parentUrl);
    params.put("parentPath", parentPath);
    params.put("path", path);
    params.put("storeKey", key);
    params.put("storeUrl", storeUrl);
    params.put("baseUrl", serviceUrl);
    params.put("sources", sources);
    // render...
    try {
        return templates.render(acceptHeader, "directory-listing", params);
    } catch (final IndyGroovyException e) {
        throw new IndyWorkflowException(e.getMessage(), e);
    }
}
Also used : DirectoryListingDTO(org.commonjava.indy.model.core.dto.DirectoryListingDTO) HashSet(java.util.HashSet) Set(java.util.Set) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException) StoreResource(org.commonjava.indy.content.StoreResource) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 9 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class ContentController method isHtmlContent.

public boolean isHtmlContent(final Transfer item) throws IndyWorkflowException {
    final byte[] head = new byte[MAX_PEEK_BYTES];
    BufferedReader reader = null;
    InputStream raw = null;
    try {
        raw = item.openInputStream(false);
        final int read = raw.read(head);
        if (read > 0) {
            reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(head)));
            String line;
            int count = 0;
            while ((line = reader.readLine()) != null && count < MAX_PEEK_COUNT) {
                if (line.matches(HTML_TAG_PATTERN)) {
                    return true;
                }
                count++;
            }
        }
    } catch (final IOException e) {
        throw new IndyWorkflowException("Cannot read: %s. Reason: %s", e, item, e.getMessage());
    } finally {
        closeQuietly(raw);
        closeQuietly(reader);
    }
    return false;
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 10 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class NfcController method getMissing.

public NotFoundCacheDTO getMissing(final StoreKey key) throws IndyWorkflowException {
    final NotFoundCacheDTO dto = new NotFoundCacheDTO();
    if (key.getType() == group) {
        List<ArtifactStore> stores;
        try {
            stores = storeManager.query().packageType(key.getPackageType()).getOrderedConcreteStoresInGroup(key.getName());
        } catch (final IndyDataException e) {
            throw new IndyWorkflowException("Failed to retrieve concrete constituent ArtifactStores for: %s.", e, key);
        }
        final List<? extends KeyedLocation> locations = toLocations(stores);
        for (final KeyedLocation location : locations) {
            final Set<String> missing = cache.getMissing(location);
            if (missing != null && !missing.isEmpty()) {
                final List<String> paths = new ArrayList<String>(missing);
                Collections.sort(paths);
                dto.addSection(location.getKey(), paths);
            }
        }
    } else {
        ArtifactStore store;
        try {
            store = storeManager.getArtifactStore(key);
        } catch (final IndyDataException e) {
            throw new IndyWorkflowException("Failed to retrieve ArtifactStore: %s.", e, key);
        }
        if (store != null) {
            final Set<String> missing = cache.getMissing(toLocation(store));
            final List<String> paths = new ArrayList<String>(missing);
            Collections.sort(paths);
            dto.addSection(key, paths);
        }
    }
    return dto;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArrayList(java.util.ArrayList) NotFoundCacheDTO(org.commonjava.indy.model.core.dto.NotFoundCacheDTO)

Aggregations

IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)109 Response (javax.ws.rs.core.Response)40 Transfer (org.commonjava.maven.galley.model.Transfer)39 IOException (java.io.IOException)36 ResponseUtils.formatResponse (org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse)36 StoreKey (org.commonjava.indy.model.core.StoreKey)36 ApiOperation (io.swagger.annotations.ApiOperation)35 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)34 ApiResponse (io.swagger.annotations.ApiResponse)33 Path (javax.ws.rs.Path)32 StoreType (org.commonjava.indy.model.core.StoreType)26 IndyDataException (org.commonjava.indy.data.IndyDataException)25 GET (javax.ws.rs.GET)24 Logger (org.slf4j.Logger)22 ApiResponses (io.swagger.annotations.ApiResponses)21 ArrayList (java.util.ArrayList)19 Produces (javax.ws.rs.Produces)18 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)18 InputStream (java.io.InputStream)15 List (java.util.List)13