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