Search in sources :

Example 61 with ConcreteResource

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

the class AbstractTransferManagerTest method retrieve_cacheIfMissing.

/**
 * Test that remote content will be downloaded then cached.
 */
@Test
public void retrieve_cacheIfMissing() throws Exception {
    final String testContent = "This is a test " + System.currentTimeMillis();
    final Location loc = new SimpleLocation("file:///test-repo");
    final String path = "/path/to/test.txt";
    final ConcreteResource resource = new ConcreteResource(loc, path);
    // put in the content that we want to "download"
    getTransport().registerDownload(resource, new TestDownload(testContent.getBytes()));
    // now, use the manager to retrieve() the path...the remote content should come through here.
    Transfer transfer = getTransferManagerImpl().retrieve(resource);
    assertTransferContent(transfer, testContent);
    // now, the right content should be cached.
    // So, we'll put in some wrong content that will cause problems if the cache isn't used.
    getTransport().registerDownload(resource, new TestDownload("This is WRONG".getBytes()));
    // now, use the manager to retrieve() the path again...the cached content should come through here.
    transfer = getTransferManagerImpl().retrieve(resource);
    assertTransferContent(transfer, testContent);
}
Also used : TestDownload(org.commonjava.maven.galley.testing.core.transport.job.TestDownload) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) Location(org.commonjava.maven.galley.model.Location) Test(org.junit.Test)

Example 62 with ConcreteResource

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

the class HttpDownloadTest method IOExceptionDuringDownloadTransferDeletesTargetFile.

@Test
@BMRule(name = "throw IOException during writeTarget copy operation", targetClass = "HttpDownload", targetMethod = "doCopy", targetLocation = "ENTRY", condition = "!flagged(\"firstCopy\")", action = "flag(\"firstCopy\"); throw new IOException(\"BMUnit exception\");")
public void IOExceptionDuringDownloadTransferDeletesTargetFile() throws Exception {
    final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
    final String path = "/path/to/file";
    fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {

        int count = 0;

        @Override
        public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
            httpServletResponse.setStatus(200);
            httpServletResponse.setHeader("Content-Length", Integer.toString(content.length()));
            PrintWriter writer = httpServletResponse.getWriter();
            if (count < 1) {
                writer.write(content.substring(0, content.length() / 2));
            } else {
                writer.write(content);
            }
            count++;
        }
    });
    final String baseUri = fixture.getBaseUri();
    final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
    final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
    final String url = fixture.formatUrl(path);
    assertThat(transfer.exists(), equalTo(false));
    // first call, server should quit transferring halfway through the transfer
    HttpDownload dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
    DownloadJob resultJob = dl.call();
    TransferException error = dl.getError();
    assertThat(error, notNullValue());
    error.printStackTrace();
    assertThat(resultJob, notNullValue());
    Transfer result = resultJob.getTransfer();
    assertThat(result, notNullValue());
    assertThat(result.exists(), equalTo(false));
    assertThat(transfer.exists(), equalTo(false));
    // second call should hit upstream again and succeed.
    dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
    resultJob = dl.call();
    error = dl.getError();
    assertThat(error, nullValue());
    assertThat(resultJob, notNullValue());
    result = resultJob.getTransfer();
    assertThat(result, notNullValue());
    assertThat(result.exists(), equalTo(true));
    assertThat(transfer.exists(), equalTo(true));
    final String urlPath = fixture.getUrlPath(url);
    assertThat(fixture.getAccessesFor(urlPath), equalTo(2));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter) BMRule(org.jboss.byteman.contrib.bmunit.BMRule) Test(org.junit.Test)

Example 63 with ConcreteResource

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

the class HttpDownloadTest method simpleRetriveOfRedirectUrl.

@Test
public void simpleRetriveOfRedirectUrl() throws Exception {
    final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
    final String redirectPath = "/path/to/file";
    final String path = "/redirect/to/file";
    fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {

        @Override
        public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
            httpServletResponse.setStatus(302);
            httpServletResponse.setHeader("Location", fixture.formatUrl(redirectPath));
        }
    });
    fixture.getServer().expect("GET", fixture.formatUrl(redirectPath), 200, content);
    final String baseUri = fixture.getBaseUri();
    final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
    final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
    final String url = fixture.formatUrl(path);
    Map<Transfer, Long> transferSizes = new HashMap<Transfer, Long>();
    assertThat(transfer.exists(), equalTo(false));
    final HttpDownload dl = new HttpDownload(url, location, transfer, transferSizes, new EventMetadata(), fixture.getHttp(), new ObjectMapper());
    final DownloadJob resultJob = dl.call();
    final TransferException error = dl.getError();
    assertThat(error, nullValue());
    assertThat(resultJob, notNullValue());
    final Transfer result = resultJob.getTransfer();
    assertThat(result, notNullValue());
    assertThat(result.exists(), equalTo(true));
    assertThat(transfer.exists(), equalTo(true));
    final String postPath = fixture.getUrlPath(url);
    assertThat(fixture.getAccessesFor(postPath), equalTo(1));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 64 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource in project pom-manipulation-ext by release-engineering.

the class MavenLocationExpander method expand.

@Override
public VirtualResource expand(final Resource resource) throws TransferException {
    final List<ConcreteResource> result = new ArrayList<>();
    if (resource instanceof ConcreteResource) {
        expandSingle((ConcreteResource) resource, result);
    } else {
        for (final ConcreteResource cr : ((VirtualResource) resource).toConcreteResources()) {
            expandSingle(cr, result);
        }
    }
    logger.debug("Expanded to:\n {}", new JoinString("\n  ", result));
    return new VirtualResource(result);
}
Also used : JoinString(org.commonjava.maven.atlas.ident.util.JoinString) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ArrayList(java.util.ArrayList) VirtualResource(org.commonjava.maven.galley.model.VirtualResource)

Example 65 with ConcreteResource

use of org.commonjava.maven.galley.model.ConcreteResource in project pom-manipulation-ext by release-engineering.

the class MavenLocationExpander method expandSingle.

private void expandSingle(final ConcreteResource cr, final List<ConcreteResource> result) {
    final Location loc = cr.getLocation();
    final List<Location> expanded = new ArrayList<>();
    expandSingle(loc, expanded);
    final String path = cr.getPath();
    for (final Location location : expanded) {
        result.add(new ConcreteResource(location, path));
    }
}
Also used : ArrayList(java.util.ArrayList) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) Location(org.commonjava.maven.galley.model.Location) SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)

Aggregations

ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)206 Test (org.junit.Test)138 Transfer (org.commonjava.maven.galley.model.Transfer)92 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)55 Location (org.commonjava.maven.galley.model.Location)45 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)36 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)33 OutputStream (java.io.OutputStream)30 InputStream (java.io.InputStream)26 Group (org.commonjava.indy.model.core.Group)25 ArrayList (java.util.ArrayList)23 HostedRepository (org.commonjava.indy.model.core.HostedRepository)23 TransferException (org.commonjava.maven.galley.TransferException)23 SimpleHttpLocation (org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)23 HashSet (java.util.HashSet)22 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)18 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)18 Logger (org.slf4j.Logger)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)16