Search in sources :

Example 1 with ExceptionReport

use of net.opengis.ows.v_1_0_0.ExceptionReport in project ddf by codice.

the class CswResponseExceptionMapper method convertToCswException.

private CswException convertToCswException(ExceptionReport report) {
    CswException cswException = null;
    List<ExceptionType> list = report.getException();
    for (ExceptionType exceptionType : list) {
        String exceptionCode = exceptionType.getExceptionCode();
        String locator = exceptionType.getLocator();
        List<String> exceptionText = exceptionType.getExceptionText();
        StringBuilder exceptionMsg = new StringBuilder();
        // Exception code is required per CSW schema, but check it anyway
        if (StringUtils.isNotBlank(exceptionCode)) {
            exceptionMsg.append("exceptionCode = " + exceptionCode + "\n");
        } else {
            exceptionMsg.append("exceptionCode = UNSPECIFIED");
        }
        // Locator and exception text(s) are both optional
        if (StringUtils.isNotBlank(locator)) {
            exceptionMsg.append("locator = " + locator + "\n");
        }
        if (!CollectionUtils.isEmpty(exceptionText)) {
            for (String text : exceptionText) {
                exceptionMsg.append(text);
            }
        }
        cswException = new CswException(exceptionMsg.toString());
    }
    if (null == cswException) {
        cswException = new CswException("Empty Exception Report (version = " + report.getVersion() + ")");
    }
    return cswException;
}
Also used : ExceptionType(net.opengis.ows.v_1_0_0.ExceptionType) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)

Example 2 with ExceptionReport

use of net.opengis.ows.v_1_0_0.ExceptionReport in project ddf by codice.

the class CswResponseExceptionMapper method fromResponse.

@Override
public CswException fromResponse(Response response) {
    CswException cswException = null;
    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                if (is.markSupported()) {
                    is.reset();
                }
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                LOGGER.info("Unable to parse exception report: {}", e.getMessage());
                LOGGER.debug("Unable to parse exception report", e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<>();
                    Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class).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(msg));
                    ExceptionReport report = (ExceptionReport) um.unmarshal(xmlStreamReader);
                    cswException = convertToCswException(report);
                } catch (JAXBException | XMLStreamException e) {
                    cswException = new CswException("Error received from remote Csw server: " + msg, e);
                    LOGGER.info("Error parsing the exception report: {}", e.getMessage());
                    LOGGER.debug("Error parsing the exception report", e);
                }
            } else {
                cswException = new CswException("Error received from remote Csw server.");
            }
        } else {
            cswException = new CswException("Error reading response, entity type not understood: " + response.getEntity().getClass().getName());
        }
        cswException.setHttpStatus(response.getStatus());
    } else {
        cswException = new CswException("Error handling response, response is null");
    }
    return cswException;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) IOException(java.io.IOException) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with ExceptionReport

use of net.opengis.ows.v_1_0_0.ExceptionReport in project ddf by codice.

the class WfsResponseExceptionMapper method unmarshalResponseEntity.

private WfsException unmarshalResponseEntity(String msg) {
    WfsException wfsEx;
    try {
        JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<>();
        Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class).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(msg));
        ExceptionReport report = (ExceptionReport) um.unmarshal(xmlStreamReader);
        wfsEx = convertToWfsException(report);
    } catch (JAXBException | XMLStreamException e) {
        wfsEx = new WfsParseException(msg, e);
    }
    return wfsEx;
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) WfsParseException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsParseException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 4 with ExceptionReport

use of net.opengis.ows.v_1_0_0.ExceptionReport in project ddf by codice.

the class WfsResponseExceptionMapper method convertToWfsException.

private WfsException convertToWfsException(ExceptionReport report) {
    WfsException wfsException = null;
    List<ExceptionType> list = new ArrayList<>(report.getException());
    if (!list.isEmpty()) {
        Collections.reverse(list);
        StringBuilder exceptionMsg = new StringBuilder();
        for (ExceptionType serviceException : list) {
            exceptionMsg.append(serviceException.getExceptionText());
        }
        wfsException = new WfsException(exceptionMsg.toString());
    }
    if (null == wfsException) {
        wfsException = new WfsException("Empty Service Exception Report (version = " + report.getVersion() + ")");
    }
    return wfsException;
}
Also used : ExceptionType(net.opengis.ows.v_1_0_0.ExceptionType) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) ArrayList(java.util.ArrayList)

Example 5 with ExceptionReport

use of net.opengis.ows.v_1_0_0.ExceptionReport in project ddf by codice.

the class CswExceptionMapperTest method testCswExceptionToServiceExceptionReport.

@Test
public void testCswExceptionToServiceExceptionReport() {
    CswException exception = new CswException(SERVICE_EXCEPTION_MSG, Status.BAD_REQUEST.getStatusCode());
    ExceptionMapper<Throwable> exceptionMapper = new CswExceptionMapper();
    Response response = exceptionMapper.toResponse(exception);
    assertThat(response.getEntity(), is(instanceOf(ExceptionReport.class)));
    ExceptionReport exceptionReport = (ExceptionReport) response.getEntity();
    assertThat(Status.BAD_REQUEST.getStatusCode(), is(response.getStatus()));
    assertThat(SERVICE_EXCEPTION_MSG, is(exceptionReport.getException().get(0).getExceptionText().get(0)));
    assertThat(exceptionReport.getException().get(0).getExceptionCode(), nullValue());
    assertThat(exceptionReport.getException().get(0).getLocator(), nullValue());
}
Also used : Response(javax.ws.rs.core.Response) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) Test(org.junit.Test)

Aggregations

ExceptionReport (net.opengis.ows.v_1_0_0.ExceptionReport)9 Response (javax.ws.rs.core.Response)6 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)6 Test (org.junit.Test)6 StringReader (java.io.StringReader)3 JAXBException (javax.xml.bind.JAXBException)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 XMLInputFactory (javax.xml.stream.XMLInputFactory)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 ExceptionType (net.opengis.ows.v_1_0_0.ExceptionType)3 JAXBElementProvider (org.apache.cxf.jaxrs.provider.JAXBElementProvider)3 WfsException (org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)1 ExceptionReport (net.opengis.ows.v_1_1_0.ExceptionReport)1 WfsParseException (org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsParseException)1