use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class CswEndpoint method loadDocElementFromResourcePath.
private Element loadDocElementFromResourcePath(String resourcePath) throws CswException {
URL recordUrl = getBundle().getResource(resourcePath);
if (recordUrl == null) {
/* Using DescribeRecordType since that is the bundle where other csw resources live */
recordUrl = Optional.of(FrameworkUtil.getBundle(DescribeRecordType.class)).map(b -> b.getResource(resourcePath)).orElse(null);
if (recordUrl == /*still*/
null) {
throw new CswException("Cannot find the resource: " + resourcePath);
}
}
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
try {
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (ParserConfigurationException e) {
LOGGER.debug("Unable to configure features on document builder.", e);
}
Document doc;
try {
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(recordUrl.openStream());
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new CswException(e);
}
if (doc == null) {
throw new CswException("Document was NULL in attempting to parse from resource path '" + resourcePath + "'");
}
return doc.getDocumentElement();
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class CswEndpoint method updateRecords.
private int updateRecords(UpdateAction updateAction) throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
if (updateAction.getMetacard() != null) {
Metacard newRecord = updateAction.getMetacard();
if (newRecord.getId() != null) {
UpdateRequest updateRequest = new UpdateRequestImpl(newRecord.getId(), newRecord);
LOGGER.debug("Attempting to update {} ", newRecord.getId());
UpdateResponse updateResponse = framework.update(updateRequest);
return updateResponse.getUpdatedMetacards().size();
} else {
throw new CswException("Unable to update record. No ID was specified in the request.", CswConstants.MISSING_PARAMETER_VALUE, updateAction.getHandle());
}
} else if (updateAction.getConstraint() != null) {
QueryConstraintType constraint = updateAction.getConstraint();
QueryRequest queryRequest = queryFactory.getQuery(constraint);
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, schemaTransformerManager.getTransformerSchemaForId(updateAction.getTypeName()));
QueryResponse response = framework.query(queryRequest);
if (response.getHits() > 0) {
Map<String, Serializable> recordProperties = updateAction.getRecordProperties();
List<String> updatedMetacardIdsList = new ArrayList<>();
List<Metacard> updatedMetacards = new ArrayList<>();
for (Result result : response.getResults()) {
Metacard metacard = result.getMetacard();
if (metacard != null) {
for (Entry<String, Serializable> recordProperty : recordProperties.entrySet()) {
Attribute attribute = new AttributeImpl(recordProperty.getKey(), recordProperty.getValue());
metacard.setAttribute(attribute);
}
updatedMetacardIdsList.add(metacard.getId());
updatedMetacards.add(metacard);
}
}
if (updatedMetacardIdsList.size() > 0) {
String[] updatedMetacardIds = updatedMetacardIdsList.toArray(new String[updatedMetacardIdsList.size()]);
UpdateRequest updateRequest = new UpdateRequestImpl(updatedMetacardIds, updatedMetacards);
LOGGER.debug("Attempting to update {} metacards.", updatedMetacardIdsList.size());
UpdateResponse updateResponse = framework.update(updateRequest);
return updateResponse.getUpdatedMetacards().size();
}
}
}
return 0;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException in project ddf by codice.
the class CswEndpoint method transaction.
@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public TransactionResponseType transaction(CswTransactionRequest request) throws CswException {
if (request == null) {
throw new CswException("TransactionRequest request is null");
}
TransactionResponseType response = new TransactionResponseType();
TransactionSummaryType summary = new TransactionSummaryType();
summary.setTotalInserted(BigInteger.valueOf(0));
summary.setTotalUpdated(BigInteger.valueOf(0));
summary.setTotalDeleted(BigInteger.valueOf(0));
response.setTransactionSummary(summary);
response.setVersion(CswConstants.VERSION_2_0_2);
int numInserted = 0;
for (InsertAction insertAction : request.getInsertActions()) {
CreateRequest createRequest = new CreateRequestImpl(insertAction.getRecords());
try {
CreateResponse createResponse = framework.create(createRequest);
if (request.isVerbose()) {
response.getInsertResult().add(getInsertResultFromResponse(createResponse));
}
numInserted += createResponse.getCreatedMetacards().size();
} catch (IngestException | SourceUnavailableException e) {
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, insertAction.getHandle());
}
}
LOGGER.debug("{} records inserted.", numInserted);
response.getTransactionSummary().setTotalInserted(BigInteger.valueOf(numInserted));
int numUpdated = 0;
for (UpdateAction updateAction : request.getUpdateActions()) {
try {
numUpdated += updateRecords(updateAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException e) {
throw new CswException("Unable to update record(s).", CswConstants.TRANSACTION_FAILED, updateAction.getHandle());
}
}
LOGGER.debug("{} records updated.", numUpdated);
response.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(numUpdated));
int numDeleted = 0;
for (DeleteAction deleteAction : request.getDeleteActions()) {
try {
numDeleted += deleteRecords(deleteAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException e) {
throw new CswException("Unable to delete record(s).", CswConstants.TRANSACTION_FAILED, deleteAction.getHandle());
}
}
LOGGER.debug("{} records deleted.", numDeleted);
response.getTransactionSummary().setTotalDeleted(BigInteger.valueOf(numDeleted));
return response;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswException 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.CswException in project ddf by codice.
the class CswQueryFactory method parseJaxB.
private Object parseJaxB(JAXBElement<?> element) throws CswException {
Parser parser = new Parser(PARSER_CONFIG);
InputStream inputStream;
try {
inputStream = marshalJaxB(element);
return parser.parse(inputStream);
} catch (JAXBException | IOException | SAXException | ParserConfigurationException | RuntimeException e) {
throw new CswException(String.format("Failed to parse Element: (%s): %s", e.getClass().getSimpleName(), e.getMessage()), CswConstants.INVALID_PARAMETER_VALUE, null);
}
}
Aggregations