001/*
002 * (C) Copyright 2006-2008 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 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.http;
023
024import java.io.IOException;
025import java.io.InputStream;
026
027import javax.servlet.http.HttpServlet;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032
033/**
034 * @author Alexandre Russel
035 */
036public class AnnotationsServlet extends HttpServlet {
037
038    private static final String REPLACE_SOURCE = "replace_source";
039
040    private static final String W3C_ANNOTATES = "w3c_annotates";
041
042    private static final String DOCUMENT_URL = "document_url";
043
044    private static final long serialVersionUID = 1L;
045
046    private AnnotationServiceFacade facade;
047
048    @Override
049    public void init() {
050        facade = new AnnotationServiceFacade();
051    }
052
053    // HTTP Methods
054    @Override
055    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
056        String w3c_annotates = req.getParameter(W3C_ANNOTATES);
057        String annId = null;
058        if (req.getPathInfo() != null) {
059            annId = req.getPathInfo().replaceFirst("/", "");
060        }
061        resp.setContentType("application/xml");
062        resp.setHeader("Cache-Control", "no-cache");
063        resp.setHeader("Pragma", "no-cache");
064        if (annId != null) {
065            facade.getAnnotation(annId, (NuxeoPrincipal) req.getUserPrincipal(), resp.getOutputStream(),
066                    req.getRequestURL() + "/");
067        } else if (w3c_annotates != null && !w3c_annotates.isEmpty()) {
068            facade.query(w3c_annotates, resp.getOutputStream(), (NuxeoPrincipal) req.getUserPrincipal());
069        }
070    }
071
072    @Override
073    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
074        resp.setHeader("Cache-Control", "no-cache");
075        resp.setHeader("Pragma", "no-cache");
076
077        String replace_source = req.getParameter(REPLACE_SOURCE);
078        if (replace_source != null) {
079            InputStream is = req.getInputStream();
080            facade.updateAnnotation(is, (NuxeoPrincipal) req.getUserPrincipal(), resp.getOutputStream(),
081                    getBaseUrl(req));
082        } else {
083            StringBuffer baseUrl = req.getRequestURL();
084            facade.createAnnotation(req.getInputStream(), (NuxeoPrincipal) req.getUserPrincipal(),
085                    resp.getOutputStream(), baseUrl.toString());
086        }
087    }
088
089    private static String getBaseUrl(HttpServletRequest req) {
090        StringBuffer url = req.getRequestURL();
091        int index = url.indexOf(req.getServletPath());
092        return url.substring(0, index);
093    }
094
095    @Override
096    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
097        resp.setHeader("Cache-Control", "no-cache");
098        resp.setHeader("Pragma", "no-cache");
099        String annId = req.getPathInfo().replaceFirst("/", "");
100        String documentUrl = req.getParameter(DOCUMENT_URL);
101        facade.deleteFor(documentUrl, annId, (NuxeoPrincipal) req.getUserPrincipal(), getBaseUrl(req) + "/");
102    }
103
104    @Override
105    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
106        resp.setHeader("Cache-Control", "no-cache");
107        resp.setHeader("Pragma", "no-cache");
108        facade.updateAnnotation(req.getInputStream(), (NuxeoPrincipal) req.getUserPrincipal(), resp.getOutputStream(),
109                req.getRequestURL() + "/");
110    }
111
112}