Search in sources :

Example 1 with ContentBrowseResult

use of org.commonjava.indy.content.browse.model.ContentBrowseResult in project indy by Commonjava.

the class ContentBrowseController method renderResult.

private ContentBrowseResult renderResult(final StoreKey key, final String requestPath, final String browseServiceUrl, final String contentServiceUrl, final UriFormatter uriFormatter, final EventMetadata eventMetadata) throws IndyWorkflowException {
    String path = requestPath.endsWith("/") ? requestPath : requestPath + "/";
    final ArtifactStore store = getStore(key);
    final List<StoreResource> listed = contentManager.list(store, path, eventMetadata);
    final Map<String, Set<String>> listingUrls = new TreeMap<>();
    final String storeBrowseUrl = uriFormatter.formatAbsolutePathTo(browseServiceUrl, key.getType().singularEndpointName(), key.getName());
    final String storeContentUrl = uriFormatter.formatAbsolutePathTo(contentServiceUrl, 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;
                }
                if (p.endsWith("-") || p.endsWith("-/")) {
                    // skip npm adduser path to avoid the sensitive info showing.
                    continue;
                } else if (pass == 1) {
                    if (!p.endsWith("/")) {
                        final String dirpath = p + "/";
                        if (listingUrls.containsKey(normalize(storeBrowseUrl, dirpath))) {
                            p = dirpath;
                        }
                    } else {
                        continue;
                    }
                }
                String localUrl;
                if (p.endsWith("/")) {
                    localUrl = normalize(storeBrowseUrl, p);
                } else {
                    // So this means current path is a file not a directory, and needs to construct it to point to content api /api/content
                    localUrl = normalize(storeContentUrl, p);
                }
                Set<String> sources = listingUrls.computeIfAbsent(localUrl, k -> new HashSet<>());
                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(browseServiceUrl, 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(browseServiceUrl, key.getType().singularEndpointName(), key.getName(), parentPath);
    }
    final List<ContentBrowseResult.ListingURLResult> listingURLResults = new ArrayList<>(listingUrls.size());
    for (String localUrl : listingUrls.keySet()) {
        final String apiPath = localUrl.replace(storeBrowseUrl, "").replace(storeContentUrl, "");
        listingURLResults.add(new ContentBrowseResult.ListingURLResult(apiPath, localUrl, listingUrls.get(localUrl)));
    }
    final ContentBrowseResult result = new ContentBrowseResult();
    result.setListingUrls(listingURLResults);
    result.setParentUrl(parentUrl);
    result.setParentPath(parentPath);
    result.setPath(path);
    result.setStoreKey(key);
    result.setStoreBrowseUrl(storeBrowseUrl);
    result.setStoreContentUrl(storeContentUrl);
    result.setBaseBrowseUrl(browseServiceUrl);
    result.setBaseContentUrl(contentServiceUrl);
    result.setSources(sources);
    return result;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ContentBrowseResult(org.commonjava.indy.content.browse.model.ContentBrowseResult) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) StoreResource(org.commonjava.indy.content.StoreResource) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource)

Example 2 with ContentBrowseResult

use of org.commonjava.indy.content.browse.model.ContentBrowseResult in project indy by Commonjava.

the class MetaListingRescheduleTimeoutTest method timeout.

// @Ignore // content listing is disabled for now, 2018/4/3
@Test
@Category(TimingDependent.class)
public void timeout() throws Exception {
    final int METADATA_TIMEOUT_SECONDS = 4;
    final int METADATA_TIMEOUT_WAITING_MILLISECONDS = 3000;
    final String repoId = "test-repo";
    String repoRootPath = "org/foo/bar/";
    final String repoRootUrl = server.formatUrl(repoId, repoRootPath);
    // now append the trailing '/' so Indy knows to try a directory listing...
    final String repoSubPath1 = "org/foo/bar/1.0/pom.xml";
    final String repoSubPath2 = "org/foo/bar/1.1/pom.xml";
    final String repoSubUrl1 = server.formatUrl(repoId, repoSubPath1);
    final String repoSubUrl2 = server.formatUrl(repoId, repoSubPath2);
    // mocking up a http server that expects access to metadata
    final String listingContent = "<html>" + "<head><title>Index of /org/foo/bar</title></head>" + "<body><h1>Index of /org/foo/bar/</h1>" + "<hr><pre>" + "<a href=\"1.0/\">1.0/</a>" + "<a href=\"1.1/\">1.1/</a>" + "</pre><hr></body></html>";
    server.expect(repoRootUrl, 200, listingContent);
    final String datetime = (new Date()).toString();
    server.expect(repoSubUrl1, 200, String.format("metadata %s", datetime));
    server.expect(repoSubUrl2, 200, String.format("metadata %s", datetime));
    // set up remote repository pointing to the test http server, and timeout little later
    final String changelog = "Timeout Testing: " + name.getMethodName();
    final RemoteRepository repository = new RemoteRepository(MavenPackageTypeDescriptor.MAVEN_PKG_KEY, repoId, server.formatUrl(repoId));
    repository.setMetadataTimeoutSeconds(METADATA_TIMEOUT_SECONDS);
    client.stores().create(repository, changelog, RemoteRepository.class);
    try (InputStream is = client.content().get(repository.getKey(), repoSubPath1)) {
    }
    try (InputStream is = client.content().get(repository.getKey(), repoSubPath2)) {
    }
    IndyContentBrowseClientModule browseClientModule = client.module(IndyContentBrowseClientModule.class);
    // first time trigger normal content storage with timeout, should be 4s
    logger.debug("Start to request listing of {}", repoRootPath);
    final ContentBrowseResult content = browseClientModule.getContentList(repository.getKey(), repoRootPath);
    assertThat("no metadata result", content, notNullValue());
    logger.debug("### will begin to get content");
    final String listingMetaPath = repoRootPath + ".listing.txt";
    KeyedLocation location = LocationUtils.toLocation(repository);
    File listingMetaFile = getPhysicalStorageFile(location, listingMetaPath);
    assertThat(".listing doesn't exist: " + listingMetaFile, listingMetaFile.exists(), equalTo(true));
    // wait for first time
    sleepAndRunFileGC(METADATA_TIMEOUT_WAITING_MILLISECONDS);
    // as the metadata content re-request, the metadata timeout interval should NOT be re-scheduled
    browseClientModule.getContentList(repository.getKey(), repoRootPath);
    // will wait second time for a longer period
    sleepAndRunFileGC(METADATA_TIMEOUT_WAITING_MILLISECONDS * getTestTimeoutMultiplier());
    // logger.info( "Checking whether metadata file {} has been deleted...", listingMetaFile );
    // as rescheduled, the artifact should not be deleted
    assertThat("artifact should be removed as the rescheduled of metadata should not succeed", listingMetaFile.exists(), equalTo(false));
}
Also used : KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) InputStream(java.io.InputStream) ContentBrowseResult(org.commonjava.indy.content.browse.model.ContentBrowseResult) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) IndyContentBrowseClientModule(org.commonjava.indy.content.browse.client.IndyContentBrowseClientModule) File(java.io.File) Date(java.util.Date) Category(org.junit.experimental.categories.Category) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest) Test(org.junit.Test)

Example 3 with ContentBrowseResult

use of org.commonjava.indy.content.browse.model.ContentBrowseResult in project indy by Commonjava.

the class ContentBrowseResource method processRequest.

private Response processRequest(final String packageType, final String type, final String name, final String path, final UriInfo uriInfo) {
    if (!PackageTypes.contains(packageType)) {
        return Response.status(400).build();
    }
    Response response;
    ContentBrowseResult result;
    try {
        result = getBrowseResult(packageType, type, name, path, uriInfo);
        response = responseHelper.formatOkResponseWithJsonEntity(result);
    } catch (IndyWorkflowException e) {
        logger.error(String.format("Failed to list content: %s from: %s. Reason: %s", StringUtils.isBlank(path) ? "/" : path, name, e.getMessage()), e);
        response = responseHelper.formatResponse(e);
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ContentBrowseResult(org.commonjava.indy.content.browse.model.ContentBrowseResult) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException)

Example 4 with ContentBrowseResult

use of org.commonjava.indy.content.browse.model.ContentBrowseResult in project indy by Commonjava.

the class RepoProxyFeaturesBundledTest method run.

@Test
public void run() throws Exception {
    // For maven artifact proxy
    try (InputStream result = client.content().get(mvnHosted.getKey(), PATH_ARTIFACT)) {
        assertThat(result, notNullValue());
        final String content = IOUtils.toString(result);
        assertThat(content, equalTo(ARTIFACT_CONTENT));
    }
    // For content listing response rewrite
    ContentBrowseResult rewrittenResult = client.module(IndyContentBrowseClientModule.class).getContentList(mvnHosted.getKey(), PATH_DIR);
    assertNotNull(rewrittenResult);
    final String originalContent = TestUtils.getExpectedRemoteContent(server, mvnHosted, PATH_DIR, mapper);
    ContentBrowseResult originalResult = mapper.readValue(originalContent, ContentBrowseResult.class);
    assertThat(rewrittenResult.getStoreKey(), equalTo(originalResult.getStoreKey()));
    assertThat(rewrittenResult.getPath(), equalTo(originalResult.getPath()));
    assertThat(rewrittenResult.getParentPath(), equalTo(originalResult.getParentPath()));
    assertThat(rewrittenResult.getStoreBrowseUrl(), not(originalResult.getStoreBrowseUrl()));
    assertThat(rewrittenResult.getStoreContentUrl(), not(originalResult.getStoreContentUrl()));
    // For npm metadata rewrite
    try (InputStream result = client.content().get(npmHosted.getKey(), PATH_NPM_ROOT)) {
        assertThat(result, notNullValue());
        final String content = IOUtils.toString(result);
        logger.debug("NPM Rewrite content: {}", content);
        assertThat(content, containsString(HOSTED_NPM_REPO_PATH));
        assertThat(content.contains(REMOTE_NPM_REPO_PATH), equalTo(false));
    }
    // For block path
    try (InputStream result = client.content().get(mvnHosted.getKey(), PATH_BLOCKED)) {
        assertThat(result, nullValue());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ContentBrowseResult(org.commonjava.indy.content.browse.model.ContentBrowseResult) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IndyContentBrowseClientModule(org.commonjava.indy.content.browse.client.IndyContentBrowseClientModule) AbstractIndyFunctionalTest(org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest) Test(org.junit.Test)

Example 5 with ContentBrowseResult

use of org.commonjava.indy.content.browse.model.ContentBrowseResult in project indy by Commonjava.

the class RepoProxyRemoteIndyListingRewriteGetTest method runGet.

@Test
public void runGet() throws Exception {
    ContentBrowseResult rewrittenResult = client.module(IndyContentBrowseClientModule.class).getContentList(hosted.getKey(), PATH);
    assertNotNull(rewrittenResult);
    ContentBrowseResult originalResult = mapper.readValue(expectedRemoteContent, ContentBrowseResult.class);
    assertThat(rewrittenResult.getStoreKey(), equalTo(originalResult.getStoreKey()));
    assertThat(rewrittenResult.getPath(), equalTo(originalResult.getPath()));
    assertThat(rewrittenResult.getParentPath(), equalTo(originalResult.getParentPath()));
    assertThat(rewrittenResult.getStoreBrowseUrl(), not(originalResult.getStoreBrowseUrl()));
    assertThat(rewrittenResult.getStoreContentUrl(), not(originalResult.getStoreContentUrl()));
}
Also used : ContentBrowseResult(org.commonjava.indy.content.browse.model.ContentBrowseResult) IndyContentBrowseClientModule(org.commonjava.indy.content.browse.client.IndyContentBrowseClientModule) AbstractIndyFunctionalTest(org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest) Test(org.junit.Test)

Aggregations

ContentBrowseResult (org.commonjava.indy.content.browse.model.ContentBrowseResult)9 IndyContentBrowseClientModule (org.commonjava.indy.content.browse.client.IndyContentBrowseClientModule)5 Test (org.junit.Test)5 AbstractContentManagementTest (org.commonjava.indy.ftest.core.AbstractContentManagementTest)3 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)3 ApiResponse (io.swagger.annotations.ApiResponse)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Response (javax.ws.rs.core.Response)2 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)2 AbstractIndyFunctionalTest (org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest)2 StoreKey (org.commonjava.indy.model.core.StoreKey)2 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1