Search in sources :

Example 31 with Transfer

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

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

the class AbstractHttpJob method executeHttp.

protected boolean executeHttp() throws TransferException {
    try {
        client = http.createClient(location);
        response = client.execute(request, http.createContext(location));
        final StatusLine line = response.getStatusLine();
        final int sc = line.getStatusCode();
        logger.trace("{} {} : {}", request.getMethod(), line, url);
        if (sc > 399 && sc != 404 && sc != 408 && sc != 502 && sc != 503 && sc != 504) {
            throw new TransferLocationException(location, "Server misconfigured or not responding normally: '%s'", line);
        } else if (!successStatuses.contains(sc)) {
            logger.trace("Detected failure respon se: " + sc);
            success = TransferResponseUtils.handleUnsuccessfulResponse(request, response, location, url);
            logger.trace("Returning non-error failure response for code: " + sc);
            return false;
        }
    } catch (final NoHttpResponseException | ConnectTimeoutException | SocketTimeoutException e) {
        throw new TransferTimeoutException(location, url, "Repository remote request failed for: {}. Reason: {}", e, url, e.getMessage());
    } catch (final IOException e) {
        throw new TransferLocationException(location, "Repository remote request failed for: {}. Reason: {}", e, url, e.getMessage());
    } catch (TransferLocationException e) {
        throw e;
    } catch (final GalleyException e) {
        throw new TransferException("Repository remote request failed for: {}. Reason: {}", e, url, e.getMessage());
    } finally {
        /*
            * we need to integrate the writeMetadata() method into the executeHttp() call in a finally block,
            * and with a condition that it only runs on HEAD or GET. This would allow us to capture metadata on failed requests too,
            * which is critical for responding consistently to the user after a failed request is cached in the NFC.
            */
        String method = request.getMethod();
        if ("GET".equalsIgnoreCase(method) || "HEAD".equalsIgnoreCase(method)) {
            Transfer target = getTransfer();
            ObjectMapper mapper = getMetadataObjectMapper();
            if (target != null && mapper != null) {
                writeMetadata(target, mapper);
            }
        }
    }
    return true;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) IOException(java.io.IOException) GalleyException(org.commonjava.maven.galley.GalleyException) StatusLine(org.apache.http.StatusLine) TransferException(org.commonjava.maven.galley.TransferException) SocketTimeoutException(java.net.SocketTimeoutException) Transfer(org.commonjava.maven.galley.model.Transfer) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 33 with Transfer

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

the class MetadataMergePomChangeListener method doClear.

private boolean doClear(final ArtifactStore store, final String path) {
    logger.trace("Updating merged metadata file: {} in store: {}", path, store.getKey());
    final Transfer item = fileManager.getStorageReference(store, path);
    final boolean isMetadata = item.getPath().endsWith(MavenMetadataMerger.METADATA_NAME);
    logger.trace("Attempting to delete: {}", item);
    if (item.exists()) {
        boolean result = deleteQuietly(store, item);
        logger.trace("Deleted: {} (success? {})", item, result);
        if (result && isMetadata) {
            Transfer info = fileManager.getStorageReference(store, path + GroupMergeHelper.MERGEINFO_SUFFIX);
            deleteQuietly(store, info);
        }
        if (fileEvent != null) {
            logger.trace("Firing deletion event for: {}", item);
            fileEvent.fire(new FileDeletionEvent(item, new EventMetadata()));
        }
        return result;
    } else if (isMetadata) {
        // we should return true here to trigger cache cleaning, because file not exists in store does not mean
        // metadata not exists in cache.
        logger.debug("Metadata clean for {}: metadata not existed, skip deletion and mark as deleted", item);
        return true;
    }
    return false;
}
Also used : Transfer(org.commonjava.maven.galley.model.Transfer) FileDeletionEvent(org.commonjava.maven.galley.event.FileDeletionEvent) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 34 with Transfer

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

the class MavenMetadataGenerator method generateMissing.

private Callable<MetadataResult> generateMissing(ArtifactStore store, String toMergePath) {
    return () -> {
        addFieldToActiveSpan("storekey", store.getKey().toString());
        addFieldToActiveSpan("path", toMergePath);
        addFieldToActiveSpan("activity", "generateMissing");
        try {
            logger.trace("Starting metadata generation: {}:{}", store.getKey(), toMergePath);
            Transfer memberMetaTxfr = generateFileContent(store, toMergePath, new EventMetadata());
            if (exists(memberMetaTxfr)) {
                final MetadataXpp3Reader reader = new MetadataXpp3Reader();
                try (InputStream in = memberMetaTxfr.openInputStream()) {
                    String content = IOUtils.toString(in);
                    Metadata memberMeta = reader.read(new StringReader(content), false);
                    clearObsoleteFiles(memberMetaTxfr);
                    return new MetadataResult(store, memberMeta);
                }
            }
        } catch (final Exception e) {
            addFieldToActiveSpan("error", e.getClass().getSimpleName());
            addFieldToActiveSpan("error.message", e.getMessage());
            String msg = String.format("EXCLUDING Failed generated metadata: %s:%s. Reason: %s", store.getKey(), toMergePath, e.getMessage());
            logger.error(msg, e);
        } finally {
            logger.trace("Ending metadata generation: {}:{}", store.getKey(), toMergePath);
        }
        return new MetadataResult(store, null);
    };
}
Also used : MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) InputStream(java.io.InputStream) Transfer(org.commonjava.maven.galley.model.Transfer) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) StringReader(java.io.StringReader) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 35 with Transfer

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

the class MavenMetadataGenerator method downloadMissing.

private Callable<MetadataResult> downloadMissing(ArtifactStore store, String toMergePath) {
    return () -> {
        addFieldToActiveSpan("storekey", store.getKey().toString());
        addFieldToActiveSpan("path", toMergePath);
        addFieldToActiveSpan("activity", "downloadMissing");
        try {
            logger.trace("Starting metadata download: {}:{}", store.getKey(), toMergePath);
            Transfer memberMetaTxfr = fileManager.retrieveRaw(store, toMergePath, new EventMetadata());
            if (exists(memberMetaTxfr)) {
                final MetadataXpp3Reader reader = new MetadataXpp3Reader();
                try (InputStream in = memberMetaTxfr.openInputStream()) {
                    String content = IOUtils.toString(in);
                    Metadata memberMeta = reader.read(new StringReader(content), false);
                    return new MetadataResult(store, memberMeta);
                }
            } else {
                return new MetadataResult(store, null);
            }
        } catch (final Exception e) {
            String msg = String.format("EXCLUDING Failed metadata download: %s:%s. Reason: %s", store.getKey(), toMergePath, e.getMessage());
            logger.error(msg, e);
        // errors.add( msg );
        } finally {
            logger.trace("Ending metadata download: {}:{}", store.getKey(), toMergePath);
        }
        return null;
    };
}
Also used : MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) InputStream(java.io.InputStream) Transfer(org.commonjava.maven.galley.model.Transfer) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) StringReader(java.io.StringReader) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Aggregations

Transfer (org.commonjava.maven.galley.model.Transfer)218 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)89 Test (org.junit.Test)80 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)64 IOException (java.io.IOException)63 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)59 InputStream (java.io.InputStream)43 StoreKey (org.commonjava.indy.model.core.StoreKey)43 Logger (org.slf4j.Logger)34 ArrayList (java.util.ArrayList)33 OutputStream (java.io.OutputStream)31 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)31 HostedRepository (org.commonjava.indy.model.core.HostedRepository)29 IndyDataException (org.commonjava.indy.data.IndyDataException)28 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)28 Group (org.commonjava.indy.model.core.Group)26 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)23 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)21 TransferException (org.commonjava.maven.galley.TransferException)20 Location (org.commonjava.maven.galley.model.Location)19