Search in sources :

Example 81 with CswRecordCollection

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

the class GetRecordsMessageBodyReaderTest method testPartialContentNotSupportedHandling.

@Test
public void testPartialContentNotSupportedHandling() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService, permissions);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    String sampleData = "SampleData";
    byte[] data = sampleData.getBytes();
    CswRecordCollection cswRecords = null;
    try (ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data)) {
        MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
        httpHeaders.add(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, "TRUE");
        httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=ResourceName"));
        MediaType mediaType = new MediaType("text", "plain");
        cswRecords = reader.readFrom(CswRecordCollection.class, null, null, mediaType, httpHeaders, dataInputStream);
    }
    Resource resource = cswRecords.getResource();
    // assert that the CswRecordCollection property is not set if the server does not support
    // Partial Content responses
    assertThat(cswRecords.getResourceProperties().get(GetRecordsMessageBodyReader.BYTES_SKIPPED), nullValue());
    // assert that the input stream has not been skipped at this stage. Since AbstractCswSource has
    // the number
    // of bytes that was attempted to be skipped, the stream must be aligned there instead.
    assertThat(resource.getByteArray(), is(data));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Resource(ddf.catalog.resource.Resource) MediaType(javax.ws.rs.core.MediaType) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 82 with CswRecordCollection

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

the class CswSourceTest 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), 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(getQueryRequestWithSubject(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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedList(java.util.LinkedList) MetacardType(ddf.catalog.data.MetacardType) ServiceReference(org.osgi.framework.ServiceReference) QueryImpl(ddf.catalog.operation.impl.QueryImpl) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 83 with CswRecordCollection

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

the class CswSourceTest method testCreateResults.

@Test
public void testCreateResults() throws SecurityServiceException {
    AbstractCswSource cswSource = getCswSource(mockCsw, mockContext, null, null, null, encryptionService, permissions);
    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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 84 with CswRecordCollection

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

the class GetRecordsMessageBodyReader method readFrom.

@Override
public CswRecordCollection readFrom(Class<CswRecordCollection> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inStream) throws IOException, WebApplicationException {
    CswRecordCollection cswRecords = null;
    Map<String, Serializable> resourceProperties = new HashMap<>();
    // Check if the server returned a Partial Content response (hopefully in response to a range
    // header)
    String contentRangeHeader = httpHeaders.getFirst(HttpHeaders.CONTENT_RANGE);
    if (StringUtils.isNotBlank(contentRangeHeader)) {
        contentRangeHeader = StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-");
        long bytesSkipped = Long.parseLong(contentRangeHeader);
        resourceProperties.put(BYTES_SKIPPED, Long.valueOf(bytesSkipped));
    }
    // If the following HTTP header exists and its value is true, the input stream will contain
    // raw product data
    String productRetrievalHeader = httpHeaders.getFirst(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER);
    if (productRetrievalHeader != null && productRetrievalHeader.equalsIgnoreCase("TRUE")) {
        String fileName = handleContentDispositionHeader(httpHeaders);
        cswRecords = new CswRecordCollection();
        cswRecords.setResource(new ResourceImpl(inStream, mediaType.toString(), fileName));
        cswRecords.setResourceProperties(resourceProperties);
        return cswRecords;
    }
    // Save original response for any exception message that might need to be
    // created
    String originalCswResponse = IOUtils.toString(inStream, StandardCharsets.UTF_8);
    LOGGER.debug("Converting to CswRecordCollection: \n {}", LogSanitizer.sanitize(originalCswResponse));
    cswRecords = unmarshalWithStaxReader(originalCswResponse);
    return cswRecords;
}
Also used : Serializable(java.io.Serializable) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) HashMap(java.util.HashMap) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)

Aggregations

CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)84 Test (org.junit.Test)56 ByteArrayInputStream (java.io.ByteArrayInputStream)37 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)29 JAXBElement (javax.xml.bind.JAXBElement)25 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)21 XStream (com.thoughtworks.xstream.XStream)20 Metacard (ddf.catalog.data.Metacard)20 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)20 Matchers.anyString (org.mockito.Matchers.anyString)20 ArrayList (java.util.ArrayList)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)15 QName (javax.xml.namespace.QName)15 GetRecordsResponseType (net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType)15 Resource (ddf.catalog.resource.Resource)14 ElementSetNameType (net.opengis.cat.csw.v_2_0_2.ElementSetNameType)14 ObjectFactory (net.opengis.cat.csw.v_2_0_2.ObjectFactory)13 SearchResultsType (net.opengis.cat.csw.v_2_0_2.SearchResultsType)13 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)12 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)12