--- /dev/null
+package com.pentila.emawsconnector.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
+import org.springframework.oxm.xstream.XStreamMarshaller;
+import org.springframework.web.client.RestTemplate;
+
+import com.pentila.evalcomp.service.ws.WsErrors;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+
+/**
+ *
+ * @author pentila.
+ * Cette classe est un connecteur vers EMaEval.
+ * Ce connecteur utilise des webservices de type REST.
+ *
+ */
+public class EmaWSConnector {
+
+ private static String urlBase;
+ private static String encodage;
+ private static String login;
+ private static XStream xstream = new XStream(new DomDriver());
+ public static RestTemplate restTemplate = new RestTemplate();
+ static Long total = 0L;
+
+ public static String getUrlBase() {
+ return urlBase;
+ }
+
+ public static String getLogin(){
+ return login;
+ }
+
+
+ /**
+ * Constructeur de la classe.
+ * @param urlBase URL d'EMaEval. Par exemple http://evalcomp.com/webservices/
+ * @param encodage format d'encodage du retour. "xml" ou "json"
+ * @param login login de l'utilisateur authentifié
+ */
+ public EmaWSConnector(String urlBase, String encodage, String login){
+ EmaWSConnector.urlBase = urlBase;
+ EmaWSConnector.encodage = encodage;
+ EmaWSConnector.login = login;
+
+ initRestTemplate();
+ }
+
+ private void initRestTemplate(){
+ XStreamMarshaller march = new XStreamMarshaller();
+ MarshallingHttpMessageConverter mm = new MarshallingHttpMessageConverter(march);
+ List<HttpMessageConverter<?>> ee = new ArrayList<HttpMessageConverter<?>>();
+ ee.add(mm);
+ restTemplate.setMessageConverters(ee);
+ }
+
+
+ /**
+ * permet l'appel à un webservice.
+ * @param uri uri du webservice. par exemple, "plan/getAll"
+ * @param clazz classe attendue en retour
+ * @return retourne le résultat de l'appel du webservice, désérialisé et casté en objet du type demandé.
+ */
+ public static <T> T read(String uri, Class<T> clazz) {
+
+ String xml = "";
+ Object o = new Object();
+ Date start = new Date();
+ try{
+ URL myUrl = new URL(urlBase + uri + "&contentType=" + encodage);
+ System.out.print("\n" + myUrl);
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(myUrl.openStream()));
+
+ String inputLine;
+ while ((inputLine = in.readLine()) != null)
+ xml += inputLine;
+ in.close();
+ //System.out.println(xml);
+ }catch(MalformedURLException e){
+ System.out.println(e);
+ }catch(IOException e){
+ System.out.println(e);
+ }
+ o = xstream.fromXML(xml);
+
+ if(o.getClass() == clazz){
+ Date end = new Date();
+ total += end.getTime() - start.getTime();
+ System.out.print(" " + String.valueOf(end.getTime() - start.getTime()) + "ms("+ total + "ms)");
+ return (T) o;
+ }
+ else if (o.getClass() == Integer.class){
+ System.out.println("\n"+WsErrors.getError((Integer) o));
+ return null;
+ }
+ else{
+ System.out.println("erreur inconnue : ");
+ System.out.println(o.getClass() + " / " + clazz);
+ return null;
+ }
+ }
+
+ public static <T> T postForObject(String uri, Map<String, String> map, Class<T> class1) {
+
+ String myUrl = urlBase + uri + "?login="+login+"&contentType="+encodage;
+ System.out.print("\n" + myUrl);
+
+ Date start = new Date();
+ T result = restTemplate.postForObject(myUrl, map, class1);
+ Date end = new Date();
+
+ System.out.print(" " + String.valueOf(end.getTime() - start.getTime()) + "ms("+ total + "ms)");
+ total += end.getTime() - start.getTime();
+ return result;
+ }
+
+ public static void delete(String uri) {
+
+ String myUrl = urlBase + uri + "&login="+login+"&contentType="+encodage;
+ System.out.print("\n" + myUrl);
+
+ Date start = new Date();
+ restTemplate.delete(myUrl);
+ Date end = new Date();
+
+ System.out.print(" " + String.valueOf(end.getTime() - start.getTime()) + "ms("+ total + "ms)");
+ total += end.getTime() - start.getTime();
+
+ }
+
+}
\ No newline at end of file