use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class CswEndpoint method getRecordById.
@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public CswRecordCollection getRecordById(GetRecordByIdType 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);
List<String> ids = request.getId();
if (!ids.isEmpty()) {
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: {}", 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 (request.isSetElementSetName() && request.getElementSetName().getValue() != null) {
response.setElementSetType(request.getElementSetName().getValue());
} else {
response.setElementSetType(ElementSetType.SUMMARY);
}
LOGGER.debug("{} successfully retrieved record(s): {}", request.getService(), 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 AbstractCswSource method retrieveResource.
@Override
public ResourceResponse retrieveResource(URI resourceUri, Map<String, Serializable> requestProperties) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
if (canRetrieveResourceById()) {
// If no resource reader was found, retrieve the product through a GetRecordById request
Serializable serializableId = null;
if (requestProperties != null) {
serializableId = requestProperties.get(Core.ID);
}
if (serializableId == null) {
throw new ResourceNotFoundException("Unable to retrieve resource because no metacard ID was found.");
}
String metacardId = serializableId.toString();
LOGGER.debug("Retrieving resource for ID : {}", metacardId);
Csw csw = factory.getClientForSubject((Subject) requestProperties.get(SecurityConstants.SECURITY_SUBJECT));
GetRecordByIdRequest getRecordByIdRequest = new GetRecordByIdRequest();
getRecordByIdRequest.setService(CswConstants.CSW);
getRecordByIdRequest.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
getRecordByIdRequest.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
getRecordByIdRequest.setId(metacardId);
String rangeValue = "";
long requestedBytesToSkip = 0;
if (requestProperties.containsKey(CswConstants.BYTES_TO_SKIP)) {
requestedBytesToSkip = (Long) requestProperties.get(CswConstants.BYTES_TO_SKIP);
rangeValue = String.format("%s%s-", CswConstants.BYTES_EQUAL, requestProperties.get(CswConstants.BYTES_TO_SKIP).toString());
LOGGER.debug("Range: {}", rangeValue);
}
CswRecordCollection recordCollection;
try {
recordCollection = csw.getRecordById(getRecordByIdRequest, rangeValue);
Resource resource = recordCollection.getResource();
if (resource != null) {
long responseBytesSkipped = 0L;
if (recordCollection.getResourceProperties().get(BYTES_SKIPPED) != null) {
responseBytesSkipped = (Long) recordCollection.getResourceProperties().get(BYTES_SKIPPED);
}
alignStream(resource.getInputStream(), requestedBytesToSkip, responseBytesSkipped);
return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(resource.getInputStream()), resource.getMimeTypeValue(), FilenameUtils.getName(resource.getName())));
}
} catch (CswException | IOException e) {
throw new ResourceNotFoundException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, metacardId), e);
}
}
LOGGER.debug("Retrieving resource at : {}", resourceUri);
return resourceReader.retrieveResource(resourceUri, requestProperties);
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class AbstractCswSource method handleWebApplicationException.
protected String handleWebApplicationException(WebApplicationException wae) {
Response response = wae.getResponse();
CswException cswException = new CswResponseExceptionMapper().fromResponse(response);
return CSW_SERVER_ERROR + " " + cswSourceConfiguration.getId() + "\n" + cswException.getMessage();
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class AbstractCswStore method update.
@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
Map<String, Serializable> properties = new HashMap<>();
validateOperation();
Subject subject = (Subject) updateRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
Csw csw = factory.getClientForSubject(subject);
CswTransactionRequest transactionRequest = getTransactionRequest();
OperationTransaction opTrans = (OperationTransaction) updateRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
String insertTypeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
HashSet<ProcessingDetails> errors = new HashSet<>();
if (insertTypeName == null) {
insertTypeName = CswConstants.CSW_RECORD;
}
ArrayList<Metacard> updatedMetacards = new ArrayList<>(updateRequest.getUpdates().size());
ArrayList<Filter> updatedMetacardFilters = new ArrayList<>(updateRequest.getUpdates().size());
for (Map.Entry<Serializable, Metacard> update : updateRequest.getUpdates()) {
Metacard metacard = update.getValue();
properties.put(metacard.getId(), metacard);
updatedMetacardFilters.add(filterBuilder.attribute(updateRequest.getAttributeName()).is().equalTo().text(update.getKey().toString()));
transactionRequest.getUpdateActions().add(new UpdateAction(metacard, insertTypeName, null));
}
try {
TransactionResponseType response = csw.transaction(transactionRequest);
if (response.getTransactionSummary().getTotalUpdated().longValue() != updateRequest.getUpdates().size()) {
errors.add(new ProcessingDetailsImpl(this.getId(), null, "One or more updates failed"));
}
} catch (CswException e) {
throw new IngestException("Csw Transaction Failed.", e);
}
try {
updatedMetacards.addAll(transactionQuery(updatedMetacardFilters, subject));
} catch (UnsupportedQueryException e) {
errors.add(new ProcessingDetailsImpl(this.getId(), e, "Failed to retrieve updated metacards"));
}
return new UpdateResponseImpl(updateRequest, properties, updatedMetacards, new ArrayList(opTrans.getPreviousStateMetacards()), errors);
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class AbstractCswStore method delete.
@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws IngestException {
Map<String, Serializable> properties = new HashMap<>();
validateOperation();
Subject subject = (Subject) deleteRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
Csw csw = factory.getClientForSubject(subject);
CswTransactionRequest transactionRequest = getTransactionRequest();
OperationTransaction opTrans = (OperationTransaction) deleteRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
String typeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
if (typeName == null) {
typeName = CswConstants.CSW_RECORD;
}
for (Serializable itemToDelete : deleteRequest.getAttributeValues()) {
try {
DeleteType deleteType = new DeleteType();
deleteType.setTypeName(typeName);
QueryConstraintType queryConstraintType = new QueryConstraintType();
Filter filter;
FilterType filterType;
filter = filterBuilder.attribute(deleteRequest.getAttributeName()).is().equalTo().text(itemToDelete.toString());
filterType = filterAdapter.adapt(filter, cswFilterDelegate);
queryConstraintType.setCqlText(CswCqlTextFilter.getInstance().getCqlText(filterType));
deleteType.setConstraint(queryConstraintType);
DeleteAction deleteAction = new DeleteAction(deleteType, DefaultCswRecordMap.getPrefixToUriMapping());
transactionRequest.getDeleteActions().add(deleteAction);
} catch (UnsupportedQueryException e) {
throw new IngestException("Unsupported Query.", e);
}
}
try {
TransactionResponseType response = csw.transaction(transactionRequest);
if (response.getTransactionSummary().getTotalDeleted().intValue() != deleteRequest.getAttributeValues().size()) {
throw new IngestException("Csw Transaction Failed. Number of metacards deleted did not match number requested.");
}
} catch (CswException e) {
throw new IngestException("Csw Transaction Failed", e);
}
return new DeleteResponseImpl(deleteRequest, properties, new ArrayList(opTrans.getPreviousStateMetacards()));
}
Aggregations