Search in sources :

Example 1 with ExceptionReport

use of net.opengis.ows.v_1_1_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) {
                cswException = new CswException("Error received from remote Csw server" + (msg != null ? ": " + msg : ""));
                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<ExceptionReport>();
                    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 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 2 with ExceptionReport

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

the class CswExceptionMapper method createServiceException.

private ExceptionReport createServiceException(CswException cswException) {
    ExceptionReport exceptionReport = new ExceptionReport();
    exceptionReport.setVersion(SERVICE_EXCEPTION_REPORT_VERSION);
    ExceptionType exception = new ExceptionType();
    exception.setExceptionCode(cswException.getExceptionCode());
    exception.setLocator(cswException.getLocator());
    exception.getExceptionText().add(cswException.getMessage());
    exceptionReport.getException().add(exception);
    return exceptionReport;
}
Also used : ExceptionType(net.opengis.ows.v_1_0_0.ExceptionType) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport)

Example 3 with ExceptionReport

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

the class TestCswExceptionMapper method testCswExceptionToServiceExceptionReportWithLocatorAndCode.

@Test
public void testCswExceptionToServiceExceptionReportWithLocatorAndCode() {
    CswException exception = new CswException(SERVICE_EXCEPTION_MSG, Status.BAD_REQUEST.getStatusCode(), EXCEPTION_CODE, LOCATOR);
    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(EXCEPTION_CODE, is(exceptionReport.getException().get(0).getExceptionCode()));
    assertThat(LOCATOR, is(exceptionReport.getException().get(0).getLocator()));
}
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)

Example 4 with ExceptionReport

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

the class TestCswExceptionMapper 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)

Example 5 with ExceptionReport

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

the class TestCswExceptionMapper method testThrowableExceptionToServiceExceptionReport.

@Test
public void testThrowableExceptionToServiceExceptionReport() {
    NullPointerException npe = new NullPointerException();
    ExceptionMapper<Throwable> exceptionMapper = new CswExceptionMapper();
    Response response = exceptionMapper.toResponse(npe);
    assertThat(response.getEntity(), is(instanceOf(ExceptionReport.class)));
    ExceptionReport exceptionReport = (ExceptionReport) response.getEntity();
    assertThat(Status.BAD_REQUEST.getStatusCode(), is(response.getStatus()));
    assertThat(XML_PARSE_FAIL_MSG, is(exceptionReport.getException().get(0).getExceptionText().get(0)));
    assertThat(CswConstants.MISSING_PARAMETER_VALUE, is(exceptionReport.getException().get(0).getExceptionCode()));
    assertThat(exceptionReport.getException().get(0).getLocator(), nullValue());
    IllegalArgumentException iae = new IllegalArgumentException();
    exceptionMapper = new CswExceptionMapper();
    response = exceptionMapper.toResponse(iae);
    assertThat(response.getEntity(), is(instanceOf(ExceptionReport.class)));
    exceptionReport = (ExceptionReport) response.getEntity();
    assertThat(Status.BAD_REQUEST.getStatusCode(), is(response.getStatus()));
    assertThat(XML_PARSE_FAIL_MSG, is(exceptionReport.getException().get(0).getExceptionText().get(0)));
    assertThat(CswConstants.MISSING_PARAMETER_VALUE, is(exceptionReport.getException().get(0).getExceptionCode()));
    assertThat(exceptionReport.getException().get(0).getLocator(), nullValue());
}
Also used : Response(javax.ws.rs.core.Response) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) Test(org.junit.Test)

Aggregations

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