Search in sources :

Example 56 with InputTransformer

use of ddf.catalog.transform.InputTransformer in project ddf by codice.

the class CswTransformProvider method unmarshal.

/**
 * Creates a Metacard from the given XML. This method is not typically be called directly, instead
 * it is called by another XStream Converter using UnmarshallingContext.convertAnother();
 *
 * @param reader
 * @param context
 * @return
 */
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
    String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
    InputTransformer transformer;
    if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
        transformer = inputTransformerManager.getTransformerByProperty(keyArg, valArg);
    } else {
        transformer = inputTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
    }
    if (transformer == null) {
        throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
    }
    /* The CswRecordConverter uses unmarshal, which requires the context */
    if (transformer instanceof CswRecordConverter) {
        return ((CswRecordConverter) transformer).unmarshal(reader, context);
    }
    Metacard metacard;
    try (InputStream is = readXml(reader, context)) {
        InputStream inputStream = is;
        if (LOGGER.isDebugEnabled()) {
            String originalInputStream = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
            LOGGER.debug("About to transform\n{}", originalInputStream);
            inputStream = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8.name()));
        }
        metacard = transformer.transform(inputStream);
    } catch (IOException | CatalogTransformerException e) {
        throw new ConversionException("Unable to transform Metacard", e);
    }
    return metacard;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Aggregations

InputTransformer (ddf.catalog.transform.InputTransformer)56 InputStream (java.io.InputStream)36 Metacard (ddf.catalog.data.Metacard)29 Test (org.junit.Test)27 ByteArrayInputStream (java.io.ByteArrayInputStream)24 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)21 ArrayList (java.util.ArrayList)20 IOException (java.io.IOException)17 ServiceReference (org.osgi.framework.ServiceReference)17 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)16 BundleContext (org.osgi.framework.BundleContext)14 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 CatalogFramework (ddf.catalog.CatalogFramework)9 MimeType (javax.activation.MimeType)9 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)8 Result (ddf.catalog.data.Result)7 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)6 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)6 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6