/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech - initial API and implementation
 *******************************************************************************/
package org.eclipse.kapua.app.api;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.Providers;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;

@Provider
@Produces(MediaType.APPLICATION_XML)
public class ListBodyWriter implements MessageBodyWriter<List> {

	@Context
	Providers providers;

	@Override
	public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) 
	{
		//TODO any ode here to do more significant check on the types ?
		return true;
	}

	@Override
	public long getSize(List t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
	{
		return 0;
	}

	@Override
	public void writeTo(List t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
			throws IOException, WebApplicationException 
	{
		try {
			if (providers == null)
				new WebApplicationException("Unable to find any provider.");

			ContextResolver<JAXBContext> cr = providers.getContextResolver(JAXBContext.class,
																		   MediaType.APPLICATION_XML_TYPE);
			JAXBContext jaxbContext = cr.getContext(JAXBContext.class);
			if (jaxbContext == null)
				new WebApplicationException("Unable to get a JAXBContext.");

			// serialize the entity myBean to the entity output stream
			jaxbContext.createMarshaller().marshal(t, entityStream);
		} catch (JAXBException e) {
			new WebApplicationException(e);
		}
	}

}

@@ -1242,16 +1242,19 @@
 ter%3CList
+%3C?%3E
 %3E %7B%0A%0A%09@C
@@ -1523,24 +1523,27 @@
 getSize(List
+%3C?%3E
  t, Class%3C?%3E
@@ -1669,16 +1669,19 @@
 eTo(List
+%3C?%3E
  t, Clas

Clear warnings by using generics