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();
}
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));
}
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;
}
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));
}
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);
}
Aggregations