use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswRecordCollectionMessageBodyWriterTest method testWriteToProductData.
@Test
public void testWriteToProductData() throws MimeTypeParseException, IOException {
CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
byte[] data = "SampleData".getBytes();
ByteArrayInputStream productData = new ByteArrayInputStream(data);
MimeType mimeType = new MimeType("text", "plain");
MultivaluedMap<String, Object> httpHeaders = new MultivaluedHashMap<>();
Resource resource = new ResourceImpl(productData, mimeType, "ResourceName");
CswRecordCollection collection = new CswRecordCollection();
collection.setMimeType(mimeType.toString());
collection.setResource(resource);
collection.setOutputSchema("http://www.iana.org/assignments/media-types/application/octet-stream");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.writeTo(collection, null, null, null, null, httpHeaders, stream);
assertThat(stream.toByteArray(), is(equalTo(resource.getByteArray())));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class AbstractCswSource method query.
protected SourceResponse query(QueryRequest queryRequest, ElementSetType elementSetName, List<QName> elementNames, Csw csw) throws UnsupportedQueryException {
Query query = queryRequest.getQuery();
LOGGER.debug("{}: Received query:\n{}", cswSourceConfiguration.getId(), query);
GetRecordsType getRecordsType = createGetRecordsRequest(query, elementSetName, elementNames);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("{}: GetRecords request:\n {}", cswSourceConfiguration.getId(), getGetRecordsTypeAsXml(getRecordsType));
}
LOGGER.debug("{}: Sending query to: {}", cswSourceConfiguration.getId(), cswSourceConfiguration.getCswUrl());
List<Result> results;
Long totalHits;
try {
CswRecordCollection cswRecordCollection = csw.getRecords(getRecordsType);
if (cswRecordCollection == null) {
throw new UnsupportedQueryException("Invalid results returned from server");
}
this.availabilityTask.updateLastAvailableTimestamp(System.currentTimeMillis());
LOGGER.debug("{}: Received [{}] record(s) of the [{}] record(s) matched from {}.", cswSourceConfiguration.getId(), cswRecordCollection.getNumberOfRecordsReturned(), cswRecordCollection.getNumberOfRecordsMatched(), cswSourceConfiguration.getCswUrl());
results = createResults(cswRecordCollection);
totalHits = cswRecordCollection.getNumberOfRecordsMatched();
} catch (CswException cswe) {
LOGGER.info(CSW_SERVER_ERROR, cswe);
throw new UnsupportedQueryException(CSW_SERVER_ERROR, cswe);
} catch (WebApplicationException wae) {
String msg = handleWebApplicationException(wae);
throw new UnsupportedQueryException(msg, wae);
} catch (Exception ce) {
String msg = handleClientException(ce);
throw new UnsupportedQueryException(msg, ce);
}
LOGGER.debug("{}: Adding {} result(s) to the source response.", cswSourceConfiguration.getId(), results.size());
SourceResponseImpl sourceResponse = new SourceResponseImpl(queryRequest, results, totalHits);
addContentTypes(sourceResponse);
return sourceResponse;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class TestGetRecordsResponseConverter method testMarshalRecordCollectionHits.
@Ignore
public void testMarshalRecordCollectionHits() throws UnsupportedEncodingException, JAXBException {
final int totalResults = 5;
XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE);
GetRecordsType getRecords = new GetRecordsType();
QueryType query = new QueryType();
ElementSetNameType set = new ElementSetNameType();
set.setValue(ElementSetType.FULL);
query.setElementSetName(set);
ObjectFactory objectFactory = new ObjectFactory();
getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query));
CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults);
collection.setElementSetType(ElementSetType.FULL);
collection.setResultType(ResultType.HITS);
String xml = xstream.toXML(collection);
// Verify the context arguments were set correctly
verify(mockProvider, never()).marshal(any(Object.class), any(HierarchicalStreamWriter.class), any(MarshallingContext.class));
JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
GetRecordsResponseType response = jaxb.getValue();
// Assert the GetRecordsResponse elements and attributes
assertThat(response, not(nullValue()));
SearchResultsType resultsType = response.getSearchResults();
assertThat(resultsType, not(nullValue()));
assertThat(resultsType.getElementSet(), is(ElementSetType.FULL));
assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults));
assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(0));
assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpoint method getRecordById.
@Override
@GET
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public CswRecordCollection getRecordById(@QueryParam("") GetRecordByIdRequest request, @HeaderParam(CswConstants.RANGE_HEADER) String rangeValue) throws CswException {
if (request == null) {
throw new CswException("GetRecordByIdRequest request is null");
}
String outputFormat = request.getOutputFormat();
String outputSchema = request.getOutputSchema();
validator.validateOutputFormat(outputFormat, mimeTypeTransformerManager);
validator.validateOutputSchema(outputSchema, schemaTransformerManager);
if (StringUtils.isNotBlank(request.getId())) {
List<String> ids = Arrays.asList(request.getId().split(CswConstants.COMMA));
String id = ids.get(0);
// Check if the request wants to retrieve a product.
if (isProductRetrieval(ids, outputFormat, outputSchema)) {
LOGGER.debug("{} is attempting to retrieve product for ID: {}", request.getService(), id);
try {
return queryProductById(id, rangeValue);
} catch (UnsupportedQueryException e) {
throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
}
}
LOGGER.debug("{} is attempting to retrieve records: {}", request.getService(), ids);
CswRecordCollection response = queryById(ids, outputSchema);
response.setOutputSchema(outputSchema);
if (StringUtils.isNotBlank(request.getElementSetName())) {
response.setElementSetType(ElementSetType.fromValue(request.getElementSetName()));
} else {
response.setElementSetType(ElementSetType.SUMMARY);
}
LOGGER.debug("{} successfully retrieved record(s): {}", request.getRequest(), request.getId());
return response;
} else {
throw new CswException("A GetRecordById Query must contain an ID.", CswConstants.MISSING_PARAMETER_VALUE, "id");
}
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.
the class CswEndpoint method queryCsw.
private CswRecordCollection queryCsw(GetRecordsType request) throws CswException {
if (LOGGER.isDebugEnabled()) {
try {
Writer writer = new StringWriter();
try {
Marshaller marshaller = CswQueryFactory.getJaxBContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
JAXBElement<GetRecordsType> jaxbElement = new ObjectFactory().createGetRecords(request);
marshaller.marshal(jaxbElement, writer);
} catch (JAXBException e) {
LOGGER.debug("Unable to marshall {} to XML. Exception {}", GetRecordsType.class, e);
}
LOGGER.debug(writer.toString());
} catch (Exception e) {
LOGGER.debug("Unable to create debug message for getRecordsType: {}", e);
}
}
QueryType query = (QueryType) request.getAbstractQuery().getValue();
CswRecordCollection response = new CswRecordCollection();
response.setRequest(request);
response.setOutputSchema(request.getOutputSchema());
response.setMimeType(request.getOutputFormat());
response.setElementName(query.getElementName());
response.setElementSetType((query.getElementSetName() != null) ? query.getElementSetName().getValue() : null);
response.setResultType((ResultType) ObjectUtils.defaultIfNull(request.getResultType(), ResultType.HITS));
if (ResultType.HITS.equals(request.getResultType()) || ResultType.RESULTS.equals(request.getResultType())) {
QueryRequest queryRequest = queryFactory.getQuery(request);
try {
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, request.getOutputSchema());
LOGGER.debug("Attempting to execute query: {}", queryRequest);
QueryResponse queryResponse = framework.query(queryRequest);
response.setSourceResponse(queryResponse);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.debug("Unable to query", e);
throw new CswException(e);
}
}
return response;
}
Aggregations