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 final AnnotationServiceFacade facade; 047 048 public AnnotationsServlet() { 049 facade = new AnnotationServiceFacade(); 050 } 051 052 // HTTP Methods 053 @Override 054 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 055 String w3c_annotates = req.getParameter(W3C_ANNOTATES); 056 String annId = null; 057 if (req.getPathInfo() != null) { 058 annId = req.getPathInfo().replaceFirst("/", ""); 059 } 060 resp.setContentType("application/xml"); 061 resp.setHeader("Cache-Control", "no-cache"); 062 resp.setHeader("Pragma", "no-cache"); 063 if (annId != null) { 064 facade.getAnnotation(annId, (NuxeoPrincipal) req.getUserPrincipal(), resp.getOutputStream(), 065 req.getRequestURL() + "/"); 066 } else if (w3c_annotates != null && !w3c_annotates.isEmpty()) { 067 facade.query(w3c_annotates, resp.getOutputStream(), (NuxeoPrincipal) req.getUserPrincipal()); 068 } 069 } 070 071 @Override 072 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 073 resp.setHeader("Cache-Control", "no-cache"); 074 resp.setHeader("Pragma", "no-cache"); 075 076 String replace_source = req.getParameter(REPLACE_SOURCE); 077 if (replace_source != null) { 078 InputStream is = req.getInputStream(); 079 facade.updateAnnotation(is, (NuxeoPrincipal) req.getUserPrincipal(), resp.getOutputStream(), 080 getBaseUrl(req)); 081 } else { 082 StringBuffer baseUrl = req.getRequestURL(); 083 facade.createAnnotation(req.getInputStream(), (NuxeoPrincipal) req.getUserPrincipal(), 084 resp.getOutputStream(), baseUrl.toString()); 085 } 086 } 087 088 private static String getBaseUrl(HttpServletRequest req) { 089 StringBuffer url = req.getRequestURL(); 090 int index = url.indexOf(req.getServletPath()); 091 return url.substring(0, index); 092 } 093 094 @Override 095 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException { 096 resp.setHeader("Cache-Control", "no-cache"); 097 resp.setHeader("Pragma", "no-cache"); 098 String annId = req.getPathInfo().replaceFirst("/", ""); 099 String documentUrl = req.getParameter(DOCUMENT_URL); 100 facade.deleteFor(documentUrl, annId, (NuxeoPrincipal) req.getUserPrincipal(), getBaseUrl(req) + "/"); 101 } 102 103 @Override 104 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { 105 resp.setHeader("Cache-Control", "no-cache"); 106 resp.setHeader("Pragma", "no-cache"); 107 facade.updateAnnotation(req.getInputStream(), (NuxeoPrincipal) req.getUserPrincipal(), resp.getOutputStream(), 108 req.getRequestURL() + "/"); 109 } 110 111}