use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class AbstractCswStore method create.
@Override
public CreateResponse create(CreateRequest createRequest) throws IngestException {
Map<String, Serializable> properties = new HashMap<>();
validateOperation();
Subject subject = (Subject) createRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
Csw csw = factory.getClientForSubject(subject);
CswTransactionRequest transactionRequest = getTransactionRequest();
List<Metacard> metacards = createRequest.getMetacards();
List<String> metacardIds = metacards.stream().map(Metacard::getId).collect(Collectors.toList());
List<Metacard> createdMetacards = new ArrayList<>();
List<Filter> createdMetacardFilters;
HashSet<ProcessingDetails> errors = new HashSet<>();
String insertTypeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
if (insertTypeName == null) {
throw new IngestException("Could not find transformer for output schema " + cswSourceConfiguration.getOutputSchema());
}
transactionRequest.getInsertActions().add(new InsertAction(insertTypeName, null, metacards));
try {
TransactionResponseType response = csw.transaction(transactionRequest);
Set<String> processedIds = new HashSet<>();
//dive down into the response to get the created ID's. We need these so we can query
//the source again to get the created metacards and put them in the result
createdMetacardFilters = response.getInsertResult().stream().map(InsertResultType::getBriefRecord).flatMap(Collection::stream).map(BriefRecordType::getIdentifier).flatMap(Collection::stream).map(JAXBElement::getValue).map(SimpleLiteral::getContent).flatMap(Collection::stream).map(id -> {
processedIds.add(id);
return filterBuilder.attribute(Core.ID).is().equalTo().text(id);
}).collect(Collectors.toList());
metacardIds.removeAll(processedIds);
errors.addAll(metacardIds.stream().map(id -> new ProcessingDetailsImpl(id, null, "Failed to create metacard")).collect(Collectors.toList()));
} catch (CswException e) {
throw new IngestException("Csw Transaction Failed : ", e);
}
try {
createdMetacards = transactionQuery(createdMetacardFilters, subject);
} catch (UnsupportedQueryException e) {
errors.add(new ProcessingDetailsImpl(this.getId(), e, "Failed to retrieve newly created metacards"));
}
return new CreateResponseImpl(createRequest, properties, createdMetacards, errors);
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException 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.CswException 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.CswException 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;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException 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;
}
Aggregations