Search in sources :

Example 1 with CatalogTransformerException

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

the class OpenSearchSource method query.

@Override
public SourceResponse query(QueryRequest queryRequest) throws UnsupportedQueryException {
    String methodName = "query";
    LOGGER.trace(methodName);
    Serializable metacardId = queryRequest.getPropertyValue(Metacard.ID);
    SourceResponseImpl response = null;
    Subject subject = null;
    WebClient restWebClient = null;
    if (queryRequest.hasProperties()) {
        Object subjectObj = queryRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
        subject = (Subject) subjectObj;
    }
    restWebClient = factory.getWebClientForSubject(subject);
    Query query = queryRequest.getQuery();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Received query: " + query);
    }
    boolean canDoOpenSearch = setOpenSearchParameters(query, subject, restWebClient);
    if (canDoOpenSearch) {
        InputStream responseStream = performRequest(restWebClient);
        response = new SourceResponseImpl(queryRequest, new ArrayList<Result>());
        if (responseStream != null) {
            response = processResponse(responseStream, queryRequest);
        }
    } else {
        if (StringUtils.isEmpty((String) metacardId)) {
            OpenSearchFilterVisitor visitor = new OpenSearchFilterVisitor();
            query.accept(visitor, null);
            metacardId = visitor.getMetacardId();
        }
        restWebClient = newRestClient(query, (String) metacardId, false, subject);
        if (restWebClient != null) {
            InputStream responseStream = performRequest(restWebClient);
            Metacard metacard = null;
            List<Result> resultQueue = new ArrayList<Result>();
            try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
                if (responseStream != null) {
                    IOUtils.copyLarge(responseStream, fileBackedOutputStream);
                    InputTransformer inputTransformer = null;
                    try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
                        inputTransformer = getInputTransformer(inputStream);
                    } catch (IOException e) {
                        LOGGER.debug("Problem with transformation.", e);
                    }
                    if (inputTransformer != null) {
                        try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
                            metacard = inputTransformer.transform(inputStream);
                        } catch (IOException e) {
                            LOGGER.debug("Problem with transformation.", e);
                        }
                    }
                }
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Problem with transformation.", e);
            }
            if (metacard != null) {
                metacard.setSourceId(getId());
                ResultImpl result = new ResultImpl(metacard);
                resultQueue.add(result);
                response = new SourceResponseImpl(queryRequest, resultQueue);
                response.setHits(resultQueue.size());
            }
        }
    }
    LOGGER.trace(methodName);
    return response;
}
Also used : Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResultImpl(ddf.catalog.data.impl.ResultImpl) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) Subject(ddf.security.Subject) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard)

Example 2 with CatalogTransformerException

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

the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSetRss.

@Test
public void testQueryBySearchPhraseContentTypeSetRss() throws UnsupportedQueryException, URISyntaxException, IOException {
    WebClient client = mock(WebClient.class);
    Response clientResponse = mock(Response.class);
    when(client.get()).thenReturn(clientResponse);
    when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    when(clientResponse.getEntity()).thenReturn(getSampleRssStream());
    SecureCxfClientFactory factory = getMockFactory(client);
    OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl generatedMetacard = new MetacardImpl();
    generatedMetacard.setMetadata(getSample());
    generatedMetacard.setId(SAMPLE_ID);
    generatedMetacard.setContentTypeName("myType");
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        fail();
    } catch (CatalogTransformerException e) {
        fail();
    }
    source.setInputTransformer(inputTransformer);
    source.setEndpointUrl("http://localhost:8181/services/catalog/query");
    source.init();
    source.setParameters(DEFAULT_PARAMETERS);
    source.factory = factory;
    Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
    SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
    Assert.assertEquals(1, response.getHits());
    List<Result> results = response.getResults();
    Assert.assertTrue(results.size() == 1);
    Result result = results.get(0);
    Metacard metacard = result.getMetacard();
    Assert.assertNotNull(metacard);
    Assert.assertEquals("myType", metacard.getContentTypeName());
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 3 with CatalogTransformerException

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

the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSet.

@Test
public void testQueryBySearchPhraseContentTypeSet() throws UnsupportedQueryException, URISyntaxException, IOException {
    WebClient client = mock(WebClient.class);
    Response clientResponse = mock(Response.class);
    when(client.get()).thenReturn(clientResponse);
    when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    when(clientResponse.getEntity()).thenReturn(getSampleAtomStream());
    SecureCxfClientFactory factory = getMockFactory(client);
    OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl generatedMetacard = new MetacardImpl();
    generatedMetacard.setMetadata(getSample());
    generatedMetacard.setId(SAMPLE_ID);
    generatedMetacard.setContentTypeName("myType");
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        fail();
    } catch (CatalogTransformerException e) {
        fail();
    }
    source.setInputTransformer(inputTransformer);
    source.setEndpointUrl("http://localhost:8181/services/catalog/query");
    source.init();
    source.setParameters(DEFAULT_PARAMETERS);
    source.factory = factory;
    Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
    SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
    Assert.assertEquals(1, response.getHits());
    List<Result> results = response.getResults();
    Assert.assertTrue(results.size() == 1);
    Result result = results.get(0);
    Metacard metacard = result.getMetacard();
    Assert.assertNotNull(metacard);
    Assert.assertEquals("myType", metacard.getContentTypeName());
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 4 with CatalogTransformerException

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

the class RegistryTransformer method transform.

@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
    MetacardImpl metacard;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(inputStream, fileBackedOutputStream);
        } catch (IOException e) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Error reading input stream.", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
            metacard = (MetacardImpl) unmarshal(inputStreamCopy);
        } catch (ParserException e) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Parser exception caught", e);
        } catch (RegistryConversionException e) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Conversion exception caught", e);
        }
        if (metacard == null) {
            throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard.");
        } else if (StringUtils.isNotEmpty(id)) {
            metacard.setAttribute(Metacard.ID, id);
        }
        String xml;
        try (Reader reader = fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream()) {
            xml = CharStreams.toString(reader);
        }
        metacard.setAttribute(Metacard.METADATA, xml);
        metacard.setTags(Collections.singleton(RegistryConstants.REGISTRY_TAG));
    } catch (IOException e) {
        throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Error using file-backed stream.", e);
    }
    return metacard;
}
Also used : ParserException(org.codice.ddf.parser.ParserException) RegistryConversionException(org.codice.ddf.registry.converter.RegistryConversionException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 5 with CatalogTransformerException

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

the class MetacardBackupPlugin method processRequest.

private void processRequest(ProcessRequest<? extends ProcessResourceItem> processRequest) throws PluginExecutionException {
    if (CollectionUtils.isEmpty(storageBackupPlugins)) {
        throw new PluginExecutionException("Unable to backup ingested metacard; no metacard backup storage provider configured.");
    }
    if (metacardTransformer == null) {
        throw new PluginExecutionException("Unable to backup ingested metacard; no Metacard Transformer found.");
    }
    List<? extends ProcessResourceItem> processResourceItems = processRequest.getProcessItems();
    for (ProcessResourceItem processResourceItem : processResourceItems) {
        Metacard metacard = processResourceItem.getMetacard();
        if (shouldBackupMetacard(metacard)) {
            try {
                LOGGER.trace("Backing up metacard : {}", metacard.getId());
                BinaryContent binaryContent = metacardTransformer.transform(metacard, Collections.emptyMap());
                backupData(binaryContent, metacard.getId());
            } catch (CatalogTransformerException e) {
                LOGGER.debug("Unable to transform metacard with id {}.", metacard.getId(), e);
                throw new PluginExecutionException(String.format("Unable to transform metacard with id %s.", metacard.getId()));
            }
        }
    }
}
Also used : ProcessResourceItem(org.codice.ddf.catalog.async.data.api.internal.ProcessResourceItem) Metacard(ddf.catalog.data.Metacard) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)112 IOException (java.io.IOException)53 Metacard (ddf.catalog.data.Metacard)44 InputStream (java.io.InputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)29 BinaryContent (ddf.catalog.data.BinaryContent)25 InputTransformer (ddf.catalog.transform.InputTransformer)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 Result (ddf.catalog.data.Result)16 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)15 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)14 ArrayList (java.util.ArrayList)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 Test (org.junit.Test)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 MimeType (javax.activation.MimeType)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)8 List (java.util.List)8