Search in sources :

Example 11 with XmlMapper

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());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 12 with XmlMapper

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BadRequestException(ninja.exceptions.BadRequestException) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 13 with XmlMapper

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());
}
Also used : JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) ArticlesDto(models.ArticlesDto) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) ArticleDto(models.ArticleDto) Test(org.junit.Test) NinjaTest(ninja.NinjaTest)

Example 14 with XmlMapper

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;
}
Also used : AfterburnerModule(com.fasterxml.jackson.module.afterburner.AfterburnerModule) JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 15 with 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);
        }
    }
}
Also used : DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JaxbAnnotationModule(com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Module(com.fasterxml.jackson.databind.Module) JaxbAnnotationModule(com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule)

Aggregations

XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)18 Test (org.junit.Test)9 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 BadRequestException (ninja.exceptions.BadRequestException)5 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 NinjaTest (ninja.NinjaTest)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 Module (com.fasterxml.jackson.databind.Module)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 BeanSerializerFactory (com.fasterxml.jackson.databind.ser.BeanSerializerFactory)1 SerializerFactory (com.fasterxml.jackson.databind.ser.SerializerFactory)1 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)1 JaxbAnnotationModule (com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule)1