Search in sources :

Example 1 with FeatureCollectionMessageBodyReaderWfs20

use of org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.reader.FeatureCollectionMessageBodyReaderWfs20 in project ddf by codice.

the class FeatureCollectionMessageBodyReaderWfs20Test method testForbiddenDeserialization.

/**
 * Negative test case to assure invalid objects are not unmarshalled
 */
@Test
public void testForbiddenDeserialization() throws WebApplicationException, IOException {
    FeatureCollectionMessageBodyReaderWfs20 reader = new FeatureCollectionMessageBodyReaderWfs20();
    InputStream dynamicProxySerializedXml = open("/dynamicProxySerialized.xml");
    Wfs20FeatureCollection response = reader.readFrom(null, null, null, null, null, dynamicProxySerializedXml);
    dynamicProxySerializedXml.close();
    assertThat(response, nullValue());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Wfs20FeatureCollection(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection) Test(org.junit.Test)

Example 2 with FeatureCollectionMessageBodyReaderWfs20

use of org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.reader.FeatureCollectionMessageBodyReaderWfs20 in project ddf by codice.

the class FeatureCollectionMessageBodyReaderWfs20 method readFrom.

@SuppressWarnings({ "unchecked", "squid:S2093" })
@Override
public Wfs20FeatureCollection readFrom(Class<Wfs20FeatureCollection> clazz, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inStream) throws IOException, WebApplicationException {
    // Save original input stream for any exception message that might need to be
    // created and additional attributes
    String originalInputStream = IOUtils.toString(inStream, "UTF-8");
    LOGGER.debug("{}", LogSanitizer.sanitize(originalInputStream));
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(FeatureCollectionMessageBodyReaderWfs20.class.getClassLoader());
        // Fetch FeatureCollection attributes
        Unmarshaller unmarshaller = null;
        JAXBElement<FeatureCollectionType> wfsFeatureCollectionType = null;
        xstream.allowTypeHierarchy(Wfs20FeatureCollection.class);
        try {
            unmarshaller = JAXB_CONTEXT.createUnmarshaller();
            XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
            xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
            xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
            XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(originalInputStream));
            wfsFeatureCollectionType = (JAXBElement<FeatureCollectionType>) unmarshaller.unmarshal(xmlStreamReader);
        } catch (ClassCastException e1) {
            LOGGER.debug("Exception unmarshalling {}, could be an OWS Exception Report from server.", e1.getMessage());
            // If an ExceptionReport is sent from the remote WFS site it will be sent with an
            // JAX-RS "OK" status, hence the ErrorResponse exception mapper will not fire.
            // Instead the ServiceExceptionReport will come here and be treated like a GetFeature
            // 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 WfsSource catches, converts
            // to a WfsException, 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(e1, response);
        } catch (VirtualMachineError e) {
            throw e;
        } catch (JAXBException | XMLStreamException e1) {
            LOGGER.debug("Error in retrieving feature collection.", e1);
        } catch (RuntimeException e) {
            LOGGER.debug("Error processing collection", e);
            throw e;
        }
        Wfs20FeatureCollection featureCollection = null;
        if (null != wfsFeatureCollectionType && null != wfsFeatureCollectionType.getValue()) {
            BigInteger numberReturned = wfsFeatureCollectionType.getValue().getNumberReturned();
            String numberMatched = wfsFeatureCollectionType.getValue().getNumberMatched();
            // Re-create the input stream (since it has already been read for potential
            // exception message creation)
            inStream = new ByteArrayInputStream(originalInputStream.getBytes("UTF-8"));
            try {
                xstream.allowTypeHierarchy(Wfs20FeatureCollection.class);
                featureCollection = (Wfs20FeatureCollection) xstream.fromXML(inStream);
                featureCollection.setNumberMatched(numberMatched);
                featureCollection.setNumberReturned(numberReturned);
            } catch (XStreamException e) {
                LOGGER.debug("Exception unmarshalling", e);
            } finally {
                IOUtils.closeQuietly(inStream);
            }
        }
        return featureCollection;
    } finally {
        Thread.currentThread().setContextClassLoader(ccl);
    }
}
Also used : FeatureCollectionType(net.opengis.wfs.v_2_0_0.FeatureCollectionType) XMLStreamReader(javax.xml.stream.XMLStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) JAXBException(javax.xml.bind.JAXBException) Wfs20FeatureCollection(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection) Response(javax.ws.rs.core.Response) XStreamException(com.thoughtworks.xstream.XStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) BigInteger(java.math.BigInteger) Unmarshaller(javax.xml.bind.Unmarshaller) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with FeatureCollectionMessageBodyReaderWfs20

use of org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.reader.FeatureCollectionMessageBodyReaderWfs20 in project ddf by codice.

the class WfsSource method initProviders.

private List<? extends Object> initProviders() {
    // We need to tell the JAXBElementProvider to marshal the GetFeatureType
    // class as an element
    // because it is missing the @XmlRootElement Annotation
    JAXBElementProvider<GetFeatureType> provider = new Wfs20JaxbElementProvider<>();
    Map<String, String> jaxbClassMap = new HashMap<>();
    // Ensure a namespace is used when the GetFeature request is generated
    String expandedName = new QName(Wfs20Constants.WFS_2_0_NAMESPACE, Wfs20Constants.GET_FEATURE).toString();
    jaxbClassMap.put(GetFeatureType.class.getName(), expandedName);
    provider.setJaxbElementClassMap(jaxbClassMap);
    provider.setMarshallAsJaxbElement(true);
    featureCollectionReader = new FeatureCollectionMessageBodyReaderWfs20();
    return Arrays.asList(provider, new XmlSchemaMessageBodyReaderWfs20(), featureCollectionReader);
}
Also used : XmlSchemaMessageBodyReaderWfs20(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.reader.XmlSchemaMessageBodyReaderWfs20) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) FeatureCollectionMessageBodyReaderWfs20(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.reader.FeatureCollectionMessageBodyReaderWfs20) Wfs20JaxbElementProvider(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20JaxbElementProvider) GetFeatureType(net.opengis.wfs.v_2_0_0.GetFeatureType)

Example 4 with FeatureCollectionMessageBodyReaderWfs20

use of org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.reader.FeatureCollectionMessageBodyReaderWfs20 in project ddf by codice.

the class FeatureCollectionMessageBodyReaderWfs20Test method testAllowedDeserialization.

/**
 * Positive test case to assure valid objects are unmarshalled
 */
@Test
public void testAllowedDeserialization() throws WebApplicationException, IOException {
    FeatureCollectionMessageBodyReaderWfs20 reader = new FeatureCollectionMessageBodyReaderWfs20();
    InputStream validWfsFeatureCollectionResponseXml = open("/validWfsFeatureCollectionResponse.xml");
    Wfs20FeatureCollection response = reader.readFrom(null, null, null, null, null, validWfsFeatureCollectionResponseXml);
    validWfsFeatureCollectionResponseXml.close();
    assertThat(response, notNullValue());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Wfs20FeatureCollection(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection) Test(org.junit.Test)

Aggregations

Wfs20FeatureCollection (org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection)3 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 XStreamException (com.thoughtworks.xstream.XStreamException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 QName (javax.xml.namespace.QName)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 FeatureCollectionType (net.opengis.wfs.v_2_0_0.FeatureCollectionType)1 GetFeatureType (net.opengis.wfs.v_2_0_0.GetFeatureType)1