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 * Thierry Delprat 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 org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035 036import com.unboundid.scim.data.UserResource; 037import com.unboundid.scim.marshal.Unmarshaller; 038import com.unboundid.scim.marshal.xml.XmlUnmarshaller; 039import com.unboundid.scim.schema.CoreSchema; 040import com.unboundid.scim.sdk.InvalidResourceException; 041 042/** 043 * Handles marshaling for SCIM {@link UserResource} 044 * 045 * @author tiry 046 * @since 7.4 047 */ 048@Provider 049@Consumes({ "application/xml", "application/json" }) 050public class UserResourceReader implements MessageBodyReader<UserResource> { 051 052 private static final Log log = LogFactory.getLog(UserResourceReader.class); 053 054 @Override 055 public boolean isReadable(Class<?> type, Type genericType, 056 Annotation[] annotations, MediaType mediaType) { 057 return UserResource.class.isAssignableFrom(type); 058 } 059 060 @Override 061 public UserResource readFrom(Class<UserResource> type, Type genericType, 062 Annotation[] annotations, MediaType mediaType, 063 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.USER_DESCRIPTOR, UserResource.USER_RESOURCE_FACTORY); 074 } catch (InvalidResourceException e) { 075 log.error(e, e); 076 } 077 return null; 078 } 079 080}