Search in sources :

Example 1 with AttachmentInfo

use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.

the class AttachmentParserImplTest method testWithoutFilenameAndUnrecognizedContentType.

@Test
public void testWithoutFilenameAndUnrecognizedContentType() throws IOException {
    String unrecognizedContentType = "foo/bar";
    try (InputStream inputStream = createTestInputStream()) {
        AttachmentInfo attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, unrecognizedContentType, null);
        assertThat(attachmentInfo.getFilename(), is("file.bin"));
        assertThat(attachmentInfo.getContentType(), is(unrecognizedContentType));
        assertThat(attachmentInfo.getStream(), is(inputStream));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) Test(org.junit.Test)

Example 2 with AttachmentInfo

use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.

the class AttachmentParserImplTest method testWithoutFilename.

@Test
public void testWithoutFilename() throws IOException {
    try (InputStream inputStream = createTestInputStream()) {
        AttachmentInfo attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, TEXT_PLAIN, null);
        assertThat(attachmentInfo.getFilename(), is("file." + TEXT_EXT));
        assertThat(attachmentInfo.getContentType(), is(TEXT_PLAIN));
        assertThat(attachmentInfo.getStream(), is(inputStream));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) Test(org.junit.Test)

Example 3 with AttachmentInfo

use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.

the class CatalogServiceImplTest method testParseAttachmentsTooLarge.

@Test
@SuppressWarnings({ "unchecked" })
public void testParseAttachmentsTooLarge() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException {
    CatalogFramework framework = givenCatalogFramework();
    BundleContext bundleContext = mock(BundleContext.class);
    Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
    ServiceReference serviceReference = mock(ServiceReference.class);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setMetadata("Some Text Again");
    when(inputTransformer.transform(any())).thenReturn(metacard);
    when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
    serviceReferences.add(serviceReference);
    when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
    CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
    when(attributeRegistry.lookup("foo")).thenReturn(Optional.empty());
    addMatchingService(catalogServiceImpl, Collections.singletonList(getSimpleTransformer()));
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
    Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
    attachments.add(attachment);
    ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
    Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
    attachments.add(attachment1);
    Pair<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, "xml");
    assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
    ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
    Attachment attachment2 = new Attachment("metadata", new ByteArrayInputStream(Strings.repeat("hi", 100_000).getBytes()), contentDisposition2);
    attachments.add(attachment2);
    ContentDisposition contentDisposition3 = new ContentDisposition("form-data; name=foo; filename=C:\\DDF\\metacard.xml");
    Attachment attachment3 = new Attachment("foo", new ByteArrayInputStream("bar".getBytes()), contentDisposition3);
    attachments.add(attachment3);
    attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, "xml");
    // Ensure that the metadata was not overriden because it was too large to be parsed
    assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
    assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
Also used : CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogFramework(ddf.catalog.CatalogFramework) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 4 with AttachmentInfo

use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.

the class CatalogServiceImplTest method testParsePartsTooLarge.

@Test
@SuppressWarnings({ "unchecked" })
public void testParsePartsTooLarge() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException, ServletException {
    CatalogFramework framework = givenCatalogFramework();
    BundleContext bundleContext = mock(BundleContext.class);
    Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
    ServiceReference serviceReference = mock(ServiceReference.class);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setMetadata("Some Text Again");
    when(inputTransformer.transform(any())).thenReturn(metacard);
    when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
    serviceReferences.add(serviceReference);
    when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
    CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
    when(attributeRegistry.lookup("foo")).thenReturn(Optional.empty());
    addMatchingService(catalogServiceImpl, Collections.singletonList(getSimpleTransformer()));
    List<Part> parts = new ArrayList<>();
    Part part = createPart("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), "form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
    Part part1 = createPart("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), "form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
    parts.add(part);
    parts.add(part1);
    HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
    when(httpServletRequest.getParts()).thenReturn(parts);
    Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
    assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
    Part part2 = createPart("metadata", new ByteArrayInputStream(Strings.repeat("hi", 100_000).getBytes()), "form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
    Part part3 = createPart("foo", new ByteArrayInputStream("bar".getBytes()), "form-data; name=foo; filename=C:\\DDF\\metacard.xml");
    parts.add(part2);
    parts.add(part3);
    attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
    // Ensure that the metadata was not overriden because it was too large to be parsed
    assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
    assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
Also used : CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) HttpServletRequest(javax.servlet.http.HttpServletRequest) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(javax.servlet.http.Part) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 5 with AttachmentInfo

use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.

the class CatalogServiceImplTest method testParseParts.

@Test
@SuppressWarnings({ "unchecked" })
public void testParseParts() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException, ServletException {
    CatalogFramework framework = givenCatalogFramework();
    BundleContext bundleContext = mock(BundleContext.class);
    Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
    ServiceReference serviceReference = mock(ServiceReference.class);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setMetadata("Some Text Again");
    when(inputTransformer.transform(any())).thenReturn(metacard);
    when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
    serviceReferences.add(serviceReference);
    when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
    CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
    when(attributeRegistry.lookup("foo")).thenReturn(Optional.empty());
    addMatchingService(catalogServiceImpl, Collections.singletonList(getSimpleTransformer()));
    List<Part> parts = new ArrayList<>();
    Part part = createPart("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), "form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
    Part part1 = createPart("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), "form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
    parts.add(part);
    parts.add(part1);
    HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
    when(httpServletRequest.getParts()).thenReturn(parts);
    Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
    assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
    Part part2 = createPart("metadata", new ByteArrayInputStream("<meta>beta</meta>".getBytes()), "form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
    Part part3 = createPart("foo", new ByteArrayInputStream("bar".getBytes()), "form-data; name=foo; filename=C:\\DDF\\metacard.xml");
    parts.add(part2);
    parts.add(part3);
    attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
    assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("<meta>beta</meta>"));
    assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
Also used : CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) HttpServletRequest(javax.servlet.http.HttpServletRequest) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(javax.servlet.http.Part) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

AttachmentInfo (org.codice.ddf.attachment.AttachmentInfo)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Test (org.junit.Test)9 Metacard (ddf.catalog.data.Metacard)8 CatalogFramework (ddf.catalog.CatalogFramework)6 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6 CoreAttributes (ddf.catalog.data.impl.types.CoreAttributes)6 ArrayList (java.util.ArrayList)6 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 InputTransformer (ddf.catalog.transform.InputTransformer)4 Part (javax.servlet.http.Part)4 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)4 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Attribute (ddf.catalog.data.Attribute)2 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)2