use of javax.xml.bind.JAXBException in project jersey by jersey.
the class JettisonListElementProvider method writeCollection.
@Override
public final void writeCollection(Class<?> elementType, Collection<?> t, MediaType mediaType, Charset c, Marshaller m, OutputStream entityStream) throws JAXBException, IOException {
final OutputStreamWriter osw = new OutputStreamWriter(entityStream, c);
JettisonConfig origJsonConfig = JettisonConfig.DEFAULT;
if (m instanceof JettisonConfigured) {
origJsonConfig = ((JettisonConfigured) m).getJSONConfiguration();
}
final JettisonConfig unwrappingJsonConfig = JettisonConfig.createJSONConfiguration(origJsonConfig);
final XMLStreamWriter jxsw = Stax2JettisonFactory.createWriter(osw, unwrappingJsonConfig);
final String invisibleRootName = getRootElementName(elementType);
try {
jxsw.writeStartDocument();
jxsw.writeStartElement(invisibleRootName);
for (Object o : t) {
m.marshal(o, jxsw);
}
jxsw.writeEndElement();
jxsw.writeEndDocument();
jxsw.flush();
} catch (XMLStreamException ex) {
Logger.getLogger(JettisonListElementProvider.class.getName()).log(Level.SEVERE, null, ex);
throw new JAXBException(ex.getMessage(), ex);
}
}
use of javax.xml.bind.JAXBException in project jersey by jersey.
the class AbstractCollectionJaxbProvider method writeTo.
@Override
public final void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
try {
final Collection c = (type.isArray()) ? Arrays.asList((Object[]) t) : (Collection) t;
final Class elementType = getElementClass(type, genericType);
final Charset charset = getCharset(mediaType);
final String charsetName = charset.name();
final Marshaller m = getMarshaller(elementType, mediaType);
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
if (charset != UTF8) {
m.setProperty(Marshaller.JAXB_ENCODING, charsetName);
}
setHeader(m, annotations);
writeCollection(elementType, c, mediaType, charset, m, entityStream);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
use of javax.xml.bind.JAXBException in project jersey by jersey.
the class AbstractJaxbElementProvider method readFrom.
@Override
public final JAXBElement<?> readFrom(Class<JAXBElement<?>> 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());
}
final ParameterizedType pt = (ParameterizedType) genericType;
final Class ta = (Class) pt.getActualTypeArguments()[0];
try {
return readFrom(ta, mediaType, getUnmarshaller(ta, mediaType), entityStream);
} catch (UnmarshalException ex) {
throw new BadRequestException(ex);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
use of javax.xml.bind.JAXBException in project killbill by killbill.
the class VersionedCatalogLoader method loadDefaultCatalog.
@Override
public VersionedCatalog loadDefaultCatalog(final String uriString) throws CatalogApiException {
try {
final List<URI> xmlURIs;
if (uriString.endsWith(XML_EXTENSION)) {
// Assume its an xml file
xmlURIs = new ArrayList<URI>();
xmlURIs.add(new URI(uriString));
} else {
// Assume its a directory
final URL url = getURLFromString(uriString);
final String directoryContents = UriAccessor.accessUriAsString(uriString);
xmlURIs = findXmlReferences(directoryContents, url);
}
final VersionedCatalog result = new VersionedCatalog(clock);
for (final URI u : xmlURIs) {
final StandaloneCatalog catalog = XMLLoader.getObjectFromUri(u, StandaloneCatalog.class);
result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID, internalCallContextFactory));
}
// Perform initialization and validation for VersionedCatalog
XMLLoader.initializeAndValidate(new URI(uriString), result);
return result;
} catch (final ValidationException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (final JAXBException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (IllegalArgumentException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (Exception e) {
logger.warn("Failed to load default catalog", e);
throw new IllegalStateException(e);
}
}
use of javax.xml.bind.JAXBException in project hibernate-orm by hibernate.
the class XmlParserHelper method getJaxbRoot.
public <T> T getJaxbRoot(InputStream stream, Class<T> clazz, Schema schema) throws XmlParsingException {
XMLEventReader staxEventReader;
try {
staxEventReader = createXmlEventReader(stream);
} catch (XMLStreamException e) {
throw new XmlParsingException("Unable to create stax reader", e);
}
ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
try {
staxEventReader = new JpaNamespaceTransformingEventReader(staxEventReader);
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(handler);
return clazz.cast(unmarshaller.unmarshal(staxEventReader));
} catch (JAXBException e) {
StringBuilder builder = new StringBuilder();
builder.append("Unable to perform unmarshalling at line number ");
builder.append(handler.getLineNumber());
builder.append(" and column ");
builder.append(handler.getColumnNumber());
builder.append(". Message: ");
builder.append(handler.getMessage());
throw new XmlParsingException(builder.toString(), e);
}
}
Aggregations