001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.scim.server.jaxrs.marshalling;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.lang.annotation.Annotation;
026import java.lang.reflect.Type;
027
028import javax.ws.rs.Consumes;
029import javax.ws.rs.WebApplicationException;
030import javax.ws.rs.core.MediaType;
031import javax.ws.rs.core.MultivaluedMap;
032import javax.ws.rs.ext.MessageBodyReader;
033import javax.ws.rs.ext.Provider;
034
035import com.unboundid.scim.data.GroupResource;
036import com.unboundid.scim.marshal.Unmarshaller;
037import com.unboundid.scim.marshal.xml.XmlUnmarshaller;
038import com.unboundid.scim.schema.CoreSchema;
039import com.unboundid.scim.sdk.InvalidResourceException;
040
041/**
042 * Handles marshaling for SCIM {@link GroupResource}
043 *
044 * @author tiry
045 * @since 7.4
046 */
047@Provider
048@Consumes({ "application/xml", "application/json" })
049public class GroupResourceReader implements MessageBodyReader<GroupResource> {
050
051    @Override
052    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
053        return GroupResource.class.isAssignableFrom(type);
054    }
055
056    @Override
057    public GroupResource readFrom(Class<GroupResource> type, Type genericType, Annotation[] annotations,
058            MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
059            throws IOException, WebApplicationException {
060
061        Unmarshaller unmarshaller = null;
062        if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
063            unmarshaller = new XmlUnmarshaller();
064        } else {
065            unmarshaller = new NXJsonUnmarshaller();
066        }
067        try {
068            return unmarshaller.unmarshal(entityStream, CoreSchema.GROUP_DESCRIPTOR,
069                    GroupResource.GROUP_RESOURCE_FACTORY);
070        } catch (InvalidResourceException e) {
071            // TODO Auto-generated catch block
072            e.printStackTrace();
073        }
074        return null;
075    }
076
077}