Search in sources :

Example 1 with BinaryContentImpl

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

the class TestCswQueryResponseTransformer method verifyResultOrderIsMaintained.

@Test
public void verifyResultOrderIsMaintained() throws CatalogTransformerException, IOException {
    // when
    when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(mockPrintWriter);
    when(mockPrintWriter.makeString()).thenReturn(new String());
    when(mockSourceResponse.getResults()).thenReturn(createResults(1, 10));
    when(mockSourceResponse.getRequest()).thenReturn(mockQueryRequest);
    when(mockQueryRequest.getQuery()).thenReturn(mockQuery);
    when(mockArguments.get(CswConstants.RESULT_TYPE_PARAMETER)).thenReturn(ResultType.RESULTS);
    when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenAnswer(invocationOnMock -> {
        Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
        BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ","), new MimeType("application/xml"));
        return bci;
    });
    // given
    transformer.init();
    transformer.transform(mockSourceResponse, mockArguments);
    transformer.destroy();
    // then
    ArgumentCaptor<String> tmCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockTransformerManager, times(1)).getTransformerBySchema(tmCaptor.capture());
    ArgumentCaptor<Map> mapCaptor = ArgumentCaptor.forClass(Map.class);
    ArgumentCaptor<Metacard> mcCaptor = ArgumentCaptor.forClass(Metacard.class);
    verify(mockMetacardTransformer, times(10)).transform(mcCaptor.capture(), mapCaptor.capture());
    ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockPrintWriter, times(2)).setRawValue(strCaptor.capture());
    String order = strCaptor.getAllValues().get(1);
    String[] ids = order.split(",");
    for (int i = 1; i < ids.length; i++) {
        assertThat(ids[i - 1], is(String.valueOf("id_" + i)));
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 2 with BinaryContentImpl

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

the class PropertyJsonMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    Map<String, Object> rootObject = convertToJSON(metacard);
    String jsonText = json.serialize(rootObject).toString();
    return new BinaryContentImpl(new ByteArrayInputStream(jsonText.getBytes(StandardCharsets.UTF_8)), DEFAULT_MIME_TYPE);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl)

Example 3 with BinaryContentImpl

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

the class TestCswTransformProvider method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.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);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_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 4 with BinaryContentImpl

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

the class ConfigurationStore method getDocument.

@GET
@Path("/config")
public Response getDocument(@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    Response response;
    Map<String, Object> config = new HashMap<>();
    config.put("branding", getProductName());
    config.put("version", getProductVersion());
    config.put("showWelcome", isSignIn);
    config.put("showTask", isTask);
    config.put("format", format);
    config.put("timeout", timeout);
    config.put("resultCount", resultCount);
    config.put("typeNameMapping", typeNameMapping);
    config.put("terrainProvider", proxiedTerrainProvider);
    config.put("imageryProviders", getProxiedImageryProviders());
    config.put("gazetteer", isGazetteer);
    config.put("showIngest", isIngest);
    config.put("projection", projection);
    config.put("helpUrl", helpUrl);
    config.put("bingKey", bingKey);
    config.put("externalAuthentication", isExternalAuthentication);
    String configJson = toJson(config);
    BinaryContent content = new BinaryContentImpl(new ByteArrayInputStream(configJson.getBytes(StandardCharsets.UTF_8)), jsonMimeType);
    response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
    return response;
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with BinaryContentImpl

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

the class XmlResponseQueueTransformer method transform.

@Override
public BinaryContent transform(SourceResponse response, Map<String, Serializable> args) throws CatalogTransformerException {
    try {
        PrintWriter writer = printWriterProvider.build(Metacard.class);
        writer.setRawValue("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
        writer.startNode("metacards");
        for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
            writer.addAttribute(nsRow.getKey(), nsRow.getValue());
        }
        if (response.getResults() != null && !response.getResults().isEmpty()) {
            StringWriter metacardContent = fjp.invoke(new MetacardForkTask(ImmutableList.copyOf(response.getResults()), fjp, geometryTransformer, threshold, metacardMarshaller));
            writer.setRawValue(metacardContent.getBuffer().toString());
        }
        // metacards
        writer.endNode();
        ByteArrayInputStream bais = new ByteArrayInputStream(writer.makeString().getBytes(StandardCharsets.UTF_8));
        return new BinaryContentImpl(bais, mimeType);
    } catch (Exception e) {
        LOGGER.info("Failed Query response transformation", e);
        throw new CatalogTransformerException("Failed Query response transformation");
    }
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) MimeTypeParseException(javax.activation.MimeTypeParseException) PrintWriter(ddf.catalog.transformer.api.PrintWriter)

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