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.client.annotea; 021 022import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration; 023import org.nuxeo.ecm.platform.annotations.gwt.client.controler.AnnotationController; 024import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation; 025 026import com.allen_sauer.gwt.log.client.Log; 027import com.google.gwt.core.client.GWT; 028import com.google.gwt.http.client.Request; 029import com.google.gwt.http.client.RequestBuilder; 030import com.google.gwt.http.client.RequestCallback; 031import com.google.gwt.http.client.RequestException; 032import com.google.gwt.http.client.Response; 033import com.google.gwt.http.client.URL; 034import com.google.gwt.user.client.Window; 035 036/** 037 * @author Alexandre Russel 038 */ 039public class AnnoteaClient { 040 041 private static AnnoteaResponseManager responseManager; 042 043 private static WebConfiguration webConfiguration; 044 045 private RequestBuilder postRequest; 046 047 private static AnnotationController controller; 048 049 public AnnoteaClient(AnnotationController annotationController) { 050 controller = annotationController; 051 responseManager = new AnnoteaResponseManager(annotationController); 052 webConfiguration = annotationController.getWebConfiguration(); 053 } 054 055 public void submitAnnotation(Annotation newAnnotation) { 056 AnnotationXmlGenerator xmlGenerator = new AnnotationXmlGenerator(webConfiguration, newAnnotation); 057 String request = xmlGenerator.generateXml(); 058 postRequest = new RequestBuilder(RequestBuilder.POST, URL.encode(controller.getAnnoteaServerUrl())); 059 try { 060 postRequest.sendRequest(request, new RequestCallback() { 061 public void onError(Request request, Throwable exception) { 062 Window.alert("Error while sending data to annotea server: " + exception.toString()); 063 } 064 065 public void onResponseReceived(Request request, Response response) { 066 responseManager.processSubmitAnnotationResponse(response.getText()); 067 getAnnotationList(controller.getDocumentUrl()); 068 controller.reloadAnnotations(); 069 } 070 }); 071 } catch (RequestException e) { 072 GWT.log("Error while sending new annotation", e); 073 Log.debug("Error while sending new annotation", e); 074 } 075 } 076 077 public void getAnnotationList(String annotates) { 078 getAnnotationList(annotates, false); 079 } 080 081 public void getAnnotationList(String annotates, final boolean forceDecorate) { 082 if (annotates.contains("?")) { 083 annotates = annotates.substring(0, annotates.indexOf('?')); 084 } 085 String url = controller.getAnnoteaServerUrl() + "?w3c_annotates=" + annotates; 086 RequestBuilder getRequest = new RequestBuilder(RequestBuilder.GET, URL.encode(url)); 087 try { 088 getRequest.sendRequest(null, new RequestCallback() { 089 public void onError(Request request, Throwable exception) { 090 } 091 092 public void onResponseReceived(Request request, Response response) { 093 responseManager.processAnnotationListResponse(response.getText()); 094 if (forceDecorate) { 095 // Force all the annotations to be redecorated 096 controller.updateAnnotations(true); 097 } 098 } 099 }); 100 } catch (RequestException e) { 101 GWT.log("Error while requesting annotations: " + url, e); 102 Log.debug("Error while requesting annotations: " + url, e); 103 Window.alert(e.toString()); 104 } 105 } 106 107 public void deleteAnnotation(String annotates, final Annotation annotation) { 108 if (annotates.contains("?")) { 109 annotates = annotates.substring(0, annotates.indexOf('?')); 110 } 111 112 String url = controller.getAnnoteaServerUrl() + "/" + annotation.getUUID(); 113 url += "?document_url=" + annotates; 114 RequestBuilder req = new RequestBuilder("DELETE", url) { 115 // nothing to override... used to make a *real* HTTP DELETE request 116 }; 117 118 try { 119 req.sendRequest(null, new RequestCallback() { 120 public void onError(Request arg0, Throwable arg1) { 121 } 122 123 public void onResponseReceived(Request arg0, Response arg1) { 124 getAnnotationList(Window.Location.getHref(), true); 125 controller.reloadAnnotations(); 126 } 127 }); 128 } catch (RequestException e) { 129 GWT.log("Error while deleting an annotation: " + url, e); 130 Log.debug("Error while deleting an annotation: " + url, e); 131 } 132 } 133 134}