package org.swellrt.server.box.events.gcm;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestException;
import com.google.inject.Inject;

import org.json.JSONException;
import org.json.JSONObject;
import org.swellrt.server.box.events.Event;
import org.swellrt.server.box.events.EventDispatcherTarget;
import org.waveprotocol.wave.util.logging.Log;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

public class GCMDispatcher implements EventDispatcherTarget {

  private static final Log LOG = Log.get(GCMDispatcher.class);


  public final static String NAME = "gcm";


  private static final String REGISTRATION_IDS = "registration_Ids";


  private static final String DATA = "data";

  private String authKey;
  private String sendUrl;
  private GCMSubscriptionStore subscriptionManager;

  @Inject
  public GCMDispatcher(GCMSubscriptionStore subscriptionManager) {
    this.subscriptionManager = subscriptionManager;
  }

  public void initialize(String confFilePath) {

    FileReader fr;
    try {
      fr = new FileReader(confFilePath);
    } catch (FileNotFoundException e) {
      LOG.warning("GCM dispatcher configuration file not found: " + confFilePath);
      return;
    }
    JsonParser jsonParser = new JsonParser();
    JsonElement jsonElement = jsonParser.parse(fr);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonElement gcmElement = jsonObject.get(NAME);

    if (gcmElement == null) {
      LOG.warning("GCM dispatcher configuration error: gcm section not found in " + confFilePath);
      return;
    }

    JsonObject gcmObject = gcmElement.getAsJsonObject();

    if (!(gcmObject.has("authKey")) || !(gcmObject.has("url"))) {
      LOG.warning("GCM dispatcher configuration error: config keys not found " + confFilePath);
      return;
    }

    initialize(gcmObject.get("authKey").getAsString(), gcmObject.get("url").getAsString());

  }

  public void initialize(String authKey, String sendUrl) {
    this.authKey = authKey;
    this.sendUrl = sendUrl;

    LOG.warning("GCM event dispatcher succesfully configured");
  }

  @Override
  public String getName() {
    return NAME;
  }

  @Override
  public void dispatch(Event event, String payload) {
    LOG.info("Event dispatched by GCM: " + event.getWaveletId() + " -> " + payload);
    event.getWaveId();

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, this.sendUrl);

    builder.setHeader("Content-Type", "application/json");
    builder.setHeader("Authorization", this.authKey);

    try {

      JSONObject dataPayload = new JSONObject(payload);

      JSONObject completePayload = new JSONObject();

      String waveId = event.getWaveId().toString();
      List<String> subscriptors = subscriptionManager.getSubscriptorsDevices(waveId);

      if (subscriptors.isEmpty()) {
        return;
      }

      completePayload.put(REGISTRATION_IDS, subscriptors);

      completePayload.put(DATA, dataPayload);

      builder.setRequestData(completePayload.toString());

      builder.send();

      LOG.info("Sending payload: " + completePayload);

    } catch (JSONException e) {

      e.printStackTrace();
      LOG.severe(e.getMessage());

    } catch (RequestException e) {
      e.printStackTrace();
      LOG.severe(e.getMessage());
    }
  }

}

@@ -165,125 +165,359 @@
 gle.
-gwt
+inject.Inject;%0A%0Aimport org.apache.commons
 .http
-.
 client.
-RequestBuilder;%0Aimport com.google.gwt
+HttpClient;%0Aimport org.apache.commons.httpclient.HttpException;%0Aimport org.apache.commons.httpclient.HttpStatus;%0Aimport org.apache.commons
 .http
-.
 client.
-RequestException;%0Aimport com.google.inject.Inject;%0A
+methods.ByteArrayRequestEntity;%0Aimport org.apache.commons.httpclient.methods.PostMethod;%0Aimport org.apache.commons.httpclient.methods.RequestEntity;
 %0Aimp
@@ -785,24 +785,85 @@
 FileReader;%0A
+import java.io.IOException;%0Aimport java.nio.charset.Charset;%0A
 import java.
@@ -1269,16 +1269,52 @@
 nager;%0A%0A
+%0A  private HttpClient httpClient;%0A%0A%0A
   @Injec
@@ -2515,16 +2515,57 @@
 ndUrl;%0A%0A
+    this.httpClient = new HttpClient();%0A%0A
     LOG.
@@ -2842,106 +2842,53 @@
 d);%0A
+%0A%0A
     
-event.getWaveId();%0A%0A    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, this.
+PostMethod postMethod = new PostMethod(
 send
@@ -2894,30 +2894,41 @@
 dUrl);%0A%0A
+%0A
     
-builder.se
+postMethod.setReques
 tHeader(
@@ -2968,26 +2968,36 @@
 %22);%0A    
-builder.se
+postMethod.setReques
 tHeader(
@@ -3092,25 +3092,24 @@
 t(payload);%0A
-%0A
       JSONOb
@@ -3409,17 +3409,16 @@
 ptors);%0A
-%0A
       co
@@ -3466,138 +3466,401 @@
     
-builder.setRequestData(completePayload.toString());%0A%0A      builder.send();%0A%0A      LOG.info(%22Sending payload: %22 + completePayload);
+String stringPayload = completePayload.toString();%0A      RequestEntity requestData = new ByteArrayRequestEntity(stringPayload.getBytes(Charset.forName(%22UTF-8%22)));%0A%0A      postMethod.setRequestEntity(requestData);%0A%0A      int resultCode = httpClient.executeMethod(postMethod);%0A%0A      if (resultCode != HttpStatus.SC_OK) %7B%0A        throw new IOException(%22HTTP response code %22 + resultCode);%0A      %7D
 %0A%0A  
@@ -3893,67 +3893,138 @@
 ) %7B%0A
-%0A
       
-e.printStackTrace();%0A      LOG.severe(e.getMessage()
+LOG.severe(%22Error sending GCM request%22, e);%0A%0A    %7D catch (HttpException e) %7B%0A      LOG.severe(%22Error sending GCM request%22, e
 );%0A%0A
@@ -4036,23 +4036,18 @@
  catch (
-Request
+IO
 Exceptio
@@ -4063,60 +4063,104 @@
     
-e.printStackTrace(
+LOG.severe(%22Error sending GCM request%22, e
 );%0A
+%0A
     
-  LOG.severe(e.getMessage()
+%7D finally %7B%0A      postMethod.releaseConnection(
 );%0A 

Fix wrong GCM module dependency brokes server