use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class MavenMetadataGenerator method writeVersionMetadata.
private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
ArtifactPathInfo samplePomInfo = null;
// first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
// as a valid version
final List<SingleVersion> versions = new ArrayList<SingleVersion>();
nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
final String topPath = topResource.getPath();
if (topPath.endsWith("/")) {
final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
for (final StoreResource fileResource : secondLevelListing) {
if (fileResource.getPath().endsWith(".pom")) {
ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
// check if the pom is valid for the path
if (filePomInfo != null) {
versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
if (samplePomInfo == null) {
samplePomInfo = filePomInfo;
}
continue nextTopResource;
}
}
}
}
}
if (versions.isEmpty()) {
return false;
}
Collections.sort(versions);
final Transfer metadataFile = fileManager.getTransfer(store, path);
OutputStream stream = null;
try {
final Document doc = xml.newDocumentBuilder().newDocument();
final Map<String, String> coordMap = new HashMap<String, String>();
coordMap.put(ARTIFACT_ID, samplePomInfo.getArtifactId());
coordMap.put(GROUP_ID, samplePomInfo.getGroupId());
final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());
doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
xml.createElement(doc.getDocumentElement(), null, coordMap);
final Map<String, String> versioningMap = new HashMap<String, String>();
versioningMap.put(LAST_UPDATED, lastUpdated);
final SingleVersion latest = versions.get(versions.size() - 1);
versioningMap.put(LATEST, latest.renderStandard());
SingleVersion release = null;
for (int i = versions.size() - 1; i >= 0; i--) {
final SingleVersion r = versions.get(i);
if (r.isRelease()) {
release = r;
break;
}
}
if (release != null) {
versioningMap.put(RELEASE, release.renderStandard());
}
xml.createElement(doc, "versioning", versioningMap);
final Element versionsElem = xml.createElement(doc, "versioning/versions", Collections.<String, String>emptyMap());
for (final SingleVersion version : versions) {
final Element vElem = doc.createElement(VERSION);
vElem.setTextContent(version.renderStandard());
versionsElem.appendChild(vElem);
}
final String xmlStr = xml.toXML(doc, true);
stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
stream.write(xmlStr.getBytes("UTF-8"));
} catch (final GalleyMavenXMLException e) {
throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path, e.getMessage());
} catch (final IOException e) {
throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e, metadataFile, e.getMessage());
} finally {
closeQuietly(stream);
}
return true;
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class MavenMetadataMergerTest method mergeOneTransferWithProviderError.
@Test
public void mergeOneTransferWithProviderError() throws Exception {
String path = "org/foo/bar/maven-metadata.xml";
HostedRepository h1 = new HostedRepository("test-hosted-1");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "simple-1.xml");
Group g = new Group("test-group", h1.getKey());
List<Transfer> sources = Arrays.asList(t1);
TestMavenMetadataProvider testProvider = new TestMavenMetadataProvider(new IndyWorkflowException("Failed to get provider content"));
byte[] output = new MavenMetadataMerger(Collections.singletonList(testProvider)).merge(sources, g, path);
Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
assertThat(merged.getGroupId(), equalTo("org.foo"));
assertThat(merged.getArtifactId(), equalTo("bar"));
Versioning versioning = merged.getVersioning();
assertThat(versioning, notNullValue());
List<String> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(1));
assertThat(versioning.getRelease(), equalTo("1.0"));
assertThat(versioning.getLatest(), equalTo("1.0"));
int idx = 0;
assertThat(versions.get(idx), equalTo("1.0"));
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class PackageMetadataMerger method merge.
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);
InputStream stream = null;
boolean merged = false;
final PackageMetadata packageMetadata = new PackageMetadata();
final IndyObjectMapper mapper = new IndyObjectMapper(true);
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);
PackageMetadata md = mapper.readValue(content, PackageMetadata.class);
merged = packageMetadata.merge(md) || merged;
} 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);
} finally {
closeQuietly(stream);
}
}
if (metadataProviders != null) {
for (PackageMetadataProvider provider : metadataProviders) {
try {
PackageMetadata toMerge = provider.getMetadata(group.getKey(), path);
if (toMerge != null) {
merged = packageMetadata.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);
}
}
}
if (merged) {
String output = null;
try {
output = mapper.writeValueAsString(packageMetadata);
} catch (JsonProcessingException e) {
logger.error(String.format("Cannot convert from metadata: %s to String. Reason: %s", packageMetadata, e.getMessage()), e);
}
return output.getBytes();
}
return null;
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class SecurityResource method getKeycloakUiJson.
@ApiOperation("Retrieve the keycloak JSON configuration (for use by the UI)")
@ApiResponses({ @ApiResponse(code = 400, message = "Keycloak is disabled"), @ApiResponse(code = 200, message = "File retrieval successful") })
@Path("/keycloak.json")
@Produces("application/json")
@GET
public Response getKeycloakUiJson() {
logger.debug("Retrieving Keycloak UI JSON file...");
Response response = null;
try {
final String content = controller.getKeycloakUiJson();
if (content == null) {
response = Response.status(Status.BAD_REQUEST).entity(DISABLED_MESSAGE).header(ApplicationHeader.cache_control.key(), NO_CACHE).build();
} else {
response = Response.ok(content).header(ApplicationHeader.cache_control.key(), NO_CACHE).build();
}
} catch (final IndyWorkflowException e) {
logger.error(String.format("Failed to load client-side keycloak.json. Reason: %s", e.getMessage()), e);
response = formatResponse(e);
}
return response;
}
use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.
the class SecurityResource method getKeycloakInit.
@ApiOperation("Retrieve the keycloak init Javascript (for use by the UI)")
@ApiResponse(code = 200, message = "Always return 200 whether Keycloak is disabled or not")
@Path("/keycloak-init.js")
@Produces("text/javascript")
@GET
public Response getKeycloakInit() {
logger.debug("Retrieving Keycloak UI-init Javascript file...");
Response response = null;
try {
response = Response.ok(controller.getKeycloakInit()).header(ApplicationHeader.cache_control.key(), NO_CACHE).build();
} catch (final IndyWorkflowException e) {
logger.error(String.format("Failed to load keycloak-init.js. Reason: %s", e.getMessage()), e);
response = formatResponse(e);
}
return response;
}
Aggregations