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