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;
}
}
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));
}
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));
}
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));
}
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);
}
}
Aggregations