use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project dhis2-core by dhis2.
the class AbstractCrudController method getXmlProperties.
private List<String> getXmlProperties(String payload) throws IOException {
XmlMapper mapper = DefaultRenderService.getXmlMapper();
JsonNode root = mapper.readTree(payload);
return Lists.newArrayList(root.fieldNames());
}
use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project ninja by ninjaframework.
the class BodyParserEngineXmlTest method testXmlBodyWithMissingVariables.
@Test
public void testXmlBodyWithMissingVariables() {
final String xmlDocument = String.format("<form><firstName>%s</firstName><lastName>%s</lastName></form>", BodyParserEngineXmlTest.DATA_FIRSTNAME, BodyParserEngineXmlTest.DATA_LASTNAME);
final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes());
final XmlMapper xmlObjMapper = new XmlMapper();
final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper);
SimpleTestForm testForm = null;
try {
Mockito.when(context.getInputStream()).thenReturn(is);
} catch (IOException ignore) {
}
try {
testForm = bodyParserEngineXml.invoke(context, SimpleTestForm.class);
} catch (BadRequestException ignore) {
} finally {
try {
is.close();
} catch (IOException ignore) {
}
}
assertTrue(testForm != null);
assertThat(testForm.firstName, equalTo(BodyParserEngineXmlTest.DATA_FIRSTNAME));
assertThat(testForm.lastName, equalTo(BodyParserEngineXmlTest.DATA_LASTNAME));
assertTrue(testForm.birthYear == null);
assertTrue(testForm.lastSeen == null);
}
use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project ninja by ninjaframework.
the class ApiControllerTest method testGetAndPostArticleViaXml.
@Test
public void testGetAndPostArticleViaXml() throws Exception {
// /////////////////////////////////////////////////////////////////////
// Test initial data:
// /////////////////////////////////////////////////////////////////////
String response = ninjaTestBrowser.makeXmlRequest(getServerAddress() + "api/bob@gmail.com/articles.xml");
System.out.println("response xml: " + response);
JacksonXmlModule module = new JacksonXmlModule();
// and then configure, for example:
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
ArticlesDto articlesDto = xmlMapper.readValue(response, ArticlesDto.class);
assertEquals(3, articlesDto.articles.size());
// /////////////////////////////////////////////////////////////////////
// Post new article:
// /////////////////////////////////////////////////////////////////////
ArticleDto articleDto = new ArticleDto();
articleDto.content = "contentcontent";
articleDto.title = "new title new title";
response = ninjaTestBrowser.postXml(getServerAddress() + "api/bob@gmail.com/article.xml", articleDto);
assertTrue(response.contains("Error. Forbidden."));
doLogin();
response = ninjaTestBrowser.postXml(getServerAddress() + "api/bob@gmail.com/article.xml", articleDto);
assertFalse(response.contains("Error. Forbidden."));
// /////////////////////////////////////////////////////////////////////
// Fetch articles again => assert we got a new one ...
// /////////////////////////////////////////////////////////////////////
response = ninjaTestBrowser.makeXmlRequest(getServerAddress() + "api/bob@gmail.com/articles.xml");
articlesDto = xmlMapper.readValue(response, ArticlesDto.class);
// one new result:
assertEquals(4, articlesDto.articles.size());
}
use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project ninja by ninjaframework.
the class XmlMapperProvider method get.
@Override
public XmlMapper get() {
JacksonXmlModule module = new JacksonXmlModule();
// Check out: https://github.com/FasterXML/jackson-dataformat-xml
// setDefaultUseWrapper produces more similar output to
// the Json output. You can change that with annotations in your
// models.
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
xmlMapper.registerModule(new AfterburnerModule());
return xmlMapper;
}
use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project camel by apache.
the class JacksonXMLDataFormat method doStart.
@Override
protected void doStart() throws Exception {
if (xmlMapper == null) {
xmlMapper = new XmlMapper();
}
if (enableJaxbAnnotationModule) {
// Enables JAXB processing
JaxbAnnotationModule module = new JaxbAnnotationModule();
LOG.info("Registering module: {}", module);
xmlMapper.registerModule(module);
}
if (useList) {
setCollectionType(ArrayList.class);
}
if (include != null) {
JsonInclude.Include inc = getCamelContext().getTypeConverter().mandatoryConvertTo(JsonInclude.Include.class, include);
xmlMapper.setSerializationInclusion(inc);
}
if (prettyPrint) {
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
if (enableFeatures != null) {
Iterator<Object> it = ObjectHelper.createIterator(enableFeatures);
while (it.hasNext()) {
String enable = it.next().toString();
// it can be different kind
SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, enable);
if (sf != null) {
xmlMapper.enable(sf);
continue;
}
DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, enable);
if (df != null) {
xmlMapper.enable(df);
continue;
}
MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, enable);
if (mf != null) {
xmlMapper.enable(mf);
continue;
}
throw new IllegalArgumentException("Enable feature: " + enable + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
}
}
if (disableFeatures != null) {
Iterator<Object> it = ObjectHelper.createIterator(disableFeatures);
while (it.hasNext()) {
String disable = it.next().toString();
// it can be different kind
SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, disable);
if (sf != null) {
xmlMapper.disable(sf);
continue;
}
DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, disable);
if (df != null) {
xmlMapper.disable(df);
continue;
}
MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, disable);
if (mf != null) {
xmlMapper.disable(mf);
continue;
}
throw new IllegalArgumentException("Disable feature: " + disable + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
}
}
if (modules != null) {
for (Module module : modules) {
LOG.info("Registering module: {}", module);
xmlMapper.registerModules(module);
}
}
if (moduleClassNames != null) {
Iterable<Object> it = ObjectHelper.createIterable(moduleClassNames);
for (Object o : it) {
String name = o.toString();
Class<Module> clazz = camelContext.getClassResolver().resolveMandatoryClass(name, Module.class);
Module module = camelContext.getInjector().newInstance(clazz);
LOG.info("Registering module: {} -> {}", name, module);
xmlMapper.registerModule(module);
}
}
if (moduleRefs != null) {
Iterable<Object> it = ObjectHelper.createIterable(moduleRefs);
for (Object o : it) {
String name = o.toString();
if (name.startsWith("#")) {
name = name.substring(1);
}
Module module = CamelContextHelper.mandatoryLookup(camelContext, name, Module.class);
LOG.info("Registering module: {} -> {}", name, module);
xmlMapper.registerModule(module);
}
}
}
Aggregations