All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tangosol.coherence.rest.providers.XmlCollectionWriter Maven / Gradle / Ivy

There is a newer version: 24.09
Show newest version
/*
 * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.tangosol.coherence.rest.providers;

import com.tangosol.coherence.rest.io.Marshaller;
import com.tangosol.coherence.rest.io.MarshallerRegistry;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import java.lang.annotation.Annotation;

import java.lang.reflect.Type;

import java.util.Collection;
import java.util.Map;

import javax.inject.Inject;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import static javax.ws.rs.core.MediaType.APPLICATION_XML;

/**
 * Provider responsible for converting Java collections to a XML formatted
 * stream.
 *
 * @author vp 2011.06.29
 */
@Provider
@Produces(APPLICATION_XML)
public class XmlCollectionWriter
        implements MessageBodyWriter
    {
    // ----- constructors ---------------------------------------------------

    /**
     * Default constructor.
     */
    public XmlCollectionWriter()
        {
        }

    /**
     * Construct an XmlCollectionWriter instance.
     *
     * @param registry  marshaller registry to use
     */
    public XmlCollectionWriter(MarshallerRegistry registry)
        {
        m_marshallerRegistry = registry;
        }

    // ----- MessageBodyWriter interface ------------------------------------

    @Override
    public boolean isWriteable(Class clz, Type type, Annotation[] aAnnotation,
            MediaType mediaType)
        {
        return Collection.class.isAssignableFrom(clz);
        }

    @Override
    public long getSize(Object col, Class clz, Type type, Annotation[] aAnnotation,
            MediaType mediaType)
        {
        return -1;
        }

    @Override
    public void writeTo(Object oCol, Class clz, Type type, Annotation[] aAnnotation,
            MediaType mediaType, MultivaluedMap httpHeaders, OutputStream stream)
            throws IOException, WebApplicationException
        {
        MarshallerRegistry registry = m_marshallerRegistry;
        if (registry == null)
            {
            throw new IllegalStateException("MarshallerRegistry not configured");
            }

        EntryWriter entryWriter = new EntryWriter(registry);
        PrintStream out         = new PrintStream(stream);

        out.print("");
        out.print("");

        for (Object o : (Collection) oCol)
            {
            if (o instanceof Map.Entry)
                {
                entryWriter.writeTo(o, o.getClass(), null, null, mediaType, null, stream);
                }
            else if (o != null)
                {
                Marshaller marshaller = registry.getMarshaller(o.getClass(), mediaType);
                marshaller.marshalAsFragment(o, stream, httpHeaders);
                }
            }

        out.print("");
        }

    // ---- data members ----------------------------------------------------

    /**
     * Marshaller registry to obtain element marshallers from.
     */
    @Inject
    protected MarshallerRegistry m_marshallerRegistry;
    }