001/*
002 * (C) Copyright 2015 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.scim.server.jaxrs.marshalling;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Type;
025
026import javax.ws.rs.Consumes;
027import javax.ws.rs.WebApplicationException;
028import javax.ws.rs.core.MediaType;
029import javax.ws.rs.core.MultivaluedMap;
030import javax.ws.rs.ext.MessageBodyReader;
031import javax.ws.rs.ext.Provider;
032
033import com.unboundid.scim.data.GroupResource;
034import com.unboundid.scim.marshal.Unmarshaller;
035import com.unboundid.scim.marshal.xml.XmlUnmarshaller;
036import com.unboundid.scim.schema.CoreSchema;
037import com.unboundid.scim.sdk.InvalidResourceException;
038
039/**
040 * Handles marshaling for SCIM {@link GroupResource}
041 *
042 * @author tiry
043 * @since 7.4
044 */
045@Provider
046@Consumes({ "application/xml", "application/json" })
047public class GroupResourceReader implements MessageBodyReader<GroupResource> {
048
049    @Override
050    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
051        return GroupResource.class.isAssignableFrom(type);
052    }
053
054    @Override
055    public GroupResource readFrom(Class<GroupResource> type, Type genericType, Annotation[] annotations,
056            MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
057            throws IOException, WebApplicationException {
058
059        Unmarshaller unmarshaller = null;
060        if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
061            unmarshaller = new XmlUnmarshaller();
062        } else {
063            unmarshaller = new NXJsonUnmarshaller();
064        }
065        try {
066            return unmarshaller.unmarshal(entityStream, CoreSchema.GROUP_DESCRIPTOR,
067                    GroupResource.GROUP_RESOURCE_FACTORY);
068        } catch (InvalidResourceException e) {
069            // TODO Auto-generated catch block
070            e.printStackTrace();
071        }
072        return null;
073    }
074
075}