001    package org.hackystat.tickertape.notifier.nabaztag;
002    
003    import java.util.logging.Logger;
004    
005    import org.restlet.Client;
006    import org.restlet.data.Method;
007    import org.restlet.data.Protocol;
008    import org.restlet.data.Request;
009    import org.restlet.data.Response;
010    
011    /**
012     * A notifier for the Nabaztag rabbit.
013     * @author Philip Johnson
014     */
015    public class NabaztagNotifier {
016      
017      /** The serial number for this device. */
018      private String serialNumber;
019      /** The token for this device. */
020      private String token;
021      /** The logger. */
022      private Logger logger;
023      /** The default Ambient service URL. */
024      private String host = "http://api.nabaztag.com/vl/FR/api.jsp";
025    
026      /**
027       * Create a new instance of a Nabaztag notifier.
028       * @param serialNumber The serial number for this Nabaztag device. 
029       * @param token The token for this device. 
030       * @param logger The logger for status messages. 
031       */
032      public NabaztagNotifier(String serialNumber, String token, Logger logger) {
033        this.serialNumber = serialNumber;
034        this.token = token;
035        this.logger = logger;
036      }
037    
038      /**
039       * Contacts the Nabaztag and sends the passed string. 
040       * All spaces are replaced by '+'.
041       * Also, all '@' are replaced by '+at+'.
042       * @param message The message to be sent to the Nabaztag. 
043       */
044      public void notify(String message) {
045        String newMessage = message.replace(' ', '+').replace("@", "+at+");
046        logger.info("Notifying Nabaztag: " + newMessage);
047        String url = String.format("%s?sn=%s&token=%s&tts=%s", host, serialNumber, token, newMessage); 
048        post(url);
049      }
050      
051      /**
052       * Performs an HTTP POST of the passed URL.
053       * @param url The URL. 
054       * @return The response from the server as a string. 
055       */
056      public String post(String url) {
057        // Prepare the request
058        Request request = new Request(Method.POST, url);
059        request.setReferrerRef("http://www.hackystat.org");
060    
061        // Handle it using an HTTP client connector
062        Client client = new Client(Protocol.HTTP);
063        Response response = client.handle(request);
064    
065        if (response.getStatus().isSuccess()) {
066          return Boolean.TRUE.toString();
067        }
068        else {
069          return response.getStatus().getDescription();
070        }
071      }
072    
073    }