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