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.BufferedReader;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.InputStreamReader;
026import java.lang.annotation.Annotation;
027import java.lang.reflect.Type;
028import java.util.List;
029
030import javax.ws.rs.WebApplicationException;
031import javax.ws.rs.core.MediaType;
032import javax.ws.rs.core.MultivaluedMap;
033import javax.ws.rs.ext.MessageBodyReader;
034import javax.ws.rs.ext.Provider;
035
036import org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultMarshaler;
037import org.nuxeo.ecm.webengine.WebEngine;
038
039@Provider
040public class RemotePubMessageReader implements MessageBodyReader<RemotePubParam> {
041
042    public boolean isReadable(Class arg0, Type arg1, Annotation[] arg2, MediaType mt) {
043        return mt.equals(RemotePubParam.mediaType);
044    }
045
046    public RemotePubParam readFrom(Class arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap arg4,
047            InputStream is) throws IOException, WebApplicationException {
048
049        DefaultMarshaler marshaler = new DefaultMarshaler(WebEngine.getActiveContext().getCoreSession());
050
051        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
052        BufferedReader br = new BufferedReader(isr);
053        StringBuffer sb = new StringBuffer();
054        int ch;
055        while ((ch = br.read()) > -1) {
056            sb.append((char) ch);
057        }
058        br.close();
059
060        String xmlData = sb.toString();
061
062        List<Object> params = marshaler.unMarshallParameters(xmlData);
063        return new RemotePubParam(params);
064    }
065
066}