Search in sources :

Example 61 with MimeType

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

the class MimeTypeToTransformerMapperImpl method findMatches.

@Override
public <T> List<T> findMatches(Class<T> clazz, MimeType userMimeType) {
    BundleContext bundleContext = getContext();
    ServiceReference[] refs = null;
    List<T> list = new ArrayList<T>();
    if (bundleContext == null) {
        LOGGER.debug("Cannot find matches, bundle context is null.");
        return list;
    }
    if (clazz == null) {
        LOGGER.debug("Cannot find matches, service argument is null.");
        throw new IllegalArgumentException("Invalid argument supplied, null service argument");
    }
    /*
         * Extract the services using the bundle context.
         */
    try {
        refs = bundleContext.getServiceReferences(clazz.getName(), null);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid syntax supplied: " + userMimeType.toString());
    }
    // If no InputTransformers found, return empty list
    if (refs == null) {
        LOGGER.debug("No {} services found - return empty list", clazz.getName());
        return list;
    }
    /*
         * Sort the list of service references based in it's Comparable interface.
         */
    Arrays.sort(refs, Collections.reverseOrder());
    /*
         * If the mime type is null return the whole list of service references
         */
    if (userMimeType == null) {
        if (refs.length > 0) {
            for (ServiceReference ref : refs) {
                Object service = (bundleContext.getService(ref));
                T typedService = clazz.cast(service);
                list.add(typedService);
            }
        }
        return list;
    }
    String userIdValue = userMimeType.getParameter(MimeTypeToTransformerMapper.ID_KEY);
    List<T> strictlyMatching = new ArrayList<T>();
    for (ServiceReference ref : refs) {
        List<String> mimeTypesServicePropertyList = getServiceMimeTypesList(ref);
        String serviceId = getServiceId(ref);
        for (String mimeTypeRawEntry : mimeTypesServicePropertyList) {
            MimeType mimeTypeEntry = constructMimeType(mimeTypeRawEntry);
            if (mimeTypeEntry != null && StringUtils.equals(mimeTypeEntry.getBaseType(), userMimeType.getBaseType()) && (userIdValue == null || StringUtils.equals(userIdValue, serviceId))) {
                try {
                    Object service = bundleContext.getService(ref);
                    T typedService = clazz.cast(service);
                    strictlyMatching.add(typedService);
                    // found exact mimetype, no need to continue within
                    break;
                // the same service
                } catch (ClassCastException cce) {
                    LOGGER.debug("Caught illegal cast to transformer type. ", cce);
                }
            }
        }
    }
    return strictlyMatching;
}
Also used : ArrayList(java.util.ArrayList) MimeType(javax.activation.MimeType) ServiceReference(org.osgi.framework.ServiceReference) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 62 with MimeType

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

the class TestMimeTypeToTransformerMapperImpl method testOnlyBaseTypeMatch2.

/**
     * Tests if a basetype matches that the list will return the correct sorted list.
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testOnlyBaseTypeMatch2() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList(MediaType.APPLICATION_ATOM_XML), "a1");
    ServiceReference ref2 = createMockReference(2, Arrays.asList(MediaType.APPLICATION_JSON), "a2");
    ServiceReference ref3 = createMockReference(3, Arrays.asList(MediaType.APPLICATION_JSON), "a3");
    ServiceReference[] refs = { ref2, ref3, ref1 };
    Object simpleTransformer1 = new Object();
    Object simpleTransformer2 = new Object();
    Object simpleTransformer3 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getService(ref2)).thenReturn(simpleTransformer2);
    when(context.getService(ref3)).thenReturn(simpleTransformer3);
    when(ref1.compareTo(ref2)).thenReturn(-1);
    when(ref1.compareTo(ref3)).thenReturn(-1);
    when(ref2.compareTo(ref1)).thenReturn(1);
    when(ref2.compareTo(ref3)).thenReturn(-1);
    when(ref3.compareTo(ref1)).thenReturn(1);
    when(ref3.compareTo(ref2)).thenReturn(1);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON));
    // then
    assertThat(matches.size(), is(2));
    assertThat(matches.get(0), is(simpleTransformer3));
    assertThat(matches.get(1), is(simpleTransformer2));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 63 with MimeType

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

the class TestMimeTypeToTransformerMapperImpl method testNullServiceList.

/**
     * We expect a null services list to be returned when no services have been registered in the
     * service registry
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testNullServiceList() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference[] refs = null;
    // when
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_ATOM_XML));
    // then
    assertThat(matches.isEmpty(), is(true));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 64 with MimeType

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

the class TestMimeTypeToTransformerMapperImpl method testEmptyServiceList.

/**
     * We expect an empty services list to be returned when no services have been registered in the
     * service registry
     *
     * @throws MimeTypeParseException
     * @throws InvalidSyntaxException
     */
@Test
public void testEmptyServiceList() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference[] refs = {};
    // when
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_ATOM_XML));
    // then
    assertThat(matches.isEmpty(), is(true));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 65 with MimeType

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

the class TestMimeTypeToTransformerMapperImpl method testInvalidMimeTypeServiceProperty.

@Test
public void testInvalidMimeTypeServiceProperty() throws MimeTypeParseException, InvalidSyntaxException {
    // given
    final BundleContext context = mock(BundleContext.class);
    ServiceReference ref1 = createMockReference(1, Arrays.asList("!INVALID@!"), null);
    ServiceReference[] refs = { ref1 };
    Object simpleTransformer1 = new Object();
    // when
    when(context.getService(ref1)).thenReturn(simpleTransformer1);
    when(context.getServiceReferences(isA(String.class), isNull(String.class))).thenReturn(refs);
    MimeTypeToTransformerMapper matcher = new MimeTypeToTransformerMapperImpl() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    List<Object> matches = matcher.findMatches(Object.class, new MimeType(MediaType.APPLICATION_JSON + "; id=a1"));
    // then
    assertThat(matches.size(), is(0));
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) MimeType(javax.activation.MimeType) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

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