001package org.nuxeo.scim.server.jaxrs.marshalling;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.lang.annotation.Annotation;
006import java.lang.reflect.Type;
007
008import javax.ws.rs.Consumes;
009import javax.ws.rs.WebApplicationException;
010import javax.ws.rs.core.MediaType;
011import javax.ws.rs.core.MultivaluedMap;
012import javax.ws.rs.ext.MessageBodyReader;
013import javax.ws.rs.ext.Provider;
014
015import com.unboundid.scim.data.UserResource;
016import com.unboundid.scim.marshal.Unmarshaller;
017import com.unboundid.scim.marshal.xml.XmlUnmarshaller;
018import com.unboundid.scim.schema.CoreSchema;
019import com.unboundid.scim.sdk.InvalidResourceException;
020
021/**
022 * Handles marshaling for SCIM {@link UserResource}
023 *
024 * @author tiry
025 * @since 7.4
026 */
027@Provider
028@Consumes({ "application/xml", "application/json" })
029public class UserResourceReader implements MessageBodyReader<UserResource> {
030
031    @Override
032    public boolean isReadable(Class<?> type, Type genericType,
033            Annotation[] annotations, MediaType mediaType) {
034        return UserResource.class.isAssignableFrom(type);
035    }
036
037    @Override
038    public UserResource readFrom(Class<UserResource> type, Type genericType,
039            Annotation[] annotations, MediaType mediaType,
040            MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
041            throws IOException, WebApplicationException {
042
043        Unmarshaller unmarshaller = null;
044        if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
045            unmarshaller = new XmlUnmarshaller();
046        } else {
047            unmarshaller = new NXJsonUnmarshaller();
048        }
049         try {
050            return unmarshaller.unmarshal(entityStream, CoreSchema.USER_DESCRIPTOR, UserResource.USER_RESOURCE_FACTORY);
051        } catch (InvalidResourceException e) {
052            // TODO Auto-generated catch block
053            e.printStackTrace();
054        }
055        return null;
056    }
057
058}