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);
}
}
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();
}
}
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;
}
};
}
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);
}
}
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);
}
}
Aggregations