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;
}
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());
}
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());
}
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;
}
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()));
}
}
}
}
Aggregations