Search in sources :

Example 16 with AttachmentContent

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

the class AttachmentPortletUtils method getAttachmentContent.

private AttachmentContent getAttachmentContent(ResumableUpload resumableUpload, InputStream stream) throws IOException, TException {
    if (!resumableUpload.isValid()) {
        return null;
    }
    final AttachmentContent attachment = getAttachmentContent(resumableUpload);
    if (resumableUpload.getChunkNumber() == 1) {
        String fileName = resumableUpload.getFilename();
        String contentType = resumableUpload.getFileType();
        if (isNullOrEmpty(contentType)) {
            contentType = guessContentTypeFromStream(stream);
        }
        if (isNullOrEmpty(contentType)) {
            contentType = guessContentTypeFromName(fileName);
        }
        if (isNullOrEmpty(contentType)) {
            contentType = "text";
        }
        int partsCount = resumableUpload.getTotalChunks();
        attachment.setContentType(contentType).setFilename(fileName).setOnlyRemote(false).setPartsCount(Integer.toString(partsCount));
        return updateAttachmentContent(attachment);
    } else {
        return attachment;
    }
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 17 with AttachmentContent

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

the class FossologyUploaderTest method testUploadToFossologyWithBadReturn.

@Test
public void testUploadToFossologyWithBadReturn() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.getId()).thenReturn("id");
    when(attachment.getFilename()).thenReturn("fileName");
    String clearingTeam = "cl";
    final InputStream inputStream = mock(InputStream.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            OutputStream outputStream = (OutputStream) invocation.getArguments()[2];
            outputStream.write("error".getBytes());
            return 0;
        }
    }).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam);
    verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    assertThat(uploadId, is((long) -1));
}
Also used : Answer(org.mockito.stubbing.Answer) InputStream(java.io.InputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OutputStream(java.io.OutputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 18 with AttachmentContent

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

the class FossologyUploaderTest method testUploadToFossologyWithEmptyId.

@Test
public void testUploadToFossologyWithEmptyId() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.getId()).thenReturn(null);
    when(attachment.getFilename()).thenReturn("fileName");
    String clearingTeam = "cl";
    final InputStream inputStream = mock(InputStream.class);
    final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam);
    verify(sshConnector, never()).runInFossologyViaSsh(anyString(), any(InputStream.class), any(OutputStream.class));
    assertThat(uploadId, is((long) -1));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 19 with AttachmentContent

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

the class FossologyUploaderTest method testUploadToFossologyReportsErrors.

@Test
public void testUploadToFossologyReportsErrors() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.getId()).thenReturn("id");
    when(attachment.getFilename()).thenReturn("fileName");
    String clearingTeam = "cl";
    final InputStream inputStream = mock(InputStream.class);
    doReturn(-1).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam);
    verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    assertThat(uploadId, is((long) -1));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 20 with AttachmentContent

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

the class AbstractCLIParser method hasThisXMLRootElement.

protected <T> boolean hasThisXMLRootElement(AttachmentContent content, String rootElementNamespace, String rootElementName, User user, T context) throws TException {
    XMLInputFactory xmlif = XMLInputFactory.newFactory();
    XMLStreamReader xmlStreamReader = null;
    InputStream attachmentStream = null;
    try {
        attachmentStream = attachmentConnector.getAttachmentStream(content, user, context);
        xmlStreamReader = xmlif.createXMLStreamReader(attachmentStream);
        // skip to first element
        while (xmlStreamReader.hasNext() && xmlStreamReader.next() != XMLStreamConstants.START_ELEMENT) ;
        xmlStreamReader.require(XMLStreamConstants.START_ELEMENT, rootElementNamespace, rootElementName);
        return true;
    } catch (XMLStreamException | SW360Exception e) {
        return false;
    } finally {
        if (null != xmlStreamReader) {
            try {
                xmlStreamReader.close();
            } catch (XMLStreamException e) {
            // ignore it
            }
        }
        closeQuietly(attachmentStream, log);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Aggregations

AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)52 InputStream (java.io.InputStream)24 Test (org.junit.Test)20 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)13 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)11 IOException (java.io.IOException)10 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)10 User (org.eclipse.sw360.datahandler.thrift.users.User)10 Release (org.eclipse.sw360.datahandler.thrift.components.Release)8 TException (org.apache.thrift.TException)7 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)6 AttachmentInputStream (org.ektorp.AttachmentInputStream)6 OutputStream (java.io.OutputStream)5 FilledAttachment (org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment)5 StringReader (java.io.StringReader)4 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)4 RequestSummary (org.eclipse.sw360.datahandler.thrift.RequestSummary)4 DatabaseConnector (org.eclipse.sw360.datahandler.couchdb.DatabaseConnector)3 LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)3 PipedInputStream (java.io.PipedInputStream)2