001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.remoting.restHandler;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.InputStreamReader;
024import java.lang.annotation.Annotation;
025import java.lang.reflect.Type;
026import java.util.List;
027
028import javax.ws.rs.WebApplicationException;
029import javax.ws.rs.core.MediaType;
030import javax.ws.rs.core.MultivaluedMap;
031import javax.ws.rs.ext.MessageBodyReader;
032import javax.ws.rs.ext.Provider;
033
034import org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultMarshaler;
035import org.nuxeo.ecm.webengine.WebEngine;
036
037@Provider
038public class RemotePubMessageReader implements MessageBodyReader<RemotePubParam> {
039
040    public boolean isReadable(Class arg0, Type arg1, Annotation[] arg2, MediaType mt) {
041        return mt.equals(RemotePubParam.mediaType);
042    }
043
044    public RemotePubParam readFrom(Class arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap arg4,
045            InputStream is) throws IOException, WebApplicationException {
046
047        DefaultMarshaler marshaler = new DefaultMarshaler(WebEngine.getActiveContext().getCoreSession());
048
049        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
050        BufferedReader br = new BufferedReader(isr);
051        StringBuffer sb = new StringBuffer();
052        int ch;
053        while ((ch = br.read()) > -1) {
054            sb.append((char) ch);
055        }
056        br.close();
057
058        String xmlData = sb.toString();
059
060        List<Object> params = marshaler.unMarshallParameters(xmlData);
061        return new RemotePubParam(params);
062    }
063
064}