Search in sources :

Example 26 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader in project ddf by codice.

the class TestBoundingBoxReader method testJTSConverterBadCrs.

@Test(expected = CswException.class)
public void testJTSConverterBadCrs() throws Exception {
    // Setup
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse("src/test/resources/BoundingBoxBadCrs.xml");
    HierarchicalStreamReader hReader = new DomReader(doc);
    BoundingBoxReader boundingBoxReader = new BoundingBoxReader(hReader, CswAxisOrder.LAT_LON);
    boundingBoxReader.getWkt();
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 27 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader in project ddf by codice.

the class TestBoundingBoxReader method testJTSConverterEPSG4326LatLon.

@Test
public void testJTSConverterEPSG4326LatLon() throws Exception {
    // Setup
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse("src/test/resources/BoundingBoxInLatLonEPSG4326.xml");
    HierarchicalStreamReader hReader = new DomReader(doc);
    BoundingBoxReader boundingBoxReader = new BoundingBoxReader(hReader, CswAxisOrder.LAT_LON);
    // Perform Test
    String wktInLonLat = boundingBoxReader.getWkt();
    LOGGER.debug("WKT: {}", wktInLonLat);
    // Verify
    assertThat(wktInLonLat, is(POLYGON_CONTROL_WKT_IN_LON_LAT));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 28 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader 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 input stream for any exception message that might need to be
    // created
    String originalInputStream = IOUtils.toString(inStream, "UTF-8");
    LOGGER.debug("Converting to CswRecordCollection: \n {}", originalInputStream);
    // Re-create the input stream (since it has already been read for potential
    // exception message creation)
    inStream = new ByteArrayInputStream(originalInputStream.getBytes("UTF-8"));
    try {
        HierarchicalStreamReader reader = new XppReader(new InputStreamReader(inStream, StandardCharsets.UTF_8), XmlPullParserFactory.newInstance().newPullParser());
        cswRecords = (CswRecordCollection) xstream.unmarshal(reader, null, argumentHolder);
    } catch (XmlPullParserException e) {
        LOGGER.debug("Unable to create XmlPullParser, and cannot parse CSW Response.", e);
    } catch (XStreamException e) {
        // If an ExceptionReport is sent from the remote CSW site it will be sent with an
        // JAX-RS "OK" status, hence the ErrorResponse exception mapper will not fire.
        // Instead the ExceptionReport will come here and be treated like a GetRecords
        // response, resulting in an XStreamException since ExceptionReport cannot be
        // unmarshalled. So this catch clause is responsible for catching that XStream
        // exception and creating a JAX-RS response containing the original stream
        // (with the ExceptionReport) and rethrowing it as a WebApplicatioNException,
        // which CXF will wrap as a ClientException that the CswSource catches, converts
        // to a CswException, and logs.
        ByteArrayInputStream bis = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8));
        ResponseBuilder responseBuilder = Response.ok(bis);
        responseBuilder.type("text/xml");
        Response response = responseBuilder.build();
        throw new WebApplicationException(e, response);
    } finally {
        IOUtils.closeQuietly(inStream);
    }
    return cswRecords;
}
Also used : Serializable(java.io.Serializable) InputStreamReader(java.io.InputStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) Response(javax.ws.rs.core.Response) XStreamException(com.thoughtworks.xstream.XStreamException) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 29 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader in project ddf by codice.

the class TestCswTransformProvider method testUnmarshalCswRecord.

@Test
public void testUnmarshalCswRecord() throws Exception {
    when(mockInputManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputManager, times(1)).getTransformerBySchema(captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 30 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader in project ddf by codice.

the class TestCswTransformProvider method testUnmarshalMissingNamespaces.

@Test
public void testUnmarshalMissingNamespaces() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
    Map<String, String> namespaces = new HashMap<>();
    namespaces.put("xmlns:csw", "http://www.opengis.net/cat/csw/2.0.2");
    namespaces.put("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    namespaces.put("xmlns:dct", "http://purl.org/dc/terms/");
    HierarchicalStreamReader reader = new XppReader(new StringReader(getRecordMissingNamespaces()), XmlPullParserFactory.newInstance().newPullParser());
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
    context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, OTHER_SCHEMA);
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputTransformer, times(1)).transform(captor.capture());
    InputStream inStream = captor.getValue();
    String result = IOUtils.toString(inStream);
    XMLUnit.setIgnoreWhitespace(true);
    XMLAssert.assertXMLEqual(getRecord(), result);
}
Also used : HashMap(java.util.HashMap) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Aggregations

HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)34 Test (org.junit.Test)28 DomReader (com.thoughtworks.xstream.io.xml.DomReader)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)13 Document (org.w3c.dom.Document)13 StringReader (java.io.StringReader)11 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)8 InputStream (java.io.InputStream)8 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)7 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6 HashMap (java.util.HashMap)6 XStream (com.thoughtworks.xstream.XStream)5 Metacard (ddf.catalog.data.Metacard)5 Serializable (java.io.Serializable)5 DataHolder (com.thoughtworks.xstream.converters.DataHolder)4 QNameMap (com.thoughtworks.xstream.io.xml.QNameMap)4 StaxReader (com.thoughtworks.xstream.io.xml.StaxReader)4 IOException (java.io.IOException)4