Search in sources :

Example 21 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project phoenix by apache.

the class XMLResultHandlerTest method testDTDInResults.

@Test
public void testDTDInResults() throws Exception {
    URL resultsUrl = XMLConfigParserTest.class.getResource("/malicious_results_with_dtd.xml");
    assertNotNull(resultsUrl);
    File resultsFile = new File(resultsUrl.getFile());
    XMLResultHandler handler = new XMLResultHandler();
    try {
        handler.readFromResultFile(resultsFile);
        fail("Expected to see an exception parsing the results with a DTD");
    } catch (UnmarshalException e) {
        // If we don't parse the DTD, the variable 'name' won't be defined in the XML
        LOG.debug("Caught expected exception", e);
        Throwable cause = e.getLinkedException();
        assertTrue("Cause was a " + cause.getClass(), cause instanceof XMLStreamException);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) File(java.io.File) URL(java.net.URL) Test(org.junit.Test) XMLConfigParserTest(org.apache.phoenix.pherf.XMLConfigParserTest)

Example 22 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project opencast by opencast.

the class AclScannerTest method testCorruptedFileUpdate.

@Test
public void testCorruptedFileUpdate() throws Exception {
    File file = new File(AclScannerTest.class.getResource("/xacml_errors.xml").toURI());
    try {
        aclScanner.update(file);
        fail("Should not be parsed.");
    } catch (XACMLParsingException e) {
        assertTrue("The file can not be parsed.", e.getCause() instanceof UnmarshalException);
    }
}
Also used : XACMLParsingException(org.opencastproject.authorization.xacml.XACMLParsingException) UnmarshalException(javax.xml.bind.UnmarshalException) File(java.io.File) Test(org.junit.Test)

Example 23 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project platformlayer by platformlayer.

the class HttpPlatformLayerClient method listMetrics.

@Override
public MetricInfoCollection listMetrics(PlatformLayerKey key) throws PlatformLayerClientException {
    String relativePath = buildRelativePath(key) + "/metrics";
    String retval = doRequest(HttpMethod.GET, relativePath, String.class, Format.XML, null, null);
    MetricInfoCollection items;
    try {
        items = JaxbHelper.deserializeXmlObject(retval, MetricInfoCollection.class);
    } catch (UnmarshalException e) {
        throw new PlatformLayerClientException("Error parsing returned data", e);
    }
    return items;
}
Also used : UnmarshalException(javax.xml.bind.UnmarshalException) MetricInfoCollection(org.platformlayer.metrics.model.MetricInfoCollection)

Example 24 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project platformlayer by platformlayer.

the class PlatformLayerHttpRequest method doRequest.

public <T> T doRequest(Class<T> retvalClass, Format acceptFormat, Object sendData, Format sendDataFormat) throws PlatformLayerClientException {
    try {
        populateHttpRequest(acceptFormat, sendDataFormat);
        if (debug != null) {
            debug.println("Request: " + httpRequest);
        }
        if (sendData != null) {
            if (sendData instanceof String) {
                if (debug != null) {
                    debug.println("Data: " + sendData);
                }
                String sendDataString = (String) sendData;
                httpRequest.setRequestContent(new Utf8StringByteSource(sendDataString));
            } else {
                switch(sendDataFormat) {
                    case XML:
                        if (debug != null) {
                            debug.println("Data: [XML Content]");
                        }
                        JaxbHelper jaxbHelper = JaxbHelper.get(sendData.getClass());
                        String xml = jaxbHelper.marshal(sendData, false);
                        httpRequest.setRequestContent(new Utf8StringByteSource(xml));
                        // jaxbHelper.marshal(sendData, false, getOutputStream());
                        break;
                    case JSON:
                        if (debug != null) {
                            debug.println("Data: [JSON Content]");
                        }
                        JsonHelper jsonHelper = JsonHelper.build(sendData.getClass());
                        String json = jsonHelper.marshal(sendData, false);
                        httpRequest.setRequestContent(new Utf8StringByteSource(json));
                        // jsonHelper.marshal(sendData, false, getOutputStream());
                        break;
                    default:
                        throw new IllegalStateException();
                }
            }
        }
    } catch (JAXBException e) {
        throw new PlatformLayerClientException("Error while building request", e);
    } catch (IOException e) {
        throw new PlatformLayerClientException("Error while building request", e);
    }
    try {
        processHttpResponseCode(getResponse());
        if (retvalClass == null) {
            return null;
        } else if (String.class.equals(retvalClass)) {
            InputStream is = getInputStream();
            String text = null;
            if (is != null) {
                text = IoUtils.readAll(is);
            }
            if (debug != null) {
                debug.println("Response: " + text);
            }
            return Casts.as(text, retvalClass);
        } else if (StreamingResponse.class.equals(retvalClass)) {
            return Casts.as(new StreamingResponse(getResponse()), retvalClass);
        } else {
            if (debug != null) {
                debug.println("Response: XML/JSON content");
            }
            InputStream is = getInputStream();
            return JaxbHelper.deserializeXmlObject(is, retvalClass, true);
        }
    } catch (ConnectException e) {
        throw new PlatformLayerClientException("Error connecting to PlatformLayer service", e);
    } catch (UnmarshalException e) {
        throw new PlatformLayerClientException("Error while reading PlatformLayer response", e);
    } catch (IOException e) {
        throw new PlatformLayerClientException("Error communicating with PlatformLayer service", e);
    }
}
Also used : Utf8StringByteSource(org.platformlayer.rest.Utf8StringByteSource) JsonHelper(org.platformlayer.xml.JsonHelper) InputStream(java.io.InputStream) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Aggregations

UnmarshalException (javax.xml.bind.UnmarshalException)24 JAXBException (javax.xml.bind.JAXBException)15 IOException (java.io.IOException)6 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)5 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 Test (org.junit.Test)5 SAXException (org.xml.sax.SAXException)5 Unmarshaller (javax.xml.bind.Unmarshaller)4 File (java.io.File)3 URL (java.net.URL)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 StringReader (java.io.StringReader)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 ArrayList (java.util.ArrayList)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NoContentException (javax.ws.rs.core.NoContentException)2 JAXBContext (javax.xml.bind.JAXBContext)2 XmlType (javax.xml.bind.annotation.XmlType)2