use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpoint method queryProductById.
private CswRecordCollection queryProductById(String id, String rangeValue) throws CswException, UnsupportedQueryException {
final ResourceRequestById resourceRequest = new ResourceRequestById(id);
long bytesToSkip = getRange(rangeValue);
if (bytesToSkip > 0) {
LOGGER.debug("Bytes to skip: {}", String.valueOf(bytesToSkip));
resourceRequest.getProperties().put(CswConstants.BYTES_TO_SKIP, bytesToSkip);
}
ResourceResponse resourceResponse;
try {
resourceResponse = framework.getLocalResource(resourceRequest);
} catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
}
Resource resource = resourceResponse.getResource();
MimeType mimeType = resource.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(MediaType.APPLICATION_OCTET_STREAM);
resource = new ResourceImpl(resource.getInputStream(), mimeType, resource.getName());
} catch (MimeTypeParseException e) {
throw new CswException(String.format("Could not create mime type upon null mimeType, for mime %s.", MediaType.APPLICATION_OCTET_STREAM), e);
}
}
CswRecordCollection cswRecordCollection = new CswRecordCollection();
cswRecordCollection.setResource(resource);
cswRecordCollection.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
LOGGER.debug("{} successfully retrieved product for ID: {}", id);
return cswRecordCollection;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpoint method queryById.
private CswRecordCollection queryById(List<String> ids, String outputSchema) throws CswException {
QueryRequest queryRequest = queryFactory.getQueryById(ids);
try {
CswRecordCollection response = new CswRecordCollection();
response.setById(true);
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, outputSchema);
QueryResponse queryResponse = framework.query(queryRequest);
response.setSourceResponse(queryResponse);
List<Metacard> metacards = new LinkedList<>();
for (Result result : queryResponse.getResults()) {
metacards.add(result.getMetacard());
}
response.setCswRecords(metacards);
return response;
} catch (FederationException | SourceUnavailableException | UnsupportedQueryException e) {
throw new CswException(e);
}
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class SendEvent method sendEvent.
private void sendEvent(String operation, Metacard... metacards) {
if (subject == null) {
return;
}
try {
List<Result> results = Arrays.asList(metacards).stream().map(ResultImpl::new).collect(Collectors.toList());
QueryResponse queryResponse = new QueryResponseImpl(query, results, true, metacards.length);
CswRecordCollection recordCollection = new CswRecordCollection();
recordCollection.setElementName(elementName);
recordCollection.setElementSetType(elementSetType);
recordCollection.setById(false);
recordCollection.setRequest(request);
recordCollection.setResultType(resultType);
recordCollection.setDoWriteNamespaces(false);
recordCollection.setMimeType(mimeType);
recordCollection.setOutputSchema(outputSchema);
queryResponse.getRequest().getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
for (AccessPlugin plugin : getAccessPlugins()) {
queryResponse = plugin.processPostQuery(queryResponse);
}
if (queryResponse.getResults().isEmpty()) {
return;
}
recordCollection.setSourceResponse(queryResponse);
send(operation, recordCollection);
} catch (StopProcessingException | InvalidSyntaxException e) {
LOGGER.debug("Unable to send event error running AccessPlugin processPostQuery. ", e);
}
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class TestCswSource method testAddingContentTypesOnQueries.
@Test
public void testAddingContentTypesOnQueries() throws CswException, UnsupportedQueryException, SecurityServiceException {
Csw mockCsw = createMockCsw();
List<String> expectedNames = new LinkedList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"));
ServiceRegistration<?> mockRegisteredMetacardType = (ServiceRegistration<?>) mock(ServiceRegistration.class);
LOGGER.info("mockRegisteredMetacardType: {}", mockRegisteredMetacardType);
doReturn(mockRegisteredMetacardType).when(mockContext).registerService(eq(MetacardType.class.getName()), any(MetacardType.class), Matchers.any());
ServiceReference<?> mockServiceReference = (ServiceReference<?>) mock(ServiceReference.class);
doReturn(mockServiceReference).when(mockRegisteredMetacardType).getReference();
when(mockServiceReference.getProperty(eq(Metacard.CONTENT_TYPE))).thenReturn(expectedNames);
AbstractCswSource source = getCswSource(mockCsw, mockContext);
assertThat(source.getContentTypes(), hasSize(10));
Set<ContentType> expected = generateContentType(expectedNames);
assertThat(source.getContentTypes(), is(expected));
CswRecordCollection collection = generateCswCollection("/getRecordsResponse.xml");
when(mockCsw.getRecords(any(GetRecordsType.class))).thenReturn(collection);
QueryImpl propertyIsLikeQuery = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text("*"));
expectedNames.add("dataset");
expectedNames.add("dataset 2");
expectedNames.add("dataset 3");
expected = generateContentType(expectedNames);
source.query(new QueryRequestImpl(propertyIsLikeQuery));
assertThat(source.getContentTypes(), hasSize(13));
assertThat(source.getContentTypes(), is(expected));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class TestCswSource method testCreateResults.
@Test
public void testCreateResults() throws SecurityServiceException {
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext, null, null, null, encryptionService);
CswRecordCollection recordCollection = new CswRecordCollection();
final int total = 2;
List<Metacard> metacards = new ArrayList<>(total);
for (int i = 0; i <= total; i++) {
String id = "ID_" + String.valueOf(i);
MetacardImpl metacard = new MetacardImpl();
metacard.setId(id);
metacard.setContentTypeName("myContentType");
metacard.setResourceURI(URI.create("http://example.com/resource"));
if (i == 1) {
metacard.setAttribute(Core.RESOURCE_DOWNLOAD_URL, "http://example.com/SECOND/RESOURCE");
}
metacards.add(metacard);
}
recordCollection.getCswRecords().addAll(metacards);
List<Result> results = cswSource.createResults(recordCollection);
assertThat(results, notNullValue());
assertThat(results.size(), is(recordCollection.getCswRecords().size()));
assertThat(results.get(0).getMetacard().getResourceURI(), is(recordCollection.getCswRecords().get(0).getResourceURI()));
assertThat(results.get(1).getMetacard().getResourceURI(), is(URI.create(recordCollection.getCswRecords().get(1).getAttribute(Core.RESOURCE_DOWNLOAD_URL).getValue().toString())));
}
Aggregations