001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.ecm.restapi.server.jaxrs.routing.io;
022
023import java.io.IOException;
024import java.io.OutputStream;
025import java.lang.annotation.Annotation;
026import java.lang.reflect.Type;
027
028import javax.ws.rs.WebApplicationException;
029import javax.ws.rs.core.MediaType;
030import javax.ws.rs.core.MultivaluedMap;
031import javax.ws.rs.ext.MessageBodyWriter;
032import javax.ws.rs.ext.Provider;
033
034import org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService;
035import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * @since 7.2
040 */
041@Provider
042public class WorkflowRequestWriter implements MessageBodyWriter<DocumentRoute> {
043
044    @Override
045    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
046        return DocumentRoute.class.isAssignableFrom(type);
047    }
048
049    @Override
050    public long getSize(DocumentRoute t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
051        return -1;
052    }
053
054    @Override
055    public void writeTo(DocumentRoute t, Class<?> type, Type genericType, Annotation[] annotations,
056            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
057            throws IOException, WebApplicationException {
058        Framework.getService(ObjectCodecService.class).write(entityStream, t);
059    }
060
061}