/* (non-Javadoc)
   * @see com.rabbitmessenger.server.RabbitCommunicationAPI#sendMessage(java.lang.String)
   */
  @Override
  public boolean sendMessage(String message) {

    String url =
        "http://api.nabaztag.com/vl/api.jsp?token="
            + config.getToken()
            + "&sn="
            + config.getSerialNumber()
            + "&tts="
            + message;

    return sendURL(url);
  }
  /* (non-Javadoc)
   * @see com.rabbitmessenger.server.RabbitCommunicationAPI#playMP3(java.lang.String)
   */
  @Override
  public boolean playMP3(String message) {

    String url =
        "http://api.nabaztag.com/vl/FR/api_stream.jsp?token="
            + config.getToken()
            + "&sn="
            + config.getSerialNumber()
            + "&urlList="
            + message;

    return sendURL(url);
  }
public class WebRabbitCommunication implements RabbitCommunication {

  public static final ConfigurationManager config = ConfigurationManager.getInstance();

  private static final Logger log = Logger.getLogger(WebRabbitCommunication.class.getName());

  /* (non-Javadoc)
   * @see com.rabbitmessenger.server.RabbitCommunicationAPI#sendMessage(java.lang.String)
   */
  @Override
  public boolean sendMessage(String message) {

    String url =
        "http://api.nabaztag.com/vl/api.jsp?token="
            + config.getToken()
            + "&sn="
            + config.getSerialNumber()
            + "&tts="
            + message;

    return sendURL(url);
  }

  /* (non-Javadoc)
   * @see com.rabbitmessenger.server.RabbitCommunicationAPI#playMP3(java.lang.String)
   */
  @Override
  public boolean playMP3(String message) {

    String url =
        "http://api.nabaztag.com/vl/FR/api_stream.jsp?token="
            + config.getToken()
            + "&sn="
            + config.getSerialNumber()
            + "&urlList="
            + message;

    return sendURL(url);
  }

  private static boolean sendURL(String inputUrl) {

    inputUrl = inputUrl.replaceAll(" ", "%20");

    log.fine("Calling URL: " + inputUrl);

    boolean result = false;

    try {
      URL url = new URL(inputUrl);

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestProperty("Cache-Control", "max-age=0");
      connection.setDoOutput(true);

      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        // OK
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null) {
          log.fine("Received: " + line);
          result = "<string>ok</string>".equals(line);
          break;
        }
        reader.close();
      } else {
        // Server returned HTTP error code.
        log.info("Received HTTP error code: " + connection.getResponseCode());
        return false;
      }

    } catch (MalformedURLException e) {
      log.severe("MalformedURLException: " + e.getLocalizedMessage());
      return false;
    } catch (IOException e) {
      log.severe("IOException: " + e.getLocalizedMessage());
      return false;
    }

    return result;
  }
}