use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class MavenMetadataMerger method merge.
@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Generating merged metadata in: {}:{}", group.getKey(), path);
final Metadata master = new Metadata();
master.setVersioning(new Versioning());
final MetadataXpp3Reader reader = new MetadataXpp3Reader();
final FileReader fr = null;
InputStream stream = null;
boolean merged = false;
Transfer snapshotProvider = null;
for (final Transfer src : sources) {
if (!src.exists()) {
continue;
}
try {
stream = src.openInputStream();
String content = IOUtils.toString(stream);
logger.debug("Adding in metadata content from: {}\n\n{}\n\n", src, content);
// there is a lot of junk in here to make up for Metadata's anemic merge() method.
final Metadata md = reader.read(new StringReader(content), false);
if (md.getGroupId() != null) {
master.setGroupId(md.getGroupId());
}
if (md.getArtifactId() != null) {
master.setArtifactId(md.getArtifactId());
}
if (md.getVersion() != null) {
master.setVersion(md.getVersion());
}
master.merge(md);
Versioning versioning = master.getVersioning();
Versioning mdVersioning = md.getVersioning();
// FIXME: Should we try to merge snapshot lists instead of using the first one we encounter??
if (versioning.getSnapshot() == null && mdVersioning != null) {
logger.info("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
snapshotProvider = src;
versioning.setSnapshot(mdVersioning.getSnapshot());
final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
boolean added = false;
for (final SnapshotVersion snap : mdVersioning.getSnapshotVersions()) {
if (!snapshotVersions.contains(snap)) {
snapshotVersions.add(snap);
added = true;
}
}
if (added) {
Collections.sort(snapshotVersions, new SnapshotVersionComparator());
}
} else {
logger.warn("SKIPPING snapshot information from: {} in: {}:{} (obscured by: {})", src, group.getKey(), path, snapshotProvider);
}
merged = true;
} catch (final IOException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot read metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} catch (final XmlPullParserException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot parse metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} finally {
closeQuietly(fr);
closeQuietly(stream);
}
}
Versioning versioning = master.getVersioning();
if (versioning != null && versioning.getVersions() != null) {
if (metadataProviders != null) {
for (MavenMetadataProvider provider : metadataProviders) {
try {
Metadata toMerge = provider.getMetadata(group.getKey(), path);
if (toMerge != null) {
merged = master.merge(toMerge) || merged;
}
} catch (IndyWorkflowException e) {
logger.error(String.format("Cannot read metadata: %s from metadata provider: %s. Reason: %s", path, provider.getClass().getSimpleName(), e.getMessage()), e);
}
}
}
List<SingleVersion> versionObjects = versioning.getVersions().stream().map(VersionUtils::createSingleVersion).collect(Collectors.toList());
Collections.sort(versionObjects);
versioning.setVersions(versionObjects.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
if (versionObjects.size() > 0) {
String latest = versionObjects.get(versionObjects.size() - 1).renderStandard();
versioning.setLatest(latest);
versioning.setRelease(latest);
}
}
if (merged) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
new MetadataXpp3Writer().write(baos, master);
return baos.toByteArray();
} catch (final IOException e) {
logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
}
}
return null;
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class PromotionValidationTools method readLocalPom.
public MavenPomView readLocalPom(final String path, final ValidationRequest request) throws IndyWorkflowException, GalleyMavenException {
ArtifactRef artifactRef = getArtifact(path);
if (artifactRef == null) {
throw new IndyWorkflowException("Invalid artifact path: %s. Could not parse ArtifactRef from path.", path);
}
Transfer transfer = retrieve(request.getSourceRepository(), path);
return pomReader.readLocalPom(artifactRef.asProjectVersionRef(), transfer, MavenPomView.ALL_PROFILES);
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class PackageMetadataMergerTest method mergeOneTransferWithProviderError.
@Test
public void mergeOneTransferWithProviderError() throws Exception {
String path = "jquery/package.json";
HostedRepository h1 = new HostedRepository("test-hosted-1");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "package-1.json");
Group g = new Group("test-group", h1.getKey());
List<Transfer> sources = Arrays.asList(t1);
TestPackageMetadataProvider testProvider = new TestPackageMetadataProvider(new IndyWorkflowException("Failed to get provider content"));
byte[] output = new PackageMetadataMerger(Collections.singletonList(testProvider)).merge(sources, g, path);
IndyObjectMapper mapper = new IndyObjectMapper(true);
PackageMetadata merged = mapper.readValue(IOUtils.toString(new ByteArrayInputStream(output)), PackageMetadata.class);
// normal object fields merging verification
assertThat(merged.getName(), equalTo("jquery"));
assertThat(merged.getAuthor().getName(), equalTo("JS Foundation and other contributors"));
assertThat(merged.getRepository().getType(), equalTo("git"));
assertThat(merged.getRepository().getUrl(), equalTo("git+https://github.com/jquery1/jquery1.git"));
assertThat(merged.getReadmeFilename(), equalTo("README1.md"));
assertThat(merged.getHomepage(), equalTo("https://jquery1.com"));
assertThat(merged.getBugs().getUrl(), equalTo("https://github.com/jquery1/jquery1/issues"));
assertThat(merged.getLicense(), equalTo("MIT1"));
// dist-tags object merging verification
assertThat(merged.getDistTags().getBeta(), equalTo("2.2.1"));
assertThat(merged.getDistTags().getLatest(), equalTo("2.2.1"));
// error, versions map merging verification
Map<String, VersionMetadata> versions = merged.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(1));
assertThat(versions.get("1.5.1").getVersion(), equalTo("1.5.1"));
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class PromotionManagerTest method promoteAllByPath_RaceToPromote_FirstLocksTargetStore.
@Test
public void promoteAllByPath_RaceToPromote_FirstLocksTargetStore() throws Exception {
Random rand = new Random();
final HostedRepository[] sources = { new HostedRepository(MAVEN_PKG_KEY, "source1"), new HostedRepository(MAVEN_PKG_KEY, "source2") };
final String[] paths = { "/path/path1", "/path/path2", "/path3", "/path/path/4" };
Stream.of(sources).forEach((source) -> {
try {
storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
Stream.of(paths).forEach((path) -> {
byte[] buf = new byte[1024 * 1024 * 2];
rand.nextBytes(buf);
try {
contentManager.store(source, path, new ByteArrayInputStream(buf), TransferOperation.UPLOAD, new EventMetadata());
} catch (IndyWorkflowException e) {
e.printStackTrace();
Assert.fail("failed to store generated file to: " + source + path);
}
});
} catch (IndyDataException e) {
e.printStackTrace();
Assert.fail("failed to store hosted repository: " + source);
}
});
final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
PathsPromoteResult[] results = new PathsPromoteResult[2];
CountDownLatch cdl = new CountDownLatch(2);
AtomicInteger counter = new AtomicInteger(0);
Stream.of(sources).forEach((source) -> {
int idx = counter.getAndIncrement();
executor.execute(() -> {
try {
results[idx] = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey(), paths), FAKE_BASE_URL);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Promotion from source: " + source + " failed.");
} finally {
cdl.countDown();
}
});
try {
Thread.sleep(25);
} catch (InterruptedException e) {
Assert.fail("Test interrupted");
}
});
assertThat("Promotions failed to finish.", cdl.await(30, TimeUnit.SECONDS), equalTo(true));
// first one should succeed.
PathsPromoteResult result = results[0];
assertThat(result.getRequest().getSource(), equalTo(sources[0].getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
Set<String> pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
Set<String> skipped = result.getSkippedPaths();
assertThat(skipped == null || skipped.isEmpty(), equalTo(true));
Set<String> completed = result.getCompletedPaths();
assertThat(completed, notNullValue());
assertThat(completed.size(), equalTo(paths.length));
assertThat(result.getError(), nullValue());
Stream.of(paths).forEach((path) -> {
HostedRepository src = sources[0];
Transfer sourceRef = downloadManager.getStorageReference(src, path);
Transfer targetRef = downloadManager.getStorageReference(target, path);
assertThat(targetRef.exists(), equalTo(true));
try (InputStream sourceIn = sourceRef.openInputStream();
InputStream targetIn = targetRef.openInputStream()) {
int s = -1, t = -1;
while ((s = sourceIn.read()) == (t = targetIn.read())) {
if (s == -1) {
break;
}
}
if (s != -1 && s != t) {
Assert.fail(path + " doesn't match between source: " + src + " and target: " + target);
}
} catch (IOException e) {
e.printStackTrace();
Assert.fail("Failed to compare contents of: " + path + " between source: " + src + " and target: " + target);
}
});
// second one should be completely skipped.
result = results[1];
assertThat(result.getRequest().getSource(), equalTo(sources[1].getKey()));
assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
pending = result.getPendingPaths();
assertThat(pending == null || pending.isEmpty(), equalTo(true));
skipped = result.getSkippedPaths();
assertThat(skipped, notNullValue());
assertThat(skipped.size(), equalTo(paths.length));
completed = result.getCompletedPaths();
assertThat(completed == null || completed.isEmpty(), equalTo(true));
assertThat(result.getError(), nullValue());
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class NfcResource method getStore.
@Path("/{packageType}/{type: (hosted|group|remote)}/{name}")
@ApiOperation("Retrieve all not-found cache entries currently tracked for a given store")
@ApiResponses({ @ApiResponse(code = 200, response = NotFoundCacheDTO.class, message = "The not-found cache for the specified artifact store") })
@GET
@Produces(ApplicationContent.application_json)
public Response getStore(@ApiParam(name = "packageType", required = true, value = "The type of package (eg. maven, npm, generic-http)") @PathParam("packageType") final String packageType, @ApiParam(allowableValues = "hosted,group,remote", name = "type", required = true, value = "The type of store") @PathParam("type") final String t, @ApiParam(name = "name", value = "The name of the store") @PathParam("name") final String name) {
Response response;
final StoreType type = StoreType.get(t);
final StoreKey key = new StoreKey(packageType, type, name);
try {
final NotFoundCacheDTO dto = controller.getMissing(key);
response = formatOkResponseWithJsonEntity(dto, serializer);
} catch (final IndyWorkflowException e) {
response = formatResponse(e);
}
return response;
}
Aggregations