Search in sources :

Example 6 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project ninja by ninjaframework.

the class BodyParserEngineXmlTest method testEmptyXmlBody.

@Test
public void testEmptyXmlBody() {
    final String xmlDocument = "";
    final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes());
    final XmlMapper xmlObjMapper = new XmlMapper();
    final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper);
    boolean badRequestThrown = false;
    try {
        Mockito.when(context.getInputStream()).thenReturn(is);
    } catch (IOException ignore) {
    }
    try {
        bodyParserEngineXml.invoke(context, SimpleTestForm.class);
    } catch (BadRequestException ignore) {
        badRequestThrown = true;
    } finally {
        try {
            is.close();
        } catch (IOException ignore) {
        }
    }
    assertTrue(badRequestThrown);
}
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 7 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project ninja by ninjaframework.

the class BodyParserEngineXmlTest method testValidXmlBody.

@Test
public void testValidXmlBody() {
    final String xmlDocument = String.format("<form><firstName>%s</firstName><lastName>%s</lastName><birthYear>%d</birthYear><lastSeen>%s</lastSeen></form>", BodyParserEngineXmlTest.DATA_FIRSTNAME, BodyParserEngineXmlTest.DATA_LASTNAME, BodyParserEngineXmlTest.DATA_BIRTHYEAR, BodyParserEngineXmlTest.DATA_LASTSEEN);
    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) {
        }
    }
    final Calendar cal = Calendar.getInstance();
    final SimpleDateFormat dateFormat = new SimpleDateFormat(BodyParserEngineXmlTest.PARSER_DATEFORMAT);
    dateFormat.setTimeZone(TimeZone.getTimeZone(BodyParserEngineXmlTest.PARSER_DATETZ));
    try {
        cal.setTime(dateFormat.parse(BodyParserEngineXmlTest.DATA_LASTSEEN));
    } catch (ParseException ignore) {
    }
    cal.setTimeZone(TimeZone.getTimeZone(BodyParserEngineXmlTest.PARSER_DATETZ));
    assertTrue(testForm != null);
    assertThat(testForm.firstName, equalTo(BodyParserEngineXmlTest.DATA_FIRSTNAME));
    assertThat(testForm.lastName, equalTo(BodyParserEngineXmlTest.DATA_LASTNAME));
    assertThat(testForm.birthYear, CoreMatchers.equalTo(BodyParserEngineXmlTest.DATA_BIRTHYEAR));
    assertTrue(testForm.lastSeen != null);
    assertTrue(testForm.lastSeen.compareTo(cal) == 0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) BadRequestException(ninja.exceptions.BadRequestException) IOException(java.io.IOException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 8 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project bigbluebutton by bigbluebutton.

the class RecordingMetadataReaderHelper method saveRecordingMetadata.

public static void saveRecordingMetadata(File metadataXml, RecordingMetadata recordingMetadata) {
    //XMLOutputFactory factory  = XMLOutputFactory.newInstance();
    JacksonXmlModule module = new JacksonXmlModule();
    module.setDefaultUseWrapper(false);
    XmlMapper mapper = new XmlMapper(module);
    //XMLStreamWriter writer   = null;
    try {
        //writer = factory.createXMLStreamWriter(new FileOutputStream(metadataXml));
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.writeValue(metadataXml, recordingMetadata);
    } catch (FileNotFoundException e) {
        log.error("File not found: " + metadataXml.getAbsolutePath(), e);
    } catch (IOException e) {
        log.error("IOException on " + metadataXml.getAbsolutePath(), e);
    }
}
Also used : JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 9 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project bigbluebutton by bigbluebutton.

the class RecordingMetadataReaderHelper method getRecordingMetadata.

public static RecordingMetadata getRecordingMetadata(File metadataXml) {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    JacksonXmlModule module = new JacksonXmlModule();
    // and then configure, for example:
    module.setDefaultUseWrapper(false);
    XmlMapper mapper = new XmlMapper(module);
    //Reading from xml file and creating XMLStreamReader
    XMLStreamReader reader = null;
    RecordingMetadata recMeta = null;
    try {
        reader = factory.createXMLStreamReader(new FileInputStream(metadataXml));
        recMeta = mapper.readValue(reader, RecordingMetadata.class);
    } catch (XMLStreamException e) {
        log.error("Failed to read metadata xml for recording: " + metadataXml.getAbsolutePath(), e);
    } catch (FileNotFoundException e) {
        log.error("File not found: " + metadataXml.getAbsolutePath(), e);
    } catch (IOException e) {
        log.error("IOException on " + metadataXml.getAbsolutePath(), e);
    }
    return recMeta;
}
Also used : JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) RecordingMetadata(org.bigbluebutton.api.domain.RecordingMetadata) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 10 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project spring-framework by spring-projects.

the class MappingJackson2XmlViewTests method renderWithCustomSerializerLocatedByFactory.

@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
    SerializerFactory factory = new DelegatingSerializerFactory(null);
    XmlMapper mapper = new XmlMapper();
    mapper.setSerializerFactory(factory);
    view.setObjectMapper(mapper);
    Object bean = new TestBeanSimple();
    Map<String, Object> model = new HashMap<>();
    model.put("foo", bean);
    view.render(model, request, response);
    String result = response.getContentAsString();
    assertTrue(result.length() > 0);
    assertTrue(result.contains("custom</testBeanSimple>"));
    validateResult();
}
Also used : SerializerFactory(com.fasterxml.jackson.databind.ser.SerializerFactory) BeanSerializerFactory(com.fasterxml.jackson.databind.ser.BeanSerializerFactory) HashMap(java.util.HashMap) ScriptableObject(org.mozilla.javascript.ScriptableObject) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Aggregations

XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)27 Test (org.junit.Test)15 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 File (java.io.File)3 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)2 NinjaTest (ninja.NinjaTest)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)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 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)1