001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (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 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.mock;
021
022import java.io.BufferedReader;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.InputStreamReader;
026import java.io.Writer;
027
028import javax.servlet.ServletException;
029import javax.servlet.http.HttpServlet;
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032
033/**
034 * @author Alexandre Russel
035 */
036public class MockAnnoteaServer extends HttpServlet {
037    private static final String DESCRIPTION = "</r:Description>";
038
039    private static final String R_DESCRIPTION = "<r:Description>";
040
041    private static final StringBuilder response = new StringBuilder(
042            " <r:RDF xmlns:r=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n"
043                    + "           xmlns:a=\"http://www.w3.org/2000/10/annotation-ns#\" \n"
044                    + "           xmlns:d=\"http://purl.org/dc/elements/1.1/\"></r:RDF>");
045
046    private static final long serialVersionUID = 1L;
047
048    @Override
049    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
050        Writer writer = resp.getWriter();
051        writer.write(response.toString());
052    }
053
054    @Override
055    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
056        InputStream is = req.getInputStream();
057        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
058        String result;
059        StringBuilder request = new StringBuilder();
060        while ((result = reader.readLine()) != null) {
061            request.append(result);
062        };
063        String r = request.toString();
064        String desc = r.substring(r.indexOf(R_DESCRIPTION), r.indexOf(DESCRIPTION) + DESCRIPTION.length());
065        response.insert(response.lastIndexOf("</r:RDF>"), desc);
066    }
067
068}