Search in sources :

Example 41 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class BinaryContentStringTypeConverter method convertTo.

@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    String mimeTypeString = exchange.getOut().getHeader(HttpHeaders.CONTENT_TYPE, String.class);
    if (null == mimeTypeString) {
        mimeTypeString = MediaType.TEXT_PLAIN;
    }
    MimeType mimeType = null;
    try {
        mimeType = new MimeType(mimeTypeString);
    } catch (MimeTypeParseException e) {
        LOGGER.debug("Failed to parse mimetype: " + mimeTypeString, e);
    }
    T result = null;
    try {
        result = type.cast(new BinaryContentImpl(exchange.getOut().getBody(InputStream.class), mimeType));
    } catch (ClassCastException e) {
        LOGGER.debug("Failed to create BinaryContent", e);
    }
    return result;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) InputStream(java.io.InputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MimeType(javax.activation.MimeType)

Example 42 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class CatalogComponentTest method testTransformMetacard.

@Test
public void testTransformMetacard() throws Exception {
    LOGGER.debug("Running testTransformMetacard()");
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(1);
    // Mock a XML InputTransformer and register it in the OSGi Registry
    // (PojoSR)
    InputTransformer mockTransformer = getMockInputTransformer();
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(MimeTypeToTransformerMapper.ID_KEY, "xml");
    props.put(MimeTypeToTransformerMapper.MIME_TYPE_KEY, "text/xml");
    bundleContext.registerService(InputTransformer.class.getName(), mockTransformer, props);
    // Mock the MimeTypeToTransformerMapper and register it in the OSGi
    // Registry (PojoSR)
    MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
    // HUGH bundleContext.registerService(
    // MimeTypeToTransformerMapper.class.getName(), matchingService, null );
    catalogComponent.setMimeTypeToTransformerMapper(matchingService);
    // Mock the MimeTypeToTransformerMapper returning the mock XML
    // InputTransformer
    when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn((List) Arrays.asList(mockTransformer));
    // Send in sample XML as InputStream to InputTransformer
    InputStream input = IOUtils.toInputStream(xmlInput);
    // Get the InputTransformer registered with the ID associated with the
    // <from> node in the Camel route
    InputTransformer transformer = getTransformer("text/xml", "identity");
    assertNotNull("InputTransformer for mimeType=text/xml&id=identity not found", transformer);
    // Transform the XML input into a Metacard
    Metacard metacard = transformer.transform(input);
    assertNotNull(metacard);
    assertMockEndpointsSatisfied();
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) Metacard(ddf.catalog.data.Metacard) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 43 with MimeType

use of javax.activation.MimeType 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 44 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class ResourceCacheImplTest method createCachedResource.

private ReliableResource createCachedResource(Metacard metacard) {
    String fileName = "15bytes.txt";
    String productLocation = System.getProperty("user.dir") + "/src/test/resources/" + fileName;
    File rrCachedFile = new File(productLocation);
    return new ReliableResource(CACHED_RESOURCE_KEY, rrCachedFile.getAbsolutePath(), new MimeType(), fileName, metacard);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) ReliableResource(ddf.catalog.resource.data.ReliableResource) MimeType(javax.activation.MimeType)

Example 45 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class ResourceCacheImplSizeLimitTest method simulateAddFileToProductCache.

private ReliableResource simulateAddFileToProductCache(String key, String fileName, String destFileName, IMap<String, ReliableResource> cacheMap) throws IOException {
    String productOriginalLocation = new File(this.getClass().getClassLoader().getResource(fileName).getPath()).getAbsolutePath();
    File rrCachedFile = new File(productCacheDir + "/" + destFileName);
    FileUtils.copyFile(new File(productOriginalLocation), rrCachedFile);
    ReliableResource rr = new ReliableResource(key, rrCachedFile.getAbsolutePath(), new MimeType(), fileName, new MetacardImpl());
    rr.setSize(rrCachedFile.length());
    LOGGER.debug("adding entry to cache: {}", key);
    cacheMap.put(key, rr);
    listener.entryAdded(new EntryEvent<Object, Object>(destFileName, null, EntryEventType.ADDED.getType(), key, rr));
    return rr;
}
Also used : File(java.io.File) ReliableResource(ddf.catalog.resource.data.ReliableResource) MimeType(javax.activation.MimeType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Aggregations

MimeType (javax.activation.MimeType)67 Test (org.junit.Test)38 Metacard (ddf.catalog.data.Metacard)21 URI (java.net.URI)14 HashMap (java.util.HashMap)14 MimeTypeParseException (javax.activation.MimeTypeParseException)14 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)13 BundleContext (org.osgi.framework.BundleContext)13 ServiceReference (org.osgi.framework.ServiceReference)13 CatalogFramework (ddf.catalog.CatalogFramework)10 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 Serializable (java.io.Serializable)9 Resource (ddf.catalog.resource.Resource)8 IOException (java.io.IOException)8 File (java.io.File)7 Matchers.anyString (org.mockito.Matchers.anyString)7 MetacardCreationException (ddf.catalog.data.MetacardCreationException)6 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 Response (javax.ws.rs.core.Response)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)5