Skip to content

Commit

Permalink
feat(SPDX): Use new SPDX library (#1496)
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Nhu Tuan <tuan2.nguyennhu@toshiba.co.jp>
  • Loading branch information
tuannn2 committed Sep 20, 2022
1 parent 41e654d commit 9ab8eac
Show file tree
Hide file tree
Showing 27 changed files with 725 additions and 106 deletions.
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,51 @@ 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 static void printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
log.info(line);
}
}

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 +2433,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 +2595,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,8 +44,9 @@
import org.apache.thrift.TException;
import org.eclipse.sw360.spdx.SpdxBOMImporter;
import org.eclipse.sw360.spdx.SpdxBOMImporterSink;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;
import org.spdx.tools.InvalidFileNameException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -1421,11 +1422,24 @@ 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());
}
}

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(Project project) {
DatabaseHandlerUtil.trimStringFields(project, listOfStringFieldsInProjToTrim);

Expand Down
Loading

0 comments on commit 9ab8eac

Please sign in to comment.