Search in sources :

Example 21 with SW360Exception

use of org.eclipse.sw360.datahandler.thrift.SW360Exception in project sw360portal by sw360.

the class ReleaseHelper method addFieldValueToRow.

private void addFieldValueToRow(List<String> row, Release._Fields field, Release release) throws SW360Exception {
    switch(field) {
        case COMPONENT_ID:
            // first, add data for given field
            row.add(release.getComponentId());
            // second, add joined data, remark that headers have already been added
            // accordingly
            // add component type in every case
            Component component = this.preloadedComponents.get(release.componentId);
            if (component == null) {
                // maybe cache was not initialized properly, so try to load manually
                try {
                    component = cClient.getComponentById(release.getComponentId(), user);
                } catch (TException e) {
                    log.warn("No component found for id " + release.getComponentId() + " which is set in release with id " + release.getId(), e);
                    component = null;
                }
            }
            // check again and add value
            if (component == null) {
                row.add("");
            } else {
                row.add(ThriftEnumUtils.enumToString(component.getComponentType()));
            }
            // and project origin only if wanted
            if (addAdditionalData()) {
                if (releaseClearingStatusDataByRelease.containsKey(release)) {
                    row.add(releaseClearingStatusDataByRelease.get(release).getProjectNames());
                } else {
                    row.add("");
                }
            }
            break;
        case VENDOR:
            addVendorToRow(release.getVendor(), row);
            break;
        case COTS_DETAILS:
            addCotsDetailsToRow(release.getCotsDetails(), row);
            break;
        case CLEARING_INFORMATION:
            addClearingInformationToRow(release.getClearingInformation(), row);
            break;
        case ECC_INFORMATION:
            addEccInformationToRow(release.getEccInformation(), row);
            break;
        case RELEASE_ID_TO_RELATIONSHIP:
            addReleaseIdToRelationShipToRow(release.getReleaseIdToRelationship(), row);
            break;
        case ATTACHMENTS:
            String size = Integer.toString(release.isSetAttachments() ? release.getAttachments().size() : 0);
            row.add(size);
            break;
        default:
            Object fieldValue = release.getFieldValue(field);
            row.add(fieldValueAsString(fieldValue));
    }
}
Also used : TException(org.apache.thrift.TException) SW360Utils.fieldValueAsString(org.eclipse.sw360.datahandler.common.SW360Utils.fieldValueAsString)

Example 22 with SW360Exception

use of org.eclipse.sw360.datahandler.thrift.SW360Exception in project sw360portal by sw360.

the class ReleaseHelper method batchloadComponents.

private void batchloadComponents(Set<String> cIds) throws SW360Exception {
    try {
        List<Component> componentsShort = cClient.getComponentsShort(cIds);
        this.preloadedComponents.putAll(ThriftUtils.getIdMap(componentsShort));
    } catch (TException e) {
        throw new SW360Exception("Could not get Components for ids [" + cIds + "] because of:\n" + e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) WrappedException.wrapSW360Exception(org.eclipse.sw360.datahandler.common.WrappedException.wrapSW360Exception) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception) WrappedSW360Exception(org.eclipse.sw360.datahandler.common.WrappedException.WrappedSW360Exception)

Example 23 with SW360Exception

use of org.eclipse.sw360.datahandler.thrift.SW360Exception in project sw360portal by sw360.

the class AttachmentStreamConnector method getAttachmentBundleStream.

/**
 * It is highly recommended to close this stream after using to avoid connection leak
 */
public <T> InputStream getAttachmentBundleStream(Set<AttachmentContent> attachments, User user, T context) throws IOException, SW360Exception {
    assertNotNull(context);
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(in);
    new Thread(() -> {
        byte[] buffer = new byte[1024];
        int length;
        try (ZipOutputStream zip = new ZipOutputStream(out)) {
            for (AttachmentContent attachment : attachments) {
                // TODO: handle attachments with equal name
                ZipEntry zipEntry = new ZipEntry(attachment.getFilename());
                zip.putNextEntry(zipEntry);
                try (InputStream attachmentStream = getAttachmentStream(attachment, user, context)) {
                    while ((length = attachmentStream.read(buffer)) >= 0) {
                        zip.write(buffer, 0, length);
                    }
                } catch (TException e) {
                    log.error("failed to get AttachmentStream, maybe due to permission problems", e);
                }
                zip.closeEntry();
            }
        } catch (IOException e) {
            log.error("failed to write zip stream", e);
        }
    }).start();
    return in;
}
Also used : TException(org.apache.thrift.TException) ZipOutputStream(java.util.zip.ZipOutputStream) ConcatClosingInputStream(org.eclipse.sw360.datahandler.common.ConcatClosingInputStream) PipedInputStream(java.io.PipedInputStream) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 24 with SW360Exception

use of org.eclipse.sw360.datahandler.thrift.SW360Exception in project sw360portal by sw360.

the class ComponentDatabaseHandlerTest method addRelease.

private String addRelease(String componentId, Set<String> licenseIds) throws SW360Exception {
    Release release = new Release().setName("REL").setVersion(nextReleaseVersion + "").setMainLicenseIds(licenseIds).setComponentId(componentId);
    nextReleaseVersion++;
    String id = handler.addRelease(release, user1.getEmail()).getId();
    assertNotNull(id);
    return id;
}
Also used : TestUtils.assertTestString(org.eclipse.sw360.datahandler.TestUtils.assertTestString)

Example 25 with SW360Exception

use of org.eclipse.sw360.datahandler.thrift.SW360Exception in project sw360portal by sw360.

the class ProjectDatabaseHandler method getProjectById.

// //////////////////////////
// GET INDIVIDUAL OBJECTS //
// //////////////////////////
public Project getProjectById(String id, User user) throws SW360Exception {
    Project project = repository.get(id);
    assertNotNull(project);
    if (!makePermission(project, user).isActionAllowed(RequestedAction.READ)) {
        throw fail("User " + user + " is not allowed to view the requested project " + project + "!");
    }
    return project;
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project)

Aggregations

SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)23 IOException (java.io.IOException)10 InputStream (java.io.InputStream)9 TException (org.apache.thrift.TException)9 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)9 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)9 ModerationRequest (org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest)8 Release (org.eclipse.sw360.datahandler.thrift.components.Release)5 User (org.eclipse.sw360.datahandler.thrift.users.User)5 Test (org.junit.Test)4 CommonUtils.isTemporaryTodo (org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)3 SW360Utils.fieldValueAsString (org.eclipse.sw360.datahandler.common.SW360Utils.fieldValueAsString)3 Component (org.eclipse.sw360.datahandler.thrift.components.Component)3 JSchException (com.jcraft.jsch.JSchException)2 Session (com.jcraft.jsch.Session)2 PipedInputStream (java.io.PipedInputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 ConcatClosingInputStream (org.eclipse.sw360.datahandler.common.ConcatClosingInputStream)2 WrappedSW360Exception (org.eclipse.sw360.datahandler.common.WrappedException.WrappedSW360Exception)2