use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.
the class LicenseInfoHandler method getLicenseInfoFile.
@Override
public LicenseInfoFile getLicenseInfoFile(Project project, User user, String outputGenerator, Map<String, Set<String>> releaseIdsToSelectedAttachmentIds, Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachment) throws TException {
assertNotNull(project);
assertNotNull(user);
assertNotNull(outputGenerator);
assertNotNull(releaseIdsToSelectedAttachmentIds);
assertNotNull(excludedLicensesPerAttachment);
Map<Release, Set<String>> releaseToAttachmentId = mapKeysToReleases(releaseIdsToSelectedAttachmentIds, user);
Collection<LicenseInfoParsingResult> projectLicenseInfoResults = getAllReleaseLicenseInfos(releaseToAttachmentId, user, excludedLicensesPerAttachment);
String[] outputGeneratorClassnameAndVariant = outputGenerator.split("::");
if (outputGeneratorClassnameAndVariant.length != 2) {
throw new TException("Unsupported output generator value: " + outputGenerator);
}
String outputGeneratorClassName = outputGeneratorClassnameAndVariant[0];
OutputFormatVariant outputGeneratorVariant = Enums.getIfPresent(OutputFormatVariant.class, outputGeneratorClassnameAndVariant[1]).orNull();
OutputGenerator<?> generator = getOutputGeneratorByClassnameAndVariant(outputGeneratorClassName, outputGeneratorVariant);
LicenseInfoFile licenseInfoFile = new LicenseInfoFile();
licenseInfoFile.setOutputFormatInfo(generator.getOutputFormatInfo());
String licenseInfoHeaderText = (project.isSetLicenseInfoHeaderText()) ? project.getLicenseInfoHeaderText() : getDefaultLicenseInfoHeaderText();
Object output = generator.generateOutputFile(projectLicenseInfoResults, project.getName(), project.getVersion(), licenseInfoHeaderText);
if (output instanceof byte[]) {
licenseInfoFile.setGeneratedOutput((byte[]) output);
} else if (output instanceof String) {
licenseInfoFile.setGeneratedOutput(((String) output).getBytes());
} else {
throw new TException("Unsupported output generator result: " + output.getClass().getSimpleName());
}
return licenseInfoFile;
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.
the class LicenseInfoHandler method getLicenseInfoForAttachment.
@Override
public List<LicenseInfoParsingResult> getLicenseInfoForAttachment(Release release, String attachmentContentId, User user) throws TException {
if (release == null) {
return Collections.singletonList(noSourceParsingResult(MSG_NO_RELEASE_GIVEN));
}
List<LicenseInfoParsingResult> cachedResults = licenseInfoCache.getIfPresent(attachmentContentId);
if (cachedResults != null) {
return cachedResults;
}
Attachment attachment = nullToEmptySet(release.getAttachments()).stream().filter(a -> a.getAttachmentContentId().equals(attachmentContentId)).findFirst().orElseThrow(() -> {
String message = String.format("Attachment selected for license info generation is not found in release's attachments. Release id: %s. Attachment content id: %s", release.getId(), attachmentContentId);
return new IllegalStateException(message);
});
try {
List<LicenseInfoParser> applicableParsers = parsers.stream().filter(parser -> wrapTException(() -> parser.isApplicableTo(attachment, user, release))).collect(Collectors.toList());
if (applicableParsers.size() == 0) {
LOGGER.warn("No applicable parser has been found for the attachment selected for license information");
return assignReleaseToLicenseInfoParsingResult(assignFileNameToLicenseInfoParsingResult(noSourceParsingResult("No applicable parser has been found for the attachment"), attachment.getFilename()), release);
} else if (applicableParsers.size() > 1) {
LOGGER.info("More than one parser claims to be able to parse attachment with contend id " + attachmentContentId);
}
List<LicenseInfoParsingResult> results = applicableParsers.stream().map(parser -> wrapTException(() -> parser.getLicenseInfos(attachment, user, release))).flatMap(Collection::stream).collect(Collectors.toList());
filterEmptyLicenses(results);
results = assignReleaseToLicenseInfoParsingResults(results, release);
licenseInfoCache.put(attachmentContentId, results);
return results;
} catch (WrappedTException exception) {
throw exception.getCause();
}
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.
the class CLIParser method getLicenseInfos.
@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
LicenseInfoParsingResult result = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
InputStream attachmentStream = null;
try {
attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
Document doc = getDocument(attachmentStream);
Set<String> copyrights = getCopyrights(doc);
licenseInfo.setCopyrights(copyrights);
Set<LicenseNameWithText> licenseNamesWithTexts = getLicenseNameWithTexts(doc);
licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTexts);
result.setStatus(LicenseInfoRequestStatus.SUCCESS);
} catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
log.error(e);
result.setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing CLI file: " + e.toString());
} finally {
closeQuietly(attachmentStream, log);
}
return Collections.singletonList(result);
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.
the class LicenseInfoHandlerTest method testThatEmptyLicensesAreFiltered.
@Test
public void testThatEmptyLicensesAreFiltered() {
LicenseInfoParsingResult emptyResult = new LicenseInfoParsingResult();
LicenseInfoParsingResult emptyLicenseInfo = new LicenseInfoParsingResult();
emptyLicenseInfo.setLicenseInfo(new LicenseInfo());
LicenseInfoParsingResult parsingResults = new LicenseInfoParsingResult();
LicenseInfo licenseInfo = new LicenseInfo();
// @formatter:off
licenseInfo.setLicenseNamesWithTexts(ImmutableSet.of(createLicense("nameOnly", null, null), createLicense(null, null, null), createLicense(null, "textOnly", null), createLicense("", null, null), createLicense(null, null, "ackOnly"), createLicense("", "", ""), createLicense("name", "text", "ack")));
// @formatter:on
parsingResults.setLicenseInfo(licenseInfo);
handler.filterEmptyLicenses(ImmutableList.of(emptyResult, emptyLicenseInfo, parsingResults));
// @formatter:off
Assert.assertThat(parsingResults.getLicenseInfo().getLicenseNamesWithTexts(), Matchers.containsInAnyOrder(createLicense("nameOnly", null, null), createLicense(null, "textOnly", null), createLicense(null, null, "ackOnly"), createLicense("name", "text", "ack")));
// @formatter:on
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.
the class LicenseInfoHandlerTest method testThatLicensesAreFilteredAndOriginalObejctIsNotTouched.
@Test
public void testThatLicensesAreFilteredAndOriginalObejctIsNotTouched() {
LicenseInfoParsingResult parsingResults = new LicenseInfoParsingResult();
LicenseInfo licenseInfo = new LicenseInfo();
// @formatter:off
licenseInfo.setLicenseNamesWithTexts(ImmutableSet.of(createLicense("l1", null, null), createLicense("l1", "t1", null), createLicense("l2", "t2", null), createLicense("l3", "t3", null), createLicense("l3", "t3", "a3"), createLicense(null, "t4", null), createLicense("l4", "t4", null), createLicense(null, null, "a5"), createLicense("l5", null, "a5"), createLicense("l", "t", "a6"), createLicense("l", "t", "a7"), createLicense("l8", null, null), createLicense("l8", "t8", null), createLicense("l9", "t9", "a9"), createLicense(null, "t9", "a9")));
// @formatter:on
parsingResults.setLicenseInfo(licenseInfo);
// @formatter:off
LicenseInfoParsingResult filteredResult = handler.filterLicenses(parsingResults, ImmutableSet.of(createLicense("l1", "t1", null), createLicense("l3", "t3", "a3"), createLicense(null, "t4", null), createLicense(null, null, "a5"), createLicense("l", "t", "a6"), createLicense("l8", null, null), createLicense(null, "t9", "a9")));
// @formatter:on
// @formatter:off
Assert.assertThat(filteredResult.getLicenseInfo().getLicenseNamesWithTexts(), Matchers.containsInAnyOrder(createLicense("l1", null, null), createLicense("l2", "t2", null), createLicense("l3", "t3", null), createLicense("l4", "t4", null), createLicense("l5", null, "a5"), createLicense("l", "t", "a7"), createLicense("l8", "t8", null), createLicense("l9", "t9", "a9")));
// @formatter:on
}
Aggregations