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.OutputStream;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Type;
025
026import javax.ws.rs.Produces;
027import javax.ws.rs.WebApplicationException;
028import javax.ws.rs.core.MediaType;
029import javax.ws.rs.core.MultivaluedMap;
030import javax.ws.rs.ext.MessageBodyWriter;
031import javax.ws.rs.ext.Provider;
032
033import com.unboundid.scim.data.UserResource;
034import com.unboundid.scim.marshal.Marshaller;
035import com.unboundid.scim.marshal.json.JsonMarshaller;
036import com.unboundid.scim.marshal.xml.XmlMarshaller;
037import com.unboundid.scim.sdk.SCIMException;
038
039/**
040 * Handles marshaling for SCIM {@link UserResource}
041 *
042 * @author tiry
043 * @since 7.4
044 */
045@Provider
046@Produces({ "application/xml", "application/json" })
047public class UserResourceWriter implements MessageBodyWriter<UserResource> {
048
049    @Override
050    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
051        return UserResource.class.isAssignableFrom(type);
052    }
053
054    @Override
055    public long getSize(UserResource t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
056        return -1;
057    }
058
059    @Override
060    public void writeTo(UserResource t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
061            MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
062            WebApplicationException {
063
064        try {
065            Marshaller marshaller = null;
066            httpHeaders.remove("Content-Type");
067            if (t instanceof UserResourceWithMimeType) {
068                if (((UserResourceWithMimeType) t).getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) {
069                    marshaller = new JsonMarshaller();
070                    httpHeaders.add("Content-Type", MediaType.APPLICATION_JSON);
071                } else {
072                    marshaller = new XmlMarshaller();
073                    httpHeaders.add("Content-Type", MediaType.APPLICATION_XML);
074                }
075            } else {
076                if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
077                    marshaller = new XmlMarshaller();
078                    httpHeaders.add("Content-Type", MediaType.APPLICATION_XML);
079                } else {
080                    marshaller = new JsonMarshaller();
081                    httpHeaders.add("Content-Type", MediaType.APPLICATION_JSON);
082                }
083            }
084            marshaller.marshal(t, entityStream);
085        } catch (SCIMException e) {
086            throw new WebApplicationException(e);
087        }
088    }
089
090}