Search in sources :

Example 81 with Attachment

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

the class CLIParserTest method testGetCLIFailsOnMalformedXML.

@Test
public void testGetCLIFailsOnMalformedXML() throws Exception {
    Attachment cliAttachment = new Attachment("A1", "a.xml");
    when(connector.getAttachmentStream(anyObject(), anyObject(), anyObject())).thenReturn(new ReaderInputStream(new StringReader(CLI_TESTFILE.replaceAll("</Content>", "</Broken>"))));
    LicenseInfoParsingResult res = parser.getLicenseInfos(cliAttachment, new User(), new Project()).stream().findFirst().orElseThrow(() -> new RuntimeException("Parser returned empty LisenceInfoParsingResult list"));
    assertLicenseInfoParsingResult(res, LicenseInfoRequestStatus.FAILURE);
    assertThat(res.getStatus(), is(LicenseInfoRequestStatus.FAILURE));
    assertThat(res.getLicenseInfo(), notNullValue());
    assertThat(res.getLicenseInfo().getFilenames(), contains("a.xml"));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) StringReader(java.io.StringReader) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) TestHelper.assertLicenseInfoParsingResult(org.eclipse.sw360.licenseinfo.TestHelper.assertLicenseInfoParsingResult) Test(org.junit.Test)

Example 82 with Attachment

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

the class ProjectHandler method removeAttachmentFromProject.

@Override
public RequestStatus removeAttachmentFromProject(String projectId, User user, String attachmentContentId) throws TException {
    Project projectByIdForEdit = getProjectByIdForEdit(projectId, user);
    Set<Attachment> attachments = projectByIdForEdit.getAttachments();
    Optional<Attachment> attachmentOptional = CommonUtils.getAttachmentOptional(attachmentContentId, attachments);
    if (attachmentOptional.isPresent()) {
        attachments.remove(attachmentOptional.get());
        return updateProject(projectByIdForEdit, user);
    } else {
        return RequestStatus.SUCCESS;
    }
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 83 with Attachment

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

the class ModerationRequestGenerator method dealWithAttachments.

protected void dealWithAttachments(U attachmentField) {
    Set<Attachment> actualAttachments = (Set<Attachment>) actualDocument.getFieldValue(attachmentField);
    Set<Attachment> updateAttachments = (Set<Attachment>) updateDocument.getFieldValue(attachmentField);
    Map<String, Attachment> actualAttachmentMap = Maps.uniqueIndex(actualAttachments, Attachment::getAttachmentContentId);
    Set<String> actualAttachmentIds = actualAttachmentMap.keySet();
    Map<String, Attachment> updateAttachmentMap = Maps.uniqueIndex(updateAttachments, Attachment::getAttachmentContentId);
    Set<String> updateAttachmentIds = updateAttachmentMap.keySet();
    Set<Attachment> attachmentAdditions = updateAttachmentMap.values().stream().filter(attachment -> !actualAttachmentIds.contains(attachment.getAttachmentContentId())).collect(Collectors.toSet());
    Set<Attachment> attachmentDeletions = actualAttachmentMap.values().stream().filter(attachment -> !updateAttachmentIds.contains(attachment.getAttachmentContentId())).collect(Collectors.toSet());
    // determine changes in common attachments
    Set<String> commonAttachmentIds = Sets.intersection(actualAttachmentIds, updateAttachmentIds);
    for (String id : commonAttachmentIds) {
        Attachment actual = actualAttachmentMap.get(id);
        Attachment update = updateAttachmentMap.get(id);
        if (actual != null && !actual.equals(update)) {
            attachmentAdditions.add(getAdditionsFromCommonAttachment(actual, update));
            attachmentDeletions.add(getDeletionsFromCommonAttachment(actual, update));
        }
    }
    documentAdditions.setFieldValue(attachmentField, attachmentAdditions);
    documentDeletions.setFieldValue(attachmentField, attachmentDeletions);
}
Also used : Set(java.util.Set) TEnum(org.apache.thrift.TEnum) HashMap(java.util.HashMap) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) CommonUtils.add(org.eclipse.sw360.datahandler.common.CommonUtils.add) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) Logger(org.apache.log4j.Logger) ModerationRequest(org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Map(java.util.Map) TType(org.apache.thrift.protocol.TType) CommonUtils.nullToEmptyMap(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyMap) TFieldIdEnum(org.apache.thrift.TFieldIdEnum) TBase(org.apache.thrift.TBase) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) CommonUtils.nullToEmptySet(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet) Set(java.util.Set) CommonUtils.nullToEmptySet(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 84 with Attachment

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

the class ComponentController method addAttachmentToComponent.

@RequestMapping(value = COMPONENTS_URL + "/{componentId}/attachments", method = RequestMethod.POST, consumes = { "multipart/mixed", "multipart/form-data" })
public ResponseEntity<HalResource> addAttachmentToComponent(@PathVariable("componentId") String componentId, OAuth2Authentication oAuth2Authentication, @RequestPart("file") MultipartFile file, @RequestPart("attachment") Attachment newAttachment) throws TException {
    final User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    Attachment attachment;
    try {
        attachment = attachmentService.uploadAttachment(file, newAttachment, sw360User);
    } catch (IOException e) {
        log.error("failed to upload attachment", e);
        throw new RuntimeException("failed to upload attachment", e);
    }
    final Component component = componentService.getComponentForUserById(componentId, sw360User);
    component.addToAttachments(attachment);
    componentService.updateComponent(component, sw360User);
    final HalResource halRelease = createHalComponent(component, sw360User);
    return new ResponseEntity<>(halRelease, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) HalResource(org.eclipse.sw360.rest.resourceserver.core.HalResource) IOException(java.io.IOException) Component(org.eclipse.sw360.datahandler.thrift.components.Component)

Example 85 with Attachment

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

the class ProjectController method addAttachmentToProject.

@RequestMapping(value = PROJECTS_URL + "/{projectId}/attachments", method = RequestMethod.POST, consumes = { "multipart/mixed", "multipart/form-data" })
public ResponseEntity<HalResource> addAttachmentToProject(@PathVariable("projectId") String projectId, OAuth2Authentication oAuth2Authentication, @RequestPart("file") MultipartFile file, @RequestPart("attachment") Attachment newAttachment) throws TException {
    final User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    Attachment attachment;
    try {
        attachment = attachmentService.uploadAttachment(file, newAttachment, sw360User);
    } catch (IOException e) {
        log.error(e.getMessage());
        throw new RuntimeException(e);
    }
    final Project project = projectService.getProjectForUserById(projectId, sw360User);
    project.addToAttachments(attachment);
    projectService.updateProject(project, sw360User);
    final HalResource<Project> halResource = createHalProject(project, sw360User);
    return new ResponseEntity<>(halResource, HttpStatus.OK);
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) IOException(java.io.IOException)

Aggregations

Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)65 Test (org.junit.Test)40 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)38 User (org.eclipse.sw360.datahandler.thrift.users.User)27 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)22 InputStream (java.io.InputStream)19 Release (org.eclipse.sw360.datahandler.thrift.components.Release)15 IOException (java.io.IOException)13 PortletRequest (javax.portlet.PortletRequest)11 TestUserCacheHolder (org.eclipse.sw360.portal.TestUserCacheHolder)9 StringReader (java.io.StringReader)8 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)8 TException (org.apache.thrift.TException)8 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)8 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)7 OutputStream (java.io.OutputStream)5 AttachmentInputStream (org.ektorp.AttachmentInputStream)5 ResponseEntity (org.springframework.http.ResponseEntity)5 ComponentService (org.eclipse.sw360.datahandler.thrift.components.ComponentService)4 Maps (com.google.common.collect.Maps)3