Search in sources :

Example 41 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource in project indy by Commonjava.

the class ContentControllerTest method detectHtml_MultipleHtmlElementsOnALine.

@Test
public void detectHtml_MultipleHtmlElementsOnALine() throws Exception {
    final ConcreteResource res = new ConcreteResource(new SimpleLocation("test:uri"), "file.html");
    final Transfer tx = fixture.getCache().getTransfer(res);
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(new OutputStreamWriter(tx.openOutputStream(TransferOperation.GENERATE)));
        writer.print("<html><body><h1>FOO</h1>");
        writer.flush();
    } finally {
        IOUtils.closeQuietly(writer);
    }
    assertThat(content.isHtmlContent(tx), equalTo(true));
}
Also used : ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 42 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource in project indy by Commonjava.

the class ContentControllerTest method detectHtml_SingleHtmlElementLineWithPrecedingWhitespace.

@Test
public void detectHtml_SingleHtmlElementLineWithPrecedingWhitespace() throws Exception {
    final ConcreteResource res = new ConcreteResource(new SimpleLocation("test:uri"), "file.html");
    final Transfer tx = fixture.getCache().getTransfer(res);
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(new OutputStreamWriter(tx.openOutputStream(TransferOperation.GENERATE)));
        writer.print("    <html>");
        writer.flush();
    } finally {
        IOUtils.closeQuietly(writer);
    }
    assertThat(content.isHtmlContent(tx), equalTo(true));
}
Also used : ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 43 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource 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 44 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource in project indy by Commonjava.

the class DefaultDownloadManager method getStorageReference.

@Override
public Transfer getStorageReference(final ArtifactStore store, final String... path) {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.trace("Retrieving cache reference (Transfer) to: {} in: {}", Arrays.asList(path), store.getKey());
    return transfers.getCacheReference(new ConcreteResource(LocationUtils.toLocation(store), path));
}
Also used : ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Logger(org.slf4j.Logger)

Example 45 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource in project indy by Commonjava.

the class DefaultDownloadManager method store.

/*
     * (non-Javadoc)
     * @see org.commonjava.indy.core.rest.util.FileManager#upload(org.commonjava.indy.core.model.DeployPoint,
     * java.lang.String, java.io.InputStream)
     */
@Override
public Transfer store(final ArtifactStore store, final String path, final InputStream stream, final TransferOperation op, final EventMetadata eventMetadata) throws IndyWorkflowException {
    if (store.getKey().getType() == StoreType.group) {
        //FIXME: Why is this null? Investigate.
        return null;
    }
    if (store.getKey().getType() != StoreType.hosted) {
        throw new IndyWorkflowException(ApplicationStatus.BAD_REQUEST.code(), "Cannot deploy to non-deploy point artifact store: {}.", store.getKey());
    }
    if (storeManager.isReadonly(store)) {
        throw new IndyWorkflowException(ApplicationStatus.METHOD_NOT_ALLOWED.code(), "The store {} is readonly. If you want to store any content to this store, please modify it to non-readonly", store.getKey());
    }
    if (store instanceof HostedRepository) {
        final HostedRepository deploy = (HostedRepository) store;
        //            final ArtifactPathInfo pathInfo = ArtifactPathInfo.parse( path );
        final ContentQuality quality = getQuality(path);
        if (quality != null && quality == ContentQuality.SNAPSHOT) {
            if (!deploy.isAllowSnapshots()) {
                logger.error("Cannot store snapshot in non-snapshot deploy point: {}", deploy.getName());
                throw new IndyWorkflowException(ApplicationStatus.BAD_REQUEST.code(), "Cannot store snapshot in non-snapshot deploy point: {}", deploy.getName());
            }
        } else if (!deploy.isAllowReleases()) {
            logger.error("Cannot store release in snapshot-only deploy point: {}", deploy.getName());
            throw new IndyWorkflowException(ApplicationStatus.BAD_REQUEST.code(), "Cannot store release in snapshot-only deploy point: {}", deploy.getName());
        }
    }
    try {
        return transfers.store(new ConcreteResource(LocationUtils.toLocation(store), path), stream, eventMetadata);
    } catch (final BadGatewayException e) {
        Location location = e.getLocation();
        KeyedLocation kl = (KeyedLocation) location;
        fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
        logger.warn("Bad gateway: " + e.getMessage(), e);
        throw new IndyWorkflowException("Failed to store path: {} in: {}. Reason: {}", e, path, store, e.getMessage());
    } catch (final TransferTimeoutException e) {
        Location location = e.getLocation();
        KeyedLocation kl = (KeyedLocation) location;
        fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
        logger.warn("Timeout: " + e.getMessage(), e);
        throw new IndyWorkflowException("Failed to store path: {} in: {}. Reason: {}", e, path, store, e.getMessage());
    } catch (final TransferLocationException e) {
        Location location = e.getLocation();
        KeyedLocation kl = (KeyedLocation) location;
        fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
        logger.warn("Location Error: " + e.getMessage(), e);
        throw new IndyWorkflowException("Failed to store path: {} in: {}. Reason: {}", e, path, store, e.getMessage());
    } catch (TransferException e) {
        logger.error(String.format("Failed to store: %s in: %s. Reason: %s", path, store.getKey(), e.getMessage()), e);
        throw new IndyWorkflowException("Failed to store: %s in: %s. Reason: %s", e, path, store.getKey(), e.getMessage());
    }
}
Also used : TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) ContentQuality(org.commonjava.indy.spi.pkg.ContentQuality) TransferException(org.commonjava.maven.galley.TransferException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) BadGatewayException(org.commonjava.maven.galley.BadGatewayException) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) IndyStoreErrorEvent(org.commonjava.indy.change.event.IndyStoreErrorEvent) HostedRepository(org.commonjava.indy.model.core.HostedRepository) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Location(org.commonjava.maven.galley.model.Location)

Aggregations

ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)122 Test (org.junit.Test)85 Transfer (org.commonjava.maven.galley.model.Transfer)63 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)35 Location (org.commonjava.maven.galley.model.Location)28 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)21 Group (org.commonjava.indy.model.core.Group)20 HashSet (java.util.HashSet)15 HostedRepository (org.commonjava.indy.model.core.HostedRepository)15 TransferException (org.commonjava.maven.galley.TransferException)15 TestDownload (org.commonjava.maven.galley.testing.core.transport.job.TestDownload)15 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 InputStream (java.io.InputStream)13 SimpleHttpLocation (org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)13 OutputStream (java.io.OutputStream)12 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)12 SimpleProjectVersionRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)12 Logger (org.slf4j.Logger)12 ListingResult (org.commonjava.maven.galley.model.ListingResult)11