Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SPDX): Use new SPDX library (#1496) #1594

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/src-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
<dependencies>
<dependency>
<groupId>org.spdx</groupId>
<artifactId>spdx-tools</artifactId>
<scope>compile</scope>
<artifactId>tools-java</artifactId>
</dependency>
<!-- Needed by spdx-tools -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@
import org.eclipse.sw360.spdx.SpdxBOMImporter;
import org.eclipse.sw360.spdx.SpdxBOMImporterSink;
import org.jetbrains.annotations.NotNull;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -513,12 +514,26 @@ public AddDocumentRequestSummary addRelease(Release release, User user) throws S
}

private boolean isDuplicate(Component component, boolean caseInsenstive){
Set<String> duplicates = componentRepository.getComponentIdsByName(component.getName(), caseInsenstive);
return duplicates.size()>0;
return isDuplicate(component.getName(), caseInsenstive);
}

private boolean isDuplicate(Release release){
List<Release> duplicates = releaseRepository.searchByNameAndVersion(release.getName(), release.getVersion());
return isDuplicate(release.getName(), release.getVersion());
}

private boolean isDuplicate(String componentName, boolean caseInsenstive) {
if (isNullEmptyOrWhitespace(componentName)) {
return false;
}
Set<String> duplicates = componentRepository.getComponentIdsByName(componentName, caseInsenstive);
return duplicates.size()>0;
}

private boolean isDuplicate(String releaseName, String releaseVersion) {
if (isNullEmptyOrWhitespace(releaseName)) {
return false;
}
List<Release> duplicates = releaseRepository.searchByNameAndVersion(releaseName, releaseVersion);
return duplicates.size()>0;
}

Expand Down Expand Up @@ -2363,6 +2378,43 @@ private void sendMailNotificationsForReleaseUpdate(Release release, String user)
release.getName(), release.getVersion());
}

public ImportBomRequestPreparation prepareImportBom(User user, String attachmentContentId) throws SW360Exception {
final AttachmentContent attachmentContent = attachmentConnector.getAttachmentContent(attachmentContentId);
final Duration timeout = Duration.durationOf(30, TimeUnit.SECONDS);
try {
final AttachmentStreamConnector attachmentStreamConnector = new AttachmentStreamConnector(timeout);
try (final InputStream inputStream = attachmentStreamConnector.unsafeGetAttachmentStream(attachmentContent)) {
final SpdxBOMImporterSink spdxBOMImporterSink = new SpdxBOMImporterSink(user, null, this);
final SpdxBOMImporter spdxBOMImporter = new SpdxBOMImporter(spdxBOMImporterSink);

String fileType = getFileType(attachmentContent.getFilename());
final String ext = "." + fileType;
final File sourceFile = DatabaseHandlerUtil.saveAsTempFile(inputStream, attachmentContentId, ext);

ImportBomRequestPreparation importBomRequestPreparation = spdxBOMImporter.prepareImportSpdxBOMAsRelease(sourceFile);
if (RequestStatus.SUCCESS.equals(importBomRequestPreparation.getRequestStatus())) {
String name = importBomRequestPreparation.getName();
String version = importBomRequestPreparation.getVersion();
if (!isDuplicate(name, true)) {
importBomRequestPreparation.setIsComponentDuplicate(false);
importBomRequestPreparation.setIsReleaseDuplicate(false);
} else if (!isDuplicate(name, version)) {
importBomRequestPreparation.setIsComponentDuplicate(true);
importBomRequestPreparation.setIsReleaseDuplicate(false);
} else {
importBomRequestPreparation.setIsComponentDuplicate(true);
importBomRequestPreparation.setIsReleaseDuplicate(true);
}
importBomRequestPreparation.setMessage(sourceFile.getAbsolutePath());
}

return importBomRequestPreparation;
}
} catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}

public RequestSummary importBomFromAttachmentContent(User user, String attachmentContentId) throws SW360Exception {
final AttachmentContent attachmentContent = attachmentConnector.getAttachmentContent(attachmentContentId);
final Duration timeout = Duration.durationOf(30, TimeUnit.SECONDS);
Expand All @@ -2373,10 +2425,24 @@ public RequestSummary importBomFromAttachmentContent(User user, String attachmen
final SpdxBOMImporter spdxBOMImporter = new SpdxBOMImporter(spdxBOMImporterSink);
return spdxBOMImporter.importSpdxBOMAsRelease(inputStream, attachmentContent);
}
} catch (InvalidSPDXAnalysisException | IOException e) {
} catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}

private String getFileType(String fileName) {
if (isNullEmptyOrWhitespace(fileName) || !fileName.contains(".")) {
log.error("Can not get file type from file name - no file extension");
return null;
}
String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if ("xml".equals(ext)) {
if (fileName.endsWith("rdf.xml")) {
ext = "rdf";
}
}
return ext;
}

private void removeLeadingTrailingWhitespace(Release release) {
DatabaseHandlerUtil.trimStringFields(release, listOfStringFieldsInReleaseToTrim);
Expand Down Expand Up @@ -2521,4 +2587,5 @@ public void sendExportSpreadsheetSuccessMail(String url, String recepient) throw
MailConstants.TEXT_SPREADSHEET_EXPORT_SUCCESS, SW360Constants.NOTIFICATION_CLASS_COMPONENT, "", false,
"component", url);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.eclipse.sw360.datahandler.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -44,6 +45,7 @@
import java.util.stream.Collectors;

import org.apache.logging.log4j.Level;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
Expand Down Expand Up @@ -690,7 +692,7 @@ private static boolean isTwoCollectionSame(Collection<?> col1, Collection<?> col
}

private static String getTimeStamp() {
SimpleDateFormat timestampPattern = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat timestampPattern = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
Date timeNow = new Date(System.currentTimeMillis());
return timestampPattern.format(timeNow);
}
Expand Down Expand Up @@ -979,5 +981,15 @@ private static void configureLog4J(String outputpath, String liferayhome) {
.add( builder.newAppenderRef("ChangeLogFile")));
Configurator.reconfigure(builder.build());
}

public static File saveAsTempFile(InputStream inputStream, String prefix, String suffix) throws IOException {
final File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
// Set append to false, overwrite if file existed
try (FileOutputStream outputStream = new FileOutputStream(tempFile, false)) {
IOUtils.copy(inputStream, outputStream);
}
return tempFile;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@
import org.apache.thrift.TException;
import org.eclipse.sw360.spdx.SpdxBOMImporter;
import org.eclipse.sw360.spdx.SpdxBOMImporterSink;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand Down Expand Up @@ -1421,7 +1419,7 @@ public RequestSummary importBomFromAttachmentContent(User user, String attachmen
final SpdxBOMImporter spdxBOMImporter = new SpdxBOMImporter(spdxBOMImporterSink);
return spdxBOMImporter.importSpdxBOMAsProject(inputStream, attachmentContent);
}
} catch (InvalidSPDXAnalysisException | IOException e) {
} catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}
Expand Down
Loading