Search in sources :

Example 16 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection 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;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) Resource(ddf.catalog.resource.Resource) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 17 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection 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 18 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class SendEvent method sendEvent.

private void sendEvent(String operation, Metacard... metacards) {
    if (subject == null) {
        return;
    }
    try {
        List<Result> results = Arrays.asList(metacards).stream().map(ResultImpl::new).collect(Collectors.toList());
        QueryResponse queryResponse = new QueryResponseImpl(query, results, true, metacards.length);
        CswRecordCollection recordCollection = new CswRecordCollection();
        recordCollection.setElementName(elementName);
        recordCollection.setElementSetType(elementSetType);
        recordCollection.setById(false);
        recordCollection.setRequest(request);
        recordCollection.setResultType(resultType);
        recordCollection.setDoWriteNamespaces(false);
        recordCollection.setMimeType(mimeType);
        recordCollection.setOutputSchema(outputSchema);
        queryResponse.getRequest().getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
        for (AccessPlugin plugin : getAccessPlugins()) {
            queryResponse = plugin.processPostQuery(queryResponse);
        }
        if (queryResponse.getResults().isEmpty()) {
            return;
        }
        recordCollection.setSourceResponse(queryResponse);
        send(operation, recordCollection);
    } catch (StopProcessingException | InvalidSyntaxException e) {
        LOGGER.debug("Unable to send event error running AccessPlugin processPostQuery. ", e);
    }
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) AccessPlugin(ddf.catalog.plugin.AccessPlugin) QueryResponse(ddf.catalog.operation.QueryResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Result(ddf.catalog.data.Result)

Example 19 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class TestCswSource method testAddingContentTypesOnQueries.

@Test
public void testAddingContentTypesOnQueries() throws CswException, UnsupportedQueryException, SecurityServiceException {
    Csw mockCsw = createMockCsw();
    List<String> expectedNames = new LinkedList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"));
    ServiceRegistration<?> mockRegisteredMetacardType = (ServiceRegistration<?>) mock(ServiceRegistration.class);
    LOGGER.info("mockRegisteredMetacardType: {}", mockRegisteredMetacardType);
    doReturn(mockRegisteredMetacardType).when(mockContext).registerService(eq(MetacardType.class.getName()), any(MetacardType.class), Matchers.any());
    ServiceReference<?> mockServiceReference = (ServiceReference<?>) mock(ServiceReference.class);
    doReturn(mockServiceReference).when(mockRegisteredMetacardType).getReference();
    when(mockServiceReference.getProperty(eq(Metacard.CONTENT_TYPE))).thenReturn(expectedNames);
    AbstractCswSource source = getCswSource(mockCsw, mockContext);
    assertThat(source.getContentTypes(), hasSize(10));
    Set<ContentType> expected = generateContentType(expectedNames);
    assertThat(source.getContentTypes(), is(expected));
    CswRecordCollection collection = generateCswCollection("/getRecordsResponse.xml");
    when(mockCsw.getRecords(any(GetRecordsType.class))).thenReturn(collection);
    QueryImpl propertyIsLikeQuery = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text("*"));
    expectedNames.add("dataset");
    expectedNames.add("dataset 2");
    expectedNames.add("dataset 3");
    expected = generateContentType(expectedNames);
    source.query(new QueryRequestImpl(propertyIsLikeQuery));
    assertThat(source.getContentTypes(), hasSize(13));
    assertThat(source.getContentTypes(), is(expected));
}
Also used : ContentType(ddf.catalog.data.ContentType) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) MetacardType(ddf.catalog.data.MetacardType) ServiceReference(org.osgi.framework.ServiceReference) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 20 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class TestCswSource method testCreateResults.

@Test
public void testCreateResults() throws SecurityServiceException {
    AbstractCswSource cswSource = getCswSource(mockCsw, mockContext, null, null, null, encryptionService);
    CswRecordCollection recordCollection = new CswRecordCollection();
    final int total = 2;
    List<Metacard> metacards = new ArrayList<>(total);
    for (int i = 0; i <= total; i++) {
        String id = "ID_" + String.valueOf(i);
        MetacardImpl metacard = new MetacardImpl();
        metacard.setId(id);
        metacard.setContentTypeName("myContentType");
        metacard.setResourceURI(URI.create("http://example.com/resource"));
        if (i == 1) {
            metacard.setAttribute(Core.RESOURCE_DOWNLOAD_URL, "http://example.com/SECOND/RESOURCE");
        }
        metacards.add(metacard);
    }
    recordCollection.getCswRecords().addAll(metacards);
    List<Result> results = cswSource.createResults(recordCollection);
    assertThat(results, notNullValue());
    assertThat(results.size(), is(recordCollection.getCswRecords().size()));
    assertThat(results.get(0).getMetacard().getResourceURI(), is(recordCollection.getCswRecords().get(0).getResourceURI()));
    assertThat(results.get(1).getMetacard().getResourceURI(), is(URI.create(recordCollection.getCswRecords().get(1).getAttribute(Core.RESOURCE_DOWNLOAD_URL).getValue().toString())));
}
Also used : Metacard(ddf.catalog.data.Metacard) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Aggregations

CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)51 Test (org.junit.Test)26 ByteArrayInputStream (java.io.ByteArrayInputStream)21 Matchers.anyString (org.mockito.Matchers.anyString)18 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)17 JAXBElement (javax.xml.bind.JAXBElement)12 Metacard (ddf.catalog.data.Metacard)11 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)11 XStream (com.thoughtworks.xstream.XStream)10 ArrayList (java.util.ArrayList)10 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 QName (javax.xml.namespace.QName)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 Resource (ddf.catalog.resource.Resource)8 Result (ddf.catalog.data.Result)7 Ignore (jdk.nashorn.internal.ir.annotations.Ignore)7 ElementSetNameType (net.opengis.cat.csw.v_2_0_2.ElementSetNameType)7 GetRecordsResponseType (net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType)7 SearchResultsType (net.opengis.cat.csw.v_2_0_2.SearchResultsType)7 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)7