Search in sources :

Example 21 with BinaryContentImpl

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

the class TestCswTransformProvider method testMarshalCswRecord.

@Test
public void testMarshalCswRecord() throws Exception {
    when(mockMetacardManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    when(mockCswRecordConverter.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord()), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerBySchema(captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 22 with BinaryContentImpl

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

the class AttributeMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (metacard == null) {
        throw new CatalogTransformerException("No attribute [" + attributeName + "] found in Metacard.");
    }
    LOGGER.debug("Attempting transformation of [{}] with transformer [{}]", metacard, this);
    Attribute attribute = metacard.getAttribute(attributeName);
    if (attribute != null && attribute.getValue() != null) {
        if (byte[].class.isAssignableFrom(attribute.getValue().getClass())) {
            return new BinaryContentImpl(new ByteArrayInputStream((byte[]) attribute.getValue()), mimeType);
        }
        if (String.class.isAssignableFrom(attribute.getValue().getClass())) {
            return new BinaryContentImpl(new ByteArrayInputStream(attribute.getValue().toString().getBytes(StandardCharsets.UTF_8)), mimeType);
        }
    }
    throw new CatalogTransformerException("No attribute [" + attributeName + "] found in Metacard.");
}
Also used : Attribute(ddf.catalog.data.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl)

Example 23 with BinaryContentImpl

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

the class GeometryTransformer method transform.

public BinaryContent transform(Attribute attribute) throws CatalogTransformerException {
    ParserConfigurator parserConfigurator = getParserConfigurator().setHandler(new DefaultValidationEventHandler());
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream(BUFFER_SIZE);
        getParser().marshal(parserConfigurator, GeometryAdapter.marshalFrom(attribute), os);
        ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());
        return new BinaryContentImpl(bais, MIME_TYPE);
    } catch (ParserException e) {
        throw new CatalogTransformerException("Failed to marshall geometry data", e);
    }
}
Also used : ParserConfigurator(org.codice.ddf.parser.ParserConfigurator) ParserException(org.codice.ddf.parser.ParserException) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler)

Example 24 with BinaryContentImpl

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

the class XmlMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (metacard == null) {
        LOGGER.debug("Attempted to transform null metacard");
        throw new CatalogTransformerException("Unable to transform null metacard");
    }
    try {
        String xmlString = metacardMarshaller.marshal(metacard, arguments);
        ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8));
        return new BinaryContentImpl(bais, MIME_TYPE);
    } catch (XmlPullParserException | IOException e) {
        throw new CatalogTransformerException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) IOException(java.io.IOException)

Example 25 with BinaryContentImpl

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

the class RESTEndpoint method getDocument.

/**
     * REST Get. Retrieves information regarding sources available.
     *
     * @param uriInfo
     * @param httpRequest
     * @return
     */
@GET
@Path(SOURCES_PATH)
public Response getDocument(@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    BinaryContent content;
    ResponseBuilder responseBuilder;
    String sourcesString = null;
    JSONArray resultsList = new JSONArray();
    SourceInfoResponse sources;
    try {
        SourceInfoRequestEnterprise sourceInfoRequestEnterprise = new SourceInfoRequestEnterprise(true);
        sources = catalogFramework.getSourceInfo(sourceInfoRequestEnterprise);
        for (SourceDescriptor source : sources.getSourceInfo()) {
            JSONObject sourceObj = new JSONObject();
            sourceObj.put("id", source.getSourceId());
            sourceObj.put("version", source.getVersion() != null ? source.getVersion() : "");
            sourceObj.put("available", Boolean.valueOf(source.isAvailable()));
            JSONArray contentTypesObj = new JSONArray();
            if (source.getContentTypes() != null) {
                for (ContentType contentType : source.getContentTypes()) {
                    if (contentType != null && contentType.getName() != null) {
                        JSONObject contentTypeObj = new JSONObject();
                        contentTypeObj.put("name", contentType.getName());
                        contentTypeObj.put("version", contentType.getVersion() != null ? contentType.getVersion() : "");
                        contentTypesObj.add(contentTypeObj);
                    }
                }
            }
            sourceObj.put("contentTypes", contentTypesObj);
            resultsList.add(sourceObj);
        }
    } catch (SourceUnavailableException e) {
        LOGGER.info("Unable to retrieve Sources. {}", e.getMessage());
        LOGGER.debug("Unable to retrieve Sources", e);
    }
    sourcesString = JSONValue.toJSONString(resultsList);
    content = new BinaryContentImpl(new ByteArrayInputStream(sourcesString.getBytes(StandardCharsets.UTF_8)), jsonMimeType);
    responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
    // Add the Accept-ranges header to let the client know that we accept ranges in bytes
    responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
    return responseBuilder.build();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) JSONObject(net.minidev.json.JSONObject) ContentType(ddf.catalog.data.ContentType) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONArray(net.minidev.json.JSONArray) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

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