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));
}
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;
}
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;
}
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);
};
}
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;
};
}
Aggregations