Search in sources :

Example 76 with BinaryContent

use of ddf.catalog.data.BinaryContent in project ddf by codice.

the class XsltResponseQueueTransformer method transform.

@Override
public ddf.catalog.data.BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.debug("Transforming ResponseQueue with XSLT tranformer");
    long grandTotal = -1;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        try {
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (ParserConfigurationException e) {
            LOGGER.debug("Unable to configure features on document builder.", e);
        }
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Node resultsElement = doc.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "results", null));
        // TODO use streaming XSLT, not DOM
        List<Result> results = upstreamResponse.getResults();
        grandTotal = upstreamResponse.getHits();
        for (Result result : results) {
            Metacard metacard = result.getMetacard();
            if (metacard != null) {
                String metadata = metacard.getMetadata();
                if (metadata != null) {
                    Element metacardElement = createElement(doc, XML_RESULTS_NAMESPACE, "metacard", null);
                    if (metacard.getId() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "id", metacard.getId()));
                    }
                    if (metacard.getMetacardType().toString() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "type", metacard.getMetacardType().getName()));
                    }
                    if (metacard.getTitle() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "title", metacard.getTitle()));
                    }
                    if (result.getRelevanceScore() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "score", result.getRelevanceScore().toString()));
                    }
                    if (result.getDistanceInMeters() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "distance", result.getDistanceInMeters().toString()));
                    }
                    if (metacard.getSourceId() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "site", metacard.getSourceId()));
                    }
                    if (metacard.getContentTypeName() != null) {
                        String contentType = metacard.getContentTypeName();
                        Element typeElement = createElement(doc, XML_RESULTS_NAMESPACE, "content-type", contentType);
                        // TODO revisit what to put in the qualifier
                        typeElement.setAttribute("qualifier", "content-type");
                        metacardElement.appendChild(typeElement);
                    }
                    if (metacard.getResourceURI() != null) {
                        try {
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "product", metacard.getResourceURI().toString()));
                        } catch (DOMException e) {
                            LOGGER.debug(" Unable to create resource uri element", e);
                        }
                    }
                    if (metacard.getThumbnail() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "thumbnail", Base64.getEncoder().encodeToString(metacard.getThumbnail())));
                        try {
                            String mimeType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(metacard.getThumbnail()));
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "t_mimetype", mimeType));
                        } catch (IOException e) {
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "t_mimetype", "image/png"));
                        }
                    }
                    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
                    if (metacard.getCreatedDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "created", fmt.print(metacard.getCreatedDate().getTime())));
                    }
                    // looking at the date last modified
                    if (metacard.getModifiedDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "updated", fmt.print(metacard.getModifiedDate().getTime())));
                    }
                    if (metacard.getEffectiveDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "effective", fmt.print(metacard.getEffectiveDate().getTime())));
                    }
                    if (metacard.getLocation() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "location", metacard.getLocation()));
                    }
                    Element documentElement = doc.createElementNS(XML_RESULTS_NAMESPACE, "document");
                    metacardElement.appendChild(documentElement);
                    resultsElement.appendChild(metacardElement);
                    Node importedNode = doc.importNode(new XPathHelper(metadata).getDocument().getFirstChild(), true);
                    documentElement.appendChild(importedNode);
                } else {
                    LOGGER.debug("Null content/document returned to XSLT ResponseQueueTransformer");
                }
            }
        }
        if (LOGGER.isDebugEnabled()) {
            DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
            LSSerializer lsSerializer = domImplementation.createLSSerializer();
            LOGGER.debug("Generated XML input for transform: " + lsSerializer.writeToString(doc));
        }
        LOGGER.debug("Starting responsequeue xslt transform.");
        Transformer transformer;
        Map<String, Object> mergedMap = new HashMap<String, Object>();
        mergedMap.put(GRAND_TOTAL, grandTotal);
        if (arguments != null) {
            mergedMap.putAll(arguments);
        }
        BinaryContent resultContent;
        StreamResult resultOutput = null;
        Source source = new DOMSource(doc);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resultOutput = new StreamResult(baos);
        try {
            transformer = templates.newTransformer();
        } catch (TransformerConfigurationException tce) {
            throw new CatalogTransformerException("Could not perform Xslt transform: ", tce);
        }
        for (Map.Entry<String, Object> entry : mergedMap.entrySet()) {
            LOGGER.trace("Adding parameter to transform {{}:{}}", entry.getKey(), entry.getValue());
            transformer.setParameter(entry.getKey(), entry.getValue());
        }
        try {
            transformer.transform(source, resultOutput);
            byte[] bytes = baos.toByteArray();
            LOGGER.debug("Transform complete.");
            resultContent = new XsltTransformedContent(bytes, mimeType);
        } catch (TransformerException te) {
            LOGGER.debug("Could not perform Xslt transform: ", te);
            throw new CatalogTransformerException("Could not perform Xslt transform: ", te);
        }
        return resultContent;
    } catch (ParserConfigurationException e) {
        LOGGER.debug("Error creating new document: ", e);
        throw new CatalogTransformerException("Error merging entries to xml feed.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XPathHelper(ddf.util.XPathHelper) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Document(org.w3c.dom.Document) BinaryContent(ddf.catalog.data.BinaryContent) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(ddf.catalog.data.Result) DOMException(org.w3c.dom.DOMException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) StreamResult(javax.xml.transform.stream.StreamResult) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Metacard(ddf.catalog.data.Metacard) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with BinaryContent

use of ddf.catalog.data.BinaryContent in project ddf by codice.

the class TestAtomTransformer method testSimple.

@Test
public void testSimple() throws CatalogTransformerException, IOException, XpathException, SAXException {
    // given
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    Action viewAction = mock(Action.class);
    when(viewAction.getUrl()).thenReturn(new URL("http://host:80/" + SAMPLE_ID));
    ActionProvider viewActionProvider = mock(ActionProvider.class);
    when(viewActionProvider.getAction(isA(Metacard.class))).thenReturn(viewAction);
    ActionProvider resourceActionProvider = mock(ActionProvider.class);
    when(resourceActionProvider.getAction(isA(Metacard.class))).thenReturn(viewAction);
    ActionProvider thumbnailActionProvider = mock(ActionProvider.class);
    when(thumbnailActionProvider.getAction(isA(Metacard.class))).thenReturn(viewAction);
    AtomTransformer transformer = new AtomTransformer();
    transformer.setViewMetacardActionProvider(viewActionProvider);
    transformer.setResourceActionProvider(resourceActionProvider);
    transformer.setMetacardTransformer(metacardTransformer);
    transformer.setThumbnailActionProvider(thumbnailActionProvider);
    setDefaultSystemConfiguration();
    SourceResponse response1 = mock(SourceResponse.class);
    when(response1.getHits()).thenReturn(new Long(1));
    when(response1.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setSourceId(SAMPLE_SOURCE_ID);
    metacard.setCreatedDate(SAMPLE_DATE_TIME.toDate());
    metacard.setModifiedDate(SAMPLE_DATE_TIME.toDate());
    result1.setMetacard(metacard);
    when(response1.getResults()).thenReturn(Arrays.asList((Result) result1));
    SourceResponse response = response1;
    Double relevanceScore = 0.3345;
    result1.setRelevanceScore(relevanceScore);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(AtomTransformer.MIME_TYPE));
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    /* feed */
    assertBasicFeedInfo(output, "1");
    /* entry */
    assertXpathEvaluatesTo(SAMPLE_SOURCE_ID, "/atom:feed/atom:entry/fs:resultSource/@fs:sourceId", output);
    assertXpathEvaluatesTo("", "/atom:feed/atom:entry/fs:resultSource", output);
    assertXpathEvaluatesTo(AtomTransformer.URN_CATALOG_ID + SAMPLE_ID, "/atom:feed/atom:entry/atom:id", output);
    assertXpathEvaluatesTo(MetacardStub.DEFAULT_TITLE, "/atom:feed/atom:entry/atom:title", output);
    assertXpathEvaluatesTo(Double.toString(relevanceScore), "/atom:feed/atom:entry/relevance:score", output);
    assertXpathExists("/atom:feed/atom:entry/atom:content", output);
    assertXpathEvaluatesTo(atomDateFormat.format(SAMPLE_DATE_TIME.toDate()), "/atom:feed/atom:entry/atom:published", output);
    assertXpathEvaluatesTo(atomDateFormat.format(SAMPLE_DATE_TIME.toDate()), "/atom:feed/atom:entry/atom:updated", output);
    assertXpathEvaluatesTo("application/xml", "/atom:feed/atom:entry/atom:content/@type", output);
    assertXpathEvaluatesTo(MetacardStub.DEFAULT_TYPE, "/atom:feed/atom:entry/atom:category/@term", output);
    assertXpathEvaluatesTo("1", "count(/atom:feed/atom:entry/georss:where)", output);
    assertXpathEvaluatesTo("1", "count(/atom:feed/atom:entry/georss:where/gml:Point)", output);
    assertXpathEvaluatesTo("56.3 13.3", "/atom:feed/atom:entry/georss:where/gml:Point", output);
    assertXpathEvaluatesTo("3", "count(/atom:feed/atom:entry/atom:link)", output);
    assertXpathExists("/atom:feed/atom:entry/atom:link[@rel='alternate']", output);
    assertXpathExists("/atom:feed/atom:entry/atom:link[@rel='related']", output);
    assertXpathEvaluatesTo("http://host:80/" + SAMPLE_ID, "/atom:feed/atom:entry/atom:link/@href", output);
}
Also used : ActionProvider(ddf.action.ActionProvider) Action(ddf.action.Action) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) URL(java.net.URL) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 78 with BinaryContent

use of ddf.catalog.data.BinaryContent in project ddf by codice.

the class TestAtomTransformer method testEntryElementsComplyToAtomSpecification.

/**
     * The following rules must be followed in order to be compliant with the Atom specification as
     * defined by http://tools.ietf.org/html/rfc4287#section-4.1.2 <br/>
     * "The following child elements are defined by this specification (note that the presence of
     * some of these elements is required):
     * <p>
     * <li/>atom:entry elements MUST contain one or more atom:author elements, unless the atom:entry
     * contains an atom:source element that contains an atom:author element or, in an Atom Feed
     * Document, the atom:feed element contains an atom:author element itself.
     * <p>
     * <li/>atom:entry elements MAY contain any number of atom:category elements.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:content element.
     * <p>
     * <li/>atom:entry elements MAY contain any number of atom:contributor elements.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:id element.
     * <p>
     * <li/>atom:entry elements that contain no child atom:content element MUST contain at least one
     * atom:link element with a rel attribute value of "alternate".
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:link element with a rel
     * attribute value of "alternate" that has the same combination of type and hreflang attribute
     * values.
     * <p>
     * <li/>atom:entry elements MAY contain additional atom:link elements beyond those described
     * above.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:published element.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:rights element.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:source element.
     * <p>
     * <li/>atom:entry elements MUST contain an atom:summary element in either of the following
     * cases:
     * <p>
     * <ul>
     * the atom:entry contains an atom:content that has a "src" attribute (and is thus empty).
     * </ul>
     * <p>
     * <ul>
     * the atom:entry contains content that is encoded in Base64; i.e., the "type" attribute of
     * atom:content is a MIME media type [MIMEREG], but is not an XML media type [RFC3023], does not
     * begin with "text/", and does not end with "/xml" or "+xml".
     * </ul>
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:summary element.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:title element.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:updated element."
     *
     * @throws CatalogTransformerException
     * @throws IOException
     * @throws SAXException
     * @throws XpathException
     */
@Test
public void testEntryElementsComplyToAtomSpecification() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    AtomTransformer transformer = new AtomTransformer();
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    transformer.setMetacardTransformer(metacardTransformer);
    setDefaultSystemConfiguration();
    SourceResponse response = mock(SourceResponse.class);
    when(response.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    ResultImpl result2 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    MetacardStub metacard2 = new MetacardStub("");
    metacard2.setId(SAMPLE_ID + 1);
    result1.setMetacard(metacard);
    result2.setMetacard(metacard2);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result1, result2));
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertEntryCompliant(output);
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) FileOutputStream(java.io.FileOutputStream) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) File(java.io.File) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 79 with BinaryContent

use of ddf.catalog.data.BinaryContent in project ddf by codice.

the class TestAtomTransformer method testMetacardTransformerBytesNull.

@Test
public void testMetacardTransformerBytesNull() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    MetacardTransformer metacardTransformer = mock(MetacardTransformer.class);
    BinaryContent metacardTransformation = mock(BinaryContent.class);
    when(metacardTransformation.getByteArray()).thenReturn(null);
    when(metacardTransformer.transform(isA(Metacard.class), isNull(Map.class))).thenReturn(metacardTransformation);
    AtomTransformer transformer = getConfiguredAtomTransformer(metacardTransformer, true);
    SourceResponse response = getSourceResponseStub(SAMPLE_ID, null);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(AtomTransformer.MIME_TYPE));
    byte[] bytes = binaryContent.getByteArray();
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    assertXpathEvaluatesTo(TEXT_TYPE, "/atom:feed/atom:entry/atom:content/@type", output);
    assertXpathEvaluatesTo(SAMPLE_ID, "/atom:feed/atom:entry/atom:content", output);
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) BinaryContent(ddf.catalog.data.BinaryContent) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 80 with BinaryContent

use of ddf.catalog.data.BinaryContent in project ddf by codice.

the class TestAtomTransformer method testNoSiteName.

@Test
public void testNoSiteName() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    AtomTransformer transformer = getConfiguredAtomTransformer(metacardTransformer, true);
    SourceResponse response1 = mock(SourceResponse.class);
    when(response1.getHits()).thenReturn(new Long(1));
    when(response1.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setSourceId(null);
    result1.setMetacard(metacard);
    when(response1.getResults()).thenReturn(Arrays.asList((Result) result1));
    SourceResponse response = response1;
    Double relevanceScore = 0.3345;
    result1.setRelevanceScore(relevanceScore);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(AtomTransformer.MIME_TYPE));
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    assertXpathEvaluatesTo(AtomTransformer.DEFAULT_SOURCE_ID, "/atom:feed/atom:entry/fs:resultSource/@fs:sourceId", output);
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) Result(ddf.catalog.data.Result) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Aggregations

BinaryContent (ddf.catalog.data.BinaryContent)112 Test (org.junit.Test)79 Metacard (ddf.catalog.data.Metacard)42 SourceResponse (ddf.catalog.operation.SourceResponse)41 HashMap (java.util.HashMap)30 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)28 Map (java.util.Map)24 Result (ddf.catalog.data.Result)22 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)19 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)19 Matchers.anyString (org.mockito.Matchers.anyString)18 Serializable (java.io.Serializable)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Date (java.util.Date)12 LineString (ddf.geo.formatter.LineString)11 MultiLineString (ddf.geo.formatter.MultiLineString)11 File (java.io.File)11 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)10 ResultImpl (ddf.catalog.data.impl.ResultImpl)10 IOException (java.io.IOException)10