Example usage for org.springframework.oxm.jaxb Jaxb2Marshaller Jaxb2Marshaller

List of usage examples for org.springframework.oxm.jaxb Jaxb2Marshaller Jaxb2Marshaller

Introduction

In this page you can find the example usage for org.springframework.oxm.jaxb Jaxb2Marshaller Jaxb2Marshaller.

Prototype

Jaxb2Marshaller

Source Link

Usage

From source file:com.hpe.elderberry.TaxiiConnection.java

public RestTemplate getRestTemplate() {
    if (restTemplate == null) {
        HttpClientBuilder builder = custom();

        if (useProxy) {
            if ("".equals(proxyHost)) {
                proxyHost = System.getProperty(discoveryUrl.getScheme() + ".proxyHost");
            }//from ww  w.  ja v  a2 s . c  o m

            if (proxyPort == 0) {
                proxyPort = Integer.parseInt(System.getProperty(discoveryUrl.getScheme() + ".proxyPort", "0"));
            }

            if ("".equals(proxyHost) || proxyHost == null || proxyPort == 0) {
                log.warn("proxy requested, but not setup, not using a proxy");
            } else {
                log.info("using " + discoveryUrl.getScheme() + " proxy: " + proxyHost + ":" + proxyPort);
                HttpHost proxy = new HttpHost(proxyHost, proxyPort);
                DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
                builder.setRoutePlanner(routePlanner);
            }
        }

        if (getTrustStore() != null || getKeyStore() != null) {
            SSLContext sslContext;
            try {
                sslContext = SSLContexts.custom()
                        .loadTrustMaterial(getTrustStore(), new TrustSelfSignedStrategy())
                        .loadKeyMaterial(getKeyStore(), keyPassword).build();
            } catch (Exception e) {
                log.error("unable to create SSL context, " + e.getMessage(), e);
                throw new RuntimeException(e);
            }
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            builder.setSSLSocketFactory(sslsf);
        }

        if (!"".equals(username)) {
            restTemplate = new RestTemplate(
                    new PreemptiveAuthHttpRequestFactor(username, password, builder.build()));
        } else {
            restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(builder.build()));
        }

        if (marshaller == null) {
            marshaller = new Jaxb2Marshaller();
            marshaller.setPackagesToScan("org.mitre");
            try {
                marshaller.afterPropertiesSet();
            } catch (Exception e) {
                log.error("unable to create Jaxb2 Marshaller: " + e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }

        MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
        converter.setSupportedMediaTypes(singletonList(APPLICATION_XML));
        //noinspection unchecked
        restTemplate.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
    }

    return restTemplate;
}

From source file:org.cruk.genologics.api.jaxb.JaxbAnnotationTest.java

@Test
@SuppressWarnings("unchecked")
public void testExceptionSimple() throws Throwable {
    // Cannot use configured because of aspects.
    Jaxb2Marshaller exceptionMarshaller = new Jaxb2Marshaller();
    exceptionMarshaller.setPackagesToScan(new String[] { "com.genologics.ri.exception" });
    exceptionMarshaller/*from w ww  .j  a  v a  2 s  .com*/
            .setMarshallerProperties(context.getBean("genologicsJaxbMarshallerProperties", Map.class));

    Jaxb2Marshaller original = marshaller;
    try {
        marshaller = exceptionMarshaller;
        fetchMarshalAndCompare(com.genologics.ri.exception.Exception.class);
    } finally {
        marshaller = original;
    }
}

From source file:org.cruk.genologics.api.jaxb.SerializationTest.java

@Test
@SuppressWarnings("unchecked")
public void testExceptionSimple() throws Throwable {
    // Cannot use configured because of aspects.
    Jaxb2Marshaller exceptionMarshaller = new Jaxb2Marshaller();
    exceptionMarshaller.setPackagesToScan(new String[] { "com.genologics.ri.exception" });
    exceptionMarshaller/*  w  ww . j  a  v  a 2  s  . c  o  m*/
            .setMarshallerProperties(context.getBean("genologicsJaxbMarshallerProperties", Map.class));

    Jaxb2Marshaller original = marshaller;
    try {
        marshaller = exceptionMarshaller;
        fetchMarshalAndSerialize(com.genologics.ri.exception.Exception.class);
    } finally {
        marshaller = original;
    }
}

From source file:org.ikasan.component.converter.xml.XmlByteArrayToObjectConverter.java

@Override
public void setConfiguration(XmlToObjectConverterConfiguration configuration) {
    this.configuration = configuration;
    marshaller = new Jaxb2Marshaller();
    if (StringUtils.isNotEmpty(configuration.getContextPath())) {
        marshaller.setContextPath(configuration.getContextPath());
    } else if (ArrayUtils.isNotEmpty(configuration.getContextPaths())) {
        marshaller.setContextPaths(configuration.getContextPaths());
    } else if (ArrayUtils.isNotEmpty(configuration.getClassesToBeBound())) {
        marshaller.setClassesToBeBound(configuration.getClassesToBeBound());
    }/*from   w ww  .j  av  a  2s . c om*/
    marshaller.setUnmarshallerProperties(configuration.getUnmarshallerProperties());
    marshaller.setMarshallerProperties(configuration.getMarshallerProperties());
    if (configuration.getValidationEventHandler() != null) {
        marshaller.setValidationEventHandler(configuration.getValidationEventHandler());
    }
    try {
        marshaller.afterPropertiesSet();
    } catch (Exception e) {
        throw new ConfigurationException(e.getMessage());
    }
}

From source file:org.ikasan.component.converter.xml.XmlStringToObjectConverter.java

@Override
public void setConfiguration(XmlStringToObjectConfiguration configuration) {
    this.configuration = configuration;
    marshaller = new Jaxb2Marshaller();
    if (StringUtils.isNotEmpty(configuration.getContextPath())) {
        marshaller.setContextPath(configuration.getContextPath());
    } else if (ArrayUtils.isNotEmpty(configuration.getContextPaths())) {
        marshaller.setContextPaths(configuration.getContextPaths());
    } else if (ArrayUtils.isNotEmpty(configuration.getClassesToBeBound())) {
        marshaller.setClassesToBeBound(configuration.getClassesToBeBound());
    }//from w  w  w .  j a v a  2  s . com
    marshaller.setUnmarshallerProperties(configuration.getUnmarshallerProperties());
    marshaller.setMarshallerProperties(configuration.getMarshallerProperties());
    if (configuration.getValidationEventHandler() != null) {
        marshaller.setValidationEventHandler(configuration.getValidationEventHandler());
    }

    try {
        marshaller.afterPropertiesSet();
    } catch (Exception e) {
        throw new ConfigurationException(e.getMessage());
    }
}

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Before
public void setUp() throws IOException {
    File directory = new File("build/data");
    directory.mkdirs();//from  ww  w.  j a  v a2  s  .com
    this.resource = new FileSystemResource(
            File.createTempFile("StaxEventItemWriterBuilderTests", ".xml", directory));

    this.items = new ArrayList<>(3);
    this.items.add(new Foo(1, "two", "three"));
    this.items.add(new Foo(4, "five", "six"));
    this.items.add(new Foo(7, "eight", "nine"));

    marshaller = new Jaxb2Marshaller();
    ((Jaxb2Marshaller) marshaller).setClassesToBeBound(Foo.class);
}

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Test(expected = IllegalArgumentException.class)
public void testMissingNameValidation() {
    new StaxEventItemWriterBuilder<Foo>().marshaller(new Jaxb2Marshaller()).build();
}

From source file:org.springframework.batch.item.xml.Jaxb2NamespaceMarshallingTests.java

protected Marshaller getMarshaller() throws Exception {

    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class });
    marshaller.afterPropertiesSet();/*www .  j a v  a  2  s .  co m*/

    StringWriter string = new StringWriter();
    marshaller.marshal(new QualifiedTrade("FOO", 100, BigDecimal.valueOf(10.), "bar"),
            new StreamResult(string));
    String content = string.toString();
    assertTrue("Wrong content: " + content, content.contains("<customer>bar</customer>"));
    return marshaller;
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

@Before
public void setUp() throws Exception {
    File directory = new File("build/data");
    directory.mkdirs();/* www . j  a v a2 s  . co m*/
    resource = new FileSystemResource(
            File.createTempFile("StaxEventWriterOutputSourceTests", ".xml", directory));
    writer = createItemWriter();
    executionContext = new ExecutionContext();
    jaxbMarshaller = new Jaxb2Marshaller();
    jaxbMarshaller.setClassesToBeBound(JAXBItem.class);
}