use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.
the class QueryOperations method doQuery.
/**
* Executes a query using the specified {@link QueryRequest} and {@link FederationStrategy}.
* Based on the isEnterprise and sourceIds list in the query request, the federated query may
* include the local provider and {@link ConnectedSource}s.
*
* @param queryRequest the {@link QueryRequest}
* @param strategy the {@link FederationStrategy}
* @return the {@link QueryResponse}
* @throws FederationException
*/
QueryResponse doQuery(QueryRequest queryRequest, FederationStrategy strategy) throws FederationException {
Set<String> sourceIds = getCombinedIdSet(queryRequest);
LOGGER.debug("source ids: {}", sourceIds);
QuerySources querySources = new QuerySources(frameworkProperties).initializeSources(this, queryRequest, sourceIds).addConnectedSources(this, frameworkProperties).addCatalogProvider(this);
if (querySources.isEmpty()) {
// TODO change to SourceUnavailableException
throw new FederationException("SiteNames could not be resolved due to invalid site names, none of the sites " + "were available, or the current subject doesn't have permission to access the sites.");
}
LOGGER.debug("Calling strategy.federate()");
Query originalQuery = queryRequest.getQuery();
if (originalQuery != null && originalQuery.getTimeoutMillis() <= 0) {
Query modifiedQuery = new QueryImpl(originalQuery, originalQuery.getStartIndex(), originalQuery.getPageSize(), originalQuery.getSortBy(), originalQuery.requestsTotalResultsCount(), queryTimeoutMillis);
queryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
}
QueryResponse response = strategy.federate(querySources.sourcesToQuery, queryRequest);
frameworkProperties.getQueryResponsePostProcessor().processResponse(response);
return addProcessingDetails(querySources.exceptions, response);
}
use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.
the class CachingFederationStrategyTest method testFederateDuplicateSources.
@Test
public void testFederateDuplicateSources() throws Exception {
Query mockQ = new QueryImpl(mock(NullFilterImpl.class), 2, 2, mock(SortBy.class), true, LONG_TIMEOUT);
QueryRequest fedQueryRequest = new QueryRequestImpl(mockQ, properties);
List<Source> sources = new ArrayList<>();
// Multiple sources needed for OffsetResultHandler to be used
for (int i = 0; i < 2; i++) {
Source mockSource = mock(Source.class);
when(mockSource.getId()).thenReturn("mock source");
sources.add(mockSource);
}
strategy.federate(sources, fedQueryRequest);
verify(sources.get(0), atLeastOnce()).query(any(QueryRequest.class));
verify(sources.get(1), times(0)).query(any(QueryRequest.class));
}
use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.
the class RESTEndpoint method getHeaders.
/**
* REST Head. Returns headers only. Primarily used to let the client know that range requests (though limited)
* are accepted.
*
* @param sourceid
* @param id
* @param uriInfo
* @param httpRequest
* @return
*/
@HEAD
@Path("/sources/{sourceid}/{id}")
public Response getHeaders(@PathParam("sourceid") String sourceid, @PathParam("id") String id, @Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
Response response;
Response.ResponseBuilder responseBuilder;
QueryResponse queryResponse;
Metacard card = null;
LOGGER.trace("getHeaders");
URI absolutePath = uriInfo.getAbsolutePath();
MultivaluedMap<String, String> map = uriInfo.getQueryParameters();
if (id != null) {
LOGGER.debug("Got id: {}", id);
LOGGER.debug("Map of query parameters: \n{}", map.toString());
Map<String, Serializable> convertedMap = convert(map);
convertedMap.put("url", absolutePath.toString());
LOGGER.debug("Map converted, retrieving product.");
// default to xml if no transformer specified
try {
String transformer = DEFAULT_METACARD_TRANSFORMER;
Filter filter = getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(id);
Collection<String> sources = null;
if (sourceid != null) {
sources = new ArrayList<String>();
sources.add(sourceid);
}
QueryRequestImpl request = new QueryRequestImpl(new QueryImpl(filter), sources);
request.setProperties(convertedMap);
queryResponse = catalogFramework.query(request, null);
// pull the metacard out of the blocking queue
List<Result> results = queryResponse.getResults();
// return null if timeout elapsed)
if (results != null && !results.isEmpty()) {
card = results.get(0).getMetacard();
}
if (card == null) {
throw new ServerErrorException("Unable to retrieve requested metacard.", Status.NOT_FOUND);
}
LOGGER.debug("Calling transform.");
final BinaryContent content = catalogFramework.transform(card, transformer, convertedMap);
LOGGER.debug("Read and transform complete, preparing response.");
responseBuilder = Response.noContent();
// Add the Accept-ranges header to let the client know that we accept ranges in bytes
responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
String filename = null;
if (content instanceof Resource) {
// If we got a resource, we can extract the filename.
filename = ((Resource) content).getName();
} else {
String fileExtension = getFileExtensionForMimeType(content.getMimeTypeValue());
if (StringUtils.isNotBlank(fileExtension)) {
filename = id + fileExtension;
}
}
if (StringUtils.isNotBlank(filename)) {
LOGGER.debug("filename: {}", filename);
responseBuilder.header(HEADER_CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"");
}
long size = content.getSize();
if (size > 0) {
responseBuilder.header(HEADER_CONTENT_LENGTH, size);
}
response = responseBuilder.build();
} catch (FederationException e) {
String exceptionMessage = "READ failed due to unexpected exception: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (CatalogTransformerException e) {
String exceptionMessage = "Unable to transform Metacard. Try different transformer: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot obtain query results because source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (UnsupportedQueryException e) {
String exceptionMessage = "Specified query is unsupported. Change query and resubmit: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
// The catalog framework will throw this if any of the transformers blow up. We need to
// catch this exception
// here or else execution will return to CXF and we'll lose this message and end up with
// a huge stack trace
// in a GUI or whatever else is connected to this endpoint
} catch (IllegalArgumentException e) {
throw new ServerErrorException(e, Status.BAD_REQUEST);
}
} else {
throw new ServerErrorException("No ID specified.", Status.BAD_REQUEST);
}
return response;
}
use of ddf.catalog.operation.impl.QueryImpl 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 ddf.catalog.operation.impl.QueryImpl in project ddf by codice.
the class TestCswSource method testQueryWitNullSorting.
@Test
public void testQueryWitNullSorting() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
// Setup
final String searchPhrase = "*";
final int pageSize = 1;
final int numRecordsReturned = 1;
final long numRecordsMatched = 1;
setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
try {
configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
} catch (CswException e) {
fail("Could not configure Mock Remote CSW: " + e.getMessage());
}
QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
query.setPageSize(pageSize);
query.setSortBy(null);
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
cswSource.setCswUrl(URL);
cswSource.setId(ID);
// Perform test
SourceResponse response = cswSource.query(new QueryRequestImpl(query));
// Verify
Assert.assertNotNull(response);
assertThat(response.getResults().size(), is(numRecordsReturned));
assertThat(response.getHits(), is(numRecordsMatched));
ArgumentCaptor<GetRecordsType> captor = ArgumentCaptor.forClass(GetRecordsType.class);
try {
verify(mockCsw, atLeastOnce()).getRecords(captor.capture());
} catch (CswException e) {
fail("Could not verify mock CSW record count: " + e.getMessage());
}
GetRecordsType getRecordsType = captor.getValue();
QueryType cswQuery = (QueryType) getRecordsType.getAbstractQuery().getValue();
assertThat(cswQuery.getSortBy(), nullValue());
}
Aggregations