Search in sources :

Example 6 with CswException

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();
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 7 with CswException

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;
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) UpdateRequest(ddf.catalog.operation.UpdateRequest) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Result(ddf.catalog.data.Result) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) Entry(java.util.Map.Entry) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with CswException

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) FederationException(ddf.catalog.federation.FederationException) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) IngestException(ddf.catalog.source.IngestException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 9 with CswException

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);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) FederationException(ddf.catalog.federation.FederationException) LinkedList(java.util.LinkedList) Result(ddf.catalog.data.Result)

Example 10 with CswException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Parser(org.geotools.xml.Parser) SAXException(org.xml.sax.SAXException)

Aggregations

CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)84 Test (org.junit.Test)55 CapabilitiesType (net.opengis.cat.csw.v_2_0_2.CapabilitiesType)19 GetCapabilitiesType (net.opengis.cat.csw.v_2_0_2.GetCapabilitiesType)19 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)17 QueryImpl (ddf.catalog.operation.impl.QueryImpl)13 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)13 DescribeRecordResponseType (net.opengis.cat.csw.v_2_0_2.DescribeRecordResponseType)13 Matchers.anyString (org.mockito.Matchers.anyString)13 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)11 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)10 ArrayList (java.util.ArrayList)10 SchemaComponentType (net.opengis.cat.csw.v_2_0_2.SchemaComponentType)10 GetCapabilitiesRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.GetCapabilitiesRequest)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 IOException (java.io.IOException)9 DescribeRecordRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.DescribeRecordRequest)9 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)7 Filter (org.opengis.filter.Filter)7 JAXBException (javax.xml.bind.JAXBException)6