Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:org.jvoicexml.demo.mmi.simpledemo.SimpleMmiDemo.java

public void send(final Mmi mmi, final URI target) throws JAXBException, IOException {
    final JAXBContext ctx = JAXBContext.newInstance(Mmi.class);
    final Marshaller marshaller = ctx.createMarshaller();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    marshaller.marshal(mmi, out);
    final HttpClient client = new DefaultHttpClient();
    final HttpPost post = new HttpPost(target);
    final HttpEntity entity = new StringEntity(out.toString(), ContentType.APPLICATION_XML);
    post.setEntity(entity);/*  w w w  .  jav a  2 s  .com*/
    client.execute(post);
    LOGGER.info("sending " + mmi + " to '" + target + "'");
}

From source file:org.mule.module.apikit.leagues.Teams.java

@Transformer(resultMimeType = "text/xml")
public String toXml(Teams teams) throws IOException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(getClass());

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    m.marshal(teams, boas);

    return new String(boas.toByteArray());
}

From source file:com.github.woozoo73.ht.format.XmlFormat.java

public String formatInternal(Invocation invocation) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(invocation.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(invocation, writer);

    String xml = writer.getBuffer().toString();

    return xml;/*  w  w w  . ja v  a  2s.  c  om*/
}

From source file:org.apache.cxf.systest.jaxrs.jaxws.BookStoreSoapRestImpl.java

public Book getBook(Long id) throws BookNotFoundFault {

    if (books.get(id) == null) {
        if (id == 0) {
            try {
                OutputStream os = jaxrsContext.getHttpServletResponse().getOutputStream();
                JAXBContext c = JAXBContext.newInstance(new Class[] { Book.class });
                Marshaller m = c.createMarshaller();
                m.marshal(books.get(123L), os);
                os.flush();// ww w. j  a  v a 2 s.  c o  m
                return null;
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new RuntimeException();
            }
        }
        int returnCode = 404;
        if (id == 321) {
            returnCode = 525;
        } else if (id == 322) {
            BookNotFoundDetails details = new BookNotFoundDetails();
            details.setId(id);
            throw new BookNotFoundFault(details);
        }
        String msg = "No Book with id " + id + " is available";
        ResponseBuilder builder = Response.status(returnCode).header("BOOK-HEADER", msg);

        if (returnCode == 404) {
            builder.type("text/plain").entity(msg);
        }
        throw new WebApplicationException(builder.build());
    }

    if (!ignoreJaxrsClient) {
        if (!invocationInProcess) {
            invocationInProcess = true;
            return webClient.getBook(id);
        }
        invocationInProcess = false;
    }

    return books.get(id);
}

From source file:org.mule.module.apikit.leagues.Leagues.java

@Transformer(resultMimeType = "text/xml")
public String toXml(Leagues leagues) throws IOException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(getClass());

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    m.marshal(leagues, boas);

    return new String(boas.toByteArray());
}

From source file:eu.learnpad.core.impl.ca.XwikiBridgeInterfaceRestResource.java

@Override
public String putValidateCollaborativeContent(CollaborativeContentAnalysis contentFile) throws LpRestException {
    HttpClient httpClient = this.getAnonymousClient();
    String uri = String.format("%s/learnpad/ca/bridge/validatecollaborativecontent", this.restPrefix);
    PostMethod postMethod = new PostMethod(uri);

    postMethod.addRequestHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
    try {//w  w w  . j av a  2s  .c  o m
        StringWriter contentWriter = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(CollaborativeContentAnalysis.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(contentFile, contentWriter);

        RequestEntity entity = new StringRequestEntity(contentWriter.toString(), MediaType.APPLICATION_XML,
                null);
        postMethod.setRequestEntity(entity);
    } catch (JAXBException | UnsupportedEncodingException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause());
    }

    try {
        httpClient.executeMethod(postMethod);
        return postMethod.getResponseBodyAsString();
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause());
    }
}

From source file:com.microsoft.exchange.impl.RequestServerTimeZoneInterceptor.java

@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
    WebServiceMessage request = messageContext.getRequest();

    if (!isTimeZoneValid()) {
        throw new ExchangeWebServicesRuntimeException(
                "RequestServerTimeZoneInterceptor - the windowsTimeZoneID specified (" + this.windowsTimeZoneID
                        + ") does not match this systems default time zone (" + TimeZone.getDefault().getID()
                        + ")");
    }/*  w  w  w .j av a2  s. com*/

    if (request instanceof SoapMessage) {
        SoapMessage soapMessage = (SoapMessage) request;
        SoapEnvelope envelope = soapMessage.getEnvelope();
        SoapHeader header = envelope.getHeader();

        TimeZoneContext tzc = new TimeZoneContext();
        TimeZoneDefinitionType timeZoneDef = new TimeZoneDefinitionType();
        timeZoneDef.setId(windowsTimeZoneID);
        tzc.setTimeZoneDefinition(timeZoneDef);

        try {
            Marshaller m = jaxbContext.createMarshaller();
            m.marshal(tzc, header.getResult());
        } catch (JAXBException e) {
            log.error("JAXBException raised while attempting to add TimeZoneContext to soap header " + tzc, e);
            throw new ExchangeWebServicesRuntimeException(
                    "JAXBException raised while attempting to add TimeZoneContext to soap header " + tzc, e);
        }
    }
    return true;
}

From source file:com.microsoft.exchange.impl.ExchangeImpersonationClientInterceptor.java

@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
    WebServiceMessage request = messageContext.getRequest();
    if (request instanceof SoapMessage) {
        SoapMessage soapMessage = (SoapMessage) request;

        ConnectingSIDType connectingSID = connectingSIDSource.getConnectingSID(soapMessage, messageContext);
        if (connectingSID != null) {
            ExchangeImpersonation impersonation = new ExchangeImpersonation();
            impersonation.setConnectingSID(connectingSID);

            SoapEnvelope envelope = soapMessage.getEnvelope();
            SoapHeader header = envelope.getHeader();

            try {
                Marshaller m = jaxbContext.createMarshaller();
                m.marshal(impersonation, header.getResult());
            } catch (JAXBException e) {
                log.error("JAXBException raised while attempting to add ExchangeImpersonation header with SID: "
                        + connectingSID, e);
                throw new ExchangeImpersonationException(
                        "JAXBException raised while attempting to add ExchangeImpersonation header with SID: "
                                + connectingSID,
                        e);//from  w w w  .j  a v  a 2 s.  c  o  m
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("no connectingSID found for " + soapMessage);
            }
        }
    }

    return true;
}

From source file:com.moss.veracity.core.cluster.jms.UpdateTransmitterJMSImpl.java

private void sendMessage(Object o) {

    Session session = null;/* w w w  . java  2 s .c o  m*/
    MessageProducer producer = null;
    try {
        StringWriter writer = new StringWriter();
        Marshaller m = jaxbContext.createMarshaller();
        m.marshal(o, writer);
        String text = writer.getBuffer().toString();

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(UpdateTopic.NAME);
        producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        TextMessage message = session.createTextMessage(text);
        producer.send(message);

        producer.close();
        session.close();
    } catch (Exception ex) {

        if (producer != null) {
            try {
                producer.close();
            } catch (JMSException e) {
                if (log.isErrorEnabled()) {
                    log.error("Failed to close producer after failure", e);
                } else {
                    ex.printStackTrace();
                }
            }
        }

        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                if (log.isErrorEnabled()) {
                    log.error("Failed to close session after failure", e);
                } else {
                    ex.printStackTrace();
                }
            }
        }

        throw new RuntimeException("Message transmission failed: " + o, ex);
    }
}

From source file:mx.bigdata.cfdi.CFDv3.java

private Comprobante copy(Comprobante comprobante) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/* w w  w.j a v  a2s . co m*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    Marshaller m = CONTEXT.createMarshaller();
    m.marshal(comprobante, doc);
    Unmarshaller u = CONTEXT.createUnmarshaller();
    return (Comprobante) u.unmarshal(doc);
}