Search in sources :

Example 11 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project ddf by codice.

the class CatalogFrameworkImplTest method testQueryTransform.

@Test
public void testQueryTransform() throws Exception {
    BundleContext context = mock(BundleContext.class);
    QueryResponseTransformer transformer = mock(QueryResponseTransformer.class);
    ServiceReference reference = mock(ServiceReference.class);
    ServiceReference[] serviceReferences = new ServiceReference[] { reference };
    when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
    when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
    when(transformer.transform(isA(SourceResponse.class), isA(Map.class))).thenReturn(new BinaryContentImpl(null));
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
    SourceResponse response = new SourceResponseImpl(null, null);
    BinaryContent content = framework.transform(response, "NONE", new HashMap<String, Serializable>());
    assertNotNull(content);
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) ServiceReference(org.osgi.framework.ServiceReference) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 12 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project ddf by codice.

the class CswRecordConverter method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (StringUtils.isNotBlank(metacard.getMetadata())) {
        // Check if the metadata is csw:Record
        try {
            StringReader xml = new StringReader(metacard.getMetadata());
            XMLEventReader reader = factory.createXMLEventReader(xml);
            boolean rootFound = false;
            while (reader.hasNext() && !rootFound) {
                XMLEvent event = reader.nextEvent();
                if (event.isStartElement()) {
                    rootFound = true;
                    QName name = event.asStartElement().getName();
                    if (StringUtils.equals(CswConstants.CSW_RECORD_LOCAL_NAME, name.getLocalPart()) && StringUtils.equals(CswConstants.CSW_OUTPUT_SCHEMA, name.getNamespaceURI())) {
                        return new BinaryContentImpl(IOUtils.toInputStream(metacard.getMetadata()), XML_MIME_TYPE);
                    }
                }
            }
        } catch (Exception e) {
        // Ignore and proceed with the transform.
        }
    }
    StringWriter stringWriter = new StringWriter();
    Boolean omitXmlDec = null;
    if (arguments != null) {
        omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
    }
    if (omitXmlDec == null || !omitXmlDec) {
        stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
    }
    PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.WRITE_NAMESPACES, true);
    copyArgumentsToContext(context, arguments);
    this.marshal(metacard, writer, context);
    BinaryContent transformedContent;
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
    transformedContent = new BinaryContentImpl(byteArrayInputStream, XML_MIME_TYPE);
    return transformedContent;
}
Also used : QName(javax.xml.namespace.QName) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) XStreamException(com.thoughtworks.xstream.XStreamException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext)

Example 13 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project ddf by codice.

the class AbstractGmdTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    StringWriter stringWriter = new StringWriter();
    Boolean omitXmlDec = null;
    if (MapUtils.isNotEmpty(arguments)) {
        omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
    }
    if (omitXmlDec == null || !omitXmlDec) {
        stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
    }
    PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter, new NoNameCoder());
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    copyArgumentsToContext(context, arguments);
    converterSupplier.get().marshal(metacard, writer, context);
    ByteArrayInputStream bais = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
    return new BinaryContentImpl(bais, CswRecordConverter.XML_MIME_TYPE);
}
Also used : TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder)

Example 14 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project alliance by codice.

the class CatalogOutputAdapter method getBinaryContent.

/**
 * @param image the BufferedImage to be converted.
 * @return a BinaryContent object containing the image data.
 * @throws IOException when the BufferedImage can't be written to temporary in-memory space.
 * @throws MimeTypeParseException thrown if the mime type is invalid
 */
@SuppressWarnings("WeakerAccess")
public BinaryContent getBinaryContent(BufferedImage image) throws IOException, MimeTypeParseException {
    validateArgument(image, "image");
    BufferedImage rgbImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D graphics = rgbImage.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    InputStream fis = new ByteArrayInputStream(createJpg(rgbImage));
    return new BinaryContentImpl(fis, new MimeType(IMAGE_JPG));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BufferedImage(java.awt.image.BufferedImage) MimeType(javax.activation.MimeType) Graphics2D(java.awt.Graphics2D)

Example 15 with BinaryContentImpl

use of ddf.catalog.data.impl.BinaryContentImpl in project ddf by codice.

the class BinaryContentImplTest method testBinaryContentImplNullInputStream.

@Test
public void testBinaryContentImplNullInputStream() {
    InputStream is = null;
    BinaryContentImpl bci = new BinaryContentImpl(is, mimeType);
    bci.setSize(content.length());
    // assertEquals(is, bci.getInputStream());
    assertEquals(mimeType, bci.getMimeType());
    assertEquals(content.length(), bci.getSize());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Test(org.junit.Test)

Aggregations

BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)33 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)12 Test (org.junit.Test)12 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)11 IOException (java.io.IOException)11 BinaryContent (ddf.catalog.data.BinaryContent)10 HashMap (java.util.HashMap)10 FileInputStream (java.io.FileInputStream)9 Metacard (ddf.catalog.data.Metacard)8 MimeType (javax.activation.MimeType)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)6 Result (ddf.catalog.data.Result)5 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)4 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)4 StringWriter (java.io.StringWriter)4 MimeTypeParseException (javax.activation.MimeTypeParseException)4 Failure (org.junit.runner.notification.Failure)4 CatalogFramework (ddf.catalog.CatalogFramework)3