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