001package org.nuxeo.scim.server.jaxrs.marshalling; 002 003import java.io.IOException; 004import java.io.OutputStream; 005import java.lang.annotation.Annotation; 006import java.lang.reflect.Type; 007 008import javax.ws.rs.Produces; 009import javax.ws.rs.WebApplicationException; 010import javax.ws.rs.core.MediaType; 011import javax.ws.rs.core.MultivaluedMap; 012import javax.ws.rs.ext.MessageBodyWriter; 013import javax.ws.rs.ext.Provider; 014 015import com.unboundid.scim.data.BaseResource; 016import com.unboundid.scim.marshal.Marshaller; 017import com.unboundid.scim.marshal.json.JsonMarshaller; 018import com.unboundid.scim.marshal.xml.XmlMarshaller; 019import com.unboundid.scim.sdk.Resources; 020import com.unboundid.scim.sdk.SCIMException; 021 022/** 023 * Handles marshaling for SCIM {@link BaseResource} 024 * @author tiry 025 * @since 7.4 026 */ 027@Provider 028@Produces({ "application/xml", "application/json" }) 029public class ResourcesWriter implements MessageBodyWriter<Resources<BaseResource>> { 030 031 @Override 032 public boolean isWriteable(Class<?> type, Type genericType, 033 Annotation[] annotations, MediaType mediaType) { 034 return Resources.class.isAssignableFrom(type); 035 } 036 037 @Override 038 public long getSize(Resources<BaseResource> t, Class<?> type, Type genericType, 039 Annotation[] annotations, MediaType mediaType) { 040 return -1; 041 } 042 043 @Override 044 public void writeTo(Resources<BaseResource> t, Class<?> type, Type genericType, 045 Annotation[] annotations, MediaType mediaType, 046 MultivaluedMap<String, Object> httpHeaders, 047 OutputStream entityStream) throws IOException, 048 WebApplicationException { 049 050 try { 051 Marshaller marshaller = null; 052 if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) { 053 marshaller = new XmlMarshaller(); 054 } else { 055 marshaller = new JsonMarshaller(); 056 } 057 marshaller.marshal(t, entityStream); 058 } catch (SCIMException e) { 059 throw new WebApplicationException(e); 060 } 061 } 062 063}