use of javax.xml.bind.JAXBContext in project openhab1-addons by openhab.
the class DenonConnector method postDocument.
private <T, S> T postDocument(String uri, Class<T> response, S request) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(request.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(request, sw);
String result = doHttpRequest("POST", uri, sw.toString());
if (StringUtils.isNotBlank(result)) {
JAXBContext jcResponse = JAXBContext.newInstance(response);
@SuppressWarnings("unchecked") T obj = (T) jcResponse.createUnmarshaller().unmarshal(IOUtils.toInputStream(result));
return obj;
}
} catch (JAXBException e) {
logger.debug("Encoding error in post", e);
}
return null;
}
use of javax.xml.bind.JAXBContext in project platformlayer by platformlayer.
the class MarshallerContextResolver method getContext.
@Override
public Marshaller getContext(Class<?> clazz) {
if (clazz.equals(ManagedItemCollection.class)) {
// OK
} else if (ItemBase.class.isAssignableFrom(clazz)) {
// OK
} else {
return null;
}
JAXBContext jaxbContext = jaxbContextHelper.getJaxbContext(clazz);
try {
Marshaller m = jaxbContext.createMarshaller();
// m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
return m;
} catch (JAXBException e) {
throw new IllegalStateException("Error creating XML marshaller", e);
}
}
use of javax.xml.bind.JAXBContext in project platformlayer by platformlayer.
the class ServiceResource method getSchema.
@GET
@Path("schema")
@Produces({ XML })
public String getSchema() throws IOException, JAXBException {
ServiceProvider serviceProvider = getServiceProvider();
String namespace = null;
List<Class<?>> javaClasses = Lists.newArrayList();
for (ModelClass<?> modelClass : serviceProvider.getModels().all()) {
javaClasses.add(modelClass.getJavaClass());
String modelNamespace = modelClass.getPrimaryNamespace();
if (namespace == null) {
namespace = modelNamespace;
} else if (!namespace.equals(modelNamespace)) {
throw new IllegalStateException();
}
}
JAXBContext jaxbContext = JAXBContext.newInstance(javaClasses.toArray(new Class<?>[javaClasses.size()]));
MemorySchemaOutputResolver schemaOutputResolver = new MemorySchemaOutputResolver();
jaxbContext.generateSchema(schemaOutputResolver);
Map<String, StringWriter> writers = schemaOutputResolver.getWriters();
StringWriter writer = writers.get(namespace);
if (writer == null) {
throw new IllegalArgumentException();
}
return writer.getBuffer().toString();
}
use of javax.xml.bind.JAXBContext in project spring-framework by spring-projects.
the class AbstractJaxb2HttpMessageConverter method createMarshaller.
/**
* Create a new {@link Marshaller} for the given class.
* @param clazz the class to create the marshaller for
* @return the {@code Marshaller}
* @throws HttpMessageConversionException in case of JAXB errors
*/
protected final Marshaller createMarshaller(Class<?> clazz) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
Marshaller marshaller = jaxbContext.createMarshaller();
customizeMarshaller(marshaller);
return marshaller;
} catch (JAXBException ex) {
throw new HttpMessageConversionException("Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
}
}
use of javax.xml.bind.JAXBContext in project camel by apache.
the class CamelContextFactoryBeanTest method contextAsString.
private String contextAsString(CamelContextFactoryBean context) throws Exception {
StringWriter stringOut = new StringWriter();
JAXBContext jaxb = JAXBContext.newInstance(CamelContextFactoryBean.class);
jaxb.createMarshaller().marshal(context, stringOut);
return stringOut.toString();
}
Aggregations