Example usage for javax.xml.bind Marshaller setAttachmentMarshaller

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

Introduction

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

Prototype

void setAttachmentMarshaller(AttachmentMarshaller am);

Source Link

Document

Associate a context that enables binary data within an XML document to be transmitted as XML-binary optimized attachment.

Usage

From source file:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

static Marshaller createMarshaller(AttachmentMarshaller attachmentMarshaller) throws Exception {
    Marshaller marshaller = JAXB_CTX.createMarshaller();

    marshaller.setAttachmentMarshaller(attachmentMarshaller);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    return marshaller;
}

From source file:de.thorstenberger.taskmodel.util.JAXBUtils.java

/**
 * releaseJAXBMarshalller Do not call this method if an exception occurred while using the
 * Marshaller. We object my be in an invalid state.
 *
 * @param context    JAXBContext/*  www .ja  va  2s .c  o  m*/
 * @param marshaller Marshaller
 */
public static void releaseJAXBMarshaller(JAXBContext context, Marshaller marshaller) {
    if (log.isDebugEnabled()) {
        log.debug("Marshaller placed back into pool");
    }
    if (ENABLE_MARSHALL_POOLING) {
        marshaller.setAttachmentMarshaller(null);
        mpool.put(context, marshaller);
    }
}

From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java

/**
 * Marshal the jaxb object/*from   w  w w  . ja  v a 2  s.c o  m*/
 * @param obj
 * @param writer
 * @param am AttachmentMarshaller, optional Attachment
 */
public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
    if (log.isDebugEnabled()) {
        log.debug("enter marshal");
    }
    boolean installedFilter = false;

    try {
        // There may be a preferred classloader that should be used
        ClassLoader cl = getClassLoader();

        // Very easy, use the Context to get the Marshaller.
        // Use the marshaller to write the object.
        JAXBContext jbc = getJAXBContext(cl);
        Marshaller m = JAXBUtils.getJAXBMarshaller(jbc);
        if (writer instanceof MTOMXMLStreamWriter && ((MTOMXMLStreamWriter) writer).getOutputFormat() != null) {
            String encoding = ((MTOMXMLStreamWriter) writer).getOutputFormat().getCharSetEncoding();

            String marshallerEncoding = (String) m.getProperty(Marshaller.JAXB_ENCODING);

            // Make sure that the marshaller respects the encoding of the message.
            // This is accomplished by setting the encoding on the Marshaller's JAXB_ENCODING property.
            if (encoding == null && marshallerEncoding == null) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "The encoding and the marshaller's JAXB_ENCODING are both set to the default (UTF-8)");
                }
            } else {
                // Must set the encoding to an actual String to set it on the Marshaller
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                if (!encoding.equalsIgnoreCase(marshallerEncoding)) {
                    if (log.isDebugEnabled()) {
                        log.debug("The Marshaller.JAXB_ENCODING is " + marshallerEncoding);
                        log.debug(
                                "The Marshaller.JAXB_ENCODING is changed to the message encoding " + encoding);
                    }
                    m.setProperty(Marshaller.JAXB_ENCODING, encoding);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("The encoding and the marshaller's JAXB_ENCODING are both set to:"
                                + marshallerEncoding);
                    }
                }
            }
        }

        AttachmentMarshaller am = createAttachmentMarshaller(writer);
        if (am != null) {
            if (DEBUG_ENABLED) {
                log.debug("Adding JAXBAttachmentMarshaller to Marshaller");
            }
            m.setAttachmentMarshaller(am);
        }

        MessageContext mc = getMessageContext();

        // If requested install a filter to remove illegal characters
        installedFilter = installFilter(mc, writer);

        // Marshal the object
        if (getProcessType() == null) {
            marshalByElement(obj, m, writer, true);
            //!am.isXOPPackage());
        } else {
            marshalByType(obj, m, writer, getProcessType(), isxmlList(), getConstructionType(), true); // Attempt to optimize by writing to OutputStream
        }

        JAXBUtils.releaseJAXBMarshaller(jbc, m);

        if (log.isDebugEnabled()) {
            log.debug("exit marshal");
        }
    } finally {
        // Make sure the filter is uninstalled
        if (installedFilter) {
            uninstallFilter(writer);
        }
    }
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

/**
 * releaseJAXBMarshalller //from   www.  j  a  v  a2s. c o  m
 * Do not call this method if an exception occurred while using the
 * Marshaller. We don't want an object in an invalid state.
 *
 * @param context    JAXBContext
 * @param marshaller Marshaller
 */
public static void releaseJAXBMarshaller(JAXBContext context, Marshaller marshaller) {
    if (log.isDebugEnabled()) {
        log.debug("Marshaller placed back into pool");
        log.debug("  Marshaller = " + JavaUtils.getObjectIdentity(marshaller));
        log.debug("  JAXBContext = " + JavaUtils.getObjectIdentity(context));
    }
    if (ENABLE_MARSHALL_POOLING) {
        // Make sure to clear any state or properties

        try {
            marshaller.setAttachmentMarshaller(null);
            // Set the JAXB_ENCODING back to the default value UTF-8
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mpool.put(context, marshaller);
        } catch (Throwable t) {
            // Log the problem, and continue without pooling
            if (log.isDebugEnabled()) {
                log.debug("The following exception is ignored. Processing continues " + t);
            }
        }

    }
}

From source file:org.apache.cxf.jaxbplus.io.DataWriterImpl.java

public Marshaller createMarshaller(Object elValue, MessagePartInfo part) {
    Class<?> cls = null;// w ww .j av a 2 s  .c o  m
    if (part != null) {
        cls = part.getTypeClass();
    }

    if (cls == null) {
        cls = null != elValue ? elValue.getClass() : null;
    }

    if (cls != null && cls.isArray() && elValue instanceof Collection) {
        Collection<?> col = (Collection<?>) elValue;
        elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size()));
    }
    Marshaller marshaller;
    try {

        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setListener(databinding.getMarshallerListener());
        if (databinding.getValidationEventHandler() != null) {
            marshaller.setEventHandler(databinding.getValidationEventHandler());
        }

        final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings();
        if (nspref != null) {
            JAXBUtils.setNamespaceWrapper(nspref, marshaller);
        }
        if (databinding.getMarshallerProperties() != null) {
            for (Map.Entry<String, Object> propEntry : databinding.getMarshallerProperties().entrySet()) {
                try {
                    marshaller.setProperty(propEntry.getKey(), propEntry.getValue());
                } catch (PropertyException pe) {
                    LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe);
                }
            }
        }

        marshaller.setSchema(schema);
        AttachmentMarshaller atmarsh = getAttachmentMarshaller();
        marshaller.setAttachmentMarshaller(atmarsh);

        if (schema != null && atmarsh instanceof JAXBAttachmentMarshaller) {
            //we need a special even handler for XOP attachments 
            marshaller.setEventHandler(new MtomValidationHandler(marshaller.getEventHandler(),
                    (JAXBAttachmentMarshaller) atmarsh));
        }
    } catch (JAXBException ex) {
        if (ex instanceof javax.xml.bind.MarshalException) {
            javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex;
            Message faultMessage = new Message("MARSHAL_ERROR", LOG,
                    marshalEx.getLinkedException().getMessage());
            throw new Fault(faultMessage, ex);
        } else {
            throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex);
        }
    }
    return marshaller;
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSOperationBinding.java

public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context) throws XFireFault {
    if (this.responseInfo == null) {
        throw new XFireFault("Unable to write message: no response info was found!", XFireFault.SENDER);
    }//from  w w w.  ja v  a  2  s.  c om

    Class beanClass = this.responseInfo.getBeanClass();
    Object[] params = (Object[]) message.getBody();
    Object bean;
    if (this.responseInfo.isBare()) {
        //bare response.  we don't need to wrap it up.
        bean = new JAXBElement(this.responseInfo.getMessageInfo().getName(), this.responseInfo.getBeanClass(),
                params[0]);
    } else {
        try {
            bean = beanClass.newInstance();
        } catch (Exception e) {
            throw new XFireFault("Problem instantiating response wrapper " + beanClass.getName() + ".", e,
                    XFireFault.RECEIVER);
        }

        PropertyDescriptor[] properties = this.responseInfo.getPropertyOrder();
        if (properties.length > 0) { //no properties implies a void method...
            if (properties.length != params.length) {
                throw new XFireFault("There are " + params.length + " parameters to the out message but "
                        + properties.length + " properties on " + beanClass.getName(), XFireFault.RECEIVER);
            }

            for (int i = 0; i < properties.length; i++) {
                PropertyDescriptor descriptor = properties[i];
                try {
                    descriptor.getWriteMethod().invoke(bean, params[i]);
                } catch (IllegalAccessException e) {
                    throw new XFireFault("Problem with property " + descriptor.getName() + " on "
                            + beanClass.getName() + ".", e, XFireFault.RECEIVER);
                } catch (InvocationTargetException e) {
                    throw new XFireFault("Problem with property " + descriptor.getName() + " on "
                            + beanClass.getName() + ".", e, XFireFault.RECEIVER);
                }
            }
        }
    }

    try {
        Marshaller marshaller = this.jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setAttachmentMarshaller(new AttachmentMarshaller(context));
        marshaller.marshal(bean, writer);
    } catch (JAXBException e) {
        throw new XFireRuntimeException("Unable to marshal type.", e);
    }
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSWebFaultHandler.java

@Override
protected void handleFault(MessageContext context, XFireFault fault, Throwable cause, MessagePartInfo faultPart)
        throws XFireFault {
    Object faultBean = getFaultBean(cause, faultPart, context);
    if (faultBean != null) {
        try {//from ww  w . j a  v a 2 s  .com
            JAXBContext jaxbContext = JAXBContext.newInstance(faultBean.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            marshaller.setAttachmentMarshaller(new AttachmentMarshaller(context));
            JDOMStreamWriter writer = new JDOMStreamWriter(fault.getDetail());
            marshaller.marshal(faultBean, writer);
        } catch (JAXBException e) {
            LOG.error("Unable to marshal the fault bean of type " + faultBean.getClass().getName() + ".", e);
            //fall through... let the fault be handled by something else...
        }
    }
}

From source file:org.paxle.core.doc.impl.BasicDocumentFactory.java

public <Doc> Map<String, DataHandler> marshal(Doc document, OutputStream output) throws IOException {
    try {//from   ww  w.j  a v a2  s.  co  m

        final JaxbAttachmentMarshaller am = new JaxbAttachmentMarshaller();
        final Marshaller m = context.createMarshaller();

        m.setAdapter(JaxbDocAdapter.class, new JaxbDocAdapter());
        m.setAdapter(JaxbFileAdapter.class, new JaxbFileAdapter(this.tempFileManager));
        m.setAdapter(JaxbFieldMapAdapter.class, new JaxbFieldMapAdapter(this.tempFileManager));
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        //         m.setProperty("com.sun.xml.bind.ObjectFactory", new BasicJaxbFactory());
        m.setAttachmentMarshaller(am);

        m.marshal(document, output);
        output.flush();

        return am.getAttachmentsMap();
    } catch (JAXBException e) {
        final IOException ioe = new IOException(
                String.format("Unable to marshal the document '%s'.", document.getClass().getName()));
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@Override
public void marshal(Object graph, Result result, @Nullable MimeContainer mimeContainer)
        throws XmlMappingException {
    try {/*from  w  w w. j a va 2  s .c  om*/
        Marshaller marshaller = createMarshaller();
        if (this.mtomEnabled && mimeContainer != null) {
            marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer));
        }
        if (StaxUtils.isStaxResult(result)) {
            marshalStaxResult(marshaller, graph, result);
        } else {
            marshaller.marshal(graph, result);
        }
    } catch (JAXBException ex) {
        throw convertJaxbException(ex);
    }
}