Search in sources :

Example 6 with UnmarshalException

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

the class AclScannerTest method testCorruptedFileInstall.

@Test
public void testCorruptedFileInstall() throws Exception {
    File file = new File(AclScannerTest.class.getResource("/xacml_errors.xml").toURI());
    try {
        aclScanner.install(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 7 with UnmarshalException

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

the class SmartDeserialization method deserialize.

public static <T> T deserialize(Class<T> c, InputStream is) throws OpsException {
    // TODO: Auto-detect XML, JSON, others?
    String data;
    try {
        data = IoUtils.readAll(is);
    } catch (IOException e) {
        throw new OpsException("Error reading data", e);
    }
    Format format = null;
    for (int i = 0; i < data.length(); i++) {
        char firstChar = data.charAt(i);
        switch(firstChar) {
            case ' ':
                continue;
            case '<':
                format = Format.XML;
                break;
            case '{':
                format = Format.JSON;
                break;
            default:
                {
                    if (Character.isLetter(firstChar)) {
                        format = Format.PROPERTIES;
                    } else {
                        throw new IllegalArgumentException("Unhandled character: " + ((int) firstChar));
                    }
                    break;
                }
        }
        if (format != null) {
            break;
        }
    }
    if (format == null) {
        throw new IllegalStateException("Could not determine format");
    }
    if (format == Format.XML) {
        JaxbHelper jaxb = JaxbHelper.get(c);
        try {
            return jaxb.deserialize(new StringReader(data), c);
        } catch (UnmarshalException e) {
            throw new OpsException("Error deserializing item", e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) UnmarshalException(javax.xml.bind.UnmarshalException) JaxbHelper(org.platformlayer.xml.JaxbHelper) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 8 with UnmarshalException

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

the class DiskImageRecipeBuilder method loadDiskImageResource.

public static Provider<DiskImageRecipe> loadDiskImageResource(final Class<?> context, final String resourceName) {
    return new ThrowingProvider<DiskImageRecipe>() {

        @Override
        public DiskImageRecipe build() throws OpsException {
            DiskImageRecipe recipe;
            try {
                String recipeXml = ResourceUtils.get(context, resourceName);
                recipe = JaxbHelper.deserializeXmlObject(recipeXml, DiskImageRecipe.class);
            } catch (IOException e) {
                throw new OpsException("Error loading recipe", e);
            } catch (UnmarshalException e) {
                throw new OpsException("Error loading recipe", e);
            }
            return recipe;
        }
    };
}
Also used : OpsException(org.platformlayer.ops.OpsException) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) UnmarshalException(javax.xml.bind.UnmarshalException) IOException(java.io.IOException)

Example 9 with UnmarshalException

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

the class CloneHelpers method cloneViaJaxb.

public static <T> T cloneViaJaxb(T o) {
    try {
        Class<T> objectClass = (Class<T>) o.getClass();
        JaxbHelper jaxbHelper = JaxbHelper.get(objectClass);
        String xml = JaxbHelper.toXml(o, false);
        return jaxbHelper.deserialize(new StringReader(xml), objectClass);
    } catch (UnmarshalException e) {
        throw new IllegalStateException("Error while cloning object", e);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error while cloning object", e);
    }
}
Also used : UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper) StringReader(java.io.StringReader)

Example 10 with UnmarshalException

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

the class AbstractCollectionJaxbProvider method readFrom.

@Override
@SuppressWarnings("unchecked")
public final Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    final EntityInputStream entityStream = EntityInputStream.create(inputStream);
    if (entityStream.isEmpty()) {
        throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
    }
    try {
        final Class<?> elementType = getElementClass(type, genericType);
        final Unmarshaller u = getUnmarshaller(elementType, mediaType);
        final XMLStreamReader r = getXMLStreamReader(elementType, mediaType, u, entityStream);
        boolean jaxbElement = false;
        Collection<Object> l = null;
        if (type.isArray()) {
            l = new ArrayList<Object>();
        } else {
            try {
                l = (Collection<Object>) type.newInstance();
            } catch (Exception e) {
                for (Class<?> c : DEFAULT_IMPLS) {
                    if (type.isAssignableFrom(c)) {
                        try {
                            l = (Collection<Object>) c.newInstance();
                            break;
                        } catch (InstantiationException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        } catch (IllegalAccessException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        } catch (SecurityException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        }
                    }
                }
            }
        }
        if (l == null) {
            l = new ArrayList<Object>();
        }
        // Move to root element
        int event = r.next();
        while (event != XMLStreamReader.START_ELEMENT) {
            event = r.next();
        }
        // Move to first child (if any)
        event = r.next();
        while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
            event = r.next();
        }
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementType.isAnnotationPresent(XmlRootElement.class)) {
                l.add(u.unmarshal(r));
            } else if (elementType.isAnnotationPresent(XmlType.class)) {
                l.add(u.unmarshal(r, elementType).getValue());
            } else {
                l.add(u.unmarshal(r, elementType));
                jaxbElement = true;
            }
            // Move to next peer (if any)
            event = r.getEventType();
            while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
                event = r.next();
            }
        }
        return (type.isArray()) ? createArray(l, jaxbElement ? JAXBElement.class : elementType) : l;
    } catch (UnmarshalException ex) {
        throw new BadRequestException(ex);
    } catch (XMLStreamException ex) {
        throw new BadRequestException(ex);
    } catch (JAXBException ex) {
        throw new InternalServerErrorException(ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) NoContentException(javax.ws.rs.core.NoContentException) XMLStreamException(javax.xml.stream.XMLStreamException) BadRequestException(javax.ws.rs.BadRequestException) UnmarshalException(javax.xml.bind.UnmarshalException) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JAXBException(javax.xml.bind.JAXBException) WebApplicationException(javax.ws.rs.WebApplicationException) XmlType(javax.xml.bind.annotation.XmlType) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) Collection(java.util.Collection) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) Unmarshaller(javax.xml.bind.Unmarshaller)

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