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 org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037 038import com.unboundid.scim.data.GroupResource; 039import com.unboundid.scim.marshal.Unmarshaller; 040import com.unboundid.scim.marshal.xml.XmlUnmarshaller; 041import com.unboundid.scim.schema.CoreSchema; 042import com.unboundid.scim.sdk.InvalidResourceException; 043 044/** 045 * Handles marshaling for SCIM {@link GroupResource} 046 * 047 * @author tiry 048 * @since 7.4 049 */ 050@Provider 051@Consumes({ "application/xml", "application/json" }) 052public class GroupResourceReader implements MessageBodyReader<GroupResource> { 053 054 private static final Log log = LogFactory.getLog(GroupResourceReader.class); 055 056 @Override 057 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 058 return GroupResource.class.isAssignableFrom(type); 059 } 060 061 @Override 062 public GroupResource readFrom(Class<GroupResource> type, Type genericType, Annotation[] annotations, 063 MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) 064 throws IOException, WebApplicationException { 065 066 Unmarshaller unmarshaller = null; 067 if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) { 068 unmarshaller = new XmlUnmarshaller(); 069 } else { 070 unmarshaller = new NXJsonUnmarshaller(); 071 } 072 try { 073 return unmarshaller.unmarshal(entityStream, CoreSchema.GROUP_DESCRIPTOR, 074 GroupResource.GROUP_RESOURCE_FACTORY); 075 } catch (InvalidResourceException e) { 076 log.error(e, e); 077 } 078 return null; 079 } 080 081}