Search in sources :

Example 41 with CswRecordCollection

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

the class TestGetRecordsResponseConverter method testMarshalRecordCollectionById.

@Ignore
public void testMarshalRecordCollectionById() throws UnsupportedEncodingException, JAXBException {
    final int totalResults = 2;
    XStream xstream = createXStream(CswConstants.GET_RECORD_BY_ID_RESPONSE);
    CswRecordCollection collection = createCswRecordCollection(null, totalResults);
    collection.setById(true);
    ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class);
    String xml = xstream.toXML(collection);
    // Verify the context arguments were set correctly
    verify(mockProvider, times(totalResults)).marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture());
    MarshallingContext context = captor.getValue();
    assertThat(context, not(nullValue()));
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(context.get(CswConstants.ELEMENT_SET_TYPE), is(nullValue()));
    assertThat(context.get(CswConstants.ELEMENT_NAMES), is(nullValue()));
    JAXBElement<GetRecordByIdResponseType> jaxb = (JAXBElement<GetRecordByIdResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    GetRecordByIdResponseType response = jaxb.getValue();
    // Assert the GetRecordsResponse elements and attributes
    assertThat(response, not(nullValue()));
}
Also used : GetRecordByIdResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordByIdResponseType) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XStream(com.thoughtworks.xstream.XStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) Ignore(jdk.nashorn.internal.ir.annotations.Ignore)

Example 42 with CswRecordCollection

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

the class TestGetRecordsResponseConverter method testMarshalRecordCollectionGetSummary.

@Ignore
public void testMarshalRecordCollectionGetSummary() throws UnsupportedEncodingException, JAXBException {
    final int totalResults = 5;
    XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE);
    GetRecordsType getRecords = new GetRecordsType();
    QueryType query = new QueryType();
    ElementSetNameType set = new ElementSetNameType();
    set.setValue(ElementSetType.SUMMARY);
    query.setElementSetName(set);
    ObjectFactory objectFactory = new ObjectFactory();
    getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query));
    CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults);
    collection.setElementSetType(ElementSetType.SUMMARY);
    ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class);
    String xml = xstream.toXML(collection);
    // Verify the context arguments were set correctly
    verify(mockProvider, times(totalResults)).marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture());
    MarshallingContext context = captor.getValue();
    assertThat(context, not(nullValue()));
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(context.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.SUMMARY));
    JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    GetRecordsResponseType response = jaxb.getValue();
    // Assert the GetRecordsResponse elements and attributes
    assertThat(response, not(nullValue()));
    SearchResultsType resultsType = response.getSearchResults();
    assertThat(resultsType, not(nullValue()));
    assertThat(resultsType.getElementSet(), is(ElementSetType.SUMMARY));
    assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults));
    assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(totalResults));
    assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) SearchResultsType(net.opengis.cat.csw.v_2_0_2.SearchResultsType) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) GetRecordsResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Ignore(jdk.nashorn.internal.ir.annotations.Ignore)

Example 43 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection 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);
}
Also used : Serializable(java.io.Serializable) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Resource(ddf.catalog.resource.Resource) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) GetRecordByIdRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.GetRecordByIdRequest) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) BufferedInputStream(java.io.BufferedInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 44 with CswRecordCollection

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

the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithMimeType.

@Test
public void testWriteToWithMimeType() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
    when(mockManager.getTransformerByMimeType(any(String.class))).thenReturn(mockTransformer);
    when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
    when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
    CswRecordCollection collection = createCswRecordCollection(6);
    collection.setNumberOfRecordsMatched(22);
    collection.setNumberOfRecordsReturned(6);
    collection.setById(true);
    collection.setResultType(ResultType.RESULTS);
    collection.setMimeType(MediaType.APPLICATION_JSON);
    QName example = new QName("example");
    collection.setElementName(Arrays.asList(example));
    collection.setElementSetType(ElementSetType.BRIEF);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.writeTo(collection, null, null, null, null, null, stream);
    verify(mockManager, times(1)).getTransformerByMimeType(any(String.class));
// TODO - assert lookup by mime type
// TODO failure case
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 45 with CswRecordCollection

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

the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithSchema.

@Test
public void testWriteToWithSchema() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
    when(mockManager.getTransformerBySchema(anyString())).thenReturn(mockTransformer);
    ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
    when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
    when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
    CswRecordCollection collection = createCswRecordCollection(6);
    collection.setNumberOfRecordsMatched(22);
    collection.setNumberOfRecordsReturned(6);
    final String exampleSchema = "http://example.com/schema";
    collection.setOutputSchema(exampleSchema);
    collection.setById(true);
    collection.setResultType(ResultType.HITS);
    QName example = new QName("example");
    collection.setElementName(Arrays.asList(example));
    collection.setElementSetType(ElementSetType.BRIEF);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.writeTo(collection, null, null, null, null, null, stream);
    verify(mockTransformer).transform(any(SourceResponse.class), captor.capture());
    Map arguments = captor.getValue();
    assertThat(arguments.get(CswConstants.WRITE_NAMESPACES), is(false));
    assertThat(arguments.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(exampleSchema));
    assertThat(arguments.get(CswConstants.RESULT_TYPE_PARAMETER), is(ResultType.HITS));
    assertThat(arguments.get(CswConstants.IS_BY_ID_QUERY), is(true));
    assertThat(arguments.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.BRIEF));
    assertThat(((QName[]) arguments.get(CswConstants.ELEMENT_NAMES))[0], is(example));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) 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