001/*
002 * (C) Copyright 2006-2009 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
018 */
019
020package org.nuxeo.ecm.platform.publisher.remoting.restHandler;
021
022import java.io.ByteArrayInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.lang.annotation.Annotation;
027import java.lang.reflect.Type;
028
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
035@Provider
036public class RemotePubMessageWriter implements MessageBodyWriter<RemotePubResult> {
037
038    protected static final int BUFFER_SIZE = 4096 * 16;
039
040    public long getSize(RemotePubResult arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
041        return -1;
042    }
043
044    public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
045        return RemotePubResult.class.isAssignableFrom(arg0);
046    }
047
048    public void writeTo(RemotePubResult result, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
049            MultivaluedMap<String, Object> arg5, OutputStream stream) throws IOException, WebApplicationException {
050        String xmlString = result.asXML();
051        InputStream in = new ByteArrayInputStream(xmlString.getBytes());
052        byte[] buffer = new byte[BUFFER_SIZE];
053        int read;
054        while ((read = in.read(buffer)) != -1) {
055            stream.write(buffer, 0, read);
056        }
057    }
058
059}